Skip to content

Commit 2a0ac04

Browse files
author
Oleg Bulatov
committed
Make registry extended tests dockerless
1 parent 45c74af commit 2a0ac04

File tree

2 files changed

+183
-66
lines changed

2 files changed

+183
-66
lines changed

test/extended/imageapis/limitrange_admission.go

+66-56
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,69 @@ import (
55
"os"
66
"strconv"
77

8+
"github.com/docker/distribution/registry/api/errcode"
9+
810
"k8s.io/apimachinery/pkg/api/resource"
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012
kapi "k8s.io/kubernetes/pkg/apis/core"
1113

1214
g "github.com/onsi/ginkgo"
1315
o "github.com/onsi/gomega"
16+
"github.com/onsi/gomega/types"
1417

1518
imageapi "github.com/openshift/origin/pkg/image/apis/image"
1619
quotautil "github.com/openshift/origin/pkg/quota/util"
1720
imagesutil "github.com/openshift/origin/test/extended/images"
1821
exutil "github.com/openshift/origin/test/extended/util"
19-
testutil "github.com/openshift/origin/test/util"
2022
)
2123

24+
type dockerErrorCodeMatcher struct {
25+
expected errcode.ErrorCode
26+
}
27+
28+
func BeDockerError(code errcode.ErrorCode) types.GomegaMatcher {
29+
return &dockerErrorCodeMatcher{
30+
expected: code,
31+
}
32+
}
33+
34+
func (matcher *dockerErrorCodeMatcher) Match(actual interface{}) (success bool, err error) {
35+
switch e := actual.(type) {
36+
case errcode.Errors:
37+
for _, item := range e {
38+
match, err := matcher.Match(item)
39+
if err != nil {
40+
return match, err
41+
}
42+
if match {
43+
return true, nil
44+
}
45+
}
46+
return false, nil
47+
case errcode.Error:
48+
return e.ErrorCode() == matcher.expected, nil
49+
case errcode.ErrorCode:
50+
return e == matcher.expected, nil
51+
}
52+
return false, fmt.Errorf("dockerErrorCodeMatcher got an expected error: %#+v", err)
53+
}
54+
55+
func (matcher *dockerErrorCodeMatcher) FailureMessage(actual interface{}) (message string) {
56+
return fmt.Sprintf("Expected\n\t%s (%#v)\nto be the error %s (%#v)", actual, actual, matcher.expected.String(), matcher.expected)
57+
}
58+
59+
func (matcher *dockerErrorCodeMatcher) NegatedFailureMessage(actual interface{}) (message string) {
60+
return fmt.Sprintf("Expected\n\t%s (%#v)\nnot to be the error %s (%#v)", actual, actual, matcher.expected.String(), matcher.expected)
61+
}
62+
2263
const limitRangeName = "limits"
2364

24-
var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/registry/serial][local] Image limit range", func() {
65+
var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/registry/serial] Image limit range", func() {
2566
defer g.GinkgoRecover()
67+
2668
var oc = exutil.NewCLI("limitrange-admission", exutil.KubeConfigPath())
2769

