forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_k8s_actions_test.go
219 lines (196 loc) · 7.3 KB
/
exec_k8s_actions_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
/*
Copyright 2023 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package integration
import (
"os"
"path/filepath"
"testing"
"github.com/google/uuid"
"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)
func TestExec_K8SActions(t *testing.T) {
tests := []struct {
description string
action string
shouldErr bool
envFile string
expectedMsgs []string
notExpectedLogs []string
}{
{
description: "fail due to action timeout",
action: "action-fail-timeout",
shouldErr: true,
expectedMsgs: []string{"context deadline exceeded"},
notExpectedLogs: []string{"[task1] bye-from-task1"},
},
{
description: "fail with fail fast",
action: "action-fail-fast",
shouldErr: true,
expectedMsgs: []string{`error in task4 job execution, job failed`},
notExpectedLogs: []string{"[task3] bye-from-task3"},
},
{
description: "fail with fail safe",
action: "action-fail-safe-logs",
shouldErr: true,
expectedMsgs: []string{
"[task5l] hello-from-task5l",
"[task5l] bye-from-task5l",
`* error in task6l job execution, job failed`,
},
},
{
description: "action succeeded",
action: "action-succeeded-logs",
envFile: "exec.env",
expectedMsgs: []string{
"[task7l] hello-from-env-file",
"[task7l] bye-from-env-file",
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
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...).InNs(ns.Name).InDir("testdata/custom-actions-k8s").RunWithCombinedOutput(t.T)
t.CheckError(test.shouldErr, err)
logs := string(out)
for _, expectedMsg := range test.expectedMsgs {
t.CheckContains(expectedMsg, logs)
}
for _, nel := range test.notExpectedLogs {
testutil.CheckNotContains(t.T, nel, logs)
}
})
}
}
func TestExec_K8SActionWithLocalArtifact(t *testing.T) {
tests := []struct {
description string
action string
shouldErr bool
shouldBuild bool
expectedMsgs []string
}{
{
description: "fail due not found image",
action: "action-with-local-built-img-1",
shouldErr: true,
expectedMsgs: []string{
"creating container for local-img-task1-1: ErrImagePull",
},
},
{
description: "build and run task",
action: "action-with-local-built-img-2",
shouldBuild: true,
expectedMsgs: []string{
"[local-img-task1-2] Hello world from-local-img! 4",
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
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").InNs(ns.Name).InDir(dir).RunOrFail(t.T)
args = append(args, "--build-artifacts", tmpfile)
}
out, err := skaffold.Exec(args...).InNs(ns.Name).InDir(dir).RunWithCombinedOutput(t.T)
t.CheckError(test.shouldErr, err)
for _, expectedMsg := range test.expectedMsgs {
t.CheckContains(expectedMsg, string(out))
}
})
}
}
func TestExec_K8SActionsEvents(t *testing.T) {
tests := []struct {
description string
action string
shouldErr bool
expectedLogs []string
}{
{
description: "events for succeeded action",
action: "action-succeeded",
expectedLogs: []string{
`"taskEvent":{"id":"Exec-0","task":"Exec","description":"Executing custom action action-succeeded","status":"InProgress"}}`,
`"execEvent":{"id":"task7","taskId":"Exec-0","status":"InProgress"}}`,
`"execEvent":{"id":"task8","taskId":"Exec-0","status":"InProgress"}}`,
`"execEvent":{"id":"task7","taskId":"Exec-0","status":"Succeeded"}}`,
`"execEvent":{"id":"task8","taskId":"Exec-0","status":"Succeeded"}}`,
// TODO(#8728): Uncomment this expected log line when the flaky behaviour is solved.
// `"taskEvent":{"id":"Exec-0","task":"Exec","status":"Succeeded"}}`,
},
},
{
description: "events for fail action - fail fast",
action: "action-fail-fast",
shouldErr: true,
expectedLogs: []string{
`"taskEvent":{"id":"Exec-0","task":"Exec","description":"Executing custom action action-fail-fast","status":"InProgress"}}`,
`"execEvent":{"id":"task3","taskId":"Exec-0","status":"InProgress"}}`,
`"execEvent":{"id":"task4","taskId":"Exec-0","status":"InProgress"}}`,
`"execEvent":{"id":"task4","taskId":"Exec-0","status":"Failed","actionableErr":{"errCode":"UNKNOWN_ERROR","message":"error in task4 job execution, job failed"`,
`"execEvent":{"id":"task3","taskId":"Exec-0","status":"Failed","actionableErr":{"errCode":"UNKNOWN_ERROR","message":"error in task3 job execution, event type: ERROR"`,
`"taskEvent":{"id":"Exec-0","task":"Exec","status":"Failed","actionableErr":{"errCode":"UNKNOWN_ERROR","message":"error in task4 job execution, job failed"`,
},
},
{
description: "events for fail action - fail safe",
action: "action-fail-safe",
shouldErr: true,
expectedLogs: []string{
`"taskEvent":{"id":"Exec-0","task":"Exec","description":"Executing custom action action-fail-safe","status":"InProgress"}}`,
`"execEvent":{"id":"task5","taskId":"Exec-0","status":"InProgress"}}`,
`"execEvent":{"id":"task6","taskId":"Exec-0","status":"InProgress"}}`,
`"execEvent":{"id":"task6","taskId":"Exec-0","status":"Failed","actionableErr":{"errCode":"UNKNOWN_ERROR","message":"error in task6 job execution, job failed"`,
`"execEvent":{"id":"task5","taskId":"Exec-0","status":"Succeeded"}}`,
`"taskEvent":{"id":"Exec-0","task":"Exec","status":"Failed","actionableErr":{"errCode":"UNKNOWN_ERROR","message":"1 error(s) occurred:\n* error in task6 job execution, job failed"`,
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
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...).InNs(ns.Name).InDir("testdata/custom-actions-k8s").RunWithCombinedOutput(t.T)
t.CheckError(test.shouldErr, err)
b, err := os.ReadFile(logFile + ".v2")
if err != nil {
t.Fatalf("error reading %s", logFile+".v2")
}
v2EventLogs := string(b)
for _, expectedLog := range test.expectedLogs {
t.CheckContains(expectedLog, v2EventLogs)
}
})
}
}