Skip to content

Commit 3e79f09

Browse files
author
Mikalai Radchuk
committed
Updates unit tests for syncResolvingNamespace
Tests now include handling of unpacking errors Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 67aebb0 commit 3e79f09

File tree

2 files changed

+223
-57
lines changed

2 files changed

+223
-57
lines changed

Diff for: pkg/controller/operators/catalog/operator_test.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import (
4949
"github.com/operator-framework/api/pkg/operators/v1alpha1"
5050
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
5151
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/informers/externalversions"
52+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/bundle"
53+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/bundle/bundlefakes"
5254
olmerrors "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/errors"
5355
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
5456
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/grpc"
@@ -1560,17 +1562,18 @@ func fakeConfigMapData() map[string]string {
15601562

15611563
// fakeOperatorConfig is the configuration for a fake operator.
15621564
type fakeOperatorConfig struct {
1563-
clock utilclock.Clock
1564-
clientObjs []runtime.Object
1565-
k8sObjs []runtime.Object
1566-
extObjs []runtime.Object
1567-
regObjs []runtime.Object
1568-
clientOptions []clientfake.Option
1569-
logger *logrus.Logger
1570-
resolver resolver.StepResolver
1571-
recorder record.EventRecorder
1572-
reconciler reconciler.RegistryReconcilerFactory
1573-
sources []sourceAddress
1565+
clock utilclock.Clock
1566+
clientObjs []runtime.Object
1567+
k8sObjs []runtime.Object
1568+
extObjs []runtime.Object
1569+
regObjs []runtime.Object
1570+
clientOptions []clientfake.Option
1571+
logger *logrus.Logger
1572+
resolver resolver.StepResolver
1573+
recorder record.EventRecorder
1574+
reconciler reconciler.RegistryReconcilerFactory
1575+
bundleUnpacker bundle.Unpacker
1576+
sources []sourceAddress
15741577
}
15751578

15761579
// fakeOperatorOption applies an option to the given fake operator configuration.
@@ -1582,6 +1585,12 @@ func withResolver(res resolver.StepResolver) fakeOperatorOption {
15821585
}
15831586
}
15841587

1588+
func withBundleUnpacker(bundleUnpacker bundle.Unpacker) fakeOperatorOption {
1589+
return func(config *fakeOperatorConfig) {
1590+
config.bundleUnpacker = bundleUnpacker
1591+
}
1592+
}
1593+
15851594
func withSources(sources ...sourceAddress) fakeOperatorOption {
15861595
return func(config *fakeOperatorConfig) {
15871596
config.sources = sources
@@ -1627,10 +1636,11 @@ type sourceAddress struct {
16271636
func NewFakeOperator(ctx context.Context, namespace string, namespaces []string, fakeOptions ...fakeOperatorOption) (*Operator, error) {
16281637
// Apply options to default config
16291638
config := &fakeOperatorConfig{
1630-
logger: logrus.StandardLogger(),
1631-
clock: utilclock.RealClock{},
1632-
resolver: &fakes.FakeStepResolver{},
1633-
recorder: &record.FakeRecorder{},
1639+
logger: logrus.StandardLogger(),
1640+
clock: utilclock.RealClock{},
1641+
resolver: &fakes.FakeStepResolver{},
1642+
recorder: &record.FakeRecorder{},
1643+
bundleUnpacker: &bundlefakes.FakeUnpacker{},
16341644
}
16351645
for _, option := range fakeOptions {
16361646
option(config)
@@ -1722,6 +1732,7 @@ func NewFakeOperator(ctx context.Context, namespace string, namespaces []string,
17221732
recorder: config.recorder,
17231733
clientAttenuator: scoped.NewClientAttenuator(logger, &rest.Config{}, opClientFake),
17241734
serviceAccountQuerier: scoped.NewUserDefinedServiceAccountQuerier(logger, clientFake),
1735+
bundleUnpacker: config.bundleUnpacker,
17251736
catsrcQueueSet: queueinformer.NewEmptyResourceQueueSet(),
17261737
clientFactory: &stubClientFactory{
17271738
operatorClient: opClientFake,

Diff for: pkg/controller/operators/catalog/subscriptions_test.go

+197-42
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package catalog
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"testing"
78
"time"
@@ -13,6 +14,8 @@ import (
1314
utilclocktesting "k8s.io/utils/clock/testing"
1415

1516
"github.com/operator-framework/api/pkg/operators/v1alpha1"
17+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/bundle"
18+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/bundle/bundlefakes"
1619
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/reconciler"
1720
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver"
1821
"github.com/operator-framework/operator-lifecycle-manager/pkg/fakes"
@@ -29,6 +32,7 @@ func TestSyncSubscriptions(t *testing.T) {
2932
resolveSteps []*v1alpha1.Step
3033
resolveSubs []*v1alpha1.Subscription
3134
resolveBundleLookups []v1alpha1.BundleLookup
35+
unpackBundleErr error
3236
existingOLMObjs []runtime.Object
3337
}
3438
type args struct {
@@ -385,11 +389,10 @@ func TestSyncSubscriptions(t *testing.T) {
385389
},
386390
Conditions: []v1alpha1.BundleLookupCondition{
387391
{
388-
Type: v1alpha1.BundleLookupPending,
389-
Status: corev1.ConditionTrue,
390-
Reason: "JobIncomplete",
391-
Message: "unpack job not completed",
392-
LastTransitionTime: &now,
392+
Type: v1alpha1.BundleLookupPending,
393+
Status: corev1.ConditionTrue,
394+
Reason: "JobIncomplete",
395+
Message: "unpack job not completed",
393396
},
394397
},
395398
},
@@ -430,56 +433,203 @@ func TestSyncSubscriptions(t *testing.T) {
430433
CatalogSourceNamespace: testNamespace,
431434
},
432435
Status: v1alpha1.SubscriptionStatus{
433-
CurrentCSV: "",
434-
State: v1alpha1.SubscriptionStateUpgradePending,
435-
Install: &v1alpha1.InstallPlanReference{
436-
Kind: v1alpha1.InstallPlanKind,
437-
APIVersion: v1alpha1.InstallPlanAPIVersion,
436+
CurrentCSV: "",
437+
State: "",
438+
LastUpdated: now,
439+
Conditions: []v1alpha1.SubscriptionCondition{
440+
{
441+
Type: v1alpha1.SubscriptionBundleUnpacking,
442+
Status: corev1.ConditionTrue,
443+
Reason: "UnpackingInProgress",
444+
},
438445
},
439-
InstallPlanRef: &corev1.ObjectReference{
440-
Namespace: testNamespace,
441-
Kind: v1alpha1.InstallPlanKind,
442-
APIVersion: v1alpha1.InstallPlanAPIVersion,
446+
},
447+
},
448+
},
449+
},
450+
{
451+
name: "NoStatus/NoCurrentCSV/BundleUnpackFailed",
452+
fields: fields{
453+
clientOptions: []clientfake.Option{clientfake.WithSelfLinks(t)},
454+
existingOLMObjs: []runtime.Object{
455+
&v1alpha1.Subscription{
456+
TypeMeta: metav1.TypeMeta{
457+
Kind: v1alpha1.SubscriptionKind,
458+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
459+
},
460+
ObjectMeta: metav1.ObjectMeta{
461+
Name: "sub",
462+
Namespace: testNamespace,
463+
},
464+
Spec: &v1alpha1.SubscriptionSpec{
465+
CatalogSource: "src",
466+
CatalogSourceNamespace: testNamespace,
467+
},
468+
Status: v1alpha1.SubscriptionStatus{
469+
CurrentCSV: "",
470+
State: "",
471+
},
472+
},
473+
},
474+
resolveSubs: []*v1alpha1.Subscription{
475+
{
476+
TypeMeta: metav1.TypeMeta{
477+
Kind: v1alpha1.SubscriptionKind,
478+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
479+
},
480+
ObjectMeta: metav1.ObjectMeta{
481+
Name: "sub",
482+
Namespace: testNamespace,
483+
},
484+
Spec: &v1alpha1.SubscriptionSpec{
485+
CatalogSource: "src",
486+
CatalogSourceNamespace: testNamespace,
487+
},
488+
Status: v1alpha1.SubscriptionStatus{
489+
CurrentCSV: "",
490+
State: v1alpha1.SubscriptionStateUpgradePending,
491+
},
492+
},
493+
},
494+
resolveBundleLookups: []v1alpha1.BundleLookup{
495+
{
496+
Path: "bundle-path-a",
497+
Identifier: "bundle-a",
498+
CatalogSourceRef: &corev1.ObjectReference{
499+
Namespace: testNamespace,
500+
Name: "src",
501+
},
502+
Conditions: []v1alpha1.BundleLookupCondition{
503+
{
504+
Type: v1alpha1.BundleLookupFailed,
505+
Status: corev1.ConditionTrue,
506+
Reason: "JobFailed",
507+
Message: "unpack job failed",
508+
LastTransitionTime: &now,
509+
},
443510
},
444-
InstallPlanGeneration: 1,
445-
LastUpdated: now,
446511
},
447512
},
448513
},
449-
wantInstallPlans: []v1alpha1.InstallPlan{
514+
args: args{
515+
obj: &v1alpha1.Subscription{
516+
TypeMeta: metav1.TypeMeta{
517+
Kind: v1alpha1.SubscriptionKind,
518+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
519+
},
520+
ObjectMeta: metav1.ObjectMeta{
521+
Name: "sub",
522+
Namespace: testNamespace,
523+
},
524+
Spec: &v1alpha1.SubscriptionSpec{
525+
CatalogSource: "src",
526+
CatalogSourceNamespace: testNamespace,
527+
},
528+
Status: v1alpha1.SubscriptionStatus{
529+
CurrentCSV: "",
530+
State: "",
531+
},
532+
},
533+
},
534+
wantSubscriptions: []*v1alpha1.Subscription{
450535
{
451-
Spec: v1alpha1.InstallPlanSpec{
452-
ClusterServiceVersionNames: []string{"bundle-a"},
453-
Approval: v1alpha1.ApprovalAutomatic,
454-
Approved: true,
455-
Generation: 1,
536+
TypeMeta: metav1.TypeMeta{
537+
Kind: v1alpha1.SubscriptionKind,
538+
APIVersion: v1alpha1.SubscriptionCRDAPIVersion,
456539
},
457-
Status: v1alpha1.InstallPlanStatus{
458-
Phase: v1alpha1.InstallPlanPhaseInstalling,
459-
CatalogSources: []string{},
460-
BundleLookups: []v1alpha1.BundleLookup{
540+
ObjectMeta: metav1.ObjectMeta{
541+
Name: "sub",
542+
Namespace: testNamespace,
543+
},
544+
Spec: &v1alpha1.SubscriptionSpec{
545+
CatalogSource: "src",
546+
CatalogSourceNamespace: testNamespace,
547+
},
548+
Status: v1alpha1.SubscriptionStatus{
549+
CurrentCSV: "",
550+
State: "",
551+
LastUpdated: now,
552+
Conditions: []v1alpha1.SubscriptionCondition{
461553
{
462-
Path: "bundle-path-a",
463-
Identifier: "bundle-a",
464-
CatalogSourceRef: &corev1.ObjectReference{
465-
Namespace: testNamespace,
466-
Name: "src",
467-
},
468-
Conditions: []v1alpha1.BundleLookupCondition{
469-
{
470-
Type: v1alpha1.BundleLookupPending,
471-
Status: corev1.ConditionTrue,
472-
Reason: "JobIncomplete",
473-
Message: "unpack job not completed",
474-
LastTransitionTime: &now,
475-
},
476-
},
554+
Type: v1alpha1.SubscriptionBundleUnpackFailed,
555+
Reason: "BundleUnpackFailed",
556+
Message: "bundle unpacking failed. Reason: JobFailed, and Message: unpack job failed",
557+
Status: corev1.ConditionTrue,
477558
},
478559
},
479560
},
480561
},
481562
},
482563
},
564+
{
565+
name: "NoStatus/NoCurrentCSV/BundleLookupError",
566+
fields: fields{
567+
clientOptions: []clientfake.Option{clientfake.WithSelfLinks(t)},
568+
existingOLMObjs: []runtime.Object{
569+
&v1alpha1.Subscription{
570+
TypeMeta: metav1.TypeMeta{
571+
Kind: v1alpha1.SubscriptionKind,
572+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
573+
},
574+
ObjectMeta: metav1.ObjectMeta{
575+
Name: "sub",
576+
Namespace: testNamespace,
577+
},
578+
Spec: &v1alpha1.SubscriptionSpec{
579+
CatalogSource: "src",
580+
CatalogSourceNamespace: testNamespace,
581+
},
582+
Status: v1alpha1.SubscriptionStatus{
583+
CurrentCSV: "",
584+
State: "",
585+
},
586+
},
587+
},
588+
resolveBundleLookups: []v1alpha1.BundleLookup{{}},
589+
unpackBundleErr: errors.New("fake unpack error"),
590+
},
591+
args: args{
592+
obj: &v1alpha1.Subscription{
593+
TypeMeta: metav1.TypeMeta{
594+
Kind: v1alpha1.SubscriptionKind,
595+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
596+
},
597+
ObjectMeta: metav1.ObjectMeta{
598+
Name: "sub",
599+
Namespace: testNamespace,
600+
},
601+
Spec: &v1alpha1.SubscriptionSpec{
602+
CatalogSource: "src",
603+
CatalogSourceNamespace: testNamespace,
604+
},
605+
Status: v1alpha1.SubscriptionStatus{
606+
CurrentCSV: "",
607+
State: "",
608+
},
609+
},
610+
},
611+
wantSubscriptions: []*v1alpha1.Subscription{
612+
{
613+
TypeMeta: metav1.TypeMeta{
614+
Kind: v1alpha1.SubscriptionKind,
615+
APIVersion: v1alpha1.SubscriptionCRDAPIVersion,
616+
},
617+
ObjectMeta: metav1.ObjectMeta{
618+
Name: "sub",
619+
Namespace: testNamespace,
620+
},
621+
Spec: &v1alpha1.SubscriptionSpec{
622+
CatalogSource: "src",
623+
CatalogSourceNamespace: testNamespace,
624+
},
625+
Status: v1alpha1.SubscriptionStatus{
626+
CurrentCSV: "",
627+
State: "",
628+
},
629+
},
630+
},
631+
wantErr: errors.New("bundle unpacking failed with an error: fake unpack error"),
632+
},
483633
{
484634
name: "Status/HaveCurrentCSV/UpdateFoundInCatalog",
485635
fields: fields{
@@ -1013,7 +1163,12 @@ func TestSyncSubscriptions(t *testing.T) {
10131163
ctx, cancel := context.WithCancel(context.TODO())
10141164
defer cancel()
10151165

1016-
o, err := NewFakeOperator(ctx, testNamespace, []string{testNamespace}, withClock(clockFake), withClientObjs(tt.fields.existingOLMObjs...), withFakeClientOptions(tt.fields.clientOptions...))
1166+
fakeBundleUnpacker := &bundlefakes.FakeUnpacker{
1167+
UnpackBundleStub: func(lookup *v1alpha1.BundleLookup, timeout time.Duration) (*bundle.BundleUnpackResult, error) {
1168+
return &bundle.BundleUnpackResult{BundleLookup: lookup.DeepCopy()}, tt.fields.unpackBundleErr
1169+
},
1170+
}
1171+
o, err := NewFakeOperator(ctx, testNamespace, []string{testNamespace}, withClock(clockFake), withClientObjs(tt.fields.existingOLMObjs...), withFakeClientOptions(tt.fields.clientOptions...), withBundleUnpacker(fakeBundleUnpacker))
10171172
require.NoError(t, err)
10181173

10191174
o.reconciler = &fakes.FakeRegistryReconcilerFactory{

0 commit comments

Comments
 (0)