28-
g.JustBeforeEach(func() {
70+
g.BeforeEach(func() {
2971
g.By("waiting for default service account")
3072
err := exutil.WaitForServiceAccount(oc.KubeClient().Core().ServiceAccounts(oc.Namespace()), "default")
3173
o.Expect(err).NotTo(o.HaveOccurred())
@@ -34,102 +76,77 @@ var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/regis
3476
o.Expect(err).NotTo(o.HaveOccurred())
3577
})
3678

37-
// needs to be run at the of of each It; cannot be run in AfterEach which is run after the project
38-
// is destroyed
39-
tearDown := func(oc *exutil.CLI) {
40-
g.By(fmt.Sprintf("Deleting limit range %s", limitRangeName))
41-
oc.AdminKubeClient().Core().LimitRanges(oc.Namespace()).Delete(limitRangeName, nil)
42-
43-
deleteTestImagesAndStreams(oc)
44-
}
45-
46-
g.It(fmt.Sprintf("[Skipped] should deny a push of built image exceeding %s limit", imageapi.LimitTypeImage), func() {
47-
g.Skip("FIXME: fill image metadata for schema1 in the registry")
48-
49-
defer tearDown(oc)
50-
51-
dClient, err := testutil.NewDockerClient()
52-
o.Expect(err).NotTo(o.HaveOccurred())
53-
54-
_, err = createLimitRangeOfType(oc, imageapi.LimitTypeImage, kapi.ResourceList{
79+
g.It(fmt.Sprintf("should deny a push of built image exceeding %s limit", imageapi.LimitTypeImage), func() {
80+
_, err := createLimitRangeOfType(oc, imageapi.LimitTypeImage, kapi.ResourceList{
5581
kapi.ResourceStorage: resource.MustParse("10Ki"),
5682
})
5783
o.Expect(err).NotTo(o.HaveOccurred())
5884

5985
g.By(fmt.Sprintf("trying to push an image exceeding size limit with just 1 layer"))
60-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "middle", 16000, 1, false)
61-
o.Expect(err).NotTo(o.HaveOccurred())
86+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "middle", 16000, 1)
87+
o.Expect(err).To(BeDockerError(errcode.ErrorCodeDenied))
6288

6389
g.By(fmt.Sprintf("trying to push an image exceeding size limit in total"))
64-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "middle", 16000, 5, false)
65-
o.Expect(err).NotTo(o.HaveOccurred())
90+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "middle", 16000, 5)
91+
o.Expect(err).To(BeDockerError(errcode.ErrorCodeDenied))
6692

6793
g.By(fmt.Sprintf("trying to push an image with one big layer below size limit"))
68-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "small", 8000, 1, true)
94+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "small", 8000, 1)
6995
o.Expect(err).NotTo(o.HaveOccurred())
7096

7197
g.By(fmt.Sprintf("trying to push an image below size limit"))
72-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "small", 8000, 2, true)
98+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "small", 8000, 2)
7399
o.Expect(err).NotTo(o.HaveOccurred())
74100
})
75101

76102
g.It(fmt.Sprintf("should deny a push of built image exceeding limit on %s resource", imageapi.ResourceImageStreamImages), func() {
77-
78-
defer tearDown(oc)
79-
80103
limits := kapi.ResourceList{
81104
imageapi.ResourceImageStreamTags: resource.MustParse("0"),
82105
imageapi.ResourceImageStreamImages: resource.MustParse("0"),
83106
}
84107
_, err := createLimitRangeOfType(oc, imageapi.LimitTypeImageStream, limits)
85108
o.Expect(err).NotTo(o.HaveOccurred())
86109

87-
dClient, err := testutil.NewDockerClient()
88-
o.Expect(err).NotTo(o.HaveOccurred())
89-
90110
g.By(fmt.Sprintf("trying to push image exceeding limits %v", limits))
91-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "refused", imageSize, 1, false)
92-
o.Expect(err).NotTo(o.HaveOccurred())
111+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "refused", imageSize, 1)
112+
o.Expect(err).To(BeDockerError(errcode.ErrorCodeDenied))
93113

94114
limits, err = bumpLimit(oc, imageapi.ResourceImageStreamImages, "1")
95115
o.Expect(err).NotTo(o.HaveOccurred())
96116

97117
g.By(fmt.Sprintf("trying to push image below limits %v", limits))
98-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "first", imageSize, 2, true)
118+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "first", imageSize, 2)
99119
o.Expect(err).NotTo(o.HaveOccurred())
100120

101121
g.By(fmt.Sprintf("trying to push image exceeding limits %v", limits))
102-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "sized", "second", imageSize, 2, false)
103-
o.Expect(err).NotTo(o.HaveOccurred())
122+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/sized", "second", imageSize, 2)
123+
o.Expect(err).To(BeDockerError(errcode.ErrorCodeDenied))
104124

105125
g.By(fmt.Sprintf("trying to push image below limits %v to another image stream", limits))
106-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "another", "second", imageSize, 1, true)
126+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/another", "second", imageSize, 1)
107127
o.Expect(err).NotTo(o.HaveOccurred())
108128

109129
limits, err = bumpLimit(oc, imageapi.ResourceImageStreamImages, "2")
110130
o.Expect(err).NotTo(o.HaveOccurred())
111131

112132
g.By(fmt.Sprintf("trying to push image below limits %v", limits))
113-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "another", "third", imageSize, 1, true)
133+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/another", "third", imageSize, 1)
114134
o.Expect(err).NotTo(o.HaveOccurred())
115135

