-
Notifications
You must be signed in to change notification settings - Fork 21
/
starter_test.go
182 lines (160 loc) · 4.18 KB
/
starter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package starter
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
"testing"
"time"
)
var echoServerTxt = `package main
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/lestrrat-go/server-starter/listener"
)
func main() {
listeners, err := listener.ListenAll()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to listen: %s\n", err)
os.Exit(1)
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, r.Body)
})
for _, l := range listeners {
http.Serve(l, handler)
}
loop := false
sigCh := make(chan os.Signal)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGHUP)
for loop {
select {
case <-sigCh:
loop = false
default:
time.Sleep(time.Second)
}
}
}
`
type config struct {
args []string
command string
dir string
interval int
pidfile string
ports []string
paths []string
sigonhup string
sigonterm string
statusfile string
}
func (c config) Args() []string { return c.args }
func (c config) Command() string { return c.command }
func (c config) Dir() string { return c.dir }
func (c config) Interval() time.Duration { return time.Duration(c.interval) * time.Second }
func (c config) PidFile() string { return c.pidfile }
func (c config) Ports() []string { return c.ports }
func (c config) Paths() []string { return c.paths }
func (c config) SignalOnHUP() os.Signal { return SigFromName(c.sigonhup) }
func (c config) SignalOnTERM() os.Signal { return SigFromName(c.sigonterm) }
func (c config) StatusFile() string { return c.statusfile }
func TestRun(t *testing.T) {
dir, err := ioutil.TempDir("", fmt.Sprintf("server-starter-test-%d", os.Getpid()))
if err != nil {
t.Errorf("Failed to create temp directory: %s", err)
return
}
defer os.RemoveAll(dir)
srcFile := filepath.Join(dir, "echod.go")
f, err := os.OpenFile(srcFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
t.Errorf("Failed to create %s: %s", srcFile, err)
return
}
io.WriteString(f, echoServerTxt)
f.Close()
_, lastComp := filepath.Split(dir)
cmd := exec.Command("go", "mod", "init", "github.com/lestrrat-go/server-starter/"+lastComp)
cmd.Dir = dir
if output, err := cmd.CombinedOutput(); err != nil {
t.Logf("%s", output)
t.Errorf("failed to run go mod init: %s", err)
return
}
cmd = exec.Command("go", "build", "-o", filepath.Join(dir, "echod"), ".")
cmd.Dir = dir
if output, err := cmd.CombinedOutput(); err != nil {
t.Errorf("Failed to compile %s: %s\n%s", dir, err, output)
return
}
ports := []string{"9090", "8080"}
sd, err := NewStarter(&config{
ports: ports,
command: filepath.Join(dir, "echod"),
})
if err != nil {
t.Errorf("Failed to create starter: %s", err)
return
}
doneCh := make(chan struct{})
readyCh := make(chan struct{})
go func() {
defer func() { doneCh <- struct{}{} }()
time.AfterFunc(500*time.Millisecond, func() {
readyCh <- struct{}{}
})
if err := sd.Run(); err != nil {
t.Errorf("sd.Run() failed: %s", err)
}
t.Logf("Exiting...")
}()
<-readyCh
for _, port := range ports {
_, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", port))
if err != nil {
t.Errorf("Error connecing to port '%s': %s", port, err)
}
}
time.AfterFunc(time.Second, sd.Stop)
<-doneCh
log.Printf("Checking ports...")
patterns := make([]string, len(ports))
for i, port := range ports {
patterns[i] = fmt.Sprintf(`%s=\d+`, port)
}
pattern := regexp.MustCompile(strings.Join(patterns, ";"))
if envPort := os.Getenv("SERVER_STARTER_PORT"); !pattern.MatchString(envPort) {
t.Errorf("SERVER_STARTER_PORT: Expected '%s', but got '%s'", pattern, envPort)
}
}
func TestSigFromName(t *testing.T) {
for sig, name := range niceSigNames {
if got := SigFromName(name); sig != got {
t.Errorf("%v: wants '%v' but got '%v'", name, sig, got)
}
}
variants := map[string]syscall.Signal{
"SIGTERM": syscall.SIGTERM,
"sigterm": syscall.SIGTERM,
"Hup": syscall.SIGHUP,
}
for name, sig := range variants {
if got := SigFromName(name); sig != got {
t.Errorf("%v: wants '%v' but got '%v'", name, sig, got)
}
}
}