|
| 1 | +package olm_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + "github.com/operator-framework/deppy/pkg/deppy" |
| 13 | + "github.com/operator-framework/deppy/pkg/deppy/input" |
| 14 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies" |
| 15 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/global_constraints" |
| 16 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm" |
| 17 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/required_package" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGlobalConstraints(t *testing.T) { |
| 21 | + RegisterFailHandler(Fail) |
| 22 | + RunSpecs(t, "OLMVariableSource Suite") |
| 23 | +} |
| 24 | + |
| 25 | +var testEntityCache = map[deppy.Identifier]input.Entity{ |
| 26 | + "operatorhub/prometheus/0.37.0": *input.NewEntity("operatorhub/prometheus/0.37.0", map[string]string{ |
| 27 | + "olm.bundle.path": `"quay.io/operatorhubio/prometheus@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35"`, |
| 28 | + "olm.channel": "{\"channelName\":\"beta\",\"priority\":0,\"replaces\":\"prometheusoperator.0.32.0\"}", |
| 29 | + "olm.gvk": "[{\"group\":\"monitoring.coreos.com\",\"kind\":\"Alertmanager\",\"version\":\"v1\"}, {\"group\":\"monitoring.coreos.com\",\"kind\":\"Prometheus\",\"version\":\"v1\"}]", |
| 30 | + "olm.package": "{\"packageName\":\"prometheus\",\"version\":\"0.37.0\"}", |
| 31 | + }), |
| 32 | + "operatorhub/prometheus/0.47.0": *input.NewEntity("operatorhub/prometheus/0.47.0", map[string]string{ |
| 33 | + "olm.bundle.path": `"quay.io/operatorhubio/prometheus@sha256:5b04c49d8d3eff6a338b56ec90bdf491d501fe301c9cdfb740e5bff6769a21ed"`, |
| 34 | + "olm.channel": "{\"channelName\":\"beta\",\"priority\":0,\"replaces\":\"prometheusoperator.0.37.0\"}", |
| 35 | + "olm.gvk": "[{\"group\":\"monitoring.coreos.com\",\"kind\":\"Alertmanager\",\"version\":\"v1\"}, {\"group\":\"monitoring.coreos.com\",\"kind\":\"Prometheus\",\"version\":\"v1alpha1\"}]", |
| 36 | + "olm.package": "{\"packageName\":\"prometheus\",\"version\":\"0.47.0\"}", |
| 37 | + }), |
| 38 | + "operatorhub/packageA/2.0.0": *input.NewEntity("operatorhub/packageA/2.0.0", map[string]string{ |
| 39 | + "olm.bundle.path": `"foo.io/packageA/packageA:v2.0.0"`, |
| 40 | + "olm.channel": "{\"channelName\":\"stable\",\"priority\":0}", |
| 41 | + "olm.gvk": "[{\"group\":\"foo.io\",\"kind\":\"Foo\",\"version\":\"v1\"}]", |
| 42 | + "olm.package": "{\"packageName\":\"packageA\",\"version\":\"2.0.0\"}", |
| 43 | + }), |
| 44 | +} |
| 45 | + |
| 46 | +func entityFromCache(name string) *input.Entity { |
| 47 | + entity := testEntityCache[deppy.IdentifierFromString(name)] |
| 48 | + return &entity |
| 49 | +} |
| 50 | + |
| 51 | +var _ = Describe("OLMVariableSource", func() { |
| 52 | + var testEntitySource input.EntitySource |
| 53 | + |
| 54 | + BeforeEach(func() { |
| 55 | + testEntitySource = input.NewCacheQuerier(testEntityCache) |
| 56 | + }) |
| 57 | + |
| 58 | + It("should produce RequiredPackage variables", func() { |
| 59 | + olmVariableSource := olm.NewOLMVariableSource("prometheus", "packageA") |
| 60 | + variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource) |
| 61 | + Expect(err).ToNot(HaveOccurred()) |
| 62 | + |
| 63 | + packageRequiredVariables := filterVariables[*required_package.RequiredPackageVariable](variables) |
| 64 | + Expect(packageRequiredVariables).To(HaveLen(2)) |
| 65 | + Expect(packageRequiredVariables[0].Identifier()).To(Equal(deppy.IdentifierFromString("required package prometheus"))) |
| 66 | + Expect(packageRequiredVariables[0].BundleEntities()).To(HaveLen(2)) |
| 67 | + Expect(packageRequiredVariables[1].Identifier()).To(Equal(deppy.IdentifierFromString("required package packageA"))) |
| 68 | + Expect(packageRequiredVariables[1].BundleEntities()).To(HaveLen(1)) |
| 69 | + }) |
| 70 | + |
| 71 | + It("should produce BundleVariables variables", func() { |
| 72 | + olmVariableSource := olm.NewOLMVariableSource("prometheus", "packageA") |
| 73 | + variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource) |
| 74 | + Expect(err).ToNot(HaveOccurred()) |
| 75 | + |
| 76 | + bundleVariables := filterVariables[*bundles_and_dependencies.BundleVariable](variables) |
| 77 | + Expect(bundleVariables).To(HaveLen(3)) |
| 78 | + Expect(bundleVariables).To(WithTransform(func(bvars []*bundles_and_dependencies.BundleVariable) []*input.Entity { |
| 79 | + var out []*input.Entity |
| 80 | + for _, variable := range bvars { |
| 81 | + out = append(out, variable.BundleEntity().Entity) |
| 82 | + } |
| 83 | + return out |
| 84 | + }, Equal([]*input.Entity{ |
| 85 | + entityFromCache("operatorhub/prometheus/0.47.0"), |
| 86 | + entityFromCache("operatorhub/prometheus/0.37.0"), |
| 87 | + entityFromCache("operatorhub/packageA/2.0.0"), |
| 88 | + }))) |
| 89 | + }) |
| 90 | + |
| 91 | + It("should produce GlobalConstraints variables", func() { |
| 92 | + olmVariableSource := olm.NewOLMVariableSource("prometheus", "packageA") |
| 93 | + variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource) |
| 94 | + Expect(err).ToNot(HaveOccurred()) |
| 95 | + |
| 96 | + globalConstraintsVariables := filterVariables[*global_constraints.GlobalConstraintsVariable](variables) |
| 97 | + Expect(globalConstraintsVariables).To(HaveLen(6)) |
| 98 | + |
| 99 | + // check global variables have the right names |
| 100 | + Expect(globalConstraintsVariables).To(WithTransform(func(gvars []*global_constraints.GlobalConstraintsVariable) []string { |
| 101 | + var out []string |
| 102 | + for _, variable := range gvars { |
| 103 | + out = append(out, string(variable.Identifier())) |
| 104 | + } |
| 105 | + sort.SliceStable(out, func(i, j int) bool { |
| 106 | + return strings.Compare(out[i], out[j]) < 0 |
| 107 | + }) |
| 108 | + return out |
| 109 | + }, Equal([]string{ |
| 110 | + "group:\"foo.io\" version:\"v1\" kind:\"Foo\" gvk uniqueness", |
| 111 | + "group:\"monitoring.coreos.com\" version:\"v1\" kind:\"Alertmanager\" gvk uniqueness", |
| 112 | + "group:\"monitoring.coreos.com\" version:\"v1\" kind:\"Prometheus\" gvk uniqueness", |
| 113 | + "group:\"monitoring.coreos.com\" version:\"v1alpha1\" kind:\"Prometheus\" gvk uniqueness", |
| 114 | + "packageA package uniqueness", |
| 115 | + "prometheus package uniqueness", |
| 116 | + }))) |
| 117 | + }) |
| 118 | + |
| 119 | + It("should return an errors when they occur", func() { |
| 120 | + olmVariableSource := olm.NewOLMVariableSource("prometheus", "packageA") |
| 121 | + _, err := olmVariableSource.GetVariables(context.Background(), FailEntitySource{}) |
| 122 | + Expect(err).To(HaveOccurred()) |
| 123 | + }) |
| 124 | +}) |
| 125 | + |
| 126 | +var _ input.EntitySource = &FailEntitySource{} |
| 127 | + |
| 128 | +type FailEntitySource struct { |
| 129 | +} |
| 130 | + |
| 131 | +func (f FailEntitySource) Get(ctx context.Context, id deppy.Identifier) *input.Entity { |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func (f FailEntitySource) Filter(ctx context.Context, filter input.Predicate) (input.EntityList, error) { |
| 136 | + return nil, fmt.Errorf("error executing filter") |
| 137 | +} |
| 138 | + |
| 139 | +func (f FailEntitySource) GroupBy(ctx context.Context, fn input.GroupByFunction) (input.EntityListMap, error) { |
| 140 | + return nil, fmt.Errorf("error executing group by") |
| 141 | +} |
| 142 | + |
| 143 | +func (f FailEntitySource) Iterate(ctx context.Context, fn input.IteratorFunction) error { |
| 144 | + return fmt.Errorf("error executing iterate") |
| 145 | +} |
| 146 | + |
| 147 | +func filterVariables[D deppy.Variable](variables []deppy.Variable) []D { |
| 148 | + var out []D |
| 149 | + for _, variable := range variables { |
| 150 | + switch v := variable.(type) { |
| 151 | + case D: |
| 152 | + out = append(out, v) |
| 153 | + } |
| 154 | + } |
| 155 | + return out |
| 156 | +} |
0 commit comments