116136
g.By(fmt.Sprintf("trying to push image exceeding limits %v", limits))
117-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "another", "fourth", imageSize, 1, false)
118-
o.Expect(err).NotTo(o.HaveOccurred())
137+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/another", "fourth", imageSize, 1)
138+
o.Expect(err).To(BeDockerError(errcode.ErrorCodeDenied))
119139

120140
g.By(`removing tag "second" from "another" image stream`)
121141
err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Delete("another:second", nil)
122142
o.Expect(err).NotTo(o.HaveOccurred())
123143

124144
g.By(fmt.Sprintf("trying to push image below limits %v", limits))
125-
err = imagesutil.BuildAndPushImageOfSizeWithBuilder(oc, dClient, oc.Namespace(), "another", "replenish", imageSize, 1, true)
145+
_, err = imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/another", "replenish", imageSize, 1)
126146
o.Expect(err).NotTo(o.HaveOccurred())
127147
})
128148

129149
g.It(fmt.Sprintf("should deny a docker image reference exceeding limit on %s resource", imageapi.ResourceImageStreamTags), func() {
130-
131-
defer tearDown(oc)
132-
133150
tag2Image, err := buildAndPushTestImagesTo(oc, "src", "tag", 2)
134151
o.Expect(err).NotTo(o.HaveOccurred())
135152

@@ -187,15 +204,12 @@ var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/regis
187204
})
188205

189206
g.It(fmt.Sprintf("should deny an import of a repository exceeding limit on %s resource", imageapi.ResourceImageStreamTags), func() {
190-
191207
maxBulkImport, err := getMaxImagesBulkImportedPerRepository()
192208
if err != nil {
193209
g.Skip(err.Error())
194210
return
195211
}
196212

197-
defer tearDown(oc)
198-
199213
s1tag2Image, err := buildAndPushTestImagesTo(oc, "src1st", "tag", maxBulkImport+1)
200214
s2tag2Image, err := buildAndPushTestImagesTo(oc, "src2nd", "t", 2)
201215
o.Expect(err).NotTo(o.HaveOccurred())
@@ -234,23 +248,19 @@ var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/regis
234248
// buildAndPushTestImagesTo builds a given number of test images. The images are pushed to a new image stream
235249
// of given name under <tagPrefix><X> where X is a number of image starting from 1.
236250
func buildAndPushTestImagesTo(oc *exutil.CLI, isName string, tagPrefix string, numberOfImages int) (tag2Image map[string]imageapi.Image, err error) {
237-
dClient, err := testutil.NewDockerClient()
238-
if err != nil {
239-
return
240-
}
241251
tag2Image = make(map[string]imageapi.Image)
242252

243253
for i := 1; i <= numberOfImages; i++ {
244254
tag := fmt.Sprintf("%s%d", tagPrefix, i)
245-
dgst, _, err := imagesutil.BuildAndPushImageOfSizeWithDocker(oc, dClient, isName, tag, imageSize, 2, g.GinkgoWriter, true, true)
255+
dgst, err := imagesutil.BuildAndPushMockImage(oc, oc.Namespace()+"/"+isName, tag, imageSize, 2)
246256
if err != nil {
247257
return nil, err
248258
}
249259
ist, err := oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get(isName+":"+tag, metav1.GetOptions{})
250260
if err != nil {
251261
return nil, err
252262
}
253-
if dgst != ist.Image.Name {
263+
if dgst.String() != ist.Image.Name {
254264
return nil, fmt.Errorf("digest of built image does not match stored: %s != %s", dgst, ist.Image.Name)
255265
}
256266
tag2Image[tag] = ist.Image
@@ -316,7 +326,7 @@ func bumpLimit(oc *exutil.CLI, resourceName kapi.ResourceName, limit string) (ka
316326
func getMaxImagesBulkImportedPerRepository() (int, error) {
317327
max := os.Getenv("MAX_IMAGES_BULK_IMPORTED_PER_REPOSITORY")
318328
if len(max) == 0 {
319-
return 0, fmt.Errorf("MAX_IMAGES_BULK_IMAGES_IMPORTED_PER_REPOSITORY is not set")
329+
return 0, fmt.Errorf("MAX_IMAGES_BULK_IMPORTED_PER_REPOSITORY is not set")
320330
}
321331
return strconv.Atoi(max)
322332
}

0 commit comments

Comments
 (0)