/
clone_test.go
500 lines (429 loc) · 18.5 KB
/
clone_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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//go:build windows && functional
// +build windows,functional
package cri_containerd
import (
"context"
"fmt"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/Microsoft/hcsshim/osversion"
"github.com/Microsoft/hcsshim/pkg/annotations"
"github.com/Microsoft/hcsshim/test/internal/require"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
// returns a request config for creating a template sandbox
func getTemplatePodConfig(name string) *runtime.RunPodSandboxRequest {
return &runtime.RunPodSandboxRequest{
Config: &runtime.PodSandboxConfig{
Metadata: &runtime.PodSandboxMetadata{
Name: name,
Uid: "0",
Namespace: testNamespace,
},
Annotations: map[string]string{
annotations.SaveAsTemplate: "true",
},
},
RuntimeHandler: wcowHypervisorRuntimeHandler,
}
}
// returns a request config for creating a template container
func getTemplateContainerConfig(name string) *runtime.CreateContainerRequest {
return &runtime.CreateContainerRequest{
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: name,
},
Image: &runtime.ImageSpec{
Image: imageWindowsNanoserver,
},
// Do not keep the ping running on template containers.
Command: []string{
"ping",
"127.0.0.1",
},
Annotations: map[string]string{
annotations.SaveAsTemplate: "true",
},
},
}
}
// returns a request config for creating a standard container
func getStandardContainerConfig(name string) *runtime.CreateContainerRequest {
return &runtime.CreateContainerRequest{
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: name,
},
Image: &runtime.ImageSpec{
Image: imageWindowsNanoserver,
},
Command: []string{
"ping",
"-t",
"127.0.0.1",
},
},
}
}
// returns a create cloned sandbox request config.
func getClonedPodConfig(uniqueID int, templateid string) *runtime.RunPodSandboxRequest {
return &runtime.RunPodSandboxRequest{
Config: &runtime.PodSandboxConfig{
Metadata: &runtime.PodSandboxMetadata{
Name: fmt.Sprintf("clonedpod-%d", uniqueID),
Uid: "0",
Namespace: testNamespace,
},
Annotations: map[string]string{
annotations.TemplateID: templateid + "@vm",
},
},
RuntimeHandler: wcowHypervisorRuntimeHandler,
}
}
// returns a create cloned container request config.
func getClonedContainerConfig(uniqueID int, templateid string) *runtime.CreateContainerRequest {
return &runtime.CreateContainerRequest{
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: fmt.Sprintf("clonedcontainer-%d", uniqueID),
},
Image: &runtime.ImageSpec{
Image: imageWindowsNanoserver,
},
// Command for cloned containers
Command: []string{
"ping",
"-t",
"127.0.0.1",
},
Annotations: map[string]string{
annotations.TemplateID: templateid,
},
},
}
}
func waitForTemplateSave(ctx context.Context, t *testing.T, templatePodID string) {
app := "hcsdiag"
arg0 := "list"
for {
cmd := exec.Command(app, arg0)
stdout, err := cmd.Output()
if err != nil {
t.Fatalf("failed while waiting for save template to finish: %s", err)
}
if strings.Contains(string(stdout), templatePodID) && strings.Contains(string(stdout), "SavedAsTemplate") {
break
}
timer := time.NewTimer(time.Millisecond * 100)
select {
case <-ctx.Done():
t.Fatalf("Timelimit exceeded for wait for template saving to finish")
case <-timer.C:
}
}
}
func createPodAndContainer(ctx context.Context, t *testing.T, client runtime.RuntimeServiceClient, sandboxRequest *runtime.RunPodSandboxRequest, containerRequest *runtime.CreateContainerRequest, podID, containerID *string) {
// This is required in order to avoid leaking a pod and/or container in case somethings fails
// during container creation of startup. The podID (and containerID if container creation was
// successful) will be set to correct values so that the caller can correctly cleanup them even
// in case of a failure.
*podID = ""
*containerID = ""
*podID = runPodSandbox(t, client, ctx, sandboxRequest)
containerRequest.PodSandboxId = *podID
containerRequest.SandboxConfig = sandboxRequest.Config
*containerID = createContainer(t, client, ctx, containerRequest)
startContainer(t, client, ctx, *containerID)
}
// Creates a template sandbox and then a template container inside it.
// Since, template container can take time to finish the init process and then exit (at which
// point it will actually be saved as a template) this function wait until the template is
// actually saved.
// It is the callers responsibility to clean the stop and remove the cloned
// containers and pods.
func createTemplateContainer(ctx context.Context, t *testing.T, client runtime.RuntimeServiceClient, templateSandboxRequest *runtime.RunPodSandboxRequest, templateContainerRequest *runtime.CreateContainerRequest, templatePodID, templateContainerID *string) {
createPodAndContainer(ctx, t, client, templateSandboxRequest, templateContainerRequest, templatePodID, templateContainerID)
// Send a context with deadline for waitForTemplateSave function
d := time.Now().Add(10 * time.Second)
ctx, cancel := context.WithDeadline(ctx, d)
defer cancel()
waitForTemplateSave(ctx, t, *templatePodID)
return
}
// Creates a clone from the given template pod and container.
// It is the callers responsibility to clean the stop and remove the cloned
// containers and pods.
func createClonedContainer(ctx context.Context, t *testing.T, client runtime.RuntimeServiceClient, templatePodID, templateContainerID string, cloneNumber int, clonedPodID, clonedContainerID *string) {
cloneSandboxRequest := getClonedPodConfig(cloneNumber, templatePodID)
cloneContainerRequest := getClonedContainerConfig(cloneNumber, templateContainerID)
createPodAndContainer(ctx, t, client, cloneSandboxRequest, cloneContainerRequest, clonedPodID, clonedContainerID)
return
}
func verifyTemplateContainerState(ctx context.Context, t *testing.T, client runtime.RuntimeServiceClient, templateContainerID string) {
// sometimes it takes a little longer for containerd to update status of the template container
// so check it in a loop.
tries := 3
var templateContainerState runtime.ContainerState
for i := 0; i < tries; i++ {
templateContainerState := getContainerStatus(t, client, ctx, templateContainerID)
if templateContainerState == runtime.ContainerState_CONTAINER_EXITED {
return
}
time.Sleep(1 * time.Second)
}
t.Fatalf("template container %s expected state EXITED actual: %s", templateContainerID, runtime.ContainerState_name[int32(templateContainerState)])
}
// Runs a command inside given container and verifies if the command executes successfully.
func verifyContainerExec(ctx context.Context, t *testing.T, client runtime.RuntimeServiceClient, containerID string) {
execCommand := []string{
"ping",
"127.0.0.1",
}
execRequest := &runtime.ExecSyncRequest{
ContainerId: containerID,
Cmd: execCommand,
Timeout: 20,
}
r := execSync(t, client, ctx, execRequest)
output := strings.TrimSpace(string(r.Stdout))
errorMsg := string(r.Stderr)
exitCode := int(r.ExitCode)
if exitCode != 0 || len(errorMsg) != 0 {
t.Fatalf("Failed execution inside container %s with error: %s, exitCode: %d", containerID, errorMsg, exitCode)
} else {
t.Logf("Exec(container: %s) stdout: %s, stderr: %s, exitCode: %d\n", containerID, output, errorMsg, exitCode)
}
}
func cleanupPod(t *testing.T, client runtime.RuntimeServiceClient, ctx context.Context, podID *string) {
if *podID == "" {
// Do nothing for empty podID
return
}
stopPodSandbox(t, client, ctx, *podID)
removePodSandbox(t, client, ctx, *podID)
}
func cleanupContainer(t *testing.T, client runtime.RuntimeServiceClient, ctx context.Context, containerID *string) {
if *containerID == "" {
// Do nothing for empty containerID
return
}
stopContainer(t, client, ctx, *containerID)
removeContainer(t, client, ctx, *containerID)
}
// A simple test to just create a template container and then create one
// cloned container from that template.
func Test_CloneContainer_WCOW(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
var templatePodID, templateContainerID string
var clonedPodID, clonedContainerID string
// send pointers so that immediate evaluation of arguments doesn't evaluate to empty strings
defer cleanupPod(t, client, ctx, &templatePodID)
defer cleanupContainer(t, client, ctx, &templateContainerID)
defer cleanupPod(t, client, ctx, &clonedPodID)
defer cleanupContainer(t, client, ctx, &clonedContainerID)
pullRequiredImages(t, []string{imageWindowsNanoserver})
createTemplateContainer(ctx, t, client, getTemplatePodConfig("templatepod"), getTemplateContainerConfig("templatecontainer"), &templatePodID, &templateContainerID)
verifyTemplateContainerState(ctx, t, client, templateContainerID)
createClonedContainer(ctx, t, client, templatePodID, templateContainerID, 1, &clonedPodID, &clonedContainerID)
verifyContainerExec(ctx, t, client, clonedContainerID)
}
// A test for creating multiple clones(3 clones) from one template container.
func Test_MultiplClonedContainers_WCOW(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
nClones := 3
var templatePodID, templateContainerID string
clonedPodIDs := make([]string, nClones)
clonedContainerIDs := make([]string, nClones)
defer cleanupPod(t, client, ctx, &templatePodID)
defer cleanupContainer(t, client, ctx, &templateContainerID)
for i := 0; i < nClones; i++ {
defer cleanupPod(t, client, ctx, &clonedPodIDs[i])
defer cleanupContainer(t, client, ctx, &clonedContainerIDs[i])
}
pullRequiredImages(t, []string{imageWindowsNanoserver})
// create template pod & container
createTemplateContainer(ctx, t, client, getTemplatePodConfig("templatepod"), getTemplateContainerConfig("templatecontainer"), &templatePodID, &templateContainerID)
verifyTemplateContainerState(ctx, t, client, templateContainerID)
// create multiple clones
for i := 0; i < nClones; i++ {
createClonedContainer(ctx, t, client, templatePodID, templateContainerID, i, &clonedPodIDs[i], &clonedContainerIDs[i])
}
for i := 0; i < nClones; i++ {
verifyContainerExec(ctx, t, client, clonedContainerIDs[i])
}
}
// Test if a normal container can be created inside a clond pod alongside the cloned
// container.
func Test_NormalContainerInClonedPod_WCOW(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
var templatePodID, templateContainerID string
var clonedPodID, clonedContainerID string
var stdContainerID string
pullRequiredImages(t, []string{imageWindowsNanoserver})
defer cleanupPod(t, client, ctx, &templatePodID)
defer cleanupContainer(t, client, ctx, &templateContainerID)
defer cleanupPod(t, client, ctx, &clonedPodID)
defer cleanupContainer(t, client, ctx, &clonedContainerID)
defer cleanupContainer(t, client, ctx, &stdContainerID)
// create template pod & container
createTemplateContainer(ctx, t, client, getTemplatePodConfig("templatepod"), getTemplateContainerConfig("templatecontainer"), &templatePodID, &templateContainerID)
verifyTemplateContainerState(ctx, t, client, templateContainerID)
// create a cloned pod and a cloned container
cloneSandboxRequest := getClonedPodConfig(1, templatePodID)
cloneContainerRequest := getClonedContainerConfig(1, templateContainerID)
createPodAndContainer(ctx, t, client, cloneSandboxRequest, cloneContainerRequest, &clonedPodID, &clonedContainerID)
// create a normal container in cloned pod
stdContainerRequest := getStandardContainerConfig("standard-container")
stdContainerRequest.PodSandboxId = clonedPodID
stdContainerRequest.SandboxConfig = cloneSandboxRequest.Config
stdContainerID = createContainer(t, client, ctx, stdContainerRequest)
startContainer(t, client, ctx, stdContainerID)
verifyContainerExec(ctx, t, client, clonedContainerID)
verifyContainerExec(ctx, t, client, stdContainerID)
}
// A test for cloning multiple pods first and then cloning one container in each
// of those pods.
func Test_CloneContainersWithClonedPodPool_WCOW(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
nClones := 3
var templatePodID, templateContainerID string
clonedPodIDs := make([]string, nClones)
clonedContainerIDs := make([]string, nClones)
defer cleanupPod(t, client, ctx, &templatePodID)
defer cleanupContainer(t, client, ctx, &templateContainerID)
for i := 0; i < nClones; i++ {
defer cleanupPod(t, client, ctx, &clonedPodIDs[i])
defer cleanupContainer(t, client, ctx, &clonedContainerIDs[i])
}
pullRequiredImages(t, []string{imageWindowsNanoserver})
// create template pod & container
createTemplateContainer(ctx, t, client, getTemplatePodConfig("templatepod"), getTemplateContainerConfig("templatecontainer"), &templatePodID, &templateContainerID)
verifyTemplateContainerState(ctx, t, client, templateContainerID)
// create multiple pods
clonedSandboxRequests := []*runtime.RunPodSandboxRequest{}
for i := 0; i < nClones; i++ {
cloneSandboxRequest := getClonedPodConfig(i, templatePodID)
clonedPodIDs[i] = runPodSandbox(t, client, ctx, cloneSandboxRequest)
clonedSandboxRequests = append(clonedSandboxRequests, cloneSandboxRequest)
}
// create multiple clones
for i := 0; i < nClones; i++ {
cloneContainerRequest := getClonedContainerConfig(i, templateContainerID)
cloneContainerRequest.PodSandboxId = clonedPodIDs[i]
cloneContainerRequest.SandboxConfig = clonedSandboxRequests[i].Config
clonedContainerIDs[i] = createContainer(t, client, ctx, cloneContainerRequest)
startContainer(t, client, ctx, clonedContainerIDs[i])
}
for i := 0; i < nClones; i++ {
verifyContainerExec(ctx, t, client, clonedContainerIDs[i])
}
}
func Test_ClonedContainerRunningAfterDeletingTemplate(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
var templatePodID, templateContainerID string
var clonedPodID, clonedContainerID string
// send pointers so that immediate evaluation of arguments doesn't evaluate to empty strings
defer cleanupPod(t, client, ctx, &templatePodID)
defer cleanupContainer(t, client, ctx, &templateContainerID)
defer cleanupPod(t, client, ctx, &clonedPodID)
defer cleanupContainer(t, client, ctx, &clonedContainerID)
pullRequiredImages(t, []string{imageWindowsNanoserver})
createTemplateContainer(ctx, t, client, getTemplatePodConfig("templatepod"), getTemplateContainerConfig("templatecontainer"), &templatePodID, &templateContainerID)
verifyTemplateContainerState(ctx, t, client, templateContainerID)
createClonedContainer(ctx, t, client, templatePodID, templateContainerID, 1, &clonedPodID, &clonedContainerID)
stopPodSandbox(t, client, ctx, templatePodID)
removePodSandbox(t, client, ctx, templatePodID)
// Make sure cleanup function doesn't try to cleanup this deleted pod.
templatePodID = ""
templateContainerID = ""
verifyContainerExec(ctx, t, client, clonedContainerID)
}
// A test to verify that multiple templats can be created and clones
// can be made from each of them simultaneously.
func Test_MultipleTemplateAndClones_WCOW(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
nTemplates := 2
var wg sync.WaitGroup
templatePodIDs := make([]string, nTemplates)
templateContainerIDs := make([]string, nTemplates)
clonedPodIDs := make([]string, nTemplates)
clonedContainerIDs := make([]string, nTemplates)
for i := 0; i < nTemplates; i++ {
defer cleanupPod(t, client, ctx, &templatePodIDs[i])
defer cleanupContainer(t, client, ctx, &templateContainerIDs[i])
defer cleanupPod(t, client, ctx, &clonedPodIDs[i])
defer cleanupContainer(t, client, ctx, &clonedContainerIDs[i])
}
pullRequiredImages(t, []string{imageWindowsNanoserver})
wg.Add(nTemplates)
for i := 0; i < nTemplates; i++ {
go func(index int) {
defer wg.Done()
createTemplateContainer(ctx, t, client, getTemplatePodConfig(fmt.Sprintf("templatepod-%d", index)), getTemplateContainerConfig(fmt.Sprintf("templatecontainer-%d", index)), &templatePodIDs[index], &templateContainerIDs[index])
verifyTemplateContainerState(ctx, t, client, templateContainerIDs[index])
createClonedContainer(ctx, t, client, templatePodIDs[index], templateContainerIDs[index], index, &clonedPodIDs[index], &clonedContainerIDs[index])
}(i)
}
// Wait before all template & clone creations are done.
wg.Wait()
for i := 0; i < nTemplates; i++ {
verifyContainerExec(ctx, t, client, clonedContainerIDs[i])
}
}
// Tries to create a clone with a different memory config than the template
// and verifies that the request correctly fails with an error.
func Test_VerifyCloneAndTemplateConfig(t *testing.T) {
requireFeatures(t, featureWCOWHypervisor)
require.Build(t, osversion.V20H2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := newTestRuntimeClient(t)
var templatePodID, templateContainerID string
var clonedPodID string
// send pointers so that immediate evaluation of arguments doesn't evaluate to empty strings
defer cleanupPod(t, client, ctx, &templatePodID)
defer cleanupContainer(t, client, ctx, &templateContainerID)
defer cleanupPod(t, client, ctx, &clonedPodID)
pullRequiredImages(t, []string{imageWindowsNanoserver})
templatePodConfig := getTemplatePodConfig("templatepod")
createTemplateContainer(ctx, t, client, templatePodConfig, getTemplateContainerConfig("templatecontainer"), &templatePodID, &templateContainerID)
verifyTemplateContainerState(ctx, t, client, templateContainerID)
// change pod config to make sure the request fails
cloneSandboxRequest := getClonedPodConfig(0, templatePodID)
cloneSandboxRequest.Config.Annotations[annotations.AllowOvercommit] = "false"
_, err := client.RunPodSandbox(ctx, cloneSandboxRequest)
if err == nil {
t.Fatalf("pod cloning should fail with mismatching configurations error")
} else if !strings.Contains(err.Error(), "doesn't match") {
t.Fatalf("Expected mismatching configurations error got: %s", err)
}
}