Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM alpine:3.20
FROM alpine:3.21
RUN apk add --no-cache tzdata ca-certificates
WORKDIR /
COPY gmc gmc
USER nobody
Expand Down
37 changes: 22 additions & 15 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestMasterAgentAccept(t *testing.T) {
_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
assert.NoError(t, err)

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

time.Sleep(2 * time.Second)
c.waitForAccepted()

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

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

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

time.Sleep(2 * time.Second)
c.waitForAccepted()

content, err := os.ReadFile("/tmp/test.systemd")
var content []byte
WaitFor(t, time.Second*3, "wait for file", func() bool {
content, err = os.ReadFile("/tmp/test.systemd")
return len(content) != 0
})
assert.NoError(t, err)
assert.EqualValues(t, "filecontentishere\n", content)

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

time.Sleep(2 * time.Second)
c.waitForAccepted()

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

_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
assert.NoError(t, err)

time.Sleep(2 * time.Second)
c.waitForAccepted()

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

_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
assert.NoError(t, err)

time.Sleep(2 * time.Second)
c.waitForAccepted()

buf := &bytes.Buffer{}
logrus.SetOutput(buf)
Expand Down Expand Up @@ -361,16 +369,15 @@ spec:

_, err = c.client.Post("/api/machines/accept-v1", bytes.NewBufferString(`{"host":"mycooltestagent"}`))
assert.NoError(t, err)

time.Sleep(1 * time.Second)
c.waitForAccepted()

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

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

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

WaitFor(t, 1*time.Second, "file to have content from git again", func() bool {
WaitFor(t, 2*time.Second, "file to have content from git again", func() bool {
content, err = os.ReadFile("./newfile.txt")
assert.NoError(t, err)
return string(content) == "itsfromgit\n"
Expand Down
22 changes: 16 additions & 6 deletions e2e/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand All @@ -21,12 +22,13 @@ import (
)

type testWrapper struct {
client *authedhttpclient.Client
master *master.Master
agent *agent.Agent
commander *mocks.MockCommander
wg *sync.WaitGroup
redis *mocks.MockCmdable
client *authedhttpclient.Client
master *master.Master
agent *agent.Agent
commander *mocks.MockCommander
wg *sync.WaitGroup
redis *mocks.MockCmdable
waitForAccepted func()
}

func initMasterAgent(t *testing.T, ctx context.Context) testWrapper {
Expand Down Expand Up @@ -85,6 +87,13 @@ func initMasterAgent(t *testing.T, ctx context.Context) testWrapper {
commander: mockedCommander,
wg: wg,
redis: redisMock,
waitForAccepted: func() {
WaitFor(t, time.Second*2, "wait for accepted", func() bool {
resp, err := client.Get("/api/machines-v1")
b := getBody(t, resp.Body)
return err == nil && strings.Contains(b, `"Accepted":true`) && strings.Contains(b, `"Online":true`)
})
},
}
}
func freePort() (port int, err error) {
Expand Down Expand Up @@ -141,6 +150,7 @@ func captureStdout() func() string {
}
}
*/

func WaitFor(t *testing.T, timeout time.Duration, msg string, ok func() bool) {
end := time.Now().Add(timeout)
for {
Expand Down