@@ -6,32 +6,22 @@ import (
66
77 . "github.com/onsi/ginkgo"
88 . "github.com/onsi/gomega"
9-
9+ "github.com/operator-framework/kubectl-operator/pkg/action"
1010 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+ "k8s.io/apimachinery/pkg/types"
1112 "sigs.k8s.io/controller-runtime/pkg/client"
1213
1314 olmv1 "github.com/operator-framework/operator-controller/api/v1"
1415
1516 internalaction "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action"
1617)
1718
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-
3019var _ = Describe ("CatalogCreate" , func () {
20+ catalogName := "testcatalog"
3121 pollInterval := 20
3222 expectedCatalog := olmv1.ClusterCatalog {
3323 ObjectMeta : metav1.ObjectMeta {
34- Name : "testcatalog" ,
24+ Name : catalogName ,
3525 Labels : map [string ]string {"a" : "b" },
3626 },
3727 Spec : olmv1.ClusterCatalogSpec {
@@ -49,9 +39,10 @@ var _ = Describe("CatalogCreate", func() {
4939
5040 It ("fails creating catalog" , func () {
5141 expectedErr := errors .New ("create failed" )
52- mockClient := & mockCreateClient {& mockCreator {createErr : expectedErr }, nil , nil , & expectedCatalog }
42+ mockClient := fakeClient {createErr : expectedErr }
43+ mockClient .Initialize ()
5344
54- creator := internalaction .NewCatalogCreate (mockClient )
45+ creator := internalaction .NewCatalogCreate (& action. Configuration { Client : mockClient } )
5546 creator .Available = true
5647 creator .CatalogName = expectedCatalog .Name
5748 creator .ImageSourceRef = expectedCatalog .Spec .Source .Image .Ref
@@ -64,17 +55,22 @@ var _ = Describe("CatalogCreate", func() {
6455 Expect (err ).To (MatchError (expectedErr ))
6556 Expect (mockClient .createCalled ).To (Equal (1 ))
6657
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))
7165 })
7266
7367 It ("fails waiting for created catalog status, successfully cleans up" , func () {
7468 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 ())
7671
77- creator := internalaction .NewCatalogCreate (mockClient )
72+ creator := internalaction .NewCatalogCreate (& action.Configuration {Client : mockClient })
73+ creator .CatalogName = expectedCatalog .Name
7874 err := creator .Run (context .TODO ())
7975
8076 Expect (err ).NotTo (BeNil ())
@@ -87,9 +83,12 @@ var _ = Describe("CatalogCreate", func() {
8783 It ("fails waiting for created catalog status, fails clean up" , func () {
8884 getErr := errors .New ("get failed" )
8985 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 ())
9188
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
9392 err := creator .Run (context .TODO ())
9493
9594 Expect (err ).NotTo (BeNil ())
@@ -98,6 +97,42 @@ var _ = Describe("CatalogCreate", func() {
9897 Expect (mockClient .getCalled ).To (Equal (1 ))
9998 Expect (mockClient .deleteCalled ).To (Equal (1 ))
10099 })
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+ })
101136})
102137
103138func validateCreateCatalog (actual , expected * olmv1.ClusterCatalog ) {
0 commit comments