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