diff --git a/Dockerfile b/Dockerfile index 8f8dfa9..67aa08a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index b506770..075cecd 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -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) @@ -115,10 +115,14 @@ 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) @@ -126,6 +130,7 @@ spec: assert.NoError(t, err) assert.EqualValues(t, "filecontentishere\n", content) + time.Sleep(time.Second) cancel() c.wg.Wait() } @@ -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() } @@ -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"}], @@ -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"}], @@ -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) @@ -361,8 +369,7 @@ 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"}], @@ -370,7 +377,7 @@ spec: 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 }) @@ -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" @@ -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" diff --git a/e2e/utils_test.go b/e2e/utils_test.go index f6f95b2..5bc6364 100644 --- a/e2e/utils_test.go +++ b/e2e/utils_test.go @@ -7,6 +7,7 @@ import ( "net" "os" "strconv" + "strings" "sync" "testing" "time" @@ -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 { @@ -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) { @@ -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 {