-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit_test.go
79 lines (64 loc) · 2.07 KB
/
init_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
package acceptance
import (
"os"
"testing"
"time"
"github.com/circleci/ex/testing/runner"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
func TestInit(t *testing.T) {
srcDir := createMockSourceFiles(t)
destDir := t.TempDir()
orchSrc := srcDir + "/orchestrator"
orchDest := destDir + "/orchestrator"
agentSrc := srcDir + "/circleci-agent"
agentDest := destDir + "/circleci-agent"
circleciDest := destDir + "/circleci"
r := runner.New(
"SOURCE="+srcDir,
"DESTINATION="+destDir,
"CIRCLECI_GOAT_SHUTDOWN_DELAY=0",
)
res, err := r.Start(orchestratorTestBinary)
assert.NilError(t, err)
t.Run("Run init", func(t *testing.T) {
select {
case err = <-res.Wait():
assert.NilError(t, err)
case <-time.After(time.Second * 5):
assert.NilError(t, res.Stop())
t.Fatal(t, "timeout before process stopped")
}
})
t.Run("Files were copied and symlink created", func(t *testing.T) {
assertFileIsCopied(t, orchSrc, orchDest)
assertFileIsCopied(t, agentSrc, agentDest)
agentLink, err := os.Readlink(circleciDest)
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(agentLink, agentDest))
})
}
// Mock source file creation for testing purposes
func createMockSourceFiles(t *testing.T) string {
t.Helper()
srcDir := t.TempDir()
err := os.WriteFile(srcDir+"/orchestrator", []byte("mock orchestrator data"), 0600)
assert.NilError(t, err)
err = os.WriteFile(srcDir+"/circleci-agent", []byte("mock agent data"), 0600)
assert.NilError(t, err)
return srcDir
}
func assertFileIsCopied(t *testing.T, src, dest string) {
t.Helper()
srcInfo, err := os.Stat(src)
assert.NilError(t, err)
destInfo, err := os.Stat(dest)
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(srcInfo.Mode().Perm(), destInfo.Mode().Perm()), "files should have same permissions")
srcContents, err := os.ReadFile(src) //#nosec:G304 // this is trusted input
assert.NilError(t, err)
destContents, err := os.ReadFile(dest) //#nosec:G304 // this is trusted input
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(srcContents, destContents), "files should have same contents")
}