@@ -5,27 +5,69 @@ import (
5
5
"os"
6
6
"strconv"
7
7
8
+ "github.com/docker/distribution/registry/api/errcode"
9
+
8
10
"k8s.io/apimachinery/pkg/api/resource"
9
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
12
kapi "k8s.io/kubernetes/pkg/apis/core"
11
13
12
14
g "github.com/onsi/ginkgo"
13
15
o "github.com/onsi/gomega"
16
+ "github.com/onsi/gomega/types"
14
17
15
18
imageapi "github.com/openshift/origin/pkg/image/apis/image"
16
19
quotautil "github.com/openshift/origin/pkg/quota/util"
17
20
imagesutil "github.com/openshift/origin/test/extended/images"
18
21
exutil "github.com/openshift/origin/test/extended/util"
19
- testutil "github.com/openshift/origin/test/util"
20
22
)
21
23
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)\n to 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)\n not to be the error %s (%#v)" , actual , actual , matcher .expected .String (), matcher .expected )
61
+ }
62
+
22
63
const limitRangeName = "limits"
23
64
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 () {
25
66
defer g .GinkgoRecover ()
67
+
26
68
var oc = exutil .NewCLI ("limitrange-admission" , exutil .KubeConfigPath ())
27
69
28
- g .JustBeforeEach (func () {
70
+ g .BeforeEach (func () {
29
71
g .By ("waiting for default service account" )
30
72
err := exutil .WaitForServiceAccount (oc .KubeClient ().Core ().ServiceAccounts (oc .Namespace ()), "default" )
31
73
o .Expect (err ).NotTo (o .HaveOccurred ())
@@ -34,102 +76,77 @@ var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/regis
34
76
o .Expect (err ).NotTo (o .HaveOccurred ())
35
77
})
36
78
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 {
55
81
kapi .ResourceStorage : resource .MustParse ("10Ki" ),
56
82
})
57
83
o .Expect (err ).NotTo (o .HaveOccurred ())
58
84
59
85
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 ))
62
88
63
89
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 ))
66
92
67
93
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 )
69
95
o .Expect (err ).NotTo (o .HaveOccurred ())
70
96
71
97
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 )
73
99
o .Expect (err ).NotTo (o .HaveOccurred ())
74
100
})
75
101
76
102
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
-
80
103
limits := kapi.ResourceList {
81
104
imageapi .ResourceImageStreamTags : resource .MustParse ("0" ),
82
105
imageapi .ResourceImageStreamImages : resource .MustParse ("0" ),
83
106
}
84
107
_ , err := createLimitRangeOfType (oc , imageapi .LimitTypeImageStream , limits )
85
108
o .Expect (err ).NotTo (o .HaveOccurred ())
86
109
87
- dClient , err := testutil .NewDockerClient ()
88
- o .Expect (err ).NotTo (o .HaveOccurred ())
89
-
90
110
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 ))
93
113
94
114
limits , err = bumpLimit (oc , imageapi .ResourceImageStreamImages , "1" )
95
115
o .Expect (err ).NotTo (o .HaveOccurred ())
96
116
97
117
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 )
99
119
o .Expect (err ).NotTo (o .HaveOccurred ())
100
120
101
121
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 ))
104
124
105
125
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 )
107
127
o .Expect (err ).NotTo (o .HaveOccurred ())
108
128
109
129
limits , err = bumpLimit (oc , imageapi .ResourceImageStreamImages , "2" )
110
130
o .Expect (err ).NotTo (o .HaveOccurred ())
111
131
112
132
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 )
114
134
o .Expect (err ).NotTo (o .HaveOccurred ())
115
135
116
136
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 ))
119
139
120
140
g .By (`removing tag "second" from "another" image stream` )
121
141
err = oc .ImageClient ().Image ().ImageStreamTags (oc .Namespace ()).Delete ("another:second" , nil )
122
142
o .Expect (err ).NotTo (o .HaveOccurred ())
123
143
124
144
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 )
126
146
o .Expect (err ).NotTo (o .HaveOccurred ())
127
147
})
128
148
129
149
g .It (fmt .Sprintf ("should deny a docker image reference exceeding limit on %s resource" , imageapi .ResourceImageStreamTags ), func () {
130
-
131
- defer tearDown (oc )
132
-
133
150
tag2Image , err := buildAndPushTestImagesTo (oc , "src" , "tag" , 2 )
134
151
o .Expect (err ).NotTo (o .HaveOccurred ())
135
152
@@ -187,15 +204,12 @@ var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/regis
187
204
})
188
205
189
206
g .It (fmt .Sprintf ("should deny an import of a repository exceeding limit on %s resource" , imageapi .ResourceImageStreamTags ), func () {
190
-
191
207
maxBulkImport , err := getMaxImagesBulkImportedPerRepository ()
192
208
if err != nil {
193
209
g .Skip (err .Error ())
194
210
return
195
211
}
196
212
197
- defer tearDown (oc )
198
-
199
213
s1tag2Image , err := buildAndPushTestImagesTo (oc , "src1st" , "tag" , maxBulkImport + 1 )
200
214
s2tag2Image , err := buildAndPushTestImagesTo (oc , "src2nd" , "t" , 2 )
201
215
o .Expect (err ).NotTo (o .HaveOccurred ())
@@ -234,23 +248,19 @@ var _ = g.Describe("[Feature:ImageQuota][registry][Serial][Suite:openshift/regis
234
248
// buildAndPushTestImagesTo builds a given number of test images. The images are pushed to a new image stream
235
249
// of given name under <tagPrefix><X> where X is a number of image starting from 1.
236
250
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
- }
241
251
tag2Image = make (map [string ]imageapi.Image )
242
252
243
253
for i := 1 ; i <= numberOfImages ; i ++ {
244
254
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 )
246
256
if err != nil {
247
257
return nil , err
248
258
}
249
259
ist , err := oc .ImageClient ().Image ().ImageStreamTags (oc .Namespace ()).Get (isName + ":" + tag , metav1.GetOptions {})
250
260
if err != nil {
251
261
return nil , err
252
262
}
253
- if dgst != ist .Image .Name {
263
+ if dgst . String () != ist .Image .Name {
254
264
return nil , fmt .Errorf ("digest of built image does not match stored: %s != %s" , dgst , ist .Image .Name )
255
265
}
256
266
tag2Image [tag ] = ist .Image
@@ -316,7 +326,7 @@ func bumpLimit(oc *exutil.CLI, resourceName kapi.ResourceName, limit string) (ka
316
326
func getMaxImagesBulkImportedPerRepository () (int , error ) {
317
327
max := os .Getenv ("MAX_IMAGES_BULK_IMPORTED_PER_REPOSITORY" )
318
328
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" )
320
330
}
321
331
return strconv .Atoi (max )
322
332
}
0 commit comments