forked from kubernetes-sigs/cri-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
259 lines (217 loc) · 8.51 KB
/
image.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
/*
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 (
"context"
"fmt"
"runtime"
"sort"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
internalapi "k8s.io/cri-api/pkg/apis"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
"sigs.k8s.io/cri-tools/pkg/framework"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = framework.KubeDescribe("Image Manager", func() {
f := framework.NewDefaultCRIFramework()
var c internalapi.ImageManagerService
BeforeEach(func() {
c = f.CRIClient.CRIImageClient
})
It("public image with tag should be pulled and removed [Conformance]", func() {
testPullPublicImage(c, testImageWithTag, testImagePodSandbox, func(s *runtimeapi.Image) {
Expect(s.RepoTags).To(Equal([]string{testImageWithTag}))
})
})
It("public image should timeout if requested [Conformance]", func() {
imageName := framework.PrepareImageName(testImageWithTag)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
res, err := c.PullImage(ctx, &runtimeapi.ImageSpec{Image: imageName}, nil, nil)
Expect(res).To(BeEmpty())
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.DeadlineExceeded))
})
It("public image without tag should be pulled and removed [Conformance]", func() {
testPullPublicImage(c, testImageWithoutTag, testImagePodSandbox, func(s *runtimeapi.Image) {
Expect(s.RepoTags).To(Equal([]string{testImageWithoutTag + ":latest"}))
})
})
It("public image with digest should be pulled and removed [Conformance]", func() {
testPullPublicImage(c, testImageWithDigest, testImagePodSandbox, func(s *runtimeapi.Image) {
Expect(s.RepoTags).To(BeEmpty())
Expect(s.RepoDigests).To(Equal([]string{testImageWithDigest}))
})
})
It("image status should support all kinds of references [Conformance]", func() {
imageName := testImageWithAllReferences
// Make sure image does not exist before testing.
removeImage(c, imageName)
framework.PullPublicImage(c, imageName, testImagePodSandbox)
status := framework.ImageStatus(c, imageName)
Expect(status).NotTo(BeNil(), "should get image status")
idStatus := framework.ImageStatus(c, status.GetId())
Expect(idStatus).To(Equal(status), "image status with %q", status.GetId())
for _, tag := range status.GetRepoTags() {
tagStatus := framework.ImageStatus(c, tag)
Expect(tagStatus).To(Equal(status), "image status with %q", tag)
}
for _, digest := range status.GetRepoDigests() {
digestStatus := framework.ImageStatus(c, digest)
Expect(digestStatus).To(Equal(status), "image status with %q", digest)
}
testRemoveImage(c, imageName)
})
if runtime.GOOS != "windows" || framework.TestContext.IsLcow {
It("image status get image fields should not have Uid|Username empty [Conformance]", func() {
for _, item := range []struct {
description string
image string
uid int64
username string
}{
{
description: "UID only",
image: testImageUserUID,
uid: imageUserUID,
username: "",
},
{
description: "Username only",
image: testImageUserUsername,
uid: int64(0),
username: imageUserUsername,
},
{
description: "UID:group",
image: testImageUserUIDGroup,
uid: imageUserUIDGroup,
username: "",
},
{
description: "Username:group",
image: testImageUserUsernameGroup,
uid: int64(0),
username: imageUserUsernameGroup,
},
} {
framework.PullPublicImage(c, item.image, testImagePodSandbox)
defer removeImage(c, item.image)
status := framework.ImageStatus(c, item.image)
Expect(status.GetUid().GetValue()).To(Equal(item.uid), fmt.Sprintf("%s, Image Uid should be %d", item.description, item.uid))
Expect(status.GetUsername()).To(Equal(item.username), fmt.Sprintf("%s, Image Username should be %s", item.description, item.username))
}
})
}
It("listImage should get exactly 3 image in the result list [Conformance]", func() {
// Make sure test image does not exist.
removeImageList(c, testDifferentTagDifferentImageList)
ids := pullImageList(c, testDifferentTagDifferentImageList, testImagePodSandbox)
ids = removeDuplicates(ids)
Expect(len(ids)).To(Equal(3), "3 image ids should be returned")
defer removeImageList(c, testDifferentTagDifferentImageList)
images := framework.ListImage(c, &runtimeapi.ImageFilter{})
for i, id := range ids {
for _, img := range images {
if img.Id == id {
Expect(len(img.RepoTags)).To(Equal(1), "Should only have 1 repo tag")
Expect(img.RepoTags[0]).To(Equal(testDifferentTagDifferentImageList[i]), "Repo tag should be correct")
break
}
}
}
})
It("listImage should get exactly 3 repoTags in the result image [Conformance]", func() {
// Make sure test image does not exist.
removeImageList(c, testDifferentTagSameImageList)
ids := pullImageList(c, testDifferentTagSameImageList, testImagePodSandbox)
ids = removeDuplicates(ids)
Expect(len(ids)).To(Equal(1), "Only 1 image id should be returned")
defer removeImageList(c, testDifferentTagSameImageList)
images := framework.ListImage(c, &runtimeapi.ImageFilter{})
sort.Strings(testDifferentTagSameImageList)
for _, img := range images {
if img.Id == ids[0] {
sort.Strings(img.RepoTags)
Expect(img.RepoTags).To(Equal(testDifferentTagSameImageList), "Should have 3 repoTags in single image")
break
}
}
})
})
// testRemoveImage removes the image name imageName and check if it successes.
func testRemoveImage(c internalapi.ImageManagerService, imageName string) {
By("Remove image : " + imageName)
removeImage(c, imageName)
By("Check image list empty")
status := framework.ImageStatus(c, imageName)
Expect(status).To(BeNil(), "Should have none image in list")
}
// testPullPublicImage pulls the image named imageName, make sure it success and remove the image.
func testPullPublicImage(c internalapi.ImageManagerService, imageName string, podConfig *runtimeapi.PodSandboxConfig, statusCheck func(*runtimeapi.Image)) {
// Make sure image does not exist before testing.
removeImage(c, imageName)
framework.PullPublicImage(c, imageName, podConfig)
By("Check image list to make sure pulling image success : " + imageName)
status := framework.ImageStatus(c, imageName)
Expect(status).NotTo(BeNil(), "Should have one image in list")
Expect(status.Id).NotTo(BeNil(), "Image Id should not be nil")
Expect(status.Size_).NotTo(BeNil(), "Image Size should not be nil")
if statusCheck != nil {
statusCheck(status)
}
testRemoveImage(c, imageName)
}
// pullImageList pulls the images listed in the imageList.
func pullImageList(c internalapi.ImageManagerService, imageList []string, podConfig *runtimeapi.PodSandboxConfig) []string {
ids := []string{}
for _, imageName := range imageList {
ids = append(ids, framework.PullPublicImage(c, imageName, podConfig))
}
return ids
}
// removeImageList removes the images listed in the imageList.
func removeImageList(c internalapi.ImageManagerService, imageList []string) {
for _, imageName := range imageList {
removeImage(c, imageName)
}
}
// removeImage removes the image named imagesName.
func removeImage(c internalapi.ImageManagerService, imageName string) {
By("Remove image : " + imageName)
image, err := c.ImageStatus(context.TODO(), &runtimeapi.ImageSpec{Image: imageName}, false)
framework.ExpectNoError(err, "failed to get image status: %v", err)
if image.Image != nil {
By("Remove image by ID : " + image.Image.Id)
err = c.RemoveImage(context.TODO(), &runtimeapi.ImageSpec{Image: image.Image.Id})
framework.ExpectNoError(err, "failed to remove image: %v", err)
}
}
// removeDuplicates remove duplicates strings from a list
func removeDuplicates(ss []string) []string {
encountered := map[string]bool{}
result := []string{}
for _, s := range ss {
if encountered[s] == true {
continue
}
encountered[s] = true
result = append(result, s)
}
return result
}