Skip to content

Commit 7304b29

Browse files
committed
test(e2e): fix cleanup, scope postsweep to runID, CI secret guard, gofmt
- Replace defer+os.Exit pattern so cleanup actually runs: call postsweep() and os.RemoveAll(tmpDir) explicitly before os.Exit(code) - Scope postsweep to this run only (prefix "e2e-<runID>-") to avoid destroying sandboxes from concurrent runs on the same account - Update presweep comment to document intentional all-runs scope - Add CI step to fail fast when CREATEOS_E2E_API_KEY secret is missing, preventing silent green pass with no tests executed - gofmt lifecycle_test.go
1 parent dca0638 commit 7304b29

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

.github/workflows/e2e.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ jobs:
2424
- name: Build binary
2525
run: go build -o createos-e2e-bin .
2626

27+
- name: Check required secrets
28+
run: |
29+
if [ -z "$CREATEOS_E2E_API_KEY" ]; then
30+
echo "Error: CREATEOS_E2E_API_KEY secret is not configured"
31+
exit 1
32+
fi
33+
env:
34+
CREATEOS_E2E_API_KEY: ${{ secrets.CREATEOS_E2E_API_KEY }}
35+
2736
- name: Run E2E tests
2837
env:
2938
CREATEOS_E2E_API_KEY: ${{ secrets.CREATEOS_E2E_API_KEY }}

test/e2e/lifecycle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package e2e
44

55
import (
66
"context"
7-
"testing"
7+
"testing"
88
)
99

1010
func TestLifecycle(t *testing.T) {

test/e2e/main_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ func TestMain(m *testing.M) {
3333
fmt.Fprintf(os.Stderr, "failed to create temp dir: %v\n", err)
3434
os.Exit(1)
3535
}
36-
defer os.RemoveAll(tmpDir)
37-
3836
testHomeDir = tmpDir
3937
os.Setenv("HOME", tmpDir) // #nosec G104 -- HOME must be set; failure is non-recoverable but unreachable in practice
4038
os.Setenv("XDG_CONFIG_HOME", tmpDir) // #nosec G104
@@ -77,10 +75,12 @@ func TestMain(m *testing.M) {
7775
// Pre-sweep: delete any leftover e2e- sandboxes from prior failed runs.
7876
presweep()
7977

80-
// Deferred post-sweep: best-effort cleanup after the suite finishes.
81-
defer postsweep()
82-
83-
os.Exit(m.Run())
78+
// Post-sweep: best-effort cleanup after the suite finishes.
79+
// Called explicitly before os.Exit so defers are not skipped.
80+
code = m.Run()
81+
postsweep()
82+
_ = os.RemoveAll(tmpDir)
83+
os.Exit(code)
8484
}
8585

8686
// discoverSmallestShape calls `sandbox shapes -o json` and returns the shape
@@ -113,7 +113,10 @@ func discoverSmallestShape() string {
113113
return best.ID
114114
}
115115

116-
// presweep deletes any existing sandboxes whose names start with "e2e-".
116+
// presweep deletes stale e2e-* sandboxes left by prior failed runs.
117+
// It intentionally matches all e2e-* prefixes (not just this runID) to
118+
// collect orphans from previous sessions. The nightly CI enforces
119+
// cancel-in-progress to prevent concurrent runs on the same account.
117120
func presweep() {
118121
stdout, _, code := runCLI("sandbox", "list", "--all")
119122
if code != 0 || strings.TrimSpace(stdout) == "" {
@@ -137,7 +140,8 @@ func presweep() {
137140
}
138141
}
139142

140-
// postsweep is deferred in TestMain; best-effort delete of e2e- sandboxes.
143+
// postsweep is called explicitly before os.Exit in TestMain; best-effort
144+
// delete of sandboxes created by THIS run only (prefix "e2e-<runID>-").
141145
func postsweep() {
142146
stdout, _, code := runCLI("sandbox", "list", "--all")
143147
if code != 0 || strings.TrimSpace(stdout) == "" {
@@ -152,8 +156,9 @@ func postsweep() {
152156
return
153157
}
154158

159+
runPrefix := "e2e-" + runID + "-"
155160
for _, sb := range sandboxes {
156-
if sb.Name == nil || !strings.HasPrefix(*sb.Name, "e2e-") {
161+
if sb.Name == nil || !strings.HasPrefix(*sb.Name, runPrefix) {
157162
continue
158163
}
159164
_, _, _ = runCLI("sandbox", "rm", "--force", sb.ID)

0 commit comments

Comments
 (0)