-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_framework.go
239 lines (210 loc) · 5.73 KB
/
test_framework.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package test
import (
"errors"
"fmt"
"math/rand"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
"time"
)
const (
retryCount = 2
isParallel = true
)
type cmdFunc func() ([]byte, error)
// try is designed with command line executions in mind
func try(blockName string, t *t, f cmdFunc, maxTime time.Duration) {
start := time.Now()
var outp []byte
var err error
for {
if t.failed {
return
}
if time.Since(start) > maxTime {
_, file, line, _ := runtime.Caller(1)
fname := filepath.Base(file)
if err != nil {
t.Fatalf("%v:%v, %v (failed after %v with '%v'), output: '%v'", fname, line, blockName, time.Since(start), err, string(outp))
}
return
}
outp, err = f()
if err == nil {
return
}
time.Sleep(1000 * time.Millisecond)
}
}
func once(blockName string, t *testing.T, f cmdFunc) {
outp, err := f()
if err != nil {
t.Fatalf("%v with '%v', output: %v", blockName, err, string(outp))
}
}
type server struct {
cmd *exec.Cmd
t *t
envName string
containerName string
}
func getFrame(skipFrames int) runtime.Frame {
// We need the frame at index skipFrames+2, since we never want runtime.Callers and getFrame
targetFrameIndex := skipFrames + 2
// Set size to targetFrameIndex+2 to ensure we have room for one more caller than we need
programCounters := make([]uintptr, targetFrameIndex+2)
n := runtime.Callers(0, programCounters)
frame := runtime.Frame{Function: "unknown"}
if n > 0 {
frames := runtime.CallersFrames(programCounters[:n])
for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ {
var frameCandidate runtime.Frame
frameCandidate, more = frames.Next()
if frameIndex == targetFrameIndex {
frame = frameCandidate
}
}
}
return frame
}
// taken from https://stackoverflow.com/questions/35212985/is-it-possible-get-information-about-caller-function-in-golang
// MyCaller returns the caller of the function that called it :)
func myCaller() string {
// Skip GetCallerFunctionName and the function to get the caller of
return getFrame(2).Function
}
type options struct {
auth string // eg. jwt
}
func newServer(t *t, opts ...options) server {
min := 8000
max := 60000
portnum := rand.Intn(max-min) + min
fname := strings.Split(myCaller(), ".")[2]
// kill container, ignore error because it might not exist,
// we dont care about this that much
exec.Command("docker", "kill", fname).CombinedOutput()
exec.Command("docker", "rm", fname).CombinedOutput()
// create env and set proxy address
exec.Command("micro", "env", "add", fname, fmt.Sprintf("127.0.0.1:%v", portnum)).CombinedOutput()
cmd := exec.Command("docker", "run", "--name", fname,
fmt.Sprintf("-p=%v:8081", portnum), "micro", "server")
if len(opts) == 1 && opts[0].auth == "jwt" {
base64 := "base64 -w0"
if runtime.GOOS == "darwin" {
base64 = "base64 -b0"
}
priv := "cat /tmp/sshkey | " + base64
privKey, err := exec.Command("bash", "-c", priv).Output()
if err != nil {
panic(string(privKey))
}
pub := "cat /tmp/sshkey.pub | " + base64
pubKey, err := exec.Command("bash", "-c", pub).Output()
if err != nil {
panic(string(pubKey))
}
cmd = exec.Command("docker", "run", "--name", fname,
fmt.Sprintf("-p=%v:8081", portnum),
"-e", "MICRO_AUTH=jwt",
"-e", "MICRO_AUTH_PRIVATE_KEY="+strings.Trim(string(privKey), "\n"),
"-e", "MICRO_AUTH_PUBLIC_KEY="+strings.Trim(string(pubKey), "\n"),
"micro", "server")
}
//fmt.Println("docker", "run", "--name", fname, fmt.Sprintf("-p=%v:8081", portnum), "micro", "server")
return server{
cmd: cmd,
t: t,
envName: fname,
containerName: fname,
}
}
func (s server) launch() {
go func() {
if err := s.cmd.Start(); err != nil {
s.t.t.Fatal(err)
}
}()
// @todo find a way to know everything is up and running
try("Calling micro server", s.t, func() ([]byte, error) {
outp, err := exec.Command("micro", s.envFlag(), "list", "services").CombinedOutput()
if !strings.Contains(string(outp), "runtime") ||
!strings.Contains(string(outp), "router") ||
!strings.Contains(string(outp), "registry") ||
!strings.Contains(string(outp), "api") ||
!strings.Contains(string(outp), "broker") ||
!strings.Contains(string(outp), "config") ||
!strings.Contains(string(outp), "debug") ||
!strings.Contains(string(outp), "proxy") ||
!strings.Contains(string(outp), "auth") ||
!strings.Contains(string(outp), "store") {
return outp, errors.New("Not ready")
}
return outp, err
}, 60*time.Second)
time.Sleep(5 * time.Second)
}
func (s server) close() {
exec.Command("docker", "kill", s.containerName).CombinedOutput()
if s.cmd.Process != nil {
s.cmd.Process.Signal(syscall.SIGKILL)
}
}
func (s server) envFlag() string {
return fmt.Sprintf("-env=%v", s.envName)
}
type t struct {
counter int
failed bool
format string
values []interface{}
t *testing.T
}
func (t *t) Fatal(values ...interface{}) {
//t.Log(values...)
t.failed = true
t.values = values
}
func (t *t) Log(values ...interface{}) {
t.t.Log(values...)
}
func (t *t) Fatalf(format string, values ...interface{}) {
//t.Log(fmt.Sprintf(format, values...))
t.failed = true
t.values = values
t.format = format
}
func (t *t) Parallel() {
if t.counter == 0 && isParallel {
t.t.Parallel()
}
t.counter++
}
func newT(te *testing.T) *t {
return &t{t: te}
}
// trySuite is designed to retry a TestXX function
func trySuite(t *testing.T, f func(t *t), times int) {
tee := newT(t)
for i := 0; i < times; i++ {
f(tee)
if !tee.failed {
return
}
if i != times-1 {
tee.failed = false
}
time.Sleep(200 * time.Millisecond)
}
if tee.failed {
if len(tee.format) > 0 {
t.Fatalf(tee.format, tee.values...)
} else {
t.Fatal(tee.values...)
}
}
}