Skip to content

Commit 4d235ee

Browse files
Merge pull request opendatahub-io#86 from HumairAK/clean_tests_2
Testsuite cleanup
2 parents 359a1e1 + 9f18dad commit 4d235ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1117
-397
lines changed

.golangci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ linters:
1010
- typecheck
1111
- unused
1212
- revive
13+
linters-settings:
14+
revive:
15+
rules:
16+
- name: dot-imports
17+
severity: warning
18+
disabled: true

controllers/dspipeline_controller_test.go

Lines changed: 35 additions & 273 deletions
Original file line numberDiff line numberDiff line change
@@ -21,299 +21,61 @@ import (
2121
mf "github.com/manifestival/manifestival"
2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
24-
dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
25-
"github.com/opendatahub-io/data-science-pipelines-operator/controllers/testutil"
24+
util "github.com/opendatahub-io/data-science-pipelines-operator/controllers/testutil"
2625
"github.com/spf13/viper"
27-
appsv1 "k8s.io/api/apps/v1"
28-
v1 "k8s.io/api/core/v1"
29-
apierrs "k8s.io/apimachinery/pkg/api/errors"
30-
"k8s.io/apimachinery/pkg/types"
3126
)
3227

33-
type TestCase struct {
34-
Description string
35-
Path string
36-
AdditionalResources map[string][]string
37-
}
38-
39-
type CaseComponentResources map[string]ResourcePath
40-
type ResourcePath map[string]string
41-
42-
const SecretKind = "Secret"
43-
44-
var cases = map[string]TestCase{
45-
"case_0": {
46-
Description: "empty CR Spec",
47-
Path: "./testdata/deploy/case_0/cr.yaml",
48-
},
49-
"case_1": {
50-
Description: "all Deploy fields are set to false",
51-
Path: "./testdata/deploy/case_1/cr.yaml",
52-
},
53-
"case_2": {
54-
Description: "standard CR Spec with components specified",
55-
Path: "./testdata/deploy/case_2/cr.yaml",
56-
},
57-
"case_3": {
58-
Description: "custom Artifact configmap is provided, custom images override defaults, no sample pipeline",
59-
Path: "./testdata/deploy/case_3/cr.yaml",
60-
AdditionalResources: map[string][]string{
61-
SecretKind: {
62-
"./testdata/deploy/case_3/secret1.yaml",
63-
"./testdata/deploy/case_3/secret2.yaml",
64-
},
65-
},
66-
},
67-
}
68-
var deploymentsCreated = CaseComponentResources{
69-
"case_0": {
70-
"apiserver": "./testdata/results/case_0/apiserver/deployment.yaml",
71-
"mariadb": "./testdata/results/case_0/mariadb/deployment.yaml",
72-
"persistenceAgentDeployment": "./testdata/results/case_0/persistence-agent/deployment.yaml",
73-
"scheduledWorkflowDeployment": "./testdata/results/case_0/scheduled-workflow/deployment.yaml",
74-
},
75-
"case_2": {
76-
"apiserver": "./testdata/results/case_2/apiserver/deployment.yaml",
77-
"mariadb": "./testdata/results/case_2/mariadb/deployment.yaml",
78-
"minioDeployment": "./testdata/results/case_2/minio/deployment.yaml",
79-
"mlpipelinesUIDeployment": "./testdata/results/case_2/mlpipelines-ui/deployment.yaml",
80-
"persistenceAgentDeployment": "./testdata/results/case_2/persistence-agent/deployment.yaml",
81-
"scheduledWorkflowDeployment": "./testdata/results/case_2/scheduled-workflow/deployment.yaml",
82-
"viewerCrdDeployment": "./testdata/results/case_2/viewer-crd/deployment.yaml",
83-
},
84-
"case_3": {
85-
"apiserver": "./testdata/results/case_3/apiserver/deployment.yaml",
86-
},
87-
}
88-
89-
var deploymentsNotCreated = CaseComponentResources{
90-
"case_0": {
91-
"viewerCrdDeployment": "./testdata/results/case_0/viewer-crd/deployment.yaml",
92-
},
93-
}
94-
95-
var configMapsCreated = CaseComponentResources{
96-
"case_0": {
97-
"apiserver": "./testdata/results/case_0/apiserver/configmap_artifact_script.yaml",
98-
},
99-
"case_2": {
100-
"apiserver": "./testdata/results/case_2/apiserver/configmap_artifact_script.yaml",
101-
},
102-
}
103-
104-
var secretsCreated = CaseComponentResources{
105-
"case_3": {
106-
"database": "./testdata/results/case_3/database/secret.yaml",
107-
"storage": "./testdata/results/case_3/storage/secret.yaml",
108-
},
109-
}
110-
111-
var configMapsNotCreated = CaseComponentResources{
112-
"case_3": {
113-
"apiserver": "./testdata/results/case_3/apiserver/configmap_artifact_script.yaml",
114-
"apiserver-sampleconfig": "./testdata/results/case_3/apiserver/sample-config.yaml",
115-
"apiserver-samplepipeline": "./testdata/results/case_3/apiserver/sample-pipeline.yaml",
116-
},
117-
}
118-
119-
func deployDSP(path string, opts mf.Option) {
120-
dsp := &dspav1alpha1.DataSciencePipelinesApplication{}
121-
err := convertToStructuredResource(path, dsp, opts)
122-
Expect(err).NotTo(HaveOccurred())
123-
Expect(k8sClient.Create(ctx, dsp)).Should(Succeed())
124-
125-
dsp2 := &dspav1alpha1.DataSciencePipelinesApplication{}
126-
Eventually(func() error {
127-
namespacedNamed := types.NamespacedName{Name: dsp.Name, Namespace: WorkingNamespace}
128-
return k8sClient.Get(ctx, namespacedNamed, dsp2)
129-
}, timeout, interval).ShouldNot(HaveOccurred())
130-
}
131-
132-
func deploySecret(path string, opts mf.Option) {
133-
secret := &v1.Secret{}
134-
err := convertToStructuredResource(path, secret, opts)
135-
Expect(err).NotTo(HaveOccurred())
136-
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
137-
138-
secret2 := &v1.Secret{}
139-
Eventually(func() error {
140-
namespacedNamed := types.NamespacedName{Name: secret.Name, Namespace: WorkingNamespace}
141-
return k8sClient.Get(ctx, namespacedNamed, secret2)
142-
}, timeout, interval).ShouldNot(HaveOccurred())
143-
}
144-
145-
func deleteDSP(path string, opts mf.Option) {
146-
dsp := &dspav1alpha1.DataSciencePipelinesApplication{}
147-
err := convertToStructuredResource(path, dsp, opts)
148-
Expect(err).NotTo(HaveOccurred())
149-
150-
Eventually(func() error {
151-
return k8sClient.Delete(ctx, dsp)
152-
}, timeout, interval).ShouldNot(HaveOccurred())
153-
154-
dsp2 := &dspav1alpha1.DataSciencePipelinesApplication{}
155-
Eventually(func() error {
156-
namespacedNamed := types.NamespacedName{Name: dsp.Name, Namespace: WorkingNamespace}
157-
err = k8sClient.Get(ctx, namespacedNamed, dsp2)
158-
if err != nil {
159-
if apierrs.IsNotFound(err) {
160-
return nil
161-
}
162-
return err
28+
var _ = Describe("The DS Pipeline Controller", Ordered, func() {
29+
uc := util.UtilContext{}
30+
BeforeAll(func() {
31+
client := mfc.NewClient(k8sClient)
32+
opts := mf.UseClient(client)
33+
uc = util.UtilContext{
34+
Ctx: ctx,
35+
Ns: WorkingNamespace,
36+
Opts: opts,
37+
Client: k8sClient,
16338
}
164-
return fmt.Errorf("resource still exists on cluster")
165-
166-
}, timeout, interval).ShouldNot(HaveOccurred())
167-
168-
}
169-
170-
func compareDeployments(path string, opts mf.Option) {
171-
expectedDeployment := &appsv1.Deployment{}
172-
Expect(convertToStructuredResource(path, expectedDeployment, opts)).NotTo(HaveOccurred())
173-
174-
actualDeployment := &appsv1.Deployment{}
175-
Eventually(func() error {
176-
namespacedNamed := types.NamespacedName{Name: expectedDeployment.Name, Namespace: WorkingNamespace}
177-
return k8sClient.Get(ctx, namespacedNamed, actualDeployment)
178-
}, timeout, interval).ShouldNot(HaveOccurred())
179-
180-
Expect(testutil.DeploymentsAreEqual(*expectedDeployment, *actualDeployment)).Should(BeTrue())
181-
182-
}
183-
184-
func compareConfigMaps(path string, opts mf.Option) {
185-
expectedConfigMap := &v1.ConfigMap{}
186-
Expect(convertToStructuredResource(path, expectedConfigMap, opts)).NotTo(HaveOccurred())
187-
188-
actualConfigMap := &v1.ConfigMap{}
189-
Eventually(func() error {
190-
namespacedNamed := types.NamespacedName{Name: expectedConfigMap.Name, Namespace: WorkingNamespace}
191-
return k8sClient.Get(ctx, namespacedNamed, actualConfigMap)
192-
}, timeout, interval).ShouldNot(HaveOccurred())
193-
194-
Expect(testutil.ConfigMapsAreEqual(*expectedConfigMap, *actualConfigMap)).Should(BeTrue())
195-
196-
}
197-
198-
func compareSecrets(path string, opts mf.Option) {
199-
expectedSecret := &v1.Secret{}
200-
Expect(convertToStructuredResource(path, expectedSecret, opts)).NotTo(HaveOccurred())
39+
})
20140

202-
actualSecret := &v1.Secret{}
203-
Eventually(func() error {
204-
namespacedNamed := types.NamespacedName{Name: expectedSecret.Name, Namespace: WorkingNamespace}
205-
return k8sClient.Get(ctx, namespacedNamed, actualSecret)
206-
}, timeout, interval).ShouldNot(HaveOccurred())
41+
testcases := util.GenerateDeclarativeTestCases()
20742

208-
Expect(testutil.SecretsAreEqual(*expectedSecret, *actualSecret)).Should(BeTrue())
209-
210-
}
211-
212-
func deploymentDoesNotExists(path string, opts mf.Option) {
213-
expectedDeployment := &appsv1.Deployment{}
214-
actualDeployment := &appsv1.Deployment{}
215-
Expect(convertToStructuredResource(path, expectedDeployment, opts)).NotTo(HaveOccurred())
216-
217-
namespacedNamed := types.NamespacedName{
218-
Name: expectedDeployment.Name,
219-
Namespace: WorkingNamespace,
220-
}
221-
222-
Eventually(func() error {
223-
return k8sClient.Get(ctx, namespacedNamed, actualDeployment)
224-
}, timeout, interval).Should(HaveOccurred())
225-
226-
Expect(actualDeployment).To(Equal(&appsv1.Deployment{}))
227-
228-
}
229-
230-
func configMapDoesNotExists(path string, opts mf.Option) {
231-
expectedConfigMap := &v1.ConfigMap{}
232-
actualConfigMap := &v1.ConfigMap{}
233-
Expect(convertToStructuredResource(path, expectedConfigMap, opts)).NotTo(HaveOccurred())
234-
235-
namespacedNamed := types.NamespacedName{
236-
Name: expectedConfigMap.Name,
237-
Namespace: WorkingNamespace,
238-
}
239-
240-
Eventually(func() error {
241-
return k8sClient.Get(ctx, namespacedNamed, actualConfigMap)
242-
}, timeout, interval).Should(HaveOccurred())
243-
244-
Expect(actualConfigMap).To(Equal(&v1.ConfigMap{}))
245-
246-
}
247-
248-
var _ = Describe("The DS Pipeline Controller", Ordered, func() {
249-
client := mfc.NewClient(k8sClient)
250-
opts := mf.UseClient(client)
251-
252-
for tc := range cases {
43+
for caseCount, tc := range testcases {
25344
// We assign local copies of all looping variables, as they are mutating
25445
// we want the correct variables captured in each `It` closure, we do this
25546
// by creating local variables
25647
// https://onsi.github.io/ginkgo/#dynamically-generating-specs
25748
testcase := tc
258-
description := cases[testcase].Description
259-
dspPath := cases[testcase].Path
260-
49+
description := testcase.Description
26150
Context(description, func() {
262-
It(fmt.Sprintf("Should successfully deploy the Custom Resource for case %s", testcase), func() {
51+
paths := testcase.Deploy
52+
It(fmt.Sprintf("[case %x] Should successfully deploy the Custom Resource (and additional resources)", caseCount), func() {
26353
viper.New()
264-
viper.SetConfigFile(fmt.Sprintf("testdata/deploy/%s/config.yaml", testcase))
54+
viper.SetConfigFile(testcase.Config)
26555
err := viper.ReadInConfig()
26656
Expect(err).ToNot(HaveOccurred(), "Failed to read config file")
267-
deployDSP(dspPath, opts)
268-
// Deploy any additional resources for this test case
269-
if cases[testcase].AdditionalResources != nil {
270-
for res, paths := range cases[testcase].AdditionalResources {
271-
if res == SecretKind {
272-
for _, p := range paths {
273-
deploySecret(p, opts)
274-
}
275-
}
276-
}
57+
for _, path := range paths {
58+
util.DeployResource(uc, path)
27759
}
27860
})
27961

280-
expectedDeployments := deploymentsCreated[testcase]
281-
for component := range expectedDeployments {
282-
component := component
283-
deploymentPath := expectedDeployments[component]
284-
It(fmt.Sprintf("[%s] Should create deployment for component %s", testcase, component), func() {
285-
compareDeployments(deploymentPath, opts)
286-
})
287-
}
288-
289-
notExpectedDeployments := deploymentsNotCreated[testcase]
290-
for component := range deploymentsNotCreated[testcase] {
291-
deploymentPath := notExpectedDeployments[component]
292-
It(fmt.Sprintf("[%s] Should NOT create deployments for component %s", testcase, component), func() {
293-
deploymentDoesNotExists(deploymentPath, opts)
294-
})
295-
}
296-
297-
for component := range configMapsCreated[testcase] {
298-
It(fmt.Sprintf("[%s] Should create configmaps for component %s", testcase, component), func() {
299-
compareConfigMaps(configMapsCreated[testcase][component], opts)
300-
})
301-
}
302-
303-
for component := range secretsCreated[testcase] {
304-
It(fmt.Sprintf("[%s] Should create secrets for component %s", testcase, component), func() {
305-
compareSecrets(secretsCreated[testcase][component], opts)
306-
})
307-
}
62+
It(fmt.Sprintf("[case %x] Should create expected resources", caseCount), func() {
63+
for _, resourcesCreated := range testcase.Expected.Created {
64+
util.CompareResources(uc, resourcesCreated)
65+
}
66+
})
30867

309-
for component := range configMapsNotCreated[testcase] {
310-
It(fmt.Sprintf("[%s] Should NOT create configmaps for component %s", testcase, component), func() {
311-
configMapDoesNotExists(configMapsNotCreated[testcase][component], opts)
312-
})
313-
}
68+
It(fmt.Sprintf("[case %x] Should expect NOT to create some resources", caseCount), func() {
69+
for _, resourcesNotCreated := range testcase.Expected.NotCreated {
70+
util.ResourceDoesNotExists(uc, resourcesNotCreated)
71+
}
72+
})
31473

315-
It(fmt.Sprintf("Should successfully delete the Custom Resource for case %s", testcase), func() {
316-
deleteDSP(dspPath, opts)
74+
It(fmt.Sprintf("[case %x] Should successfully delete the Custom Resource (and additional resources)", testcase), func() {
75+
for _, path := range testcase.Deploy {
76+
p := path
77+
util.DeleteResource(uc, p)
78+
}
31779
})
31880
})
31981
}

controllers/suite_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package controllers
1818

1919
import (
2020
"context"
21-
"github.com/manifestival/manifestival"
22-
mf "github.com/manifestival/manifestival"
2321
buildv1 "github.com/openshift/api/build/v1"
2422
imagev1 "github.com/openshift/api/image/v1"
2523
routev1 "github.com/openshift/api/route/v1"
@@ -139,22 +137,3 @@ var _ = AfterSuite(func() {
139137
err := testEnv.Stop()
140138
Expect(err).NotTo(HaveOccurred())
141139
})
142-
143-
func convertToStructuredResource(path string, out interface{}, opts manifestival.Option) error {
144-
m, err := mf.ManifestFrom(mf.Recursive(path), opts)
145-
if err != nil {
146-
return err
147-
}
148-
m, err = m.Transform(mf.InjectNamespace(WorkingNamespace))
149-
if err != nil {
150-
return err
151-
}
152-
if err != nil {
153-
return err
154-
}
155-
err = scheme.Scheme.Convert(&m.Resources()[0], out, nil)
156-
if err != nil {
157-
return err
158-
}
159-
return nil
160-
}

0 commit comments

Comments
 (0)