Skip to content

Commit a52214c

Browse files
committed
Modify and improve unit test on deployment
Signed-off-by: Vu Dinh <[email protected]>
1 parent cd6bde3 commit a52214c

File tree

5 files changed

+101
-27
lines changed

5 files changed

+101
-27
lines changed

pkg/controller/install/deployment.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,15 @@ func (i *StrategyDeploymentInstaller) checkForDeployments(deploymentSpecs []Stra
139139
depNames = append(depNames, dep.Name)
140140
}
141141

142-
existingDeployments, err := i.strategyClient.FindAnyDeploymentsMatchingNames(depNames)
142+
// Check the owner is a CSV
143+
csv, ok := i.owner.(*v1alpha1.ClusterServiceVersion)
144+
if !ok {
145+
return StrategyError{Reason: StrategyErrReasonComponentMissing, Message: fmt.Sprintf("owner %s is not a CSV", i.owner.GetName())}
146+
}
147+
148+
existingDeployments, err := i.strategyClient.FindAnyDeploymentsMatchingLabels(ownerutil.CSVOwnerSelector(csv))
143149
if err != nil {
144-
return StrategyError{Reason: StrategyErrReasonComponentMissing, Message: fmt.Sprintf("error querying for %s: %s", depNames, err)}
150+
return StrategyError{Reason: StrategyErrReasonComponentMissing, Message: fmt.Sprintf("error querying existing deployments for CSV %s: %s", csv.GetName(), err)}
145151
}
146152

147153
// compare deployments to see if any need to be created/updated

pkg/controller/install/deployment_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ func TestInstallStrategyDeploymentCheckInstallErrors(t *testing.T) {
286286
},
287287
}
288288

289+
mockOwnerLabel := ownerutil.CSVOwnerSelector(&mockOwner)
290+
289291
tests := []struct {
290292
createDeploymentErr error
291293
description string
@@ -304,13 +306,13 @@ func TestInstallStrategyDeploymentCheckInstallErrors(t *testing.T) {
304306

305307
dep := testDeployment("olm-dep-1", namespace, &mockOwner)
306308
dep.Spec.Template.SetAnnotations(map[string]string{"test": "annotation"})
307-
fakeClient.FindAnyDeploymentsMatchingNamesReturns(
309+
fakeClient.FindAnyDeploymentsMatchingLabelsReturns(
308310
[]*appsv1.Deployment{
309311
&dep,
310312
}, nil,
311313
)
312314
defer func() {
313-
require.Equal(t, []string{dep.Name}, fakeClient.FindAnyDeploymentsMatchingNamesArgsForCall(0))
315+
require.Equal(t, mockOwnerLabel, fakeClient.FindAnyDeploymentsMatchingLabelsArgsForCall(0))
314316
}()
315317

316318
installed, err := installer.CheckInstalled(strategy)

pkg/controller/operators/catalog/operator_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ func TestExecutePlan(t *testing.T) {
204204
}
205205

206206
require.NoError(t, err, "couldn't fetch %s %v", namespace, obj)
207-
fmt.Printf("fetched: %v", fetched)
208207
require.EqualValues(t, obj, fetched)
209208
}
210209
})

