Skip to content

Commit ebe90d2

Browse files
authored
Merge pull request #24 from IBM/tests8
test files refactoring
2 parents 0e31f57 + 0a87b1f commit ebe90d2

File tree

6 files changed

+105
-128
lines changed

6 files changed

+105
-128
lines changed

cmd/manager/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
if flag.Lookup("kubeconfig") == nil {
5454
flag.String("kubeconfig", os.Getenv("KUBECONFIG"), "Path to a kube config. Only required if out-of-cluster.")
5555
}
56-
syncPeriod := flag.Duration("syncPeriod", 30 * time.Second, "Defines the minimum frequency at which watched Compsable resources are reconciled." )
56+
syncPeriod := flag.Duration("syncPeriod", 30*time.Second, "Defines the minimum frequency at which watched Compsable resources are reconciled.")
5757
flag.Parse()
5858
// build config for the cluster
5959
cfg, err := BuildConfig(kubeconfig)

pkg/controller/composable/composable_controller_test.go

+69-75
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"time"
2525

2626
"github.com/ibm/composable/pkg/apis"
27-
"github.com/ibm/composable/pkg/context"
2827
"github.com/ibm/composable/test"
2928
. "github.com/onsi/ginkgo"
3029
. "github.com/onsi/gomega"
@@ -35,19 +34,14 @@ import (
3534
"k8s.io/client-go/kubernetes/scheme"
3635
"k8s.io/client-go/rest"
3736
"k8s.io/klog"
38-
"sigs.k8s.io/controller-runtime/pkg/client"
3937
"sigs.k8s.io/controller-runtime/pkg/envtest"
4038
"sigs.k8s.io/controller-runtime/pkg/manager"
41-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4239
)
4340

4441
var (
45-
c client.Client
46-
cfg *rest.Config
47-
testNs string
48-
scontext context.Context
49-
t *envtest.Environment
50-
stop chan struct{}
42+
testContext test.TestContext
43+
testEnv *envtest.Environment
44+
stop chan struct{}
5145
)
5246

5347
func TestComposable(t *testing.T) {
@@ -63,35 +57,36 @@ func TestComposable(t *testing.T) {
6357

6458
var _ = BeforeSuite(func() {
6559

66-
t = &envtest.Environment{
60+
testEnv = &envtest.Environment{
6761
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds"),
6862
filepath.Join("./testdata", "crds")},
6963
ControlPlaneStartTimeout: 2 * time.Minute,
7064
}
7165
apis.AddToScheme(scheme.Scheme)
7266

7367
var err error
74-
if cfg, err = t.Start(); err != nil {
68+
var cfg *rest.Config
69+
if cfg, err = testEnv.Start(); err != nil {
7570
log.Fatal(err)
7671
}
7772

7873
syncPeriod := 30 * time.Second // set a sync period
7974
mgr, err := manager.New(cfg, manager.Options{SyncPeriod: &syncPeriod})
8075
Expect(err).NotTo(HaveOccurred())
8176

82-
c = mgr.GetClient()
77+
client := mgr.GetClient()
8378

8479
recFn := newReconciler(mgr)
8580
Expect(add(mgr, recFn)).NotTo(HaveOccurred())
8681
stop = test.StartTestManager(mgr)
87-
testNs = test.SetupKubeOrDie(cfg, "test-ns-")
88-
scontext = context.New(c, reconcile.Request{NamespacedName: types.NamespacedName{Name: "", Namespace: testNs}})
82+
testNs := test.SetupKubeOrDie(cfg, "test-ns-")
83+
testContext = test.NewTestContext(client, testNs)
8984

9085
})
9186

9287
var _ = AfterSuite(func() {
9388
close(stop)
94-
t.Stop()
89+
testEnv.Stop()
9590
})
9691

9792
var _ = Describe("test Composable operations", func() {
@@ -107,28 +102,27 @@ var _ = Describe("test Composable operations", func() {
107102
AfterEach(func() {
108103
// delete the Composable object
109104
comp := test.LoadCompasable(dataDir + "compCopy.yaml")
110-
test.DeleteInNs(scontext, &comp, false)
111-
Eventually(test.GetObject(scontext, &comp)).Should(BeNil())
105+
test.DeleteInNs(testContext, &comp, false)
106+
Eventually(test.GetObject(testContext, &comp)).Should(BeNil())
112107

113108
obj := test.LoadObject(dataDir+"inputDataObject.yaml", &unstructured.Unstructured{})
114-
test.DeleteObject(scontext, obj, false)
115-
Eventually(test.GetObject(scontext, obj)).Should(BeNil())
109+
test.DeleteObject(testContext, obj, false)
110+
Eventually(test.GetObject(testContext, obj)).Should(BeNil())
116111
})
117112

118113
It("Composable should successfully set default values to the output object", func() {
119114

120-
121115
By("Deploy Composable object")
122116
comp := test.LoadCompasable(dataDir + "compCopy.yaml")
123-
test.PostInNs(scontext, &comp, true, 0)
124-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
117+
test.PostInNs(testContext, &comp, true, 0)
118+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
125119

126120
By("Get Output object")
127121
groupVersionKind := schema.GroupVersionKind{Kind: "OutputValue", Version: "v1", Group: "test.ibmcloud.ibm.com"}
128122
unstrObj.SetGroupVersionKind(groupVersionKind)
129-
objNamespacedname := types.NamespacedName{Namespace: testNs, Name: "comp-out"}
123+
objNamespacedname := types.NamespacedName{Namespace: testContext.Namespace(), Name: "comp-out"}
130124
klog.V(5).Infof("Get Object %s\n", objNamespacedname)
131-
Eventually(test.GetUnstructuredObject(scontext, objNamespacedname, &unstrObj)).Should(Succeed())
125+
Eventually(test.GetUnstructuredObject(testContext, objNamespacedname, &unstrObj)).Should(Succeed())
132126
testSpec, ok := unstrObj.Object[spec].(map[string]interface{})
133127
Ω(ok).Should(BeTrue())
134128

@@ -148,13 +142,13 @@ var _ = Describe("test Composable operations", func() {
148142
Ω(testSpec["stringFromBase64"]).Should(Equal("default"))
149143

150144
By("default arrayStrings")
151-
Ω(testSpec["arrayStrings"]).Should(BeEquivalentTo([]interface{}{"aa","bb","cc"}))
145+
Ω(testSpec["arrayStrings"]).Should(BeEquivalentTo([]interface{}{"aa", "bb", "cc"}))
152146

153147
By("default arrayIntegers")
154-
Ω(testSpec["arrayIntegers"]).Should(BeEquivalentTo([]interface{}{int64(1),int64(0),int64(1)}))
148+
Ω(testSpec["arrayIntegers"]).Should(BeEquivalentTo([]interface{}{int64(1), int64(0), int64(1)}))
155149

156150
By("default objectValue")
157-
Ω(testSpec["objectValue"]).Should(BeEquivalentTo(map[string]interface {}{"family": "DefaultFamilyName", "first": "DefaultFirstName", "age": int64(-1)}))
151+
Ω(testSpec["objectValue"]).Should(BeEquivalentTo(map[string]interface{}{"family": "DefaultFamilyName", "first": "DefaultFirstName", "age": int64(-1)}))
158152

159153
By("default stringJson2Value")
160154
Ω(testSpec["stringJson2Value"]).Should(BeEquivalentTo("default1,default2,default3"))
@@ -165,25 +159,25 @@ var _ = Describe("test Composable operations", func() {
165159

166160
By("Deploy input Object")
167161
obj := test.LoadObject(dataDir+"inputDataObject.yaml", &unstructured.Unstructured{})
168-
test.CreateObject(scontext, obj, true, 0)
162+
test.CreateObject(testContext, obj, true, 0)
169163

170164
groupVersionKind := schema.GroupVersionKind{Kind: "InputValue", Version: "v1", Group: "test.ibmcloud.ibm.com"}
171165
unstrObj.SetGroupVersionKind(groupVersionKind)
172166
objNamespacedname := types.NamespacedName{Namespace: "default", Name: "inputdata"}
173167
klog.V(5).Infof("Get Object %s\n", objNamespacedname)
174-
Eventually(test.GetUnstructuredObject(scontext, objNamespacedname, &unstrObj)).Should(Succeed())
168+
Eventually(test.GetUnstructuredObject(testContext, objNamespacedname, &unstrObj)).Should(Succeed())
175169

176170
By("Deploy Composable object")
177171
comp := test.LoadCompasable(dataDir + "compCopy.yaml")
178-
test.PostInNs(scontext, &comp, true, 0)
179-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
172+
test.PostInNs(testContext, &comp, true, 0)
173+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
180174

181175
By("Get Output object")
182176
groupVersionKind = schema.GroupVersionKind{Kind: "OutputValue", Version: "v1", Group: "test.ibmcloud.ibm.com"}
183177
unstrObj.SetGroupVersionKind(groupVersionKind)
184-
objNamespacedname = types.NamespacedName{Namespace: testNs, Name: "comp-out"}
178+
objNamespacedname = types.NamespacedName{Namespace: testContext.Namespace(), Name: "comp-out"}
185179
klog.V(5).Infof("Get Object %s\n", objNamespacedname)
186-
Eventually(test.GetUnstructuredObject(scontext, objNamespacedname, &unstrObj)).Should(Succeed())
180+
Eventually(test.GetUnstructuredObject(testContext, objNamespacedname, &unstrObj)).Should(Succeed())
187181
testSpec, ok := unstrObj.Object[spec].(map[string]interface{})
188182
Ω(ok).Should(BeTrue())
189183

@@ -208,10 +202,10 @@ var _ = Describe("test Composable operations", func() {
208202
Ω(testSpec["arrayStrings"]).Should(Equal(strArray))
209203

210204
By("copy arrayIntegers")
211-
Ω(testSpec["arrayIntegers"]).Should(Equal([]interface{}{int64(1),int64(2),int64(3),int64(4)}))
205+
Ω(testSpec["arrayIntegers"]).Should(Equal([]interface{}{int64(1), int64(2), int64(3), int64(4)}))
212206

213207
By("copy objectValue")
214-
Ω(testSpec["objectValue"]).Should(Equal(map[string]interface {}{"family": "FamilyName", "first": "FirstName", "age": int64(27)}))
208+
Ω(testSpec["objectValue"]).Should(Equal(map[string]interface{}{"family": "FamilyName", "first": "FirstName", "age": int64(27)}))
215209

216210
By("copy stringJson2Value")
217211
val, _ := Array2CSStringTransformer(strArray)
@@ -223,32 +217,32 @@ var _ = Describe("test Composable operations", func() {
223217

224218
gvkIn := schema.GroupVersionKind{Kind: "InputValue", Version: "v1", Group: "test.ibmcloud.ibm.com"}
225219
gvkOut := schema.GroupVersionKind{Kind: "OutputValue", Version: "v1", Group: "test.ibmcloud.ibm.com"}
226-
objNamespacednameIn := types.NamespacedName{Namespace: "default", Name: "inputdata"}
227-
objNamespacednameOut := types.NamespacedName{Namespace: testNs, Name: "comp-out"}
220+
objNamespacednameIn := types.NamespacedName{Namespace: "default", Name: "inputdata"}
221+
objNamespacednameOut := types.NamespacedName{Namespace: testContext.Namespace(), Name: "comp-out"}
228222

229223
//unstrObj.SetGroupVersionKind(gvkOut)
230224
// First, the output object is created with defult values, after that we deploy the inputObject and will check
231225
// that all Output object filed are updated.
232226
By("check that input object doesn't exist") // the object should not exist
233227
unstrObj.SetGroupVersionKind(gvkIn)
234-
Ω(test.GetUnstructuredObject(scontext, objNamespacednameIn, &unstrObj)()).Should(HaveOccurred())
228+
Ω(test.GetUnstructuredObject(testContext, objNamespacednameIn, &unstrObj)()).Should(HaveOccurred())
235229

236230
By("check taht output object doesn't exist. If it does => remove it ") // the object should not exist, or we delete it
237231
unstrObj.SetGroupVersionKind(gvkOut)
238-
err2 := test.GetUnstructuredObject(scontext, objNamespacednameOut, &unstrObj)()
232+
err2 := test.GetUnstructuredObject(testContext, objNamespacednameOut, &unstrObj)()
239233
if err2 == nil {
240-
test.DeleteObject(scontext, &unstrObj, false)
241-
Eventually(test.GetObject(scontext, &unstrObj)).Should(BeNil())
234+
test.DeleteObject(testContext, &unstrObj, false)
235+
Eventually(test.GetObject(testContext, &unstrObj)).Should(BeNil())
242236
}
243237

244238
By("deploy Composable object")
245239
comp := test.LoadCompasable(dataDir + "compCopy.yaml")
246-
test.PostInNs(scontext, &comp, true, 0)
247-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
240+
test.PostInNs(testContext, &comp, true, 0)
241+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
248242

249243
By("get Output object")
250244
unstrObj.SetGroupVersionKind(gvkOut)
251-
Eventually(test.GetUnstructuredObject(scontext, objNamespacednameOut, &unstrObj)).Should(Succeed())
245+
Eventually(test.GetUnstructuredObject(testContext, objNamespacednameOut, &unstrObj)).Should(Succeed())
252246
testSpec, ok := unstrObj.Object[spec].(map[string]interface{})
253247
Ω(ok).Should(BeTrue())
254248

@@ -261,16 +255,16 @@ var _ = Describe("test Composable operations", func() {
261255

262256
By("deploy input Object")
263257
obj := test.LoadObject(dataDir+"inputDataObject.yaml", &unstructured.Unstructured{})
264-
test.CreateObject(scontext, obj, true, 0)
258+
test.CreateObject(testContext, obj, true, 0)
265259

266260
unstrObj.SetGroupVersionKind(gvkIn)
267-
Eventually(test.GetUnstructuredObject(scontext, objNamespacednameIn, &unstrObj)).Should(Succeed())
261+
Eventually(test.GetUnstructuredObject(testContext, objNamespacednameIn, &unstrObj)).Should(Succeed())
268262

269263
By("check updated inValue")
270264
unstrObj = unstructured.Unstructured{}
271265
unstrObj.SetGroupVersionKind(gvkOut)
272-
Eventually( func() (int64, error) {
273-
err := test.GetUnstructuredObject(scontext, objNamespacednameOut, &unstrObj)()
266+
Eventually(func() (int64, error) {
267+
err := test.GetUnstructuredObject(testContext, objNamespacednameOut, &unstrObj)()
274268
if err != nil {
275269
return int64(0), err
276270
}
@@ -295,10 +289,10 @@ var _ = Describe("test Composable operations", func() {
295289
Ω(testSpec["arrayStrings"]).Should(Equal(strArray))
296290

297291
By("check updated arrayIntegers")
298-
Ω(testSpec["arrayIntegers"]).Should(Equal([]interface{}{int64(1),int64(2),int64(3),int64(4)}))
292+
Ω(testSpec["arrayIntegers"]).Should(Equal([]interface{}{int64(1), int64(2), int64(3), int64(4)}))
299293

300294
By("check updated objectValue")
301-
Ω(testSpec["objectValue"]).Should(Equal(map[string]interface {}{"family": "FamilyName", "first": "FirstName", "age": int64(27)}))
295+
Ω(testSpec["objectValue"]).Should(Equal(map[string]interface{}{"family": "FamilyName", "first": "FirstName", "age": int64(27)}))
302296

303297
By("check updated stringJson2Value")
304298
val, _ := Array2CSStringTransformer(strArray)
@@ -315,22 +309,22 @@ var _ = Describe("IBM cloud-operators compatibility", func() {
315309
It("should correctly create the Service instance", func() {
316310

317311
comp := test.LoadCompasable(dataDir + "comp.yaml")
318-
test.PostInNs(scontext, &comp, true, 0)
319-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
312+
test.PostInNs(testContext, &comp, true, 0)
313+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
320314

321-
objNamespacedname := types.NamespacedName{Namespace: scontext.Namespace(), Name: "mymessagehub"}
315+
objNamespacedname := types.NamespacedName{Namespace: testContext.Namespace(), Name: "mymessagehub"}
322316
unstrObj := unstructured.Unstructured{}
323317
unstrObj.SetGroupVersionKind(groupVersionKind)
324318
klog.V(5).Infof("Get Object %s\n", objNamespacedname)
325-
Eventually(test.GetUnstructuredObject(scontext, objNamespacedname, &unstrObj)).Should(Succeed())
326-
Eventually(test.GetState(scontext, &comp)).Should(Equal(OnlineStatus))
319+
Eventually(test.GetUnstructuredObject(testContext, objNamespacedname, &unstrObj)).Should(Succeed())
320+
Eventually(test.GetState(testContext, &comp)).Should(Equal(OnlineStatus))
327321

328322
})
329323

330324
It("should delete the Composable and Service instances", func() {
331325
comp := test.LoadCompasable(dataDir + "comp.yaml")
332-
test.DeleteInNs(scontext, &comp, false)
333-
Eventually(test.GetObject(scontext, &comp)).Should(BeNil())
326+
test.DeleteInNs(testContext, &comp, false)
327+
Eventually(test.GetObject(testContext, &comp)).Should(BeNil())
334328
})
335329

336330
})
@@ -340,48 +334,48 @@ var _ = Describe("IBM cloud-operators compatibility", func() {
340334

341335
BeforeEach(func() {
342336
obj := test.LoadObject(dataDir+"mysecret.yaml", &v1.Secret{})
343-
test.PostInNs(scontext, obj, true, 0)
344-
objNamespacedname = types.NamespacedName{Namespace: scontext.Namespace(), Name: "mymessagehub"}
337+
test.PostInNs(testContext, obj, true, 0)
338+
objNamespacedname = types.NamespacedName{Namespace: testContext.Namespace(), Name: "mymessagehub"}
345339
})
346340

347341
AfterEach(func() {
348342
obj := test.LoadObject(dataDir+"mysecret.yaml", &v1.Secret{})
349-
test.DeleteInNs(scontext, obj, false)
343+
test.DeleteInNs(testContext, obj, false)
350344
})
351345

352346
It("should correctly create the Service instance according to parameters from a Secret object", func() {
353347
comp := test.LoadCompasable(dataDir + "comp1.yaml")
354-
test.PostInNs(scontext, &comp, false, 0)
355-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
348+
test.PostInNs(testContext, &comp, false, 0)
349+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
356350

357351
unstrObj := unstructured.Unstructured{}
358352
unstrObj.SetGroupVersionKind(groupVersionKind)
359353
klog.V(5).Infof("Get Object %s\n", objNamespacedname)
360-
Eventually(test.GetUnstructuredObject(scontext, objNamespacedname, &unstrObj)).Should(Succeed())
354+
Eventually(test.GetUnstructuredObject(testContext, objNamespacedname, &unstrObj)).Should(Succeed())
361355
Ω(getPlan(unstrObj.Object)).Should(Equal("standard"))
362-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
363-
Eventually(test.GetState(scontext, &comp)).Should(Equal(OnlineStatus))
364-
test.DeleteInNs(scontext, &comp, false)
365-
Eventually(test.GetObject(scontext, &comp)).Should(BeNil())
356+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
357+
Eventually(test.GetState(testContext, &comp)).Should(Equal(OnlineStatus))
358+
test.DeleteInNs(testContext, &comp, false)
359+
Eventually(test.GetObject(testContext, &comp)).Should(BeNil())
366360
})
367361

368362
It("should correctly create the Service instance according to parameters from a ConfigMap", func() {
369363
obj := test.LoadObject(dataDir+"myconfigmap.yaml", &v1.ConfigMap{})
370-
test.PostInNs(scontext, obj, true, 0)
364+
test.PostInNs(testContext, obj, true, 0)
371365

372366
comp := test.LoadCompasable(dataDir + "comp2.yaml")
373-
test.PostInNs(scontext, &comp, false, 0)
374-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
367+
test.PostInNs(testContext, &comp, false, 0)
368+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
375369

376370
unstrObj := unstructured.Unstructured{}
377371
unstrObj.SetGroupVersionKind(groupVersionKind)
378372
klog.V(5).Infof("Get Object %s\n", objNamespacedname)
379-
Eventually(test.GetUnstructuredObject(scontext, objNamespacedname, &unstrObj)).Should(Succeed())
373+
Eventually(test.GetUnstructuredObject(testContext, objNamespacedname, &unstrObj)).Should(Succeed())
380374
Ω(getPlan(unstrObj.Object)).Should(Equal("standard"))
381-
Eventually(test.GetObject(scontext, &comp)).ShouldNot(BeNil())
382-
Eventually(test.GetState(scontext, &comp)).Should(Equal(OnlineStatus))
383-
test.DeleteInNs(scontext, &comp, false)
384-
Eventually(test.GetObject(scontext, &comp)).Should(BeNil())
375+
Eventually(test.GetObject(testContext, &comp)).ShouldNot(BeNil())
376+
Eventually(test.GetState(testContext, &comp)).Should(Equal(OnlineStatus))
377+
test.DeleteInNs(testContext, &comp, false)
378+
Eventually(test.GetObject(testContext, &comp)).Should(BeNil())
385379
})
386380
})
387381

0 commit comments

Comments
 (0)