Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 35802eb

Browse files
committed
Add integration test.
Signed-off-by: Lantao Liu <[email protected]>
1 parent d3bc918 commit 35802eb

File tree

3 files changed

+156
-30
lines changed

3 files changed

+156
-30
lines changed

integration/container_log_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2018 The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
23+
"path/filepath"
24+
"strings"
25+
"testing"
26+
"time"
27+
28+
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
30+
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
31+
32+
"github.com/containerd/cri/pkg/config"
33+
)
34+
35+
func TestLongContainerLog(t *testing.T) {
36+
testPodLogDir, err := ioutil.TempDir("/tmp", "long-container-log")
37+
require.NoError(t, err)
38+
defer os.RemoveAll(testPodLogDir)
39+
40+
t.Log("Create a sandbox with log directory")
41+
sbConfig := PodSandboxConfig("sandbox", "long-container-log",
42+
WithPodLogDirectory(testPodLogDir),
43+
)
44+
sb, err := runtimeService.RunPodSandbox(sbConfig)
45+
require.NoError(t, err)
46+
defer func() {
47+
assert.NoError(t, runtimeService.StopPodSandbox(sb))
48+
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
49+
}()
50+
51+
const (
52+
testImage = "busybox"
53+
containerName = "test-container"
54+
)
55+
t.Logf("Pull test image %q", testImage)
56+
img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil)
57+
require.NoError(t, err)
58+
defer func() {
59+
assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img}))
60+
}()
61+
62+
t.Log("Create a container with log path")
63+
defaultMaxSize := config.DefaultConfig().MaxContainerLogSize
64+
shortLineCmd := fmt.Sprintf("i=0; while [ $i -lt %d ]; do printf %s; i=$((i+1)); done", defaultMaxSize-1, "a")
65+
longLineCmd := fmt.Sprintf("i=0; while [ $i -lt %d ]; do printf %s; i=$((i+1)); done", defaultMaxSize+1, "b")
66+
cnConfig := ContainerConfig(
67+
containerName,
68+
"busybox",
69+
WithCommand("sh", "-c",
70+
fmt.Sprintf("%s; echo; %s", shortLineCmd, longLineCmd)),
71+
WithLogPath(containerName),
72+
)
73+
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
74+
require.NoError(t, err)
75+
76+
t.Log("Start the container")
77+
require.NoError(t, runtimeService.StartContainer(cn))
78+
79+
t.Log("Wait for container to finish running")
80+
require.NoError(t, Eventually(func() (bool, error) {
81+
s, err := runtimeService.ContainerStatus(cn)
82+
if err != nil {
83+
return false, err
84+
}
85+
if s.GetState() == runtime.ContainerState_CONTAINER_EXITED {
86+
return true, nil
87+
}
88+
return false, nil
89+
}, time.Second, 30*time.Second))
90+
91+
t.Log("Check container log")
92+
content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
93+
assert.NoError(t, err)
94+
checkContainerLog(t, string(content), []string{
95+
fmt.Sprintf("%s %s %s", runtime.Stdout, runtime.LogTagFull, strings.Repeat("a", defaultMaxSize-1)),
96+
fmt.Sprintf("%s %s %s", runtime.Stdout, runtime.LogTagPartial, strings.Repeat("b", defaultMaxSize)),
97+
fmt.Sprintf("%s %s %s", runtime.Stdout, runtime.LogTagFull, "b"),
98+
})
99+
}
100+
101+
func checkContainerLog(t *testing.T, log string, messages []string) {
102+
lines := strings.Split(strings.TrimSpace(log), "\n")
103+
require.Len(t, lines, len(messages), "log line number should match")
104+
for i, line := range lines {
105+
parts := strings.SplitN(line, " ", 2)
106+
require.Len(t, parts, 2)
107+
_, err := time.Parse(time.RFC3339Nano, parts[0])
108+
assert.NoError(t, err, "timestamp should be in RFC3339Nano format")
109+
assert.Equal(t, messages[i], parts[1], "log content should match")
110+
}
111+
}

integration/container_update_resources_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func checkMemoryLimit(t *testing.T, spec *runtimespec.Spec, memLimit int64) {
3737
}
3838

3939
func TestUpdateContainerResources(t *testing.T) {
40-
t.Logf("Create a sandbox")
40+
t.Log("Create a sandbox")
4141
sbConfig := PodSandboxConfig("sandbox", "update-container-resources")
4242
sb, err := runtimeService.RunPodSandbox(sbConfig)
4343
require.NoError(t, err)
@@ -46,7 +46,7 @@ func TestUpdateContainerResources(t *testing.T) {
4646
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
4747
}()
4848

49-
t.Logf("Create a container with memory limit")
49+
t.Log("Create a container with memory limit")
5050
cnConfig := ContainerConfig(
5151
"container",
5252
pauseImage,
@@ -57,48 +57,48 @@ func TestUpdateContainerResources(t *testing.T) {
5757
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
5858
require.NoError(t, err)
5959

60-
t.Logf("Check memory limit in container OCI spec")
60+
t.Log("Check memory limit in container OCI spec")
6161
container, err := containerdClient.LoadContainer(context.Background(), cn)
6262
require.NoError(t, err)
6363
spec, err := container.Spec(context.Background())
6464
require.NoError(t, err)
6565
checkMemoryLimit(t, spec, 2*1024*1024)
6666

67-
t.Logf("Update container memory limit after created")
67+
t.Log("Update container memory limit after created")
6868
err = runtimeService.UpdateContainerResources(cn, &runtime.LinuxContainerResources{
6969
MemoryLimitInBytes: 4 * 1024 * 1024,
7070
})
7171
require.NoError(t, err)
7272

73-
t.Logf("Check memory limit in container OCI spec")
73+
t.Log("Check memory limit in container OCI spec")
7474
spec, err = container.Spec(context.Background())
7575
require.NoError(t, err)
7676
checkMemoryLimit(t, spec, 4*1024*1024)
7777

78-
t.Logf("Start the container")
78+
t.Log("Start the container")
7979
require.NoError(t, runtimeService.StartContainer(cn))
8080
task, err := container.Task(context.Background(), nil)
8181
require.NoError(t, err)
8282

83-
t.Logf("Check memory limit in cgroup")
83+
t.Log("Check memory limit in cgroup")
8484
cgroup, err := cgroups.Load(cgroups.V1, cgroups.PidPath(int(task.Pid())))
8585
require.NoError(t, err)
8686
stat, err := cgroup.Stat(cgroups.IgnoreNotExist)
8787
require.NoError(t, err)
8888
assert.Equal(t, uint64(4*1024*1024), stat.Memory.Usage.Limit)
8989

90-
t.Logf("Update container memory limit after started")
90+
t.Log("Update container memory limit after started")
9191
err = runtimeService.UpdateContainerResources(cn, &runtime.LinuxContainerResources{
9292
MemoryLimitInBytes: 8 * 1024 * 1024,
9393
})
9494
require.NoError(t, err)
9595

96-
t.Logf("Check memory limit in container OCI spec")
96+
t.Log("Check memory limit in container OCI spec")
9797
spec, err = container.Spec(context.Background())
9898
require.NoError(t, err)
9999
checkMemoryLimit(t, spec, 8*1024*1024)
100100

101-
t.Logf("Check memory limit in cgroup")
101+
t.Log("Check memory limit in cgroup")
102102
stat, err = cgroup.Stat(cgroups.IgnoreNotExist)
103103
require.NoError(t, err)
104104
assert.Equal(t, uint64(8*1024*1024), stat.Memory.Usage.Limit)

integration/test_utils.go

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func ConnectDaemons() error {
9797
// Opts sets specific information in pod sandbox config.
9898
type PodSandboxOpts func(*runtime.PodSandboxConfig)
9999

100+
// Set host network.
100101
func WithHostNetwork(p *runtime.PodSandboxConfig) {
101102
if p.Linux == nil {
102103
p.Linux = &runtime.LinuxPodSandboxConfig{}
@@ -111,6 +112,13 @@ func WithHostNetwork(p *runtime.PodSandboxConfig) {
111112
}
112113
}
113114

115+
// Add pod log directory.
116+
func WithPodLogDirectory(dir string) PodSandboxOpts {
117+
return func(p *runtime.PodSandboxConfig) {
118+
p.LogDirectory = dir
119+
}
120+
}
121+
114122
// PodSandboxConfig generates a pod sandbox config for test.
115123
func PodSandboxConfig(name, ns string, opts ...PodSandboxOpts) *runtime.PodSandboxConfig {
116124
config := &runtime.PodSandboxConfig{
@@ -134,52 +142,59 @@ func PodSandboxConfig(name, ns string, opts ...PodSandboxOpts) *runtime.PodSandb
134142
type ContainerOpts func(*runtime.ContainerConfig)
135143

136144
func WithTestLabels() ContainerOpts {
137-
return func(cf *runtime.ContainerConfig) {
138-
cf.Labels = map[string]string{"key": "value"}
145+
return func(c *runtime.ContainerConfig) {
146+
c.Labels = map[string]string{"key": "value"}
139147
}
140148
}
141149

142150
func WithTestAnnotations() ContainerOpts {
143-
return func(cf *runtime.ContainerConfig) {
144-
cf.Annotations = map[string]string{"a.b.c": "test"}
151+
return func(c *runtime.ContainerConfig) {
152+
c.Annotations = map[string]string{"a.b.c": "test"}
145153
}
146154
}
147155

148156
// Add container resource limits.
149157
func WithResources(r *runtime.LinuxContainerResources) ContainerOpts {
150-
return func(cf *runtime.ContainerConfig) {
151-
if cf.Linux == nil {
152-
cf.Linux = &runtime.LinuxContainerConfig{}
158+
return func(c *runtime.ContainerConfig) {
159+
if c.Linux == nil {
160+
c.Linux = &runtime.LinuxContainerConfig{}
153161
}
154-
cf.Linux.Resources = r
162+
c.Linux.Resources = r
155163
}
156164
}
157165

158166
// Add container command.
159-
func WithCommand(c string, args ...string) ContainerOpts {
160-
return func(cf *runtime.ContainerConfig) {
161-
cf.Command = []string{c}
162-
cf.Args = args
167+
func WithCommand(cmd string, args ...string) ContainerOpts {
168+
return func(c *runtime.ContainerConfig) {
169+
c.Command = []string{cmd}
170+
c.Args = args
163171
}
164172
}
165173

166174
// Add pid namespace mode.
167175
func WithPidNamespace(mode runtime.NamespaceMode) ContainerOpts {
168-
return func(cf *runtime.ContainerConfig) {
169-
if cf.Linux == nil {
170-
cf.Linux = &runtime.LinuxContainerConfig{}
176+
return func(c *runtime.ContainerConfig) {
177+
if c.Linux == nil {
178+
c.Linux = &runtime.LinuxContainerConfig{}
171179
}
172-
if cf.Linux.SecurityContext == nil {
173-
cf.Linux.SecurityContext = &runtime.LinuxContainerSecurityContext{}
180+
if c.Linux.SecurityContext == nil {
181+
c.Linux.SecurityContext = &runtime.LinuxContainerSecurityContext{}
174182
}
175-
if cf.Linux.SecurityContext.NamespaceOptions == nil {
176-
cf.Linux.SecurityContext.NamespaceOptions = &runtime.NamespaceOption{}
183+
if c.Linux.SecurityContext.NamespaceOptions == nil {
184+
c.Linux.SecurityContext.NamespaceOptions = &runtime.NamespaceOption{}
177185
}
178-
cf.Linux.SecurityContext.NamespaceOptions.Pid = mode
186+
c.Linux.SecurityContext.NamespaceOptions.Pid = mode
179187
}
180188

181189
}
182190

191+
// Add container log path.
192+
func WithLogPath(path string) ContainerOpts {
193+
return func(c *runtime.ContainerConfig) {
194+
c.LogPath = path
195+
}
196+
}
197+
183198
// ContainerConfig creates a container config given a name and image name
184199
// and additional container config options
185200
func ContainerConfig(name, image string, opts ...ContainerOpts) *runtime.ContainerConfig {

0 commit comments

Comments
 (0)