pkg/controller/operators/olm/operator_test.go

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,10 @@ func TestTransitionCSV(t *testing.T) {
10811081
crd("c1", "v1", "g1"),
10821082
},
10831083
objs: []runtime.Object{
1084-
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
1084+
withLabels(
1085+
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
1086+
ownerLabelFromCSV("csv1", namespace),
1087+
),
10851088
},
10861089
},
10871090
expected: expected{
@@ -1122,9 +1125,12 @@ func TestTransitionCSV(t *testing.T) {
11221125
clientObjs: []runtime.Object{addAnnotation(defaultOperatorGroup, v1.OperatorGroupProvidedAPIsAnnotationKey, "a1Kind.v1.a1")},
11231126
apis: []runtime.Object{apiService("a1", "v1", "v1-a1", namespace, "", validCAPEM, apiregistrationv1.ConditionTrue, ownerLabelFromCSV("csv1", namespace))},
11241127
objs: []runtime.Object{
1125-
deployment("a1", namespace, "sa", addAnnotations(defaultTemplateAnnotations, map[string]string{
1126-
OLMCAHashAnnotationKey: validCAHash,
1127-
})),
1128+
withLabels(
1129+
deployment("a1", namespace, "sa", addAnnotations(defaultTemplateAnnotations, map[string]string{
1130+
OLMCAHashAnnotationKey: validCAHash,
1131+
})),
1132+
ownerLabelFromCSV("csv1", namespace),
1133+
),
11281134
withAnnotations(keyPairToTLSSecret("v1.a1-cert", namespace, signedServingPair(time.Now().Add(24*time.Hour), validCA, []string{"v1-a1.ns", "v1-a1.ns.svc"})), map[string]string{
11291135
OLMCAHashAnnotationKey: validCAHash,
11301136
}),
@@ -2195,7 +2201,10 @@ func TestTransitionCSV(t *testing.T) {
21952201
crd("c1", "v1", "g1"),
21962202
},
21972203
objs: []runtime.Object{
2198-
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2204+
withLabels(
2205+
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2206+
ownerLabelFromCSV("csv1", namespace),
2207+
),
21992208
deployment("extra-dep", namespace, "sa", nil),
22002209
},
22012210
},
@@ -2335,8 +2344,14 @@ func TestTransitionCSV(t *testing.T) {
23352344
crd("c1", "v1", "g1"),
23362345
},
23372346
objs: []runtime.Object{
2338-
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2339-
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2347+
withLabels(
2348+
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2349+
ownerLabelFromCSV("csv1", namespace),
2350+
),
2351+
withLabels(
2352+
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2353+
ownerLabelFromCSV("csv2", namespace),
2354+
),
23402355
},
23412356
},
23422357
expected: expected{
@@ -2374,8 +2389,14 @@ func TestTransitionCSV(t *testing.T) {
23742389
crd("c1", "v1", "g1"),
23752390
},
23762391
objs: []runtime.Object{
2377-
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2378-
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2392+
withLabels(
2393+
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2394+
ownerLabelFromCSV("csv1", namespace),
2395+
),
2396+
withLabels(
2397+
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2398+
ownerLabelFromCSV("csv2", namespace),
2399+
),
23792400
},
23802401
},
23812402
expected: expected{
@@ -2429,9 +2450,18 @@ func TestTransitionCSV(t *testing.T) {
24292450
crd("c1", "v1", "g1"),
24302451
},
24312452
objs: []runtime.Object{
2432-
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2433-
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2434-
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2453+
withLabels(
2454+
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2455+
ownerLabelFromCSV("csv1", namespace),
2456+
),
2457+
withLabels(
2458+
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2459+
ownerLabelFromCSV("csv2", namespace),
2460+
),
2461+
withLabels(
2462+
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2463+
ownerLabelFromCSV("csv3", namespace),
2464+
),
24352465
},
24362466
},
24372467
expected: expected{
@@ -2479,9 +2509,18 @@ func TestTransitionCSV(t *testing.T) {
24792509
crd("c1", "v1", "g1"),
24802510
},
24812511
objs: []runtime.Object{
2482-
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2483-
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2484-
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2512+
withLabels(
2513+
deployment("csv1-dep1", namespace, "sa", defaultTemplateAnnotations),
2514+
ownerLabelFromCSV("csv1", namespace),
2515+
),
2516+
withLabels(
2517+
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2518+
ownerLabelFromCSV("csv2", namespace),
2519+
),
2520+
withLabels(
2521+
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2522+
ownerLabelFromCSV("csv3", namespace),
2523+
),
24852524
},
24862525
},
24872526
expected: expected{
@@ -2520,12 +2559,19 @@ func TestTransitionCSV(t *testing.T) {
25202559
crd("c1", "v1", "g1"),
25212560
},
25222561
objs: []runtime.Object{
2523-
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2524-
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2562+
withLabels(
2563+
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2564+
ownerLabelFromCSV("csv2", namespace),
2565+
),
2566+
withLabels(
2567+
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2568+
ownerLabelFromCSV("csv3", namespace),
2569+
),
25252570
},
25262571
},
25272572
expected: expected{
25282573
csvStates: map[string]csvState{
2574+
25292575
"csv1": {exists: false, phase: v1alpha1.CSVPhaseNone},
25302576
"csv2": {exists: true, phase: v1alpha1.CSVPhaseDeleting},
25312577
"csv3": {exists: true, phase: v1alpha1.CSVPhaseSucceeded},
@@ -2560,8 +2606,14 @@ func TestTransitionCSV(t *testing.T) {
25602606
crd("c1", "v1", "g1"),
25612607
},
25622608
objs: []runtime.Object{
2563-
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2564-
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2609+
withLabels(
2610+
deployment("csv2-dep1", namespace, "sa", defaultTemplateAnnotations),
2611+
ownerLabelFromCSV("csv2", namespace),
2612+
),
2613+
withLabels(
2614+
deployment("csv3-dep1", namespace, "sa", defaultTemplateAnnotations),
2615+
ownerLabelFromCSV("csv3", namespace),
2616+
),
25652617
},
25662618
},
25672619
expected: expected{

test/e2e/csv_e2e_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,10 @@ func TestUpdateCSVModifyDeploymentName(t *testing.T) {
25902590
Name: genName("dep-"),
25912591
Spec: newNginxDeployment(genName("nginx-")),
25922592
},
2593+
{
2594+
Name: "dep2-test",
2595+
Spec: newNginxDeployment("nginx2"),
2596+
},
25932597
},
25942598
}
25952599
strategyRaw, err := json.Marshal(strategy)
@@ -2648,17 +2652,24 @@ func TestUpdateCSVModifyDeploymentName(t *testing.T) {
26482652
_, err = fetchCSV(t, crc, csv.Name, testNamespace, csvSucceededChecker)
26492653
require.NoError(t, err)
26502654

2651-
// Should have created deployment
2655+
// Should have created deployments
26522656
dep, err := c.GetDeployment(testNamespace, strategy.DeploymentSpecs[0].Name)
26532657
require.NoError(t, err)
26542658
require.NotNil(t, dep)
2659+
dep2, err := c.GetDeployment(testNamespace, strategy.DeploymentSpecs[1].Name)
2660+
require.NoError(t, err)
2661+
require.NotNil(t, dep2)
26552662

26562663
// Create "updated" CSV
26572664
strategyNew := install.StrategyDetailsDeployment{
26582665
DeploymentSpecs: []install.StrategyDeploymentSpec{
26592666
{
2660-
Name: genName("dep2-"),
2661-
Spec: newNginxDeployment(genName("nginx-")),
2667+
Name: genName("dep3-"),
2668+
Spec: newNginxDeployment(genName("nginx3-")),
2669+
},
2670+
{
2671+
Name: "dep2-test",
2672+
Spec: newNginxDeployment("nginx2"),
26622673
},
26632674
},
26642675
}
@@ -2684,6 +2695,10 @@ func TestUpdateCSVModifyDeploymentName(t *testing.T) {
26842695
depNew, err := c.GetDeployment(testNamespace, strategyNew.DeploymentSpecs[0].Name)
26852696
require.NoError(t, err)
26862697
require.NotNil(t, depNew)
2698+
// Make sure the unchanged deployment still exists
2699+
depNew2, err := c.GetDeployment(testNamespace, strategyNew.DeploymentSpecs[1].Name)
2700+
require.NoError(t, err)
2701+
require.NotNil(t, depNew2)
26872702
err = waitForDeploymentToDelete(t, c, strategy.DeploymentSpecs[0].Name)
26882703
require.NoError(t, err)
26892704
}

0 commit comments

Comments
 (0)