Skip to content

Commit c1d7ee1

Browse files
authored
Merge pull request #9 from FortnoxAB/bugfix/build
Fix some racy tests
2 parents 51f900f + 162e24a commit c1d7ee1

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM alpine:3.20
1+
FROM alpine:3.21
2+
RUN apk add --no-cache tzdata ca-certificates
23
WORKDIR /
34
COPY gmc gmc
45
USER nobody

e2e/e2e_test.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestMasterAgentAccept(t *testing.T) {
3535
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
3636
assert.NoError(t, err)
3737

38-
time.Sleep(200 * time.Millisecond)
38+
c.waitForAccepted()
3939
resp, err = c.client.Get("/machines")
4040
assert.NoError(t, err)
4141
body = getBody(t, resp.Body)
@@ -115,17 +115,22 @@ spec:
115115
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
116116
assert.NoError(t, err)
117117

118-
time.Sleep(2 * time.Second)
118+
c.waitForAccepted()
119119

120120
// make sure we fetched file from http server with sha256 hash
121-
content, err := os.ReadFile("/tmp/testfromurl")
121+
var content []byte
122+
WaitFor(t, time.Second*3, "wait for file", func() bool {
123+
content, err = os.ReadFile("/tmp/testfromurl")
124+
return len(content) != 0
125+
})
122126
assert.NoError(t, err)
123127
assert.EqualValues(t, "the file content", content)
124128

125129
content, err = os.ReadFile("/tmp/test.systemd")
126130
assert.NoError(t, err)
127131
assert.EqualValues(t, "filecontentishere\n", content)
128132

133+
time.Sleep(time.Second)
129134
cancel()
130135
c.wg.Wait()
131136
}
@@ -184,12 +189,17 @@ spec:
184189
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
185190
assert.NoError(t, err)
186191

187-
time.Sleep(2 * time.Second)
192+
c.waitForAccepted()
188193

189-
content, err := os.ReadFile("/tmp/test.systemd")
194+
var content []byte
195+
WaitFor(t, time.Second*3, "wait for file", func() bool {
196+
content, err = os.ReadFile("/tmp/test.systemd")
197+
return len(content) != 0
198+
})
190199
assert.NoError(t, err)
191200
assert.EqualValues(t, "filecontentishere\n", content)
192201

202+
time.Sleep(time.Second)
193203
cancel()
194204
c.wg.Wait()
195205
}
@@ -223,7 +233,7 @@ spec:
223233
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
224234
assert.NoError(t, err)
225235

226-
time.Sleep(2 * time.Second)
236+
c.waitForAccepted()
227237

228238
err = os.WriteFile("./adminConfig", []byte(fmt.Sprintf(`
229239
{"masters":[{"name":"http://127.0.0.1:%s","zone":"zone1"}],
@@ -269,8 +279,7 @@ spec:
269279

270280
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
271281
assert.NoError(t, err)
272-
273-
time.Sleep(2 * time.Second)
282+
c.waitForAccepted()
274283

275284
err = os.WriteFile("./adminConfig", []byte(fmt.Sprintf(`
276285
{"masters":[{"name":"http://127.0.0.1:%s","zone":"zone1"}],
@@ -315,8 +324,7 @@ spec:
315324

316325
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
317326
assert.NoError(t, err)
318-
319-
time.Sleep(2 * time.Second)
327+
c.waitForAccepted()
320328

321329
buf := &bytes.Buffer{}
322330
logrus.SetOutput(buf)
@@ -361,16 +369,15 @@ spec:
361369

362370
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
363371
assert.NoError(t, err)
364-
365-
time.Sleep(1 * time.Second)
372+
c.waitForAccepted()
366373

367374
err = os.WriteFile("./adminConfig", []byte(fmt.Sprintf(`
368375
{"masters":[{"name":"http://127.0.0.1:%s","zone":"zone1"}],
369376
"token":"%s"}`, c.master.WsPort, c.client.Token)), 0666)
370377
assert.NoError(t, err)
371378

372379
var content []byte
373-
WaitFor(t, 1*time.Second, "file to have content from git", func() bool {
380+
WaitFor(t, 2*time.Second, "file to have content from git", func() bool {
374381
content, err = os.ReadFile("./newfile.txt")
375382
return err == nil
376383
})
@@ -406,7 +413,7 @@ spec:
406413
out := stdout()
407414
assert.Contains(t, out, "apply file: newspec.yml")
408415

409-
WaitFor(t, 1*time.Second, "file to have content from apply", func() bool {
416+
WaitFor(t, 2*time.Second, "file to have content from apply", func() bool {
410417
content, err = os.ReadFile("./newfile.txt")
411418
assert.NoError(t, err)
412419
return string(content) == "filecontentishere\n"
@@ -441,7 +448,7 @@ spec:
441448
err = a.Apply(ctx, []string{"newspec.yml"})
442449
assert.NoError(t, err)
443450

444-
WaitFor(t, 1*time.Second, "file to have content from git again", func() bool {
451+
WaitFor(t, 2*time.Second, "file to have content from git again", func() bool {
445452
content, err = os.ReadFile("./newfile.txt")
446453
assert.NoError(t, err)
447454
return string(content) == "itsfromgit\n"

e2e/utils_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"os"
99
"strconv"
10+
"strings"
1011
"sync"
1112
"testing"
1213
"time"
@@ -21,12 +22,13 @@ import (
2122
)
2223

2324
type testWrapper struct {
24-
client *authedhttpclient.Client
25-
master *master.Master
26-
agent *agent.Agent
27-
commander *mocks.MockCommander
28-
wg *sync.WaitGroup
29-
redis *mocks.MockCmdable
25+
client *authedhttpclient.Client
26+
master *master.Master
27+
agent *agent.Agent
28+
commander *mocks.MockCommander
29+
wg *sync.WaitGroup
30+
redis *mocks.MockCmdable
31+
waitForAccepted func()
3032
}
3133

3234
func initMasterAgent(t *testing.T, ctx context.Context) testWrapper {
@@ -85,6 +87,13 @@ func initMasterAgent(t *testing.T, ctx context.Context) testWrapper {
8587
commander: mockedCommander,
8688
wg: wg,
8789
redis: redisMock,
90+
waitForAccepted: func() {
91+
WaitFor(t, time.Second*2, "wait for accepted", func() bool {
92+
resp, err := client.Get("/api/machines-v1")
93+
b := getBody(t, resp.Body)
94+
return err == nil && strings.Contains(b, `"Accepted":true`) && strings.Contains(b, `"Online":true`)
95+
})
96+
},
8897
}
8998
}
9099
func freePort() (port int, err error) {
@@ -141,6 +150,7 @@ func captureStdout() func() string {
141150
}
142151
}
143152
*/
153+
144154
func WaitFor(t *testing.T, timeout time.Duration, msg string, ok func() bool) {
145155
end := time.Now().Add(timeout)
146156
for {

0 commit comments

Comments
 (0)