1
1
package catalog
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"testing"
@@ -9,7 +10,9 @@ import (
9
10
"github.com/ghodss/yaml"
10
11
"github.com/sirupsen/logrus"
11
12
"github.com/stretchr/testify/require"
13
+ appsv1 "k8s.io/api/apps/v1"
12
14
corev1 "k8s.io/api/core/v1"
15
+ rbacv1 "k8s.io/api/rbac/v1"
13
16
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
14
17
apiextensionsfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
15
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -113,6 +116,98 @@ func TestTransitionInstallPlan(t *testing.T) {
113
116
}
114
117
}
115
118
119
+ func TestExecutePlan (t * testing.T ) {
120
+ namespace := "ns"
121
+
122
+ tests := []struct {
123
+ testName string
124
+ in * v1alpha1.InstallPlan
125
+ want []runtime.Object
126
+ err error
127
+ }{
128
+ {
129
+ testName : "NoSteps" ,
130
+ in : installPlan ("p" , namespace , v1alpha1 .InstallPlanPhaseInstalling ),
131
+ want : []runtime.Object {},
132
+ err : nil ,
133
+ },
134
+ {
135
+ testName : "MultipleSteps" ,
136
+ in : withSteps (installPlan ("p" , namespace , v1alpha1 .InstallPlanPhaseInstalling , "csv" ),
137
+ []* v1alpha1.Step {
138
+ & v1alpha1.Step {
139
+ Resource : v1alpha1.StepResource {
140
+ CatalogSource : "catalog" ,
141
+ CatalogSourceNamespace : namespace ,
142
+ Group : "" ,
143
+ Version : "v1" ,
144
+ Kind : "Service" ,
145
+ Name : "service" ,
146
+ Manifest : toManifest (service ("service" , namespace )),
147
+ },
148
+ Status : v1alpha1 .StepStatusUnknown ,
149
+ },
150
+ & v1alpha1.Step {
151
+ Resource : v1alpha1.StepResource {
152
+ CatalogSource : "catalog" ,
153
+ CatalogSourceNamespace : namespace ,
154
+ Group : "operators.coreos.com" ,
155
+ Version : "v1alpha1" ,
156
+ Kind : "ClusterServiceVersion" ,
157
+ Name : "csv" ,
158
+ Manifest : toManifest (csv ("csv" , namespace , nil , nil )),
159
+ },
160
+ Status : v1alpha1 .StepStatusUnknown ,
161
+ },
162
+ },
163
+ ),
164
+ want : []runtime.Object {service ("service" , namespace ), csv ("csv" , namespace , nil , nil )},
165
+ err : nil ,
166
+ },
167
+ }
168
+
169
+ for _ , tt := range tests {
170
+ t .Run (tt .testName , func (t * testing.T ) {
171
+ stopCh := make (chan struct {})
172
+ defer func () { stopCh <- struct {}{} }()
173
+ op , _ , err := NewFakeOperator ([]runtime.Object {tt .in }, nil , nil , nil , namespace , stopCh )
174
+ require .NoError (t , err )
175
+
176
+ err = op .ExecutePlan (tt .in )
177
+ require .Equal (t , tt .err , err )
178
+
179
+ for _ , obj := range tt .want {
180
+ var err error
181
+ var fetched runtime.Object
182
+ switch o := obj .(type ) {
183
+ case * appsv1.Deployment :
184
+ fetched , err = op .OpClient .GetDeployment (namespace , o .GetName ())
185
+ case * rbacv1.ClusterRole :
186
+ fetched , err = op .OpClient .GetClusterRole (o .GetName ())
187
+ case * rbacv1.Role :
188
+ fetched , err = op .OpClient .GetRole (namespace , o .GetName ())
189
+ case * rbacv1.ClusterRoleBinding :
190
+ fetched , err = op .OpClient .GetClusterRoleBinding (o .GetName ())
191
+ case * rbacv1.RoleBinding :
192
+ fetched , err = op .OpClient .GetRoleBinding (namespace , o .GetName ())
193
+ case * corev1.ServiceAccount :
194
+ fetched , err = op .OpClient .GetServiceAccount (namespace , o .GetName ())
195
+ case * corev1.Service :
196
+ fetched , err = op .OpClient .GetService (namespace , o .GetName ())
197
+ case * v1alpha1.ClusterServiceVersion :
198
+ fetched , err = op .client .OperatorsV1alpha1 ().ClusterServiceVersions (namespace ).Get (o .GetName (), metav1.GetOptions {})
199
+ default :
200
+ require .Failf (t , "couldn't find expected object" , "%#v" , obj )
201
+ }
202
+
203
+ require .NoError (t , err , "couldn't fetch %s %v" , namespace , obj )
204
+ fmt .Printf ("fetched: %v" , fetched )
205
+ require .EqualValues (t , obj , fetched )
206
+ }
207
+ })
208
+ }
209
+ }
210
+
116
211
func TestSyncCatalogSources (t * testing.T ) {
117
212
tests := []struct {
118
213
testName string
@@ -284,21 +379,21 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
284
379
testNamespace := "default"
285
380
tests := []struct {
286
381
name string
287
- csv v1alpha1.ClusterServiceVersion
382
+ csv * v1alpha1.ClusterServiceVersion
288
383
existingCRDOwners map [string ][]string
289
384
expectedErr error
290
385
expectedResult bool
291
386
}{
292
387
{
293
388
name : "NoCompetingOwnersExist" ,
294
- csv : csv ("turkey" , []string {"feathers" }, nil ),
389
+ csv : csv ("turkey" , testNamespace , []string {"feathers" }, nil ),
295
390
existingCRDOwners : nil ,
296
391
expectedErr : nil ,
297
392
expectedResult : false ,
298
393
},
299
394
{
300
395
name : "OnlyCompetingWithSelf" ,
301
- csv : csv ("turkey" , []string {"feathers" }, nil ),
396
+ csv : csv ("turkey" , testNamespace , []string {"feathers" }, nil ),
302
397
existingCRDOwners : map [string ][]string {
303
398
"feathers" : {"turkey" },
304
399
},
@@ -307,7 +402,7 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
307
402
},
308
403
{
309
404
name : "CompetingOwnersExist" ,
310
- csv : csv ("turkey" , []string {"feathers" }, nil ),
405
+ csv : csv ("turkey" , testNamespace , []string {"feathers" }, nil ),
311
406
existingCRDOwners : map [string ][]string {
312
407
"feathers" : {"seagull" },
313
408
},
@@ -316,7 +411,7 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
316
411
},
317
412
{
318
413
name : "CompetingOwnerExistsOnSecondCRD" ,
319
- csv : csv ("turkey" , []string {"feathers" , "beak" }, nil ),
414
+ csv : csv ("turkey" , testNamespace , []string {"feathers" , "beak" }, nil ),
320
415
existingCRDOwners : map [string ][]string {
321
416
"milk" : {"cow" },
322
417
"beak" : {"squid" },
@@ -326,7 +421,7 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
326
421
},
327
422
{
328
423
name : "MoreThanOneCompetingOwnerExists" ,
329
- csv : csv ("turkey" , []string {"feathers" }, nil ),
424
+ csv : csv ("turkey" , testNamespace , []string {"feathers" }, nil ),
330
425
existingCRDOwners : map [string ][]string {
331
426
"feathers" : {"seagull" , "turkey" },
332
427
},
@@ -338,7 +433,7 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
338
433
t .Run (tt .name , func (t * testing.T ) {
339
434
t .Parallel ()
340
435
341
- competing , err := competingCRDOwnersExist (testNamespace , & tt .csv , tt .existingCRDOwners )
436
+ competing , err := competingCRDOwnersExist (testNamespace , tt .csv , tt .existingCRDOwners )
342
437
343
438
// Assert the error is as expected
344
439
if tt .expectedErr == nil {
@@ -440,31 +535,48 @@ func NewFakeOperator(clientObjs []runtime.Object, k8sObjs []runtime.Object, extO
440
535
return op , hasSyncedCheckFns , nil
441
536
}
442
537
443
- func installPlan (names ... string ) v1alpha1.InstallPlan {
444
- return v1alpha1.InstallPlan {
538
+ func installPlan (name , namespace string , phase v1alpha1.InstallPlanPhase , names ... string ) * v1alpha1.InstallPlan {
539
+ return & v1alpha1.InstallPlan {
540
+ ObjectMeta : metav1.ObjectMeta {
541
+ Name : name ,
542
+ Namespace : namespace ,
543
+ },
445
544
Spec : v1alpha1.InstallPlanSpec {
446
545
ClusterServiceVersionNames : names ,
447
546
},
448
547
Status : v1alpha1.InstallPlanStatus {
449
- Plan : []* v1alpha1.Step {},
548
+ Phase : phase ,
549
+ Plan : []* v1alpha1.Step {},
450
550
},
451
551
}
452
552
}
453
553
454
- func csv (name string , owned , required []string ) v1alpha1.ClusterServiceVersion {
554
+ func withSteps (plan * v1alpha1.InstallPlan , steps []* v1alpha1.Step ) * v1alpha1.InstallPlan {
555
+ plan .Status .Plan = steps
556
+ return plan
557
+ }
558
+
559
+ func csv (name , namespace string , owned , required []string ) * v1alpha1.ClusterServiceVersion {
455
560
requiredCRDDescs := make ([]v1alpha1.CRDDescription , 0 )
456
561
for _ , name := range required {
457
562
requiredCRDDescs = append (requiredCRDDescs , v1alpha1.CRDDescription {Name : name , Version : "v1" , Kind : name })
458
563
}
564
+ if len (requiredCRDDescs ) == 0 {
565
+ requiredCRDDescs = nil
566
+ }
459
567
460
568
ownedCRDDescs := make ([]v1alpha1.CRDDescription , 0 )
461
569
for _ , name := range owned {
462
570
ownedCRDDescs = append (ownedCRDDescs , v1alpha1.CRDDescription {Name : name , Version : "v1" , Kind : name })
463
571
}
572
+ if len (ownedCRDDescs ) == 0 {
573
+ ownedCRDDescs = nil
574
+ }
464
575
465
- return v1alpha1.ClusterServiceVersion {
576
+ return & v1alpha1.ClusterServiceVersion {
466
577
ObjectMeta : metav1.ObjectMeta {
467
- Name : name ,
578
+ Name : name ,
579
+ Namespace : namespace ,
468
580
},
469
581
Spec : v1alpha1.ClusterServiceVersionSpec {
470
582
CustomResourceDefinitions : v1alpha1.CustomResourceDefinitions {
@@ -489,3 +601,17 @@ func crd(name string) v1beta1.CustomResourceDefinition {
489
601
},
490
602
}
491
603
}
604
+
605
+ func service (name , namespace string ) * corev1.Service {
606
+ return & corev1.Service {
607
+ ObjectMeta : metav1.ObjectMeta {
608
+ Name : name ,
609
+ Namespace : namespace ,
610
+ },
611
+ }
612
+ }
613
+
614
+ func toManifest (obj runtime.Object ) string {
615
+ raw , _ := json .Marshal (obj )
616
+ return string (raw )
617
+ }
0 commit comments