forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplatform_test.go
336 lines (288 loc) · 11.9 KB
/
multiplatform_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Copyright 2022 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 (
"fmt"
"os"
"strings"
"testing"
"github.com/google/uuid"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
k8sv1 "k8s.io/api/core/v1"
"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)
const (
defaultRepo = "us-central1-docker.pkg.dev/k8s-skaffold/testing"
hybridClusterName = "integration-tests-hybrid"
armClusterName = "integration-tests-arm"
)
func TestMultiPlatformWithRun(t *testing.T) {
isRunningInHybridCluster := os.Getenv("GKE_CLUSTER_NAME") == hybridClusterName
type image struct {
name string
pod string
}
tests := []struct {
description string
dir string
images []image
tag string
expectedPlatforms []v1.Platform
}{
{
description: "Run with multiplatform linux/arm64 and linux/amd64",
dir: "examples/cross-platform-builds",
images: []image{{name: "skaffold-example", pod: "getting-started"}},
tag: "multiplatform-integration-test",
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
{
description: "Run with multiplatform linux/arm64 and linux/amd64 in a multi config project",
dir: "testdata/multi-config-pods",
images: []image{
{name: "multi-config-module1", pod: "module1"},
{name: "multi-config-module2", pod: "module2"},
},
tag: "multiplatform-integration-test",
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
MarkIntegrationTest(t, NeedsGcp)
platforms := platformsCliValue(test.expectedPlatforms)
ns, client := SetupNamespace(t)
tag := fmt.Sprintf("%s-%s", test.tag, uuid.New().String())
args := []string{"--platform", platforms, "--default-repo", defaultRepo, "--tag", tag, "--cache-artifacts=false"}
expectedPlatforms := expectedPlatformsForRunningCluster(test.expectedPlatforms)
skaffold.Run(args...).InDir(test.dir).InNs(ns.Name).RunOrFail(t)
defer skaffold.Delete().InDir(test.dir).InNs(ns.Name).RunOrFail(t)
for _, image := range test.images {
checkRemoteImagePlatforms(t, fmt.Sprintf("%s/%s:%s", defaultRepo, image.name, tag), expectedPlatforms)
if isRunningInHybridCluster {
pod := client.GetPod(image.pod)
checkNodeAffinity(t, test.expectedPlatforms, pod)
}
}
})
}
}
func TestMultiplatformWithDevAndDebug(t *testing.T) {
const platformsExpectedInNodeAffinity = 1
const platformsExpectedInCreatedImage = 1
isRunningInHybridCluster := os.Getenv("GKE_CLUSTER_NAME") == hybridClusterName
type image struct {
name string
pod string
}
tests := []struct {
description string
dir string
images []image
tag string
command func(args ...string) *skaffold.RunBuilder
expectedPlatforms []v1.Platform
}{
{
description: "Debug with multiplatform linux/arm64 and linux/amd64",
dir: "examples/cross-platform-builds",
images: []image{{name: "skaffold-example", pod: "getting-started"}},
tag: "multiplatform-integration-test",
command: skaffold.Debug,
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
{
description: "Debug with multiplatform linux/arm64 and linux/amd64 in a multi config project",
dir: "testdata/multi-config-pods",
images: []image{
{name: "multi-config-module1", pod: "module1"},
{name: "multi-config-module2", pod: "module2"},
},
tag: "multiplatform-integration-test",
command: skaffold.Debug,
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
{
description: "Dev with multiplatform linux/arm64 and linux/amd64",
dir: "examples/cross-platform-builds",
images: []image{{name: "skaffold-example", pod: "getting-started"}},
tag: "multiplatform-integration-test",
command: skaffold.Dev,
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
{
description: "Dev with multiplatform linux/arm64 and linux/amd64 in a multi config project",
dir: "testdata/multi-config-pods",
images: []image{
{name: "multi-config-module1", pod: "module1"},
{name: "multi-config-module2", pod: "module2"},
},
tag: "multiplatform-integration-test",
command: skaffold.Dev,
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
MarkIntegrationTest(t, NeedsGcp)
platforms := platformsCliValue(test.expectedPlatforms)
tag := fmt.Sprintf("%s-%s", test.tag, uuid.New().String())
ns, client := SetupNamespace(t)
args := []string{"--platform", platforms, "--default-repo", defaultRepo, "--tag", tag, "--cache-artifacts=false"}
expectedPlatforms := expectedPlatformsForRunningCluster(test.expectedPlatforms)
test.command(args...).InDir(test.dir).InNs(ns.Name).RunBackground(t)
defer skaffold.Delete().InDir(test.dir).InNs(ns.Name).Run(t)
for _, image := range test.images {
client.WaitForPodsReady(image.pod)
createdImagePlatforms, err := docker.GetPlatforms(fmt.Sprintf("%s/%s:%s", defaultRepo, image.name, tag))
failNowIfError(t, err)
if len(createdImagePlatforms) != platformsExpectedInCreatedImage {
t.Fatalf("there are more platforms in created Image than expected, found %v, expected %v", len(createdImagePlatforms), platformsExpectedInCreatedImage)
}
checkIfAPlatformMatch(t, expectedPlatforms, createdImagePlatforms[0])
if isRunningInHybridCluster {
pod := client.GetPod(image.pod)
failIfNodeAffinityNotSet(t, pod)
nodeAffinityPlatforms := getPlatformsFromNodeAffinity(pod)
platformsInNodeAffinity := len(nodeAffinityPlatforms)
if platformsInNodeAffinity != platformsExpectedInNodeAffinity {
t.Fatalf("there are more platforms in NodeAffinity than expected, found %v, expected %v", platformsInNodeAffinity, platformsExpectedInNodeAffinity)
}
checkIfAPlatformMatch(t, expectedPlatforms, nodeAffinityPlatforms[0])
}
}
})
}
}
func TestMultiplatformWithDeploy(t *testing.T) {
isRunningInHybridCluster := os.Getenv("GKE_CLUSTER_NAME") == hybridClusterName
type image struct {
name string
pod string
}
tests := []struct {
description string
dir string
images []image
tag string
expectedPlatforms []v1.Platform
}{
{
description: "Deploy with multiplatform linux/arm64 and linux/amd64",
dir: "examples/cross-platform-builds",
images: []image{{name: "skaffold-example", pod: "getting-started"}},
tag: "multiplatform-integration-test",
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
{
description: "Deploy with multiplatform linux/arm64 and linux/amd64 in a multi config project",
dir: "testdata/multi-config-pods",
images: []image{
{name: "multi-config-module1", pod: "module1"},
{name: "multi-config-module2", pod: "module2"},
},
tag: "multiplatform-integration-test",
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}, {OS: "linux", Architecture: "amd64"}},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
MarkIntegrationTest(t, NeedsGcp)
tmpfile := testutil.TempFile(t, "", []byte{})
tag := fmt.Sprintf("%s-%s", test.tag, uuid.New().String())
platforms := platformsCliValue(test.expectedPlatforms)
argsBuild := []string{"--platform", platforms, "--default-repo", defaultRepo, "--tag", tag, "--cache-artifacts=false", "--file-output", tmpfile}
argsDeploy := []string{"--build-artifacts", tmpfile, "--default-repo", defaultRepo, "--enable-platform-node-affinity=true"}
skaffold.Build(argsBuild...).InDir(test.dir).RunOrFail(t)
ns, client := SetupNamespace(t)
skaffold.Deploy(argsDeploy...).InDir(test.dir).InNs(ns.Name).RunOrFail(t)
defer skaffold.Delete().InDir(test.dir).InNs(ns.Name).RunOrFail(t)
for _, image := range test.images {
checkRemoteImagePlatforms(t, fmt.Sprintf("%s/%s:%s", defaultRepo, image.name, tag), test.expectedPlatforms)
if isRunningInHybridCluster {
pod := client.GetPod(image.pod)
checkNodeAffinity(t, test.expectedPlatforms, pod)
}
}
})
}
}
func checkNodeAffinity(t *testing.T, expectedPlatforms []v1.Platform, pod *k8sv1.Pod) {
failIfNodeAffinityNotSet(t, pod)
nodeAffinityPlatforms := getPlatformsFromNodeAffinity(pod)
checkPlatformsEqual(t, nodeAffinityPlatforms, expectedPlatforms)
}
func failIfNodeAffinityNotSet(t *testing.T, pod *k8sv1.Pod) {
if pod.Spec.Affinity == nil {
t.Fatalf("Affinity not defined in spec")
}
if pod.Spec.Affinity.NodeAffinity == nil {
t.Fatalf("NodeAffinity not defined in spec")
}
if pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
t.Fatalf("RequiredDuringSchedulingIgnoredDuringExecution not defined in spec")
}
if pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms == nil {
t.Fatalf("NodeSelectorTerms not defined in spec")
}
}
func getPlatformsFromNodeAffinity(pod *k8sv1.Pod) []v1.Platform {
var platforms []v1.Platform
nodeAffinityPlatforms := pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
for _, np := range nodeAffinityPlatforms {
os, arch := "", ""
for _, me := range np.MatchExpressions {
if me.Key == "kubernetes.io/os" {
os = strings.Join(me.Values, "")
}
if me.Key == "kubernetes.io/arch" {
arch = strings.Join(me.Values, "")
}
}
platforms = append(platforms, v1.Platform{OS: os, Architecture: arch})
}
return platforms
}
func platformsCliValue(platforms []v1.Platform) string {
var platformsCliValue []string
for _, platform := range platforms {
platformsCliValue = append(platformsCliValue, fmt.Sprintf("%s/%s", platform.OS, platform.Architecture))
}
return strings.Join(platformsCliValue, ",")
}
func expectedPlatformsForRunningCluster(platforms []v1.Platform) []v1.Platform {
switch clusterName := os.Getenv("GKE_CLUSTER_NAME"); clusterName {
case hybridClusterName:
return platforms
case armClusterName:
return []v1.Platform{{OS: "linux", Architecture: "arm64"}}
default:
return []v1.Platform{{OS: "linux", Architecture: "amd64"}}
}
}
func checkIfAPlatformMatch(t *testing.T, platforms []v1.Platform, expectedPlatform v1.Platform) {
const expectedMatchedPlatforms = 1
matchedPlatforms := 0
nodeAffinityPlatformValue := expectedPlatform.OS + "/" + expectedPlatform.Architecture
for _, platform := range platforms {
expectedPlatformValue := platform.OS + "/" + platform.Architecture
if nodeAffinityPlatformValue == expectedPlatformValue {
matchedPlatforms++
}
}
if matchedPlatforms != expectedMatchedPlatforms {
t.Fatalf("Number of matched platforms should be %v", expectedMatchedPlatforms)
}
}