|
| 1 | +package ingressconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + . "github.com/onsi/gomega" |
| 7 | + |
| 8 | + "github.com/openshift/cluster-network-operator/pkg/client/fake" |
| 9 | + "github.com/openshift/cluster-network-operator/pkg/hypershift" |
| 10 | + "k8s.io/apimachinery/pkg/api/meta" |
| 11 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func TestIsIngressCapabilityEnabled(t *testing.T) { |
| 15 | + testCases := []struct { |
| 16 | + name string |
| 17 | + hypershiftEnabled bool |
| 18 | + crdExists bool |
| 19 | + restMapperError error |
| 20 | + expectedResult bool |
| 21 | + expectedError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Non-hypershift cluster should always return true (IngressController CRD doesn't exists)", |
| 25 | + hypershiftEnabled: false, |
| 26 | + crdExists: false, // doesn't matter for non-hypershift |
| 27 | + expectedResult: true, |
| 28 | + expectedError: false, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "Non-hypershift cluster should always return true (even with IngressController CRD)", |
| 32 | + hypershiftEnabled: false, |
| 33 | + crdExists: true, // doesn't matter for non-hypershift |
| 34 | + expectedResult: true, |
| 35 | + expectedError: false, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Hypershift cluster with IngressController CRD should return true", |
| 39 | + hypershiftEnabled: true, |
| 40 | + crdExists: true, |
| 41 | + expectedResult: true, |
| 42 | + expectedError: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Hypershift cluster without IngressController CRD should return false", |
| 46 | + hypershiftEnabled: true, |
| 47 | + crdExists: false, |
| 48 | + restMapperError: &meta.NoKindMatchError{ |
| 49 | + GroupKind: schema.GroupKind{ |
| 50 | + Group: "operator.openshift.io", |
| 51 | + Kind: "IngressController", |
| 52 | + }, |
| 53 | + SearchedVersions: []string{"v1"}, |
| 54 | + }, |
| 55 | + expectedResult: false, |
| 56 | + expectedError: false, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tc := range testCases { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + g := NewGomegaWithT(t) |
| 63 | + |
| 64 | + // Create mock REST mapper |
| 65 | + restMapper := &mockRESTMapper{ |
| 66 | + shouldReturnError: !tc.crdExists, |
| 67 | + errorToReturn: tc.restMapperError, |
| 68 | + } |
| 69 | + |
| 70 | + // Create mock client with the REST mapper |
| 71 | + fakeClient := fake.NewFakeClient() |
| 72 | + mockClient := &mockClient{ |
| 73 | + Client: fakeClient.Default().CRClient(), |
| 74 | + restMapper: restMapper, |
| 75 | + } |
| 76 | + |
| 77 | + // Create hypershift config with the desired enabled state |
| 78 | + hcpCfg := &hypershift.HyperShiftConfig{ |
| 79 | + Enabled: tc.hypershiftEnabled, |
| 80 | + } |
| 81 | + |
| 82 | + result, err := isIngressCapabilityEnabledWithConfig(mockClient, hcpCfg) |
| 83 | + |
| 84 | + if tc.expectedError { |
| 85 | + g.Expect(err).To(HaveOccurred()) |
| 86 | + } else { |
| 87 | + g.Expect(err).NotTo(HaveOccurred()) |
| 88 | + } |
| 89 | + g.Expect(result).To(Equal(tc.expectedResult)) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestIsIngressCapabilityEnabledEdgeCases(t *testing.T) { |
| 95 | + g := NewGomegaWithT(t) |
| 96 | + |
| 97 | + // Test with nil client - should handle gracefully |
| 98 | + hcpCfg := &hypershift.HyperShiftConfig{Enabled: false} |
| 99 | + result, err := isIngressCapabilityEnabledWithConfig(nil, hcpCfg) |
| 100 | + g.Expect(err).To(HaveOccurred()) |
| 101 | + g.Expect(result).To(BeFalse()) |
| 102 | + |
| 103 | + // Test REST mapper error handling (in non-hypershift environment) |
| 104 | + // This simulates what would happen if there were issues with the REST mapper |
| 105 | + restMapper := &mockRESTMapper{ |
| 106 | + shouldReturnError: true, |
| 107 | + errorToReturn: &meta.NoKindMatchError{ |
| 108 | + GroupKind: schema.GroupKind{ |
| 109 | + Group: "operator.openshift.io", |
| 110 | + Kind: "IngressController", |
| 111 | + }, |
| 112 | + SearchedVersions: []string{"v1"}, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + fakeClient := fake.NewFakeClient() |
| 117 | + mockClient := &mockClient{ |
| 118 | + Client: fakeClient.Default().CRClient(), |
| 119 | + restMapper: restMapper, |
| 120 | + } |
| 121 | + |
| 122 | + // In non-hypershift mode, this should still return true regardless of REST mapper errors |
| 123 | + result, err = isIngressCapabilityEnabledWithConfig(mockClient, hcpCfg) |
| 124 | + g.Expect(err).NotTo(HaveOccurred()) |
| 125 | + g.Expect(result).To(BeTrue()) // Non-hypershift always returns true |
| 126 | +} |
0 commit comments