|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + apimeta "k8s.io/apimachinery/pkg/api/meta" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/apimachinery/pkg/util/rand" |
| 15 | + ctrl "sigs.k8s.io/controller-runtime" |
| 16 | + |
| 17 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 18 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 19 | + rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" |
| 20 | + |
| 21 | + ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 22 | + "github.com/operator-framework/operator-controller/internal/catalogmetadata" |
| 23 | + "github.com/operator-framework/operator-controller/internal/controllers" |
| 24 | + testutil "github.com/operator-framework/operator-controller/test/util" |
| 25 | +) |
| 26 | + |
| 27 | +func TestClusterExtensionRegistryV1DisallowDependencies(t *testing.T) { |
| 28 | + ctx := context.Background() |
| 29 | + cl := newClient(t) |
| 30 | + |
| 31 | + for _, tt := range []struct { |
| 32 | + name string |
| 33 | + bundle *catalogmetadata.Bundle |
| 34 | + wantErr string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "package with no dependencies", |
| 38 | + bundle: &catalogmetadata.Bundle{ |
| 39 | + Bundle: declcfg.Bundle{ |
| 40 | + Name: "fake-catalog/no-dependencies-package/alpha/1.0.0", |
| 41 | + Package: "no-dependencies-package", |
| 42 | + Image: "quay.io/fake-catalog/no-dependencies-package@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", |
| 43 | + Properties: []property.Property{ |
| 44 | + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"no-dependencies-package","version":"1.0.0"}`)}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + CatalogName: "fake-catalog", |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "package with olm.package.required property", |
| 52 | + bundle: &catalogmetadata.Bundle{ |
| 53 | + Bundle: declcfg.Bundle{ |
| 54 | + Name: "fake-catalog/package-required-test/alpha/1.0.0", |
| 55 | + Package: "package-required-test", |
| 56 | + Image: "quay.io/fake-catalog/package-required-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", |
| 57 | + Properties: []property.Property{ |
| 58 | + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"package-required-test","version":"1.0.0"}`)}, |
| 59 | + {Type: property.TypePackageRequired, Value: json.RawMessage("content-is-not-relevant")}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + CatalogName: "fake-catalog", |
| 63 | + }, |
| 64 | + wantErr: `bundle "fake-catalog/package-required-test/alpha/1.0.0" has a dependency declared via property "olm.package.required" which is currently not supported`, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "package with olm.gvk.required property", |
| 68 | + bundle: &catalogmetadata.Bundle{ |
| 69 | + Bundle: declcfg.Bundle{ |
| 70 | + Name: "fake-catalog/gvk-required-test/alpha/1.0.0", |
| 71 | + Package: "gvk-required-test", |
| 72 | + Image: "quay.io/fake-catalog/gvk-required-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", |
| 73 | + Properties: []property.Property{ |
| 74 | + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"gvk-required-test","version":"1.0.0"}`)}, |
| 75 | + {Type: property.TypeGVKRequired, Value: json.RawMessage(`content-is-not-relevant`)}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + CatalogName: "fake-catalog", |
| 79 | + }, |
| 80 | + wantErr: `bundle "fake-catalog/gvk-required-test/alpha/1.0.0" has a dependency declared via property "olm.gvk.required" which is currently not supported`, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "package with olm.constraint property", |
| 84 | + bundle: &catalogmetadata.Bundle{ |
| 85 | + Bundle: declcfg.Bundle{ |
| 86 | + Name: "fake-catalog/constraint-test/alpha/1.0.0", |
| 87 | + Package: "constraint-test", |
| 88 | + Image: "quay.io/fake-catalog/constraint-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", |
| 89 | + Properties: []property.Property{ |
| 90 | + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"constraint-test","version":"1.0.0"}`)}, |
| 91 | + {Type: property.TypeConstraint, Value: json.RawMessage(`content-is-not-relevant`)}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + CatalogName: "fake-catalog", |
| 95 | + }, |
| 96 | + wantErr: `bundle "fake-catalog/constraint-test/alpha/1.0.0" has a dependency declared via property "olm.constraint" which is currently not supported`, |
| 97 | + }, |
| 98 | + } { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + defer func() { |
| 101 | + require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) |
| 102 | + require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) |
| 103 | + }() |
| 104 | + |
| 105 | + fakeCatalogClient := testutil.NewFakeCatalogClient([]*catalogmetadata.Bundle{tt.bundle}) |
| 106 | + reconciler := &controllers.ClusterExtensionReconciler{ |
| 107 | + Client: cl, |
| 108 | + BundleProvider: &fakeCatalogClient, |
| 109 | + } |
| 110 | + |
| 111 | + extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} |
| 112 | + clusterExtension := &ocv1alpha1.ClusterExtension{ |
| 113 | + ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, |
| 114 | + Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: tt.bundle.Package}, |
| 115 | + } |
| 116 | + require.NoError(t, cl.Create(ctx, clusterExtension)) |
| 117 | + |
| 118 | + res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey}) |
| 119 | + require.Equal(t, ctrl.Result{}, res) |
| 120 | + if tt.wantErr == "" { |
| 121 | + assert.NoError(t, err) |
| 122 | + } else { |
| 123 | + assert.EqualError(t, err, tt.wantErr) |
| 124 | + |
| 125 | + // In case of an error we want it to be included in the installed condition |
| 126 | + require.NoError(t, cl.Get(ctx, extKey, clusterExtension)) |
| 127 | + cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeInstalled) |
| 128 | + require.NotNil(t, cond) |
| 129 | + require.Equal(t, metav1.ConditionFalse, cond.Status) |
| 130 | + require.Equal(t, ocv1alpha1.ReasonInstallationFailed, cond.Reason) |
| 131 | + require.Equal(t, tt.wantErr, cond.Message) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments