|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/util/rand" |
| 15 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 16 | + "k8s.io/utils/pointer" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 18 | + |
| 19 | + "github.com/operator-framework/deppy/pkg/deppy" |
| 20 | + "github.com/operator-framework/deppy/pkg/deppy/constraint" |
| 21 | + "github.com/operator-framework/deppy/pkg/deppy/input" |
| 22 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 23 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 24 | + rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1" |
| 25 | + |
| 26 | + operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 27 | + "github.com/operator-framework/operator-controller/internal/catalogmetadata" |
| 28 | + "github.com/operator-framework/operator-controller/internal/controllers" |
| 29 | + olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" |
| 30 | + testutil "github.com/operator-framework/operator-controller/test/util" |
| 31 | +) |
| 32 | + |
| 33 | +func TestVariableSource(t *testing.T) { |
| 34 | + sch := runtime.NewScheme() |
| 35 | + utilruntime.Must(operatorsv1alpha1.AddToScheme(sch)) |
| 36 | + utilruntime.Must(rukpakv1alpha1.AddToScheme(sch)) |
| 37 | + |
| 38 | + stableChannel := catalogmetadata.Channel{Channel: declcfg.Channel{ |
| 39 | + Name: "stable", |
| 40 | + Entries: []declcfg.ChannelEntry{ |
| 41 | + { |
| 42 | + Name: "packageA.v2.0.0", |
| 43 | + }, |
| 44 | + }, |
| 45 | + }} |
| 46 | + bundleSet := map[string]*catalogmetadata.Bundle{ |
| 47 | + "packageA.v2.0.0": { |
| 48 | + Bundle: declcfg.Bundle{ |
| 49 | + Name: "packageA.v2.0.0", |
| 50 | + Package: "packageA", |
| 51 | + Image: "foo.io/packageA/packageA:v2.0.0", |
| 52 | + Properties: []property.Property{ |
| 53 | + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"packageA","version":"2.0.0"}`)}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + CatalogName: "fake-catalog", |
| 57 | + InChannels: []*catalogmetadata.Channel{&stableChannel}, |
| 58 | + }, |
| 59 | + } |
| 60 | + allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet)) |
| 61 | + for _, bundle := range bundleSet { |
| 62 | + allBundles = append(allBundles, bundle) |
| 63 | + } |
| 64 | + |
| 65 | + pkgName := "packageA" |
| 66 | + opName := fmt.Sprintf("operator-test-%s", rand.String(8)) |
| 67 | + operator := &operatorsv1alpha1.Operator{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{Name: opName}, |
| 69 | + Spec: operatorsv1alpha1.OperatorSpec{ |
| 70 | + PackageName: pkgName, |
| 71 | + Channel: "stable", |
| 72 | + Version: "2.0.0", |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + bd := &rukpakv1alpha1.BundleDeployment{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Name: opName, |
| 79 | + OwnerReferences: []metav1.OwnerReference{ |
| 80 | + { |
| 81 | + APIVersion: operatorsv1alpha1.GroupVersion.String(), |
| 82 | + Kind: "Operator", |
| 83 | + Name: operator.Name, |
| 84 | + UID: operator.UID, |
| 85 | + Controller: pointer.Bool(true), |
| 86 | + BlockOwnerDeletion: pointer.Bool(true), |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + Spec: rukpakv1alpha1.BundleDeploymentSpec{ |
| 91 | + ProvisionerClassName: "core-rukpak-io-plain", |
| 92 | + Template: &rukpakv1alpha1.BundleTemplate{ |
| 93 | + Spec: rukpakv1alpha1.BundleSpec{ |
| 94 | + ProvisionerClassName: "core-rukpak-io-registry", |
| 95 | + Source: rukpakv1alpha1.BundleSource{ |
| 96 | + Type: rukpakv1alpha1.SourceTypeImage, |
| 97 | + Image: &rukpakv1alpha1.ImageSource{ |
| 98 | + Ref: "foo.io/packageA/packageA:v2.0.0", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + fakeClient := fake.NewClientBuilder().WithScheme(sch).WithObjects(operator, bd).Build() |
| 106 | + fakeCatalogClient := testutil.NewFakeCatalogClient(allBundles) |
| 107 | + |
| 108 | + vs := controllers.NewVariableSource(fakeClient, &fakeCatalogClient) |
| 109 | + |
| 110 | + vars, err := vs.GetVariables(context.Background()) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + expectedVars := []deppy.Variable{ |
| 114 | + olmvariables.NewRequiredPackageVariable("packageA", []*catalogmetadata.Bundle{ |
| 115 | + bundleSet["packageA.v2.0.0"], |
| 116 | + }), |
| 117 | + olmvariables.NewInstalledPackageVariable("packageA", []*catalogmetadata.Bundle{ |
| 118 | + bundleSet["packageA.v2.0.0"], |
| 119 | + }), |
| 120 | + olmvariables.NewBundleVariable(bundleSet["packageA.v2.0.0"], nil), |
| 121 | + olmvariables.NewBundleUniquenessVariable( |
| 122 | + "packageA package uniqueness", |
| 123 | + deppy.Identifier("fake-catalog-packageA-packageA.v2.0.0"), |
| 124 | + ), |
| 125 | + } |
| 126 | + gocmpopts := []cmp.Option{ |
| 127 | + cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}), |
| 128 | + cmp.AllowUnexported( |
| 129 | + olmvariables.RequiredPackageVariable{}, |
| 130 | + olmvariables.InstalledPackageVariable{}, |
| 131 | + olmvariables.BundleVariable{}, |
| 132 | + olmvariables.BundleUniquenessVariable{}, |
| 133 | + input.SimpleVariable{}, |
| 134 | + constraint.DependencyConstraint{}, |
| 135 | + constraint.AtMostConstraint{}, |
| 136 | + ), |
| 137 | + } |
| 138 | + require.Empty(t, cmp.Diff(vars, expectedVars, gocmpopts...)) |
| 139 | +} |
0 commit comments