Skip to content

Commit 438c664

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 2ebeb27 commit 438c664

File tree

2 files changed

+210
-51
lines changed

2 files changed

+210
-51
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

+184-36
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 {
@@ -431,55 +435,194 @@ func TestSyncSubscriptions(t *testing.T) {
431435
},
432436
Status: v1alpha1.SubscriptionStatus{
433437
CurrentCSV: "",
434-
State: v1alpha1.SubscriptionStateUpgradePending,
435-
Install: &v1alpha1.InstallPlanReference{
436-
Kind: v1alpha1.InstallPlanKind,
437-
APIVersion: v1alpha1.InstallPlanAPIVersion,
438+
State: "",
439+
},
440+
},
441+
},
442+
},
443+
{
444+
name: "NoStatus/NoCurrentCSV/BundleLookupFailed",
445+
fields: fields{
446+
clientOptions: []clientfake.Option{clientfake.WithSelfLinks(t)},
447+
existingOLMObjs: []runtime.Object{
448+
&v1alpha1.Subscription{
449+
TypeMeta: metav1.TypeMeta{
450+
Kind: v1alpha1.SubscriptionKind,
451+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
438452
},
439-
InstallPlanRef: &corev1.ObjectReference{
440-
Namespace: testNamespace,
441-
Kind: v1alpha1.InstallPlanKind,
442-
APIVersion: v1alpha1.InstallPlanAPIVersion,
453+
ObjectMeta: metav1.ObjectMeta{
454+
Name: "sub",
455+
Namespace: testNamespace,
456+
},
457+
Spec: &v1alpha1.SubscriptionSpec{
458+
CatalogSource: "src",
459+
CatalogSourceNamespace: testNamespace,
460+
},
461+
Status: v1alpha1.SubscriptionStatus{
462+
CurrentCSV: "",
463+
State: "",
464+
},
465+
},
466+
},
467+
resolveSubs: []*v1alpha1.Subscription{
468+
{
469+
TypeMeta: metav1.TypeMeta{
470+
Kind: v1alpha1.SubscriptionKind,
471+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
472+
},
473+
ObjectMeta: metav1.ObjectMeta{
474+
Name: "sub",
475+
Namespace: testNamespace,
476+
},
477+
Spec: &v1alpha1.SubscriptionSpec{
478+
CatalogSource: "src",
479+
CatalogSourceNamespace: testNamespace,
480+
},
481+
Status: v1alpha1.SubscriptionStatus{
482+
CurrentCSV: "",
483+
State: v1alpha1.SubscriptionStateUpgradePending,
484+
},
485+
},
486+
},
487+
resolveBundleLookups: []v1alpha1.BundleLookup{
488+
{
489+
Path: "bundle-path-a",
490+
Identifier: "bundle-a",
491+
CatalogSourceRef: &corev1.ObjectReference{
492+
Namespace: testNamespace,
493+
Name: "src",
494+
},
495+
Conditions: []v1alpha1.BundleLookupCondition{
496+
{
497+
Type: bundle.BundleLookupFailed,
498+
Status: corev1.ConditionTrue,
499+
Reason: "JobFailed",
500+
Message: "unpack job failed",
501+
LastTransitionTime: &now,
502+
},
443503
},
444-
InstallPlanGeneration: 1,
445-
LastUpdated: now,
446504
},
447505
},
448506
},
449-
wantInstallPlans: []v1alpha1.InstallPlan{
507+
args: args{
508+
obj: &v1alpha1.Subscription{
509+
TypeMeta: metav1.TypeMeta{
510+
Kind: v1alpha1.SubscriptionKind,
511+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
512+
},
513+
ObjectMeta: metav1.ObjectMeta{
514+
Name: "sub",
515+
Namespace: testNamespace,
516+
},
517+
Spec: &v1alpha1.SubscriptionSpec{
518+
CatalogSource: "src",
519+
CatalogSourceNamespace: testNamespace,
520+
},
521+
Status: v1alpha1.SubscriptionStatus{
522+
CurrentCSV: "",
523+
State: "",
524+
},
525+
},
526+
},
527+
wantSubscriptions: []*v1alpha1.Subscription{
450528
{
451-
Spec: v1alpha1.InstallPlanSpec{
452-
ClusterServiceVersionNames: []string{"bundle-a"},
453-
Approval: v1alpha1.ApprovalAutomatic,
454-
Approved: true,
455-
Generation: 1,
529+
TypeMeta: metav1.TypeMeta{
530+
Kind: v1alpha1.SubscriptionKind,
531+
APIVersion: v1alpha1.SubscriptionCRDAPIVersion,
456532
},
457-
Status: v1alpha1.InstallPlanStatus{
458-
Phase: v1alpha1.InstallPlanPhaseInstalling,
459-
CatalogSources: []string{},
460-
BundleLookups: []v1alpha1.BundleLookup{
533+
ObjectMeta: metav1.ObjectMeta{
534+
Name: "sub",
535+
Namespace: testNamespace,
536+
},
537+
Spec: &v1alpha1.SubscriptionSpec{
538+
CatalogSource: "src",
539+
CatalogSourceNamespace: testNamespace,
540+
},
541+
Status: v1alpha1.SubscriptionStatus{
542+
CurrentCSV: "",
543+
State: "",
544+
LastUpdated: now,
545+
Conditions: []v1alpha1.SubscriptionCondition{
461546
{
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-
},
547+
Type: v1alpha1.SubscriptionResolutionFailed,
548+
Reason: "BundleUnpackFailurePreventedResolution",
549+
Message: "bundle unpacking failed. Reason: JobFailed, and Message: unpack job failed",
550+
Status: corev1.ConditionTrue,
477551
},
478552
},
479553
},
480554
},
481555
},
482556
},
557+
{
558+
name: "NoStatus/NoCurrentCSV/BundleLookupError",
559+
fields: fields{
560+
clientOptions: []clientfake.Option{clientfake.WithSelfLinks(t)},
561+
existingOLMObjs: []runtime.Object{
562+
&v1alpha1.Subscription{
563+
TypeMeta: metav1.TypeMeta{
564+
Kind: v1alpha1.SubscriptionKind,
565+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
566+
},
567+
ObjectMeta: metav1.ObjectMeta{
568+
Name: "sub",
569+
Namespace: testNamespace,
570+
},
571+
Spec: &v1alpha1.SubscriptionSpec{
572+
CatalogSource: "src",
573+
CatalogSourceNamespace: testNamespace,
574+
},
575+
Status: v1alpha1.SubscriptionStatus{
576+
CurrentCSV: "",
577+
State: "",
578+
},
579+
},
580+
},
581+
resolveBundleLookups: []v1alpha1.BundleLookup{{}},
582+
unpackBundleErr: errors.New("fake unpack error"),
583+
},
584+
args: args{
585+
obj: &v1alpha1.Subscription{
586+
TypeMeta: metav1.TypeMeta{
587+
Kind: v1alpha1.SubscriptionKind,
588+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
589+
},
590+
ObjectMeta: metav1.ObjectMeta{
591+
Name: "sub",
592+
Namespace: testNamespace,
593+
},
594+
Spec: &v1alpha1.SubscriptionSpec{
595+
CatalogSource: "src",
596+
CatalogSourceNamespace: testNamespace,
597+
},
598+
Status: v1alpha1.SubscriptionStatus{
599+
CurrentCSV: "",
600+
State: "",
601+
},
602+
},
603+
},
604+
wantSubscriptions: []*v1alpha1.Subscription{
605+
{
606+
TypeMeta: metav1.TypeMeta{
607+
Kind: v1alpha1.SubscriptionKind,
608+
APIVersion: v1alpha1.SubscriptionCRDAPIVersion,
609+
},
610+
ObjectMeta: metav1.ObjectMeta{
611+
Name: "sub",
612+
Namespace: testNamespace,
613+
},
614+
Spec: &v1alpha1.SubscriptionSpec{
615+
CatalogSource: "src",
616+
CatalogSourceNamespace: testNamespace,
617+
},
618+
Status: v1alpha1.SubscriptionStatus{
619+
CurrentCSV: "",
620+
State: "",
621+
},
622+
},
623+
},
624+
wantErr: errors.New("bundle unpacking failed with an error: fake unpack error"),
625+
},
483626
{
484627
name: "Status/HaveCurrentCSV/UpdateFoundInCatalog",
485628
fields: fields{
@@ -1013,7 +1156,12 @@ func TestSyncSubscriptions(t *testing.T) {
10131156
ctx, cancel := context.WithCancel(context.TODO())
10141157
defer cancel()
10151158

1016-
o, err := NewFakeOperator(ctx, testNamespace, []string{testNamespace}, withClock(clockFake), withClientObjs(tt.fields.existingOLMObjs...), withFakeClientOptions(tt.fields.clientOptions...))
1159+
fakeBundleUnpacker := &bundlefakes.FakeUnpacker{
1160+
UnpackBundleStub: func(lookup *v1alpha1.BundleLookup, timeout time.Duration) (*bundle.BundleUnpackResult, error) {
1161+
return &bundle.BundleUnpackResult{BundleLookup: lookup.DeepCopy()}, tt.fields.unpackBundleErr
1162+
},
1163+
}
1164+
o, err := NewFakeOperator(ctx, testNamespace, []string{testNamespace}, withClock(clockFake), withClientObjs(tt.fields.existingOLMObjs...), withFakeClientOptions(tt.fields.clientOptions...), withBundleUnpacker(fakeBundleUnpacker))
10171165
require.NoError(t, err)
10181166

10191167
o.reconciler = &fakes.FakeRegistryReconcilerFactory{

0 commit comments

Comments
 (0)