|
| 1 | +package olm |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1" |
| 8 | + operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | +) |
| 12 | + |
| 13 | +func TestReconcileCertifiedOperatorsCatalogSource(t *testing.T) { |
| 14 | + testsCases := []struct { |
| 15 | + name string |
| 16 | + placement hyperv1.OLMCatalogPlacement |
| 17 | + params *OperatorLifecycleManagerParams |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "when placement is Management it should set Address", |
| 21 | + placement: hyperv1.ManagementOLMCatalogPlacement, |
| 22 | + params: &OperatorLifecycleManagerParams{ |
| 23 | + CertifiedOperatorsImage: "registry.example.com/certified:latest", |
| 24 | + OLMCatalogPlacement: hyperv1.ManagementOLMCatalogPlacement, |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "when placement is Guest it should set Image", |
| 29 | + placement: hyperv1.GuestOLMCatalogPlacement, |
| 30 | + params: &OperatorLifecycleManagerParams{ |
| 31 | + CertifiedOperatorsImage: "registry.example.com/certified:latest", |
| 32 | + OLMCatalogPlacement: hyperv1.GuestOLMCatalogPlacement, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tc := range testsCases { |
| 38 | + t.Run(tc.name, func(t *testing.T) { |
| 39 | + g := NewWithT(t) |
| 40 | + cs := &operatorsv1alpha1.CatalogSource{} |
| 41 | + |
| 42 | + ReconcileCertifiedOperatorsCatalogSource(cs, tc.params) |
| 43 | + |
| 44 | + // Verify common fields |
| 45 | + g.Expect(cs.Spec.SourceType).To(Equal(operatorsv1alpha1.SourceTypeGrpc)) |
| 46 | + g.Expect(cs.Spec.DisplayName).To(Equal("Certified Operators")) |
| 47 | + g.Expect(cs.Spec.Publisher).To(Equal("Red Hat")) |
| 48 | + g.Expect(cs.Spec.Priority).To(Equal(-200)) |
| 49 | + g.Expect(cs.Spec.GrpcPodConfig.SecurityContextConfig).To(Equal(operatorsv1alpha1.Restricted)) |
| 50 | + g.Expect(cs.Annotations).To(HaveKeyWithValue("target.workload.openshift.io/management", `{"effect": "PreferredDuringScheduling"}`)) |
| 51 | + |
| 52 | + // Verify RegistryPoll interval is 240 minutes |
| 53 | + g.Expect(cs.Spec.UpdateStrategy).ToNot(BeNil()) |
| 54 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll).ToNot(BeNil()) |
| 55 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval).ToNot(BeNil()) |
| 56 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.RawInterval).To(Equal("240m")) |
| 57 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval.Duration).To(Equal(240 * time.Minute)) |
| 58 | + |
| 59 | + // Verify placement-specific fields |
| 60 | + if tc.placement == hyperv1.ManagementOLMCatalogPlacement { |
| 61 | + g.Expect(cs.Spec.Address).To(Equal("certified-operators:50051")) |
| 62 | + g.Expect(cs.Spec.Image).To(BeEmpty()) |
| 63 | + } else { |
| 64 | + g.Expect(cs.Spec.Image).To(Equal("registry.example.com/certified:latest")) |
| 65 | + g.Expect(cs.Spec.Address).To(BeEmpty()) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestReconcileCommunityOperatorsCatalogSource(t *testing.T) { |
| 72 | + g := NewWithT(t) |
| 73 | + |
| 74 | + params := &OperatorLifecycleManagerParams{ |
| 75 | + CommunityOperatorsImage: "registry.example.com/community:latest", |
| 76 | + OLMCatalogPlacement: hyperv1.GuestOLMCatalogPlacement, |
| 77 | + } |
| 78 | + |
| 79 | + cs := &operatorsv1alpha1.CatalogSource{} |
| 80 | + ReconcileCommunityOperatorsCatalogSource(cs, params) |
| 81 | + |
| 82 | + // Verify RegistryPoll interval is 240 minutes |
| 83 | + g.Expect(cs.Spec.UpdateStrategy).ToNot(BeNil()) |
| 84 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll).ToNot(BeNil()) |
| 85 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval).ToNot(BeNil()) |
| 86 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.RawInterval).To(Equal("240m")) |
| 87 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval.Duration).To(Equal(240 * time.Minute)) |
| 88 | + |
| 89 | + g.Expect(cs.Spec.DisplayName).To(Equal("Community Operators")) |
| 90 | + g.Expect(cs.Spec.Priority).To(Equal(-400)) |
| 91 | + g.Expect(cs.Spec.Image).To(Equal("registry.example.com/community:latest")) |
| 92 | +} |
| 93 | + |
| 94 | +func TestReconcileRedHatMarketplaceCatalogSource(t *testing.T) { |
| 95 | + g := NewWithT(t) |
| 96 | + |
| 97 | + params := &OperatorLifecycleManagerParams{ |
| 98 | + RedHatMarketplaceImage: "registry.example.com/marketplace:latest", |
| 99 | + OLMCatalogPlacement: hyperv1.ManagementOLMCatalogPlacement, |
| 100 | + } |
| 101 | + |
| 102 | + cs := &operatorsv1alpha1.CatalogSource{} |
| 103 | + ReconcileRedHatMarketplaceCatalogSource(cs, params) |
| 104 | + |
| 105 | + // Verify RegistryPoll interval is 240 minutes |
| 106 | + g.Expect(cs.Spec.UpdateStrategy).ToNot(BeNil()) |
| 107 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll).ToNot(BeNil()) |
| 108 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval).ToNot(BeNil()) |
| 109 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.RawInterval).To(Equal("240m")) |
| 110 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval.Duration).To(Equal(240 * time.Minute)) |
| 111 | + |
| 112 | + g.Expect(cs.Spec.DisplayName).To(Equal("Red Hat Marketplace")) |
| 113 | + g.Expect(cs.Spec.Priority).To(Equal(-300)) |
| 114 | + g.Expect(cs.Spec.Address).To(Equal("redhat-marketplace:50051")) |
| 115 | +} |
| 116 | + |
| 117 | +func TestReconcileRedHatOperatorsCatalogSource(t *testing.T) { |
| 118 | + g := NewWithT(t) |
| 119 | + |
| 120 | + params := &OperatorLifecycleManagerParams{ |
| 121 | + RedHatOperatorsImage: "registry.example.com/redhat-operators:latest", |
| 122 | + OLMCatalogPlacement: hyperv1.GuestOLMCatalogPlacement, |
| 123 | + } |
| 124 | + |
| 125 | + cs := &operatorsv1alpha1.CatalogSource{} |
| 126 | + ReconcileRedHatOperatorsCatalogSource(cs, params) |
| 127 | + |
| 128 | + // Verify RegistryPoll interval is 240 minutes |
| 129 | + g.Expect(cs.Spec.UpdateStrategy).ToNot(BeNil()) |
| 130 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll).ToNot(BeNil()) |
| 131 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval).ToNot(BeNil()) |
| 132 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.RawInterval).To(Equal("240m")) |
| 133 | + g.Expect(cs.Spec.UpdateStrategy.RegistryPoll.Interval.Duration).To(Equal(240 * time.Minute)) |
| 134 | + |
| 135 | + g.Expect(cs.Spec.DisplayName).To(Equal("Red Hat Operators")) |
| 136 | + g.Expect(cs.Spec.Priority).To(Equal(-100)) |
| 137 | + g.Expect(cs.Spec.Image).To(Equal("registry.example.com/redhat-operators:latest")) |
| 138 | +} |
| 139 | + |
| 140 | +func TestReconcileCatalogSourcePreservesExistingAnnotations(t *testing.T) { |
| 141 | + g := NewWithT(t) |
| 142 | + |
| 143 | + params := &OperatorLifecycleManagerParams{ |
| 144 | + RedHatOperatorsImage: "registry.example.com/redhat-operators:latest", |
| 145 | + OLMCatalogPlacement: hyperv1.GuestOLMCatalogPlacement, |
| 146 | + } |
| 147 | + |
| 148 | + cs := &operatorsv1alpha1.CatalogSource{ |
| 149 | + ObjectMeta: metav1.ObjectMeta{ |
| 150 | + Annotations: map[string]string{ |
| 151 | + "existing-annotation": "existing-value", |
| 152 | + }, |
| 153 | + }, |
| 154 | + } |
| 155 | + |
| 156 | + ReconcileRedHatOperatorsCatalogSource(cs, params) |
| 157 | + |
| 158 | + // Verify existing annotation is preserved and management annotation is added |
| 159 | + g.Expect(cs.Annotations).To(HaveKeyWithValue("existing-annotation", "existing-value")) |
| 160 | + g.Expect(cs.Annotations).To(HaveKeyWithValue("target.workload.openshift.io/management", `{"effect": "PreferredDuringScheduling"}`)) |
| 161 | +} |
0 commit comments