Skip to content

Commit 218045e

Browse files
authored
Add unit tests for operator_installer.go (#4165)
Add unit tests for operator_installer.go and olm_resources.go
1 parent aa0c623 commit 218045e

File tree

3 files changed

+212
-10
lines changed

3 files changed

+212
-10
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package registry
16+
17+
import (
18+
. "github.com/onsi/ginkgo"
19+
. "github.com/onsi/gomega"
20+
)
21+
22+
var _ = Describe("newCatalogSource", func() {
23+
Describe("newCatalogSource", func() {
24+
It("should create a CatalogSource with name and namespace set", func() {
25+
cs := newCatalogSource("fakeName", "fakeNS")
26+
Expect(cs.ObjectMeta.Name).To(Equal("fakeName"))
27+
Expect(cs.ObjectMeta.Namespace).To(Equal("fakeNS"))
28+
})
29+
})
30+
Describe("withSDKPublisher", func() {
31+
It("should set the display name and publisher of a CatalogSource", func() {
32+
cs := newCatalogSource("fakeName", "fakeNS", withSDKPublisher("fakeDisplay"))
33+
Expect(cs.Spec.DisplayName).To(Equal("fakeDisplay"))
34+
Expect(cs.Spec.Publisher).To(Equal("operator-sdk"))
35+
})
36+
})
37+
Describe("withInstallPlanApproval", func() {
38+
It("should set the display name and publisher of a CatalogSource", func() {
39+
cs := newCatalogSource("fakeName", "fakeNS", withSDKPublisher("fakeDisplay"))
40+
Expect(cs.Spec.DisplayName).To(Equal("fakeDisplay"))
41+
Expect(cs.Spec.Publisher).To(Equal("operator-sdk"))
42+
})
43+
})
44+
45+
})

internal/olm/operator/registry/operator_installer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (o OperatorInstaller) InstallOperator(ctx context.Context) (*v1alpha1.Clust
7272

7373
var subscription *v1alpha1.Subscription
7474
// Create Subscription
75-
if subscription, err = o.createSubscription(ctx, cs); err != nil {
75+
if subscription, err = o.createSubscription(ctx, cs.GetName()); err != nil {
7676
return nil, err
7777
}
7878

@@ -208,10 +208,10 @@ func (o OperatorInstaller) getOperatorGroup(ctx context.Context) (*v1.OperatorGr
208208
return &ogList.Items[0], true, nil
209209
}
210210

211-
func (o OperatorInstaller) createSubscription(ctx context.Context, cs *v1alpha1.CatalogSource) (*v1alpha1.Subscription, error) {
211+
func (o OperatorInstaller) createSubscription(ctx context.Context, csName string) (*v1alpha1.Subscription, error) {
212212
sub := newSubscription(o.StartingCSV, o.cfg.Namespace,
213213
withPackageChannel(o.PackageName, o.Channel, o.StartingCSV),
214-
withCatalogSource(cs.GetName(), o.cfg.Namespace),
214+
withCatalogSource(csName, o.cfg.Namespace),
215215
withInstallPlanApproval(v1alpha1.ApprovalManual))
216216

217217
if err := o.cfg.Client.Create(ctx, sub); err != nil {

internal/olm/operator/registry/operator_installer_test.go

Lines changed: 164 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121
. "github.com/onsi/gomega"
2222
v1 "github.com/operator-framework/api/pkg/operators/v1"
2323
"github.com/operator-framework/api/pkg/operators/v1alpha1"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/types"
2528
"k8s.io/apimachinery/pkg/util/sets"
2629
crclient "sigs.k8s.io/controller-runtime/pkg/client"
2730
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -30,8 +33,167 @@ import (
3033
)
3134

3235
var _ = Describe("OperatorInstaller", func() {
33-
Describe("InstallOperator", func() {
34-
// TODO: fill this in once run bundle is done
36+
Describe("NewOperatorInstaller", func() {
37+
It("should create an OperatorInstaller", func() {
38+
cfg := &operator.Configuration{}
39+
oi := NewOperatorInstaller(cfg)
40+
Expect(oi).ToNot(BeNil())
41+
})
42+
})
43+
44+
Describe("createSubscription", func() {
45+
var (
46+
oi *OperatorInstaller
47+
sch *runtime.Scheme
48+
)
49+
BeforeEach(func() {
50+
// Setup and fake client
51+
cfg := &operator.Configuration{}
52+
sch = runtime.NewScheme()
53+
Expect(v1.AddToScheme(sch)).To(Succeed())
54+
Expect(v1alpha1.AddToScheme(sch)).To(Succeed())
55+
cfg.Client = fake.NewFakeClientWithScheme(sch)
56+
57+
oi = NewOperatorInstaller(cfg)
58+
oi.StartingCSV = "fakeName"
59+
oi.cfg.Namespace = "fakeNS"
60+
})
61+
62+
It("should create the subscription with the fake client", func() {
63+
sub, err := oi.createSubscription(context.TODO(), "huzzah")
64+
Expect(err).ToNot(HaveOccurred())
65+
66+
retSub := &v1alpha1.Subscription{}
67+
subKey := types.NamespacedName{
68+
Namespace: sub.GetNamespace(),
69+
Name: sub.GetName(),
70+
}
71+
err = oi.cfg.Client.Get(context.TODO(), subKey, retSub)
72+
Expect(err).ToNot(HaveOccurred())
73+
Expect(retSub.GetName()).To(Equal(sub.GetName()))
74+
Expect(retSub.GetNamespace()).To(Equal(sub.GetNamespace()))
75+
})
76+
77+
It("should pass through any client errors (duplicate)", func() {
78+
79+
sub := newSubscription(oi.StartingCSV, oi.cfg.Namespace, withCatalogSource("duplicate", oi.cfg.Namespace))
80+
oi.cfg.Client = fake.NewFakeClientWithScheme(sch, sub)
81+
82+
_, err := oi.createSubscription(context.TODO(), "duplicate")
83+
Expect(err).To(HaveOccurred())
84+
Expect(err.Error()).Should(ContainSubstring("error creating subscription"))
85+
})
86+
})
87+
88+
Describe("approveInstallPlan", func() {
89+
var (
90+
oi *OperatorInstaller
91+
sch *runtime.Scheme
92+
)
93+
BeforeEach(func() {
94+
cfg := &operator.Configuration{}
95+
sch = runtime.NewScheme()
96+
Expect(v1alpha1.AddToScheme(sch)).To(Succeed())
97+
oi = NewOperatorInstaller(cfg)
98+
})
99+
100+
It("should update the install plan", func() {
101+
oi.cfg.Client = fake.NewFakeClientWithScheme(sch,
102+
&v1alpha1.InstallPlan{
103+
ObjectMeta: metav1.ObjectMeta{
104+
Name: "fakeName",
105+
Namespace: "fakeNS",
106+
},
107+
},
108+
)
109+
110+
ip := &v1alpha1.InstallPlan{}
111+
ipKey := types.NamespacedName{
112+
Namespace: "fakeNS",
113+
Name: "fakeName",
114+
}
115+
116+
err := oi.cfg.Client.Get(context.TODO(), ipKey, ip)
117+
Expect(err).ToNot(HaveOccurred())
118+
Expect(ip.Name).To(Equal("fakeName"))
119+
Expect(ip.Namespace).To(Equal("fakeNS"))
120+
121+
// Test
122+
sub := &v1alpha1.Subscription{
123+
Status: v1alpha1.SubscriptionStatus{
124+
InstallPlanRef: &corev1.ObjectReference{
125+
Name: "fakeName",
126+
Namespace: "fakeNS",
127+
},
128+
},
129+
}
130+
err = oi.approveInstallPlan(context.TODO(), sub)
131+
Expect(err).ToNot(HaveOccurred())
132+
err = oi.cfg.Client.Get(context.TODO(), ipKey, ip)
133+
Expect(err).ToNot(HaveOccurred())
134+
Expect(ip.Name).To(Equal("fakeName"))
135+
Expect(ip.Namespace).To(Equal("fakeNS"))
136+
Expect(ip.Spec.Approved).To(Equal(true))
137+
})
138+
It("should return an error if the install plan does not exist.", func() {
139+
oi.cfg.Client = fake.NewFakeClientWithScheme(sch)
140+
sub := &v1alpha1.Subscription{
141+
Status: v1alpha1.SubscriptionStatus{
142+
InstallPlanRef: &corev1.ObjectReference{
143+
Name: "fakeName",
144+
Namespace: "fakeNS",
145+
},
146+
},
147+
}
148+
err := oi.approveInstallPlan(context.TODO(), sub)
149+
Expect(err).To(HaveOccurred())
150+
Expect(err.Error()).Should(ContainSubstring("error getting install plan"))
151+
})
152+
})
153+
154+
Describe("waitForInstallPlan", func() {
155+
var (
156+
oi *OperatorInstaller
157+
sch *runtime.Scheme
158+
)
159+
BeforeEach(func() {
160+
// Setup and fake client
161+
cfg := &operator.Configuration{}
162+
sch = runtime.NewScheme()
163+
Expect(v1alpha1.AddToScheme(sch)).To(Succeed())
164+
cfg.Client = fake.NewFakeClientWithScheme(sch)
165+
166+
oi = NewOperatorInstaller(cfg)
167+
oi.StartingCSV = "fakeName"
168+
oi.cfg.Namespace = "fakeNS"
169+
})
170+
It("should return an error if the subscription does not exist.", func() {
171+
sub := newSubscription(oi.StartingCSV, oi.cfg.Namespace, withCatalogSource("duplicate", oi.cfg.Namespace))
172+
173+
err := oi.waitForInstallPlan(context.TODO(), sub)
174+
Expect(err).To(HaveOccurred())
175+
Expect(err.Error()).Should(ContainSubstring("install plan is not available for the subscription"))
176+
177+
})
178+
It("should return if subscription has an install plan.", func() {
179+
sub := &v1alpha1.Subscription{
180+
ObjectMeta: metav1.ObjectMeta{
181+
Name: "fakeName",
182+
Namespace: "fakeNS",
183+
},
184+
Status: v1alpha1.SubscriptionStatus{
185+
InstallPlanRef: &corev1.ObjectReference{
186+
Name: "fakeName",
187+
Namespace: "fakeNS",
188+
},
189+
},
190+
}
191+
err := oi.cfg.Client.Create(context.TODO(), sub)
192+
Expect(err).ToNot(HaveOccurred())
193+
194+
err = oi.waitForInstallPlan(context.TODO(), sub)
195+
Expect(err).ToNot(HaveOccurred())
196+
})
35197
})
36198

37199
Describe("ensureOperatorGroup", func() {
@@ -212,7 +374,6 @@ var _ = Describe("OperatorInstaller", func() {
212374
})
213375
})
214376
})
215-
216377
Describe("createOperatorGroup", func() {
217378
var (
218379
oi OperatorInstaller
@@ -335,10 +496,6 @@ var _ = Describe("OperatorInstaller", func() {
335496
})
336497
})
337498

338-
Describe("createSubscription", func() {
339-
// TODO: add them as part of a different story
340-
})
341-
342499
Describe("getTargetNamespaces", func() {
343500
var (
344501
oi OperatorInstaller

0 commit comments

Comments
 (0)