@@ -6,32 +6,21 @@ import (
6
6
7
7
. "github.com/onsi/ginkgo"
8
8
. "github.com/onsi/gomega"
9
-
10
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
+ "k8s.io/apimachinery/pkg/types"
11
11
"sigs.k8s.io/controller-runtime/pkg/client"
12
12
13
13
olmv1 "github.com/operator-framework/operator-controller/api/v1"
14
14
15
15
internalaction "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action"
16
16
)
17
17
18
- type mockCreateClient struct {
19
- * mockCreator
20
- * mockGetter
21
- * mockDeleter
22
- createCatalog * olmv1.ClusterCatalog
23
- }
24
-
25
- func (mcc * mockCreateClient ) Create (ctx context.Context , obj client.Object , opts ... client.CreateOption ) error {
26
- mcc .createCatalog = obj .(* olmv1.ClusterCatalog )
27
- return mcc .mockCreator .Create (ctx , obj , opts ... )
28
- }
29
-
30
18
var _ = Describe ("CatalogCreate" , func () {
19
+ catalogName := "testcatalog"
31
20
pollInterval := 20
32
21
expectedCatalog := olmv1.ClusterCatalog {
33
22
ObjectMeta : metav1.ObjectMeta {
34
- Name : "testcatalog" ,
23
+ Name : catalogName ,
35
24
Labels : map [string ]string {"a" : "b" },
36
25
},
37
26
Spec : olmv1.ClusterCatalogSpec {
@@ -49,9 +38,10 @@ var _ = Describe("CatalogCreate", func() {
49
38
50
39
It ("fails creating catalog" , func () {
51
40
expectedErr := errors .New ("create failed" )
52
- mockClient := & mockCreateClient {& mockCreator {createErr : expectedErr }, nil , nil , & expectedCatalog }
41
+ testClient := fakeClient {createErr : expectedErr }
42
+ Expect (testClient .Initialize ()).To (Succeed ())
53
43
54
- creator := internalaction .NewCatalogCreate (mockClient )
44
+ creator := internalaction .NewCatalogCreate (testClient )
55
45
creator .Available = true
56
46
creator .CatalogName = expectedCatalog .Name
57
47
creator .ImageSourceRef = expectedCatalog .Spec .Source .Image .Ref
@@ -62,41 +52,78 @@ var _ = Describe("CatalogCreate", func() {
62
52
63
53
Expect (err ).NotTo (BeNil ())
64
54
Expect (err ).To (MatchError (expectedErr ))
65
- Expect (mockClient .createCalled ).To (Equal (1 ))
66
-
67
- // there is no way of testing a happy path in unit tests because we have no way to
68
- // set/mock the catalog status condition we're waiting for in waitUntilCatalogStatusCondition
69
- // but we can still at least verify that CR would have been created with expected attribute values
70
- validateCreateCatalog (mockClient .createCatalog , & expectedCatalog )
55
+ Expect (testClient .createCalled ).To (Equal (1 ))
71
56
})
72
57
73
58
It ("fails waiting for created catalog status, successfully cleans up" , func () {
74
59
expectedErr := errors .New ("get failed" )
75
- mockClient := & mockCreateClient {& mockCreator {}, & mockGetter {getErr : expectedErr }, & mockDeleter {}, nil }
60
+ testClient := fakeClient {getErr : expectedErr }
61
+ Expect (testClient .Initialize ()).To (Succeed ())
76
62
77
- creator := internalaction .NewCatalogCreate (mockClient )
63
+ creator := internalaction .NewCatalogCreate (testClient )
64
+ // fakeClient requires at least the catalogName to be set to run
65
+ creator .CatalogName = expectedCatalog .Name
78
66
err := creator .Run (context .TODO ())
79
67
80
68
Expect (err ).NotTo (BeNil ())
81
69
Expect (err ).To (MatchError (expectedErr ))
82
- Expect (mockClient .createCalled ).To (Equal (1 ))
83
- Expect (mockClient .getCalled ).To (Equal (1 ))
84
- Expect (mockClient .deleteCalled ).To (Equal (1 ))
70
+ Expect (testClient .createCalled ).To (Equal (1 ))
71
+ Expect (testClient .getCalled ).To (Equal (1 ))
72
+ Expect (testClient .deleteCalled ).To (Equal (1 ))
85
73
})
86
74
87
75
It ("fails waiting for created catalog status, fails clean up" , func () {
88
76
getErr := errors .New ("get failed" )
89
77
deleteErr := errors .New ("delete failed" )
90
- mockClient := & mockCreateClient {& mockCreator {}, & mockGetter {getErr : getErr }, & mockDeleter {deleteErr : deleteErr }, nil }
78
+ testClient := fakeClient {deleteErr : deleteErr , getErr : getErr }
79
+ Expect (testClient .Initialize ()).To (Succeed ())
91
80
92
- creator := internalaction .NewCatalogCreate (mockClient )
81
+ creator := internalaction .NewCatalogCreate (testClient )
82
+ // fakeClient requires at least the catalogName to be set to run
83
+ creator .CatalogName = expectedCatalog .Name
93
84
err := creator .Run (context .TODO ())
94
85
95
86
Expect (err ).NotTo (BeNil ())
96
87
Expect (err ).To (MatchError (getErr ))
97
- Expect (mockClient .createCalled ).To (Equal (1 ))
98
- Expect (mockClient .getCalled ).To (Equal (1 ))
99
- Expect (mockClient .deleteCalled ).To (Equal (1 ))
88
+ Expect (testClient .createCalled ).To (Equal (1 ))
89
+ Expect (testClient .getCalled ).To (Equal (1 ))
90
+ Expect (testClient .deleteCalled ).To (Equal (1 ))
91
+ })
92
+ It ("succeeds creating catalog" , func () {
93
+ testClient := fakeClient {
94
+ transformers : []objectTransformer {
95
+ {
96
+ verb : verbCreate ,
97
+ objectKey : types.NamespacedName {Name : catalogName },
98
+ transformFunc : func (obj * client.Object ) {
99
+ if obj == nil {
100
+ return
101
+ }
102
+ catalogObj , ok := (* obj ).(* olmv1.ClusterCatalog )
103
+ if ! ok {
104
+ return
105
+ }
106
+ catalogObj .Status .Conditions = []metav1.Condition {{Type : olmv1 .TypeServing , Status : metav1 .ConditionTrue }}
107
+ },
108
+ },
109
+ },
110
+ }
111
+ Expect (testClient .Initialize ()).To (Succeed ())
112
+
113
+ creator := internalaction .NewCatalogCreate (testClient )
114
+ creator .Available = true
115
+ creator .CatalogName = expectedCatalog .Name
116
+ creator .ImageSourceRef = expectedCatalog .Spec .Source .Image .Ref
117
+ creator .Priority = expectedCatalog .Spec .Priority
118
+ creator .Labels = expectedCatalog .Labels
119
+ creator .PollIntervalMinutes = * expectedCatalog .Spec .Source .Image .PollIntervalMinutes
120
+ Expect (creator .Run (context .TODO ())).To (Succeed ())
121
+
122
+ Expect (testClient .createCalled ).To (Equal (1 ))
123
+
124
+ actualCatalog := & olmv1.ClusterCatalog {TypeMeta : metav1.TypeMeta {Kind : "ClusterCatalog" , APIVersion : "olm.operatorframework.io/v1" }}
125
+ Expect (testClient .Client .Get (context .TODO (), types.NamespacedName {Name : catalogName }, actualCatalog )).To (Succeed ())
126
+ validateCreateCatalog (actualCatalog , & expectedCatalog )
100
127
})
101
128
})
102
129
0 commit comments