/
truncindex_test.go
157 lines (132 loc) · 5.01 KB
/
truncindex_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
/*
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 integration
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
)
func genTruncIndex(normalName string) string {
return normalName[:(len(normalName)+1)/2]
}
func TestTruncIndex(t *testing.T) {
t.Logf("Pull an image")
const appImage = "busybox"
imgID, err := imageService.PullImage(&runtimeapi.ImageSpec{Image: appImage}, nil)
require.NoError(t, err)
imgTruncId := genTruncIndex(imgID)
defer func() {
assert.NoError(t, imageService.RemoveImage(&runtimeapi.ImageSpec{Image: imgTruncId}))
}()
t.Logf("Get image status by truncindex, truncId: %s", imgTruncId)
res, err := imageService.ImageStatus(&runtimeapi.ImageSpec{Image: imgTruncId})
require.NoError(t, err)
require.NotEqual(t, nil, res)
assert.Equal(t, imgID, res.Id)
// TODO(yanxuean): for failure test case where there are two images with the same truncindex.
// if you add n images at least two will share the same leading digit.
// "sha256:n" where n is the a number from 0-9 where two images have the same trunc,
// for example sha256:9
// https://github.com/containerd/cri/pull/352
// I am thinking how I get the two image which have same trunc.
// TODO(yanxuean): add test case for ListImages
t.Logf("Create a sandbox")
sbConfig := PodSandboxConfig("sandbox", "truncindex")
sb, err := runtimeService.RunPodSandbox(sbConfig)
require.NoError(t, err)
sbTruncIndex := genTruncIndex(sb)
var hasStoppedSandbox bool
defer func() {
// The 2th StopPodSandbox will fail, the 2th RemovePodSandbox will success.
if !hasStoppedSandbox {
assert.NoError(t, runtimeService.StopPodSandbox(sbTruncIndex))
}
assert.NoError(t, runtimeService.RemovePodSandbox(sbTruncIndex))
}()
t.Logf("Get sandbox status by truncindex")
sbStatus, err := runtimeService.PodSandboxStatus(sbTruncIndex)
require.NoError(t, err)
assert.Equal(t, sb, sbStatus.Id)
t.Logf("Forward port for sandbox by truncindex")
_, err = runtimeService.PortForward(&runtimeapi.PortForwardRequest{sbTruncIndex, []int32{80}})
assert.NoError(t, err)
// TODO(yanxuean): add test case for ListPodSandbox
t.Logf("Create a container")
cnConfig := ContainerConfig(
"containerTruncIndex",
appImage,
WithCommand("top"),
)
cn, err := runtimeService.CreateContainer(sbTruncIndex, cnConfig, sbConfig)
require.NoError(t, err)
cnTruncIndex := genTruncIndex(cn)
defer func() {
// the 2th RemovePodSandbox will success.
assert.NoError(t, runtimeService.RemoveContainer(cnTruncIndex))
}()
t.Logf("Get container status by truncindex")
cStatus, err := runtimeService.ContainerStatus(cnTruncIndex)
require.NoError(t, err)
assert.Equal(t, cn, cStatus.Id)
t.Logf("Start the container")
require.NoError(t, runtimeService.StartContainer(cnTruncIndex))
var hasStoppedContainer bool
defer func() {
// The 2th StopPodSandbox will fail
if !hasStoppedContainer {
assert.NoError(t, runtimeService.StopContainer(cnTruncIndex, 10))
}
}()
t.Logf("Stats the container")
cStats, err := runtimeService.ContainerStats(cnTruncIndex)
require.NoError(t, err)
assert.Equal(t, cn, cStats.Attributes.Id)
t.Logf("Update container memory limit after started")
err = runtimeService.UpdateContainerResources(cnTruncIndex, &runtimeapi.LinuxContainerResources{
MemoryLimitInBytes: 50 * 1024 * 1024,
})
assert.NoError(t, err)
t.Logf("Execute cmd in container")
execReq := &runtimeapi.ExecRequest{
ContainerId: cnTruncIndex,
Cmd: []string{"pwd"},
Stdout: true,
}
_, err = runtimeService.Exec(execReq)
assert.NoError(t, err)
t.Logf("Execute cmd in container by sync")
_, _, err = runtimeService.ExecSync(cnTruncIndex, []string{"pwd"}, 10)
assert.NoError(t, err)
// TODO(yanxuean): add test case for ListContainers
t.Logf("Get a non exist container status by truncindex")
err = runtimeService.StopContainer(cnTruncIndex, 10)
assert.NoError(t, err)
if err == nil {
hasStoppedContainer = true
}
_, err = runtimeService.ContainerStats(cnTruncIndex)
assert.Error(t, err)
assert.NoError(t, runtimeService.RemoveContainer(cnTruncIndex))
_, err = runtimeService.ContainerStatus(cnTruncIndex)
assert.Error(t, err)
t.Logf("Get a non exist sandbox status by truncindex")
err = runtimeService.StopPodSandbox(sbTruncIndex)
assert.NoError(t, err)
if err == nil {
hasStoppedSandbox = true
}
assert.NoError(t, runtimeService.RemovePodSandbox(sbTruncIndex))
_, err = runtimeService.PodSandboxStatus(sbTruncIndex)
assert.Error(t, err)
}