@@ -2,9 +2,9 @@ package controllers_test
2
2
3
3
import (
4
4
"context"
5
+ "testing"
5
6
6
- . "github.com/onsi/ginkgo/v2"
7
- . "github.com/onsi/gomega"
7
+ "github.com/stretchr/testify/require"
8
8
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
9
10
10
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
@@ -19,159 +19,178 @@ func operator(spec operatorsv1alpha1.OperatorSpec) *operatorsv1alpha1.Operator {
19
19
}
20
20
}
21
21
22
- var _ = Describe ("Operator Spec Validations" , func () {
23
- var (
24
- ctx context.Context
25
- cancel context.CancelFunc
26
- )
27
- BeforeEach (func () {
28
- ctx , cancel = context .WithCancel (context .Background ())
29
- })
30
- AfterEach (func () {
31
- cancel ()
32
- })
33
- It ("should fail if the spec is empty" , func () {
34
- err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {}))
35
- Expect (err ).To (HaveOccurred ())
36
- Expect (err .Error ()).To (ContainSubstring ("spec.packageName in body should match '^[a-z0-9]+(-[a-z0-9]+)*$'" ))
37
- })
38
- It ("should fail if package name length is greater than 48 characters" , func () {
39
- err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
40
- PackageName : "this-is-a-really-long-package-name-that-is-greater-than-48-characters" ,
41
- }))
42
- Expect (err ).To (HaveOccurred ())
43
- Expect (err .Error ()).To (ContainSubstring ("Too long: may not be longer than 48" ))
44
- })
45
- It ("should fail if version is valid semver but length is greater than 64 characters" , func () {
22
+ func TestOperatorSpecIsEmpty (t * testing.T ) {
23
+ ctx , cancel := context .WithCancel (context .Background ())
24
+ defer cancel ()
25
+
26
+ err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {}))
27
+ require .Error (t , err )
28
+ require .ErrorContains (t , err , "spec.packageName in body should match '^[a-z0-9]+(-[a-z0-9]+)*$'" )
29
+ }
30
+
31
+ func TestOperatorLongPackageName (t * testing.T ) {
32
+ ctx , cancel := context .WithCancel (context .Background ())
33
+ defer cancel ()
34
+
35
+ err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
36
+ PackageName : "this-is-a-really-long-package-name-that-is-greater-than-48-characters" ,
37
+ }))
38
+ require .Error (t , err )
39
+ require .ErrorContains (t , err , "Too long: may not be longer than 48" )
40
+ }
41
+
42
+ func TestOperatorLongValidSemver (t * testing.T ) {
43
+ ctx , cancel := context .WithCancel (context .Background ())
44
+ defer cancel ()
45
+
46
+ err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
47
+ PackageName : "package" ,
48
+ Version : "1234567890.1234567890.12345678901234567890123456789012345678901234" ,
49
+ }))
50
+ require .Error (t , err )
51
+ require .ErrorContains (t , err , "Too long: may not be longer than 64" )
52
+ }
53
+
54
+ func TestOperatorInvalidSemver (t * testing.T ) {
55
+ ctx , cancel := context .WithCancel (context .Background ())
56
+ defer cancel ()
57
+
58
+ invalidSemvers := []string {
59
+ "1.2.3.4" ,
60
+ "1.02.3" ,
61
+ "1.2.03" ,
62
+ "1.2.3-beta!" ,
63
+ "1.2.3.alpha" ,
64
+ "1..2.3" ,
65
+ "1.2.3-pre+bad_metadata" ,
66
+ "1.2.-3" ,
67
+ ".1.2.3" ,
68
+ "<<1.2.3" ,
69
+ ">>1.2.3" ,
70
+ ">~1.2.3" ,
71
+ "==1.2.3" ,
72
+ "=!1.2.3" ,
73
+ "!1.2.3" ,
74
+ "1.Y" ,
75
+ ">1.2.3 && <2.3.4" ,
76
+ ">1.2.3;<2.3.4" ,
77
+ "1.2.3 - 2.3.4" ,
78
+ }
79
+ for _ , invalidSemver := range invalidSemvers {
46
80
err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
47
81
PackageName : "package" ,
48
- Version : "1234567890.1234567890.12345678901234567890123456789012345678901234" ,
82
+ Version : invalidSemver ,
49
83
}))
50
- Expect (err ).To (HaveOccurred ())
51
- Expect (err .Error ()).To (ContainSubstring ("Too long: may not be longer than 64" ))
52
- })
53
- It ("should fail if an invalid semver is given" , func () {
54
- invalidSemvers := []string {
55
- "1.2.3.4" ,
56
- "1.02.3" ,
57
- "1.2.03" ,
58
- "1.2.3-beta!" ,
59
- "1.2.3.alpha" ,
60
- "1..2.3" ,
61
- "1.2.3-pre+bad_metadata" ,
62
- "1.2.-3" ,
63
- ".1.2.3" ,
64
- "<<1.2.3" ,
65
- ">>1.2.3" ,
66
- ">~1.2.3" ,
67
- "==1.2.3" ,
68
- "=!1.2.3" ,
69
- "!1.2.3" ,
70
- "1.Y" ,
71
- ">1.2.3 && <2.3.4" ,
72
- ">1.2.3;<2.3.4" ,
73
- "1.2.3 - 2.3.4" ,
74
- }
75
- for _ , invalidSemver := range invalidSemvers {
76
- err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
77
- PackageName : "package" ,
78
- Version : invalidSemver ,
79
- }))
80
-
81
- Expect (err ).To (HaveOccurred (), "expected error for invalid semver %q" , invalidSemver )
82
- // Don't need to include the whole regex, this should be enough to match the MasterMinds regex
83
- Expect (err .Error ()).To (ContainSubstring ("spec.version in body should match '^(\\ s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\\ ^)" ))
84
- }
85
- })
86
- It ("should pass if a valid semver range given" , func () {
87
- validSemvers := []string {
88
- ">=1.2.3" ,
89
- "=>1.2.3" ,
90
- ">= 1.2.3" ,
91
- ">=v1.2.3" ,
92
- ">= v1.2.3" ,
93
- "<=1.2.3" ,
94
- "=<1.2.3" ,
95
- "=1.2.3" ,
96
- "!=1.2.3" ,
97
- "<1.2.3" ,
98
- ">1.2.3" ,
99
- "~1.2.2" ,
100
- "~>1.2.3" ,
101
- "^1.2.3" ,
102
- "1.2.3" ,
103
- "v1.2.3" ,
104
- "1.x" ,
105
- "1.X" ,
106
- "1.*" ,
107
- "1.2.x" ,
108
- "1.2.X" ,
109
- "1.2.*" ,
110
- ">=1.2.3 <2.3.4" ,
111
- ">=1.2.3,<2.3.4" ,
112
- ">=1.2.3, <2.3.4" ,
113
- "<1.2.3||>2.3.4" ,
114
- "<1.2.3|| >2.3.4" ,
115
- "<1.2.3 ||>2.3.4" ,
116
- "<1.2.3 || >2.3.4" ,
117
- ">1.0.0,<1.2.3 || >2.1.0" ,
118
- "<1.2.3-abc >2.3.4-def" ,
119
- "<1.2.3-abc+def >2.3.4-ghi+jkl" ,
120
- }
121
- for _ , validSemver := range validSemvers {
122
- op := operator (operatorsv1alpha1.OperatorSpec {
123
- PackageName : "package" ,
124
- Version : validSemver ,
125
- })
126
- err := cl .Create (ctx , op )
127
- Expect (err ).NotTo (HaveOccurred (), "expected success for semver range '%q': %w" , validSemver , err )
128
- err = cl .Delete (ctx , op )
129
- Expect (err ).NotTo (HaveOccurred (), "unexpected error deleting valid semver '%q': %w" , validSemver , err )
130
- }
131
- })
132
- It ("should fail if an invalid channel name is given" , func () {
133
- invalidChannels := []string {
134
- "spaces spaces" ,
135
- "Capitalized" ,
136
- "camelCase" ,
137
- "many/invalid$characters+in_name" ,
138
- "-start-with-hyphen" ,
139
- "end-with-hyphen-" ,
140
- ".start-with-period" ,
141
- "end-with-period." ,
142
- }
143
- for _ , invalidChannel := range invalidChannels {
144
- err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
145
- PackageName : "package" ,
146
- Channel : invalidChannel ,
147
- }))
148
- Expect (err ).To (HaveOccurred (), "expected error for invalid channel '%q'" , invalidChannel )
149
- Expect (err .Error ()).To (ContainSubstring ("spec.channel in body should match '^[a-z0-9]+([\\ .-][a-z0-9]+)*$'" ))
150
- }
151
- })
152
- It ("should pass if a valid channel name is given" , func () {
153
- validChannels := []string {
154
- "hyphenated-name" ,
155
- "dotted.name" ,
156
- "channel-has-version-1.0.1" ,
157
- }
158
- for _ , validChannel := range validChannels {
159
- op := operator (operatorsv1alpha1.OperatorSpec {
160
- PackageName : "package" ,
161
- Channel : validChannel ,
162
- })
163
- err := cl .Create (ctx , op )
164
- Expect (err ).NotTo (HaveOccurred (), "unexpected error creating valid channel '%q': %w" , validChannel , err )
165
- err = cl .Delete (ctx , op )
166
- Expect (err ).NotTo (HaveOccurred (), "unexpected error deleting valid channel '%q': %w" , validChannel , err )
167
- }
168
- })
169
- It ("should fail if an invalid channel name length" , func () {
84
+ require .Errorf (t , err , "expected error for invalid semver %q" , invalidSemver )
85
+ // Don't need to include the whole regex, this should be enough to match the MasterMinds regex
86
+ require .ErrorContains (t , err , "spec.version in body should match '^(\\ s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\\ ^)" )
87
+ }
88
+ }
89
+
90
+ func TestOperatorValidSemver (t * testing.T ) {
91
+ ctx , cancel := context .WithCancel (context .Background ())
92
+ defer cancel ()
93
+
94
+ validSemvers := []string {
95
+ ">=1.2.3" ,
96
+ "=>1.2.3" ,
97
+ ">= 1.2.3" ,
98
+ ">=v1.2.3" ,
99
+ ">= v1.2.3" ,
100
+ "<=1.2.3" ,
101
+ "=<1.2.3" ,
102
+ "=1.2.3" ,
103
+ "!=1.2.3" ,
104
+ "<1.2.3" ,
105
+ ">1.2.3" ,
106
+ "~1.2.2" ,
107
+ "~>1.2.3" ,
108
+ "^1.2.3" ,
109
+ "1.2.3" ,
110
+ "v1.2.3" ,
111
+ "1.x" ,
112
+ "1.X" ,
113
+ "1.*" ,
114
+ "1.2.x" ,
115
+ "1.2.X" ,
116
+ "1.2.*" ,
117
+ ">=1.2.3 <2.3.4" ,
118
+ ">=1.2.3,<2.3.4" ,
119
+ ">=1.2.3, <2.3.4" ,
120
+ "<1.2.3||>2.3.4" ,
121
+ "<1.2.3|| >2.3.4" ,
122
+ "<1.2.3 ||>2.3.4" ,
123
+ "<1.2.3 || >2.3.4" ,
124
+ ">1.0.0,<1.2.3 || >2.1.0" ,
125
+ "<1.2.3-abc >2.3.4-def" ,
126
+ "<1.2.3-abc+def >2.3.4-ghi+jkl" ,
127
+ }
128
+ for _ , validSemver := range validSemvers {
129
+ op := operator (operatorsv1alpha1.OperatorSpec {
130
+ PackageName : "package" ,
131
+ Version : validSemver ,
132
+ })
133
+ err := cl .Create (ctx , op )
134
+ require .NoErrorf (t , err , "unexpected error for semver range '%q': %w" , validSemver , err )
135
+ err = cl .Delete (ctx , op )
136
+ require .NoErrorf (t , err , "unexpected error deleting valid semver '%q': %w" , validSemver , err )
137
+ }
138
+ }
139
+
140
+ func TestOperatorInvalidChannel (t * testing.T ) {
141
+ ctx , cancel := context .WithCancel (context .Background ())
142
+ defer cancel ()
143
+
144
+ invalidChannels := []string {
145
+ "spaces spaces" ,
146
+ "Capitalized" ,
147
+ "camelCase" ,
148
+ "many/invalid$characters+in_name" ,
149
+ "-start-with-hyphen" ,
150
+ "end-with-hyphen-" ,
151
+ ".start-with-period" ,
152
+ "end-with-period." ,
153
+ }
154
+ for _ , invalidChannel := range invalidChannels {
170
155
err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
171
156
PackageName : "package" ,
172
- Channel : "longname01234567890123456789012345678901234567890" ,
157
+ Channel : invalidChannel ,
173
158
}))
174
- Expect (err ).To (HaveOccurred (), "expected error for invalid channel length" )
175
- Expect (err .Error ()).To (ContainSubstring ("spec.channel: Too long: may not be longer than 48" ))
176
- })
177
- })
159
+ require .Errorf (t , err , "expected error for invalid channel '%q'" , invalidChannel )
160
+ require .ErrorContains (t , err , "spec.channel in body should match '^[a-z0-9]+([\\ .-][a-z0-9]+)*$'" )
161
+ }
162
+ }
163
+
164
+ func TestOperatorValidChannel (t * testing.T ) {
165
+ ctx , cancel := context .WithCancel (context .Background ())
166
+ defer cancel ()
167
+
168
+ validChannels := []string {
169
+ "hyphenated-name" ,
170
+ "dotted.name" ,
171
+ "channel-has-version-1.0.1" ,
172
+ }
173
+ for _ , validChannel := range validChannels {
174
+ op := operator (operatorsv1alpha1.OperatorSpec {
175
+ PackageName : "package" ,
176
+ Channel : validChannel ,
177
+ })
178
+ err := cl .Create (ctx , op )
179
+ require .NoErrorf (t , err , "unexpected error creating valid channel '%q': %w" , validChannel , err )
180
+ err = cl .Delete (ctx , op )
181
+ require .NoErrorf (t , err , "unexpected error deleting valid channel '%q': %w" , validChannel , err )
182
+ }
183
+ }
184
+
185
+ func TestOperatorLongValidChannel (t * testing.T ) {
186
+ ctx , cancel := context .WithCancel (context .Background ())
187
+ defer cancel ()
188
+
189
+ err := cl .Create (ctx , operator (operatorsv1alpha1.OperatorSpec {
190
+ PackageName : "package" ,
191
+ Channel : "longname01234567890123456789012345678901234567890" ,
192
+ }))
193
+
194
+ require .Error (t , err )
195
+ require .ErrorContains (t , err , "spec.channel: Too long: may not be longer than 48" )
196
+ }
0 commit comments