-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintegration_suite_test.go
249 lines (200 loc) · 6.49 KB
/
integration_suite_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
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
240
241
242
243
244
245
246
247
248
249
//go:build integration
package integration_test
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rwx-research/captain-cli/test/helpers"
)
func TestCaptainBinary(t *testing.T) {
t.Parallel()
RegisterFailHandler(Fail)
RunSpecs(t, "Integration Suite")
}
var _ = SynchronizedAfterSuite(
func() {}, // do nothing on all processes
func() { // on the first process, delete the captain dir
err := os.RemoveAll(".captain")
Expect(err).ToNot(HaveOccurred())
})
// helpers
type captainArgs struct {
args []string
env map[string]string // note: we always set PATH
}
type captainResult struct {
exitCode int
stdout string
stderr string
}
func print(output string) {
fmt.Fprintf(GinkgoWriter, "%s", output)
}
// used to debug tests
func printResults(result captainResult) {
print(fmt.Sprintf("captain stdout: %s, captain stderr: %s, captain exit code: %d", result.stdout, result.stderr, result.exitCode))
}
func captainCmd(args captainArgs) *exec.Cmd {
const captainPath = "../captain"
_, err := os.Stat(captainPath)
Expect(err).ToNot(HaveOccurred(), "integration tests depend on a captain binary in the root directory. This should be created automatically with `mage integrationTest` and `mage test`")
cmd := exec.Command("../captain", args.args...)
env := []string{
fmt.Sprintf("%s=%s", "PATH", os.Getenv("PATH")),
}
for key, value := range args.env {
env = append(env, fmt.Sprintf("%s=%s", key, value))
}
cmd.Env = env
fmt.Fprintf(GinkgoWriter, "Executing command: %s\n with env %s\n", cmd.String(), cmd.Env)
return cmd
}
func runCaptain(args captainArgs) captainResult {
cmd := captainCmd(args)
var stdoutBuffer, stderrBuffer bytes.Buffer
cmd.Stdout = &stdoutBuffer
cmd.Stderr = &stderrBuffer
err := cmd.Run()
exitCode := 0
if err != nil {
exitErr, ok := err.(*exec.ExitError)
Expect(ok).To(BeTrue(), "captain exited with an error that wasn't an ExitError")
exitCode = exitErr.ExitCode()
}
return captainResult{
stdout: strings.TrimSuffix(stdoutBuffer.String(), "\n"),
stderr: strings.TrimSuffix(stderrBuffer.String(), "\n"),
exitCode: exitCode,
}
}
func envAsMap() map[string]string {
env := os.Environ()
result := map[string]string{}
for _, envVar := range env {
parts := strings.SplitN(envVar, "=", 2)
result[parts[0]] = parts[1]
}
return result
}
// this is git-sha-ish. We could throw the `sha` function in between
// but a random hex string should be good enough
func randomGitSha() string {
b := make([]byte, 20)
rand.Read(b)
return hex.EncodeToString(b)
}
func copyMap(m map[string]string) map[string]string {
result := map[string]string{}
for key, value := range m {
result[key] = value
}
return result
}
func withAndWithoutInheritedEnv(sharedTests sharedTestGen) {
// Intuitively I think this should be static per test run but I'm not sure if that's actually helpful or true
randomGitSha := randomGitSha()
if os.Getenv("CI") != "" {
env := envAsMap()
// don't overwrite the commit sha, but let's get rid of RWX_ACCESS_TOKEN
delete(env, "RWX_ACCESS_TOKEN")
// these tests are only run in CI
Describe("using CI environment", func() {
sharedTests(func() map[string]string { return copyMap(env) }, "inherited-env")
})
}
if os.Getenv("ONLY_TEST_INHERITED_ENV") == "" {
// don't run these on buildkite
Describe("using the generic provider", func() {
env := helpers.ReadEnvFromFile(".env.captain")
env["CAPTAIN_SHA"] = randomGitSha
sharedTests(func() map[string]string {
return copyMap(env)
}, "generic")
})
if os.Getenv("FAST_INTEGRATION") == "" {
Describe("using the Mint provider", func() {
env := helpers.ReadEnvFromFile(".env.mint")
env["MINT_GIT_COMMIT_SHA"] = randomGitSha
sharedTests(func() map[string]string {
return copyMap(env)
}, "mint")
})
Describe("using the GitHub Actions provider", func() {
env := helpers.ReadEnvFromFile(".env.github.actions")
env["GITHUB_SHA"] = randomGitSha
sharedTests(func() map[string]string {
return copyMap(env)
}, "github-actions")
})
Describe("using the GitLab CI provider", func() {
env := helpers.ReadEnvFromFile(".env.gitlab_ci")
env["CI_COMMIT_SHA"] = randomGitSha
sharedTests(func() map[string]string {
return copyMap(env)
}, "gitlab-ci")
})
Describe("using the CircleCI provider", func() {
env := helpers.ReadEnvFromFile(".env.circleci")
env["CIRCLE_SHA1"] = randomGitSha
sharedTests(func() map[string]string {
return copyMap(env)
}, "circleci")
})
Describe("using the Buildkite provider", func() {
env := helpers.ReadEnvFromFile(".env.buildkite")
env["BUILDKITE_COMMIT"] = randomGitSha
sharedTests(func() map[string]string {
return copyMap(env)
}, "buildkite")
})
}
}
}
type envGenerator func() map[string]string
type sharedTestGen func(envGenerator, string)
func withCaptainConfig(cfg string, rootDir string, body func(configPath string)) {
// TODO set RootDir on the config file once we get that merged
configPath := filepath.Join(rootDir, ".captain/config.yaml")
err := os.MkdirAll(filepath.Dir(configPath), 0o750)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(configPath, []byte(cfg), 0o600)
Expect(err).NotTo(HaveOccurred())
defer func() {
err := os.Remove(configPath)
Expect(err).NotTo(HaveOccurred())
}()
body(configPath)
}
// Integration tests are run to ensure that captain behaves as expected as well as to ensure background compatibility
// They're here to ensure
// - old CLI args should work
// - old ENV vars should work
// - old config files should work
// Some guidelines for writing them:
// - avoid asserting on STDERR
// - assertions should be as lenient as possible (e.g. avoid asserting on exact values when it's unnecessary to guarantee compatibility)
//
// If you want to write tighter assertions that you expect to break slightly future versions, wrap those assertions
// (or the whole tests) in `withoutBackwardsCompatibility`
func withoutBackwardsCompatibility(incompatibleClosure func()) {
if os.Getenv("LEGACY_VERSION_TO_TEST") == "" {
incompatibleClosure()
}
}
// we add a prefix to top level describes to allow quarantining (disabling) of legacy tests that become invalid / are subject to a breaking change
func versionedPrefixForQuarantining() string {
version := os.Getenv("LEGACY_VERSION_TO_TEST")
if os.Getenv("LEGACY_VERSION_TO_TEST") == "" {
return ""
} else {
return version + ": "
}
}