forked from kubernetes-sigs/cri-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
647 lines (534 loc) · 23.9 KB
/
container.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
Copyright 2017 The Kubernetes 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 validate
import (
"bufio"
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/kubernetes-sigs/cri-tools/pkg/framework"
internalapi "k8s.io/cri-api/pkg/apis"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// streamType is the type of the stream.
type streamType string
const (
defaultStopContainerTimeout int64 = 60
defaultExecSyncTimeout int64 = 30
defaultLog string = "hello World"
stdoutType streamType = "stdout"
stderrType streamType = "stderr"
)
// logMessage is the internal log type.
type logMessage struct {
timestamp time.Time
stream streamType
log string
}
var _ = framework.KubeDescribe("Container", func() {
f := framework.NewDefaultCRIFramework()
var rc internalapi.RuntimeService
var ic internalapi.ImageManagerService
BeforeEach(func() {
rc = f.CRIClient.CRIRuntimeClient
ic = f.CRIClient.CRIImageClient
})
Context("runtime should support basic operations on container", func() {
var podID string
var podConfig *runtimeapi.PodSandboxConfig
BeforeEach(func() {
podID, podConfig = framework.CreatePodSandboxForContainer(rc)
})
AfterEach(func() {
By("stop PodSandbox")
rc.StopPodSandbox(context.TODO(), podID)
By("delete PodSandbox")
rc.RemovePodSandbox(context.TODO(), podID)
})
It("runtime should support creating container [Conformance]", func() {
By("test create a default container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-create-")
By("test list container")
containers := listContainerForID(rc, containerID)
Expect(containerFound(containers, containerID)).To(BeTrue(), "Container should be created")
})
It("runtime should support starting container [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-start-test-")
By("test start container")
testStartContainer(rc, containerID)
})
It("runtime should support stopping container [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stop-test-")
By("start container")
startContainer(rc, containerID)
By("test stop container")
testStopContainer(rc, containerID)
})
It("runtime should support removing created container [Conformance]", func() {
By("create container")
containerID := framework.CreatePauseContainer(rc, ic, podID, podConfig, "container-for-remove-created-test-")
By("test remove container")
removeContainer(rc, containerID)
containers := listContainerForID(rc, containerID)
Expect(containerFound(containers, containerID)).To(BeFalse(), "Container should be removed")
})
It("runtime should support removing running container [Conformance]", func() {
By("create container")
containerID := framework.CreatePauseContainer(rc, ic, podID, podConfig, "container-for-remove-running-test-")
By("start container")
startContainer(rc, containerID)
By("test remove container")
removeContainer(rc, containerID)
containers := listContainerForID(rc, containerID)
Expect(containerFound(containers, containerID)).To(BeFalse(), "Container should be removed")
})
It("runtime should support removing stopped container [Conformance]", func() {
By("create container")
containerID := framework.CreatePauseContainer(rc, ic, podID, podConfig, "container-for-remove-stopped-test-")
By("start container")
startContainer(rc, containerID)
By("test stop container")
testStopContainer(rc, containerID)
By("test remove container")
removeContainer(rc, containerID)
containers := listContainerForID(rc, containerID)
Expect(containerFound(containers, containerID)).To(BeFalse(), "Container should be removed")
})
It("runtime should support execSync [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-execSync-test-")
By("start container")
startContainer(rc, containerID)
By("test execSync")
verifyExecSyncOutput(rc, containerID, echoHelloCmd, echoHelloOutput)
})
It("runtime should support execSync with timeout [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-execSync-timeout-test-")
By("start container")
startContainer(rc, containerID)
By("test execSync with timeout")
_, _, err := rc.ExecSync(context.TODO(), containerID, sleepCmd, time.Second)
Expect(err).Should(HaveOccurred(), "execSync should timeout")
By("timeout exec process should be gone")
stdout, stderr, err := rc.ExecSync(context.TODO(), containerID, checkSleepCmd,
time.Duration(defaultExecSyncTimeout)*time.Second)
framework.ExpectNoError(err)
Expect(string(stderr)).To(BeEmpty())
Expect(strings.TrimSpace(string(stdout))).To(BeEmpty())
})
It("runtime should support listing container stats [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stats-")
By("start container")
startContainer(rc, containerID)
By("test container stats")
stats := listContainerStatsForID(rc, containerID)
Expect(stats.Attributes.Id).To(Equal(containerID))
Expect(stats.Attributes.Metadata.Name).To(ContainSubstring("container-for-stats-"))
Expect(stats.Cpu.Timestamp).NotTo(BeZero())
Expect(stats.Memory.Timestamp).NotTo(BeZero())
})
It("runtime should support listing stats for started containers [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stats-")
By("start container")
startContainer(rc, containerID)
filter := &runtimeapi.ContainerStatsFilter{
Id: containerID,
}
By("test container stats")
stats := listContainerStats(rc, filter)
Expect(statFound(stats, containerID)).To(BeTrue(), "Container should be created")
})
It("runtime should support listing stats for started containers when filter is nil [Conformance]", func() {
By("create container")
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stats-with-nil-filter-")
By("start container")
startContainer(rc, containerID)
By("test container stats")
stats := listContainerStats(rc, nil)
Expect(statFound(stats, containerID)).To(BeTrue(), "Stats should be found")
})
It("runtime should support listing stats for three created containers when filter is nil. [Conformance]", func() {
By("create first container ")
firstContainerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stats-with-nil-filter-")
By("create second container ")
secondContainerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stats-with-nil-filter-")
By("create third container ")
thirdContainerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-stats-with-nil-filter-")
By("start first container")
startContainer(rc, firstContainerID)
By("start second container")
startContainer(rc, secondContainerID)
By("start third container")
startContainer(rc, thirdContainerID)
By("test containers stats")
stats := listContainerStats(rc, nil)
Expect(statFound(stats, firstContainerID)).To(BeTrue(), "Stats should be found")
Expect(statFound(stats, secondContainerID)).To(BeTrue(), "Stats should be found")
Expect(statFound(stats, thirdContainerID)).To(BeTrue(), "Stats should be found")
})
})
Context("runtime should support adding volume and device", func() {
var podID string
var podConfig *runtimeapi.PodSandboxConfig
BeforeEach(func() {
podID, podConfig = framework.CreatePodSandboxForContainer(rc)
})
AfterEach(func() {
By("stop PodSandbox")
rc.StopPodSandbox(context.TODO(), podID)
By("delete PodSandbox")
rc.RemovePodSandbox(context.TODO(), podID)
})
It("runtime should support starting container with volume [Conformance]", func() {
By("create host path and flag file")
hostPath, _ := createHostPath(podID)
defer os.RemoveAll(hostPath) // clean up the TempDir
By("create container with volume")
containerID := createVolumeContainer(rc, ic, "container-with-volume-test-", podID, podConfig, hostPath)
By("test start container with volume")
testStartContainer(rc, containerID)
By("check whether 'hostPath' contains file or dir in container")
output := execSyncContainer(rc, containerID, checkPathCmd(hostPath))
Expect(len(output)).NotTo(BeZero(), "len(output) should not be zero.")
})
It("runtime should support starting container with volume when host path is a symlink [Conformance]", func() {
By("create host path and flag file")
hostPath, _ := createHostPath(podID)
defer os.RemoveAll(hostPath) // clean up the TempDir
By("create symlink")
symlinkPath := createSymlink(hostPath)
defer os.RemoveAll(symlinkPath) // clean up the symlink
By("create volume container with symlink host path")
containerID := createVolumeContainer(rc, ic, "container-with-symlink-host-path-test-", podID, podConfig, symlinkPath)
By("test start volume container with symlink host path")
testStartContainer(rc, containerID)
By("check whether 'symlink' contains file or dir in container")
output := execSyncContainer(rc, containerID, checkPathCmd(symlinkPath))
Expect(len(output)).NotTo(BeZero(), "len(output) should not be zero.")
})
// TODO(random-liu): Decide whether to add host path not exist test when https://github.com/kubernetes/kubernetes/pull/61460
// is finalized.
})
Context("runtime should support log", func() {
var podID, hostPath string
var podConfig *runtimeapi.PodSandboxConfig
BeforeEach(func() {
podID, podConfig, hostPath = createPodSandboxWithLogDirectory(rc)
})
AfterEach(func() {
By("stop PodSandbox")
rc.StopPodSandbox(context.TODO(), podID)
By("delete PodSandbox")
rc.RemovePodSandbox(context.TODO(), podID)
By("clean up the TempDir")
os.RemoveAll(hostPath)
})
It("runtime should support starting container with log [Conformance]", func() {
By("create container with log")
logPath, containerID := createLogContainer(rc, ic, "container-with-log-test-", podID, podConfig)
By("start container with log")
startContainer(rc, containerID)
// wait container exited and check the status.
Eventually(func() runtimeapi.ContainerState {
return getContainerStatus(rc, containerID).State
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_EXITED))
By("check the log context")
expectedLogMessage := defaultLog + "\n"
verifyLogContents(podConfig, logPath, expectedLogMessage, stdoutType)
})
It("runtime should support reopening container log [Conformance]", func() {
By("create container with log")
logPath, containerID := createKeepLoggingContainer(rc, ic, "container-reopen-log-test-", podID, podConfig)
By("start container with log")
startContainer(rc, containerID)
Eventually(func() []logMessage {
return parseLogLine(podConfig, logPath)
}, time.Minute, time.Second).ShouldNot(BeEmpty(), "container log should be generated")
By("rename container log")
newLogPath := logPath + ".new"
Expect(os.Rename(filepath.Join(podConfig.LogDirectory, logPath),
filepath.Join(podConfig.LogDirectory, newLogPath))).To(Succeed())
By("reopen container log")
Expect(rc.ReopenContainerLog(context.TODO(), containerID)).To(Succeed())
Expect(pathExists(filepath.Join(podConfig.LogDirectory, logPath))).To(
BeTrue(), "new container log file should be created")
Eventually(func() []logMessage {
return parseLogLine(podConfig, logPath)
}, time.Minute, time.Second).ShouldNot(BeEmpty(), "new container log should be generated")
oldLength := len(parseLogLine(podConfig, newLogPath))
Consistently(func() int {
return len(parseLogLine(podConfig, newLogPath))
}, 5*time.Second, time.Second).Should(Equal(oldLength), "old container log should not change")
})
})
})
// containerFound returns whether containers is found.
func containerFound(containers []*runtimeapi.Container, containerID string) bool {
for _, container := range containers {
if container.Id == containerID {
return true
}
}
return false
}
// statFound returns whether stat is found.
func statFound(stats []*runtimeapi.ContainerStats, containerID string) bool {
for _, stat := range stats {
if stat.Attributes.Id == containerID {
return true
}
}
return false
}
// getContainerStatus gets ContainerState for containerID and fails if it gets error.
func getContainerStatus(c internalapi.RuntimeService, containerID string) *runtimeapi.ContainerStatus {
By("Get container status for containerID: " + containerID)
status, err := c.ContainerStatus(context.TODO(), containerID, false)
framework.ExpectNoError(err, "failed to get container %q status: %v", containerID, err)
return status.GetStatus()
}
// createShellContainer creates a container to run /bin/sh.
func createShellContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string) string {
containerName := prefix + framework.NewUUID()
containerConfig := &runtimeapi.ContainerConfig{
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Image: &runtimeapi.ImageSpec{Image: framework.TestContext.TestImageList.DefaultTestContainerImage},
Command: shellCmd,
Linux: &runtimeapi.LinuxContainerConfig{},
Stdin: true,
StdinOnce: true,
Tty: false,
}
return framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
}
// startContainer start the container for containerID.
func startContainer(c internalapi.RuntimeService, containerID string) {
By("Start container for containerID: " + containerID)
err := c.StartContainer(context.TODO(), containerID)
framework.ExpectNoError(err, "failed to start container: %v", err)
framework.Logf("Started container %q\n", containerID)
}
// testStartContainer starts the container for containerID and make sure it's running.
func testStartContainer(rc internalapi.RuntimeService, containerID string) {
startContainer(rc, containerID)
Eventually(func() runtimeapi.ContainerState {
return getContainerStatus(rc, containerID).State
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))
}
// stopContainer stops the container for containerID.
func stopContainer(c internalapi.RuntimeService, containerID string, timeout int64) {
By("Stop container for containerID: " + containerID)
stopped := make(chan bool, 1)
go func() {
defer GinkgoRecover()
err := c.StopContainer(context.TODO(), containerID, timeout)
framework.ExpectNoError(err, "failed to stop container: %v", err)
stopped <- true
}()
select {
case <-time.After(time.Duration(timeout) * time.Second):
framework.Failf("stop container %q timeout.\n", containerID)
case <-stopped:
framework.Logf("Stopped container %q\n", containerID)
}
}
// testStopContainer stops the container for containerID and make sure it's exited.
func testStopContainer(c internalapi.RuntimeService, containerID string) {
stopContainer(c, containerID, defaultStopContainerTimeout)
Eventually(func() runtimeapi.ContainerState {
return getContainerStatus(c, containerID).State
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_EXITED))
}
// removeContainer removes the container for containerID.
func removeContainer(c internalapi.RuntimeService, containerID string) {
By("Remove container for containerID: " + containerID)
err := c.RemoveContainer(context.TODO(), containerID)
framework.ExpectNoError(err, "failed to remove container: %v", err)
framework.Logf("Removed container %q\n", containerID)
}
// listContainerForID lists container for containerID.
func listContainerForID(c internalapi.RuntimeService, containerID string) []*runtimeapi.Container {
By("List containers for containerID: " + containerID)
filter := &runtimeapi.ContainerFilter{
Id: containerID,
}
containers, err := c.ListContainers(context.TODO(), filter)
framework.ExpectNoError(err, "failed to list containers %q status: %v", containerID, err)
return containers
}
// execSyncContainer test execSync for containerID and make sure the response is right.
func execSyncContainer(c internalapi.RuntimeService, containerID string, command []string) string {
By("execSync for containerID: " + containerID)
stdout, stderr, err := c.ExecSync(context.TODO(), containerID, command, time.Duration(defaultExecSyncTimeout)*time.Second)
framework.ExpectNoError(err, "failed to execSync in container %q", containerID)
Expect(string(stderr)).To(BeEmpty(), "The stderr should be empty.")
framework.Logf("Execsync succeed")
return string(stdout)
}
// execSyncContainer test execSync for containerID and make sure the response is right.
func verifyExecSyncOutput(c internalapi.RuntimeService, containerID string, command []string, expectedLogMessage string) {
By("verify execSync output")
stdout := execSyncContainer(c, containerID, command)
Expect(stdout).To(Equal(expectedLogMessage), "The stdout output of execSync should be %s", expectedLogMessage)
framework.Logf("verify Execsync output succeed")
}
// createHostPath creates the hostPath and flagFile for volume.
func createHostPath(podID string) (string, string) {
hostPath, err := ioutil.TempDir("", "test"+podID)
framework.ExpectNoError(err, "failed to create TempDir %q: %v", hostPath, err)
flagFile := "testVolume.file"
_, err = os.Create(filepath.Join(hostPath, flagFile))
framework.ExpectNoError(err, "failed to create volume file %q: %v", flagFile, err)
return hostPath, flagFile
}
// createSymlink creates a symlink of path.
func createSymlink(path string) string {
symlinkPath := path + "-symlink"
framework.ExpectNoError(os.Symlink(path, symlinkPath), "failed to create symlink %q", symlinkPath)
return symlinkPath
}
// createVolumeContainer creates a container with volume and the prefix of containerName and fails if it gets error.
func createVolumeContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig, hostPath string) string {
By("create a container with volume and name")
containerName := prefix + framework.NewUUID()
containerConfig := &runtimeapi.ContainerConfig{
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Image: &runtimeapi.ImageSpec{Image: framework.TestContext.TestImageList.DefaultTestContainerImage},
Command: pauseCmd,
// mount host path to the same directory in container, and will check if hostPath isn't empty
Mounts: []*runtimeapi.Mount{
{
HostPath: hostPath,
ContainerPath: hostPath,
SelinuxRelabel: true,
},
},
}
return framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
}
// createLogContainer creates a container with log and the prefix of containerName.
func createLogContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, string) {
By("create a container with log and name")
containerName := prefix + framework.NewUUID()
path := fmt.Sprintf("%s.log", containerName)
containerConfig := &runtimeapi.ContainerConfig{
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Image: &runtimeapi.ImageSpec{Image: framework.TestContext.TestImageList.DefaultTestContainerImage},
Command: logDefaultCmd,
LogPath: path,
}
return containerConfig.LogPath, framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
}
// createKeepLoggingContainer creates a container keeps logging defaultLog to output.
func createKeepLoggingContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, string) {
By("create a container with log and name")
containerName := prefix + framework.NewUUID()
path := fmt.Sprintf("%s.log", containerName)
containerConfig := &runtimeapi.ContainerConfig{
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Image: &runtimeapi.ImageSpec{Image: framework.TestContext.TestImageList.DefaultTestContainerImage},
Command: loopLogDefaultCmd,
LogPath: path,
}
return containerConfig.LogPath, framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
}
// pathExists check whether 'path' does exist or not
func pathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if errors.Is(err, os.ErrNotExist) {
return false
}
framework.ExpectNoError(err, "failed to check whether %q Exists: %v", path, err)
return false
}
// parseCRILog parses logs in CRI log format.
// CRI log format example :
//
// 2016-10-06T00:17:09.669794202Z stdout P The content of the log entry 1
// 2016-10-06T00:17:10.113242941Z stderr F The content of the log entry 2
func parseCRILog(log string, msg *logMessage) {
logMessage := strings.SplitN(log, " ", 4)
if len(log) < 4 {
err := errors.New("invalid CRI log")
framework.ExpectNoError(err, "failed to parse CRI log: %v", err)
}
timeStamp, err := time.Parse(time.RFC3339Nano, logMessage[0])
framework.ExpectNoError(err, "failed to parse timeStamp: %v", err)
stream := logMessage[1]
msg.timestamp = timeStamp
msg.stream = streamType(stream)
// Skip the tag field.
msg.log = logMessage[3] + "\n"
}
// parseLogLine parses log by row.
func parseLogLine(podConfig *runtimeapi.PodSandboxConfig, logPath string) []logMessage {
path := filepath.Join(podConfig.LogDirectory, logPath)
f, err := os.Open(path)
framework.ExpectNoError(err, "failed to open log file: %v", err)
framework.Logf("Open log file %s", path)
defer f.Close()
var msg logMessage
var msgLog []logMessage
scanner := bufio.NewScanner(f)
for scanner.Scan() {
parseCRILog(scanner.Text(), &msg)
msgLog = append(msgLog, msg)
}
if err := scanner.Err(); err != nil {
framework.ExpectNoError(err, "failed to read log by row: %v", err)
}
framework.Logf("Parse container log succeed")
return msgLog
}
// verifyLogContents verifies the contents of container log.
func verifyLogContents(podConfig *runtimeapi.PodSandboxConfig, logPath string, log string, stream streamType) {
By("verify log contents")
msgs := parseLogLine(podConfig, logPath)
found := false
for _, msg := range msgs {
if msg.log == log && msg.stream == stream {
found = true
break
}
}
Expect(found).To(BeTrue(), "expected log %q (stream=%q) not found in logs %+v", log, stream, msgs)
}
// listContainerStatsForID lists container for containerID.
func listContainerStatsForID(c internalapi.RuntimeService, containerID string) *runtimeapi.ContainerStats {
By("List container stats for containerID: " + containerID)
stats, err := c.ContainerStats(context.TODO(), containerID)
framework.ExpectNoError(err, "failed to list container stats for %q status: %v", containerID, err)
return stats
}
// listContainerStats lists stats for containers based on filter
func listContainerStats(c internalapi.RuntimeService, filter *runtimeapi.ContainerStatsFilter) []*runtimeapi.ContainerStats {
By("List container stats for all containers:")
stats, err := c.ListContainerStats(context.TODO(), filter)
framework.ExpectNoError(err, "failed to list container stats for containers status: %v", err)
return stats
}