Skip to content

Commit 61783da

Browse files
committed
Parallelize the admission tests
Signed-off-by: Todd Short <[email protected]>
1 parent a9b35dc commit 61783da

File tree

1 file changed

+80
-57
lines changed

1 file changed

+80
-57
lines changed

internal/controllers/admission_test.go

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ func operator(spec operatorsv1alpha1.OperatorSpec) *operatorsv1alpha1.Operator {
1919
}
2020
}
2121

22-
type testData struct {
22+
var operatorData = []struct {
2323
spec *operatorsv1alpha1.Operator
2424
comment string
2525
errMsg string
26-
}
27-
28-
var operatorData = []testData{
26+
}{
2927
{
3028
operator(operatorsv1alpha1.OperatorSpec{}),
3129
"operator spec is empty",
@@ -57,23 +55,29 @@ var operatorData = []testData{
5755
}
5856

5957
func TestOperatorSpecs(t *testing.T) {
58+
t.Parallel()
6059
ctx, cancel := context.WithCancel(context.Background())
61-
defer cancel()
62-
63-
for _, d := range operatorData {
64-
t.Logf("Running %s", d.comment)
65-
cl, err := newClient()
66-
require.NoError(t, err)
67-
require.NotNil(t, cl)
68-
err = cl.Create(ctx, d.spec)
69-
require.Error(t, err)
70-
require.ErrorContains(t, err, d.errMsg)
60+
t.Cleanup(cancel)
61+
62+
for _, od := range operatorData {
63+
d := od
64+
t.Run(d.comment, func(t *testing.T) {
65+
t.Parallel()
66+
t.Logf("Running %s", d.comment)
67+
cl, err := newClient()
68+
require.NoError(t, err)
69+
require.NotNil(t, cl)
70+
err = cl.Create(ctx, d.spec)
71+
require.Error(t, err)
72+
require.ErrorContains(t, err, d.errMsg)
73+
})
7174
}
7275
}
7376

7477
func TestOperatorInvalidSemver(t *testing.T) {
78+
t.Parallel()
7579
ctx, cancel := context.WithCancel(context.Background())
76-
defer cancel()
80+
t.Cleanup(cancel)
7781

7882
invalidSemvers := []string{
7983
"1.2.3.4",
@@ -96,23 +100,28 @@ func TestOperatorInvalidSemver(t *testing.T) {
96100
">1.2.3;<2.3.4",
97101
"1.2.3 - 2.3.4",
98102
}
99-
for _, invalidSemver := range invalidSemvers {
100-
cl, err := newClient()
101-
require.NoError(t, err)
102-
require.NotNil(t, cl)
103-
err = cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{
104-
PackageName: "package",
105-
Version: invalidSemver,
106-
}))
107-
require.Errorf(t, err, "expected error for invalid semver %q", invalidSemver)
108-
// Don't need to include the whole regex, this should be enough to match the MasterMinds regex
109-
require.ErrorContains(t, err, "spec.version in body should match '^(\\s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\\^)")
103+
for _, sm := range invalidSemvers {
104+
d := sm
105+
t.Run(d, func(t *testing.T) {
106+
t.Parallel()
107+
cl, err := newClient()
108+
require.NoError(t, err)
109+
require.NotNil(t, cl)
110+
err = cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{
111+
PackageName: "package",
112+
Version: d,
113+
}))
114+
require.Errorf(t, err, "expected error for invalid semver %q", d)
115+
// Don't need to include the whole regex, this should be enough to match the MasterMinds regex
116+
require.ErrorContains(t, err, "spec.version in body should match '^(\\s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\\^)")
117+
})
110118
}
111119
}
112120

113121
func TestOperatorValidSemver(t *testing.T) {
122+
t.Parallel()
114123
ctx, cancel := context.WithCancel(context.Background())
115-
defer cancel()
124+
t.Cleanup(cancel)
116125

117126
validSemvers := []string{
118127
">=1.2.3",
@@ -148,22 +157,27 @@ func TestOperatorValidSemver(t *testing.T) {
148157
"<1.2.3-abc >2.3.4-def",
149158
"<1.2.3-abc+def >2.3.4-ghi+jkl",
150159
}
151-
for _, validSemver := range validSemvers {
152-
op := operator(operatorsv1alpha1.OperatorSpec{
153-
PackageName: "package",
154-
Version: validSemver,
160+
for _, smx := range validSemvers {
161+
d := smx
162+
t.Run(d, func(t *testing.T) {
163+
t.Parallel()
164+
op := operator(operatorsv1alpha1.OperatorSpec{
165+
PackageName: "package",
166+
Version: d,
167+
})
168+
cl, err := newClient()
169+
require.NoError(t, err)
170+
require.NotNil(t, cl)
171+
err = cl.Create(ctx, op)
172+
require.NoErrorf(t, err, "unexpected error for semver range %q: %w", d, err)
155173
})
156-
cl, err := newClient()
157-
require.NoError(t, err)
158-
require.NotNil(t, cl)
159-
err = cl.Create(ctx, op)
160-
require.NoErrorf(t, err, "unexpected error for semver range '%q': %w", validSemver, err)
161174
}
162175
}
163176

164177
func TestOperatorInvalidChannel(t *testing.T) {
178+
t.Parallel()
165179
ctx, cancel := context.WithCancel(context.Background())
166-
defer cancel()
180+
t.Cleanup(cancel)
167181

168182
invalidChannels := []string{
169183
"spaces spaces",
@@ -175,37 +189,46 @@ func TestOperatorInvalidChannel(t *testing.T) {
175189
".start-with-period",
176190
"end-with-period.",
177191
}
178-
for _, invalidChannel := range invalidChannels {
179-
cl, err := newClient()
180-
require.NoError(t, err)
181-
require.NotNil(t, cl)
182-
err = cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{
183-
PackageName: "package",
184-
Channel: invalidChannel,
185-
}))
186-
require.Errorf(t, err, "expected error for invalid channel '%q'", invalidChannel)
187-
require.ErrorContains(t, err, "spec.channel in body should match '^[a-z0-9]+([\\.-][a-z0-9]+)*$'")
192+
for _, ch := range invalidChannels {
193+
d := ch
194+
t.Run(d, func(t *testing.T) {
195+
t.Parallel()
196+
cl, err := newClient()
197+
require.NoError(t, err)
198+
require.NotNil(t, cl)
199+
err = cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{
200+
PackageName: "package",
201+
Channel: d,
202+
}))
203+
require.Errorf(t, err, "expected error for invalid channel %q", d)
204+
require.ErrorContains(t, err, "spec.channel in body should match '^[a-z0-9]+([\\.-][a-z0-9]+)*$'")
205+
})
188206
}
189207
}
190208

191209
func TestOperatorValidChannel(t *testing.T) {
210+
t.Parallel()
192211
ctx, cancel := context.WithCancel(context.Background())
193-
defer cancel()
212+
t.Cleanup(cancel)
194213

195214
validChannels := []string{
196215
"hyphenated-name",
197216
"dotted.name",
198217
"channel-has-version-1.0.1",
199218
}
200-
for _, validChannel := range validChannels {
201-
op := operator(operatorsv1alpha1.OperatorSpec{
202-
PackageName: "package",
203-
Channel: validChannel,
219+
for _, ch := range validChannels {
220+
d := ch
221+
t.Run(d, func(t *testing.T) {
222+
t.Parallel()
223+
op := operator(operatorsv1alpha1.OperatorSpec{
224+
PackageName: "package",
225+
Channel: d,
226+
})
227+
cl, err := newClient()
228+
require.NoError(t, err)
229+
require.NotNil(t, cl)
230+
err = cl.Create(ctx, op)
231+
require.NoErrorf(t, err, "unexpected error creating valid channel %q: %w", d, err)
204232
})
205-
cl, err := newClient()
206-
require.NoError(t, err)
207-
require.NotNil(t, cl)
208-
err = cl.Create(ctx, op)
209-
require.NoErrorf(t, err, "unexpected error creating valid channel '%q': %w", validChannel, err)
210233
}
211234
}

0 commit comments

Comments
 (0)