Skip to content

Commit

Permalink
fix: verify flaky tests (#9050)
Browse files Browse the repository at this point in the history
* fix: flaky verify tests
  • Loading branch information
ericzzzzzzz authored Aug 29, 2023
1 parent f2cacc3 commit a66f3fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
18 changes: 10 additions & 8 deletions integration/exec_k8s_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ func TestExec_K8SActions(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, NeedsGcp)
MarkIntegrationTest(t.T, CanRunWithoutGcp)
args := []string{test.action}

if test.envFile != "" {
args = append(args, "--env-file", test.envFile)
}
ns, _ := SetupNamespace(t.T)

out, err := skaffold.Exec(args...).InDir("testdata/custom-actions-k8s").RunWithCombinedOutput(t.T)
out, err := skaffold.Exec(args...).InNs(ns.Name).InDir("testdata/custom-actions-k8s").RunWithCombinedOutput(t.T)
t.CheckError(test.shouldErr, err)
logs := string(out)

Expand Down Expand Up @@ -123,17 +124,17 @@ func TestExec_K8SActionWithLocalArtifact(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, NeedsGcp)
MarkIntegrationTest(t.T, CanRunWithoutGcp)
dir := "testdata/custom-actions-k8s"
args := []string{test.action}

ns, _ := SetupNamespace(t.T)
if test.shouldBuild {
tmpfile := testutil.TempFile(t.T, "", []byte{})
skaffold.Build("--file-output", tmpfile, "--tag", uuid.New().String(), "--check-cluster-node-platforms=true").InDir(dir).RunOrFail(t.T)
skaffold.Build("--file-output", tmpfile, "--tag", uuid.New().String(), "--check-cluster-node-platforms=true").InNs(ns.Name).InDir(dir).RunOrFail(t.T)
args = append(args, "--build-artifacts", tmpfile)
}

out, err := skaffold.Exec(args...).InDir(dir).RunWithCombinedOutput(t.T)
out, err := skaffold.Exec(args...).InNs(ns.Name).InDir(dir).RunWithCombinedOutput(t.T)
t.CheckError(test.shouldErr, err)

for _, expectedMsg := range test.expectedMsgs {
Expand Down Expand Up @@ -193,14 +194,15 @@ func TestExec_K8SActionsEvents(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, NeedsGcp)
MarkIntegrationTest(t.T, CanRunWithoutGcp)
ns, _ := SetupNamespace(t.T)
rpcAddr := randomPort()
tmp := t.TempDir()
logFile := filepath.Join(tmp, uuid.New().String()+"logs.json")

args := []string{test.action, "--rpc-port", rpcAddr, "--event-log-file", logFile}

_, err := skaffold.Exec(args...).InDir("testdata/custom-actions-k8s").RunWithCombinedOutput(t.T)
_, err := skaffold.Exec(args...).InNs(ns.Name).InDir("testdata/custom-actions-k8s").RunWithCombinedOutput(t.T)

t.CheckError(test.shouldErr, err)

Expand Down
12 changes: 6 additions & 6 deletions integration/testdata/verify-succeed-k8s/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ verify:
image: docker.io/hello-world:latest
executionMode:
kubernetesCluster: {}
- name: alpine-1
- name: verify-succeed-k8s-1
container:
name: alpine-1
name: verify-succeed-k8s-1
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo $FOO; sleep 10; echo bye"]
Expand All @@ -39,11 +39,11 @@ verify:
profiles:
- name: no-duplicated-logs
verify:
- name: alpine-1
- name: no-duplicated-logs-1
executionMode:
kubernetesCluster: {}
container:
name: alpine-1
name: no-duplicated-logs-1
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo alpine-1; sleep 1; echo bye alpine-1"]
Expand Down Expand Up @@ -91,11 +91,11 @@ profiles:

- name: local-built-artifact
verify:
- name: alpine-1
- name: local-built-artifact-1
executionMode:
kubernetesCluster: {}
container:
name: alpine-1
name: local-built-artifact-1
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo alpine-1; sleep 2; echo bye alpine-1"]
Expand Down
20 changes: 12 additions & 8 deletions integration/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ func TestKubernetesJobVerifyPassingTestsWithEnvVar(t *testing.T) {

rpcPort := randomPort()
// `--default-repo=` is used to cancel the default repo that is set by default.
ns, _ := SetupNamespace(t)
out, err := skaffold.Verify("--default-repo=", "--rpc-port", rpcPort,
"--event-log-file", logFile, "--env-file", "verify.env").InDir("testdata/verify-succeed-k8s").RunWithCombinedOutput(t)
"--event-log-file", logFile, "--env-file", "verify.env").InNs(ns.Name).InDir("testdata/verify-succeed-k8s").RunWithCombinedOutput(t)
logs := string(out)

testutil.CheckError(t, false, err)
testutil.CheckContains(t, "Hello from Docker!", logs)
testutil.CheckContains(t, "foo-var", logs)
testutil.CheckContains(t, "alpine-1", logs)
testutil.CheckContains(t, "verify-succeed-k8s-1", logs)
testutil.CheckContains(t, "alpine-2", logs)

// verify logs are in the event output as well
Expand All @@ -138,8 +139,9 @@ func TestKubernetesJobVerifyOneTestFailsWithEnvVar(t *testing.T) {

rpcPort := randomPort()
// `--default-repo=` is used to cancel the default repo that is set by default.
ns, _ := SetupNamespace(t)
out, err := skaffold.Verify("--default-repo=", "--rpc-port", rpcPort,
"--event-log-file", logFile, "--env-file", "verify.env").InDir("testdata/verify-fail-k8s").RunWithCombinedOutput(t)
"--event-log-file", logFile, "--env-file", "verify.env").InNs(ns.Name).InDir("testdata/verify-fail-k8s").RunWithCombinedOutput(t)
logs := string(out)

testutil.CheckError(t, true, err)
Expand Down Expand Up @@ -217,8 +219,8 @@ func TestNoDuplicateLogsK8SJobs(t *testing.T) {
dir: "testdata/verify-succeed-k8s",
profile: "no-duplicated-logs",
expectedUniqueLogs: []string{
"[alpine-1] alpine-1",
"[alpine-1] bye alpine-1",
"[no-duplicated-logs-1] alpine-1",
"[no-duplicated-logs-1] bye alpine-1",
},
},
{
Expand All @@ -237,10 +239,11 @@ func TestNoDuplicateLogsK8SJobs(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, NeedsGcp)
MarkIntegrationTest(t.T, CanRunWithoutGcp)

args := []string{"-p", test.profile}
out, err := skaffold.Verify(args...).InDir(test.dir).RunWithCombinedOutput(t.T)
ns, _ := SetupNamespace(t.T)
out, err := skaffold.Verify(args...).InNs(ns.Name).InDir(test.dir).RunWithCombinedOutput(t.T)

t.CheckError(test.shouldErr, err)

Expand Down Expand Up @@ -339,7 +342,8 @@ func TestTimeoutK8s(t *testing.T) {
MarkIntegrationTest(t.T, CanRunWithoutGcp)

args := []string{"-p", test.profile}
out, err := skaffold.Verify(args...).InDir(test.dir).RunWithCombinedOutput(t.T)
ns, _ := SetupNamespace(t.T)
out, err := skaffold.Verify(args...).InNs(ns.Name).InDir(test.dir).RunWithCombinedOutput(t.T)
logs := string(out)

t.CheckError(test.shouldErr, err)
Expand Down

0 comments on commit a66f3fa

Please sign in to comment.