|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package endpoints |
| 18 | + |
| 19 | +import ( |
| 20 | + . "github.com/onsi/ginkgo/v2" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | +) |
| 25 | + |
| 26 | +var _ = Describe("DiscoverBaseDomain", func() { |
| 27 | + // Note: These tests require OpenShift CRDs to be available in the test environment. |
| 28 | + // They will be skipped if the CRDs are not present, which is expected in non-OpenShift environments. |
| 29 | + |
| 30 | + Context("when OpenShift is available", func() { |
| 31 | + BeforeEach(func() { |
| 32 | + // Check if OpenShift CRDs are available |
| 33 | + dns := &unstructured.Unstructured{} |
| 34 | + dns.SetGroupVersionKind(schema.GroupVersionKind{ |
| 35 | + Group: "config.openshift.io", |
| 36 | + Version: "v1", |
| 37 | + Kind: "DNS", |
| 38 | + }) |
| 39 | + dns.SetName("cluster") |
| 40 | + dns.Object["spec"] = map[string]interface{}{ |
| 41 | + "baseDomain": "test-check.com", |
| 42 | + } |
| 43 | + |
| 44 | + // Try to create a test DNS object to check if the CRD is available |
| 45 | + err := k8sClient.Create(ctx, dns) |
| 46 | + if err != nil { |
| 47 | + Skip("Skipping OpenShift baseDomain auto-detection tests: OpenShift CRDs not available in test environment") |
| 48 | + } |
| 49 | + // Clean up test object |
| 50 | + _ = k8sClient.Delete(ctx, dns) |
| 51 | + }) |
| 52 | + |
| 53 | + Context("when OpenShift DNS cluster config exists", func() { |
| 54 | + It("should successfully auto-detect baseDomain", func() { |
| 55 | + // Create a mock OpenShift DNS cluster config |
| 56 | + dns := &unstructured.Unstructured{} |
| 57 | + dns.SetGroupVersionKind(schema.GroupVersionKind{ |
| 58 | + Group: "config.openshift.io", |
| 59 | + Version: "v1", |
| 60 | + Kind: "DNS", |
| 61 | + }) |
| 62 | + dns.SetName("cluster") |
| 63 | + dns.Object["spec"] = map[string]interface{}{ |
| 64 | + "baseDomain": "example.com", |
| 65 | + } |
| 66 | + |
| 67 | + // Create the DNS object in the cluster |
| 68 | + err := k8sClient.Create(ctx, dns) |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + |
| 71 | + // Test auto-detection |
| 72 | + detectedBaseDomain, err := DiscoverBaseDomain(ctx, k8sClient, "test-namespace") |
| 73 | + Expect(err).NotTo(HaveOccurred()) |
| 74 | + Expect(detectedBaseDomain).To(Equal("test-namespace.apps.example.com")) |
| 75 | + |
| 76 | + // Cleanup |
| 77 | + err = k8sClient.Delete(ctx, dns) |
| 78 | + Expect(err).NotTo(HaveOccurred()) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + Context("when OpenShift DNS cluster config has empty baseDomain", func() { |
| 83 | + It("should return an error", func() { |
| 84 | + // Create a mock OpenShift DNS cluster config with empty baseDomain |
| 85 | + dns := &unstructured.Unstructured{} |
| 86 | + dns.SetGroupVersionKind(schema.GroupVersionKind{ |
| 87 | + Group: "config.openshift.io", |
| 88 | + Version: "v1", |
| 89 | + Kind: "DNS", |
| 90 | + }) |
| 91 | + dns.SetName("cluster") |
| 92 | + dns.Object["spec"] = map[string]interface{}{ |
| 93 | + "baseDomain": "", |
| 94 | + } |
| 95 | + |
| 96 | + // Create the DNS object in the cluster |
| 97 | + err := k8sClient.Create(ctx, dns) |
| 98 | + Expect(err).NotTo(HaveOccurred()) |
| 99 | + |
| 100 | + // Test auto-detection with empty baseDomain |
| 101 | + _, err = DiscoverBaseDomain(ctx, k8sClient, "test-namespace") |
| 102 | + Expect(err).To(HaveOccurred()) |
| 103 | + Expect(err.Error()).To(ContainSubstring("baseDomain not found or empty")) |
| 104 | + |
| 105 | + // Cleanup |
| 106 | + err = k8sClient.Delete(ctx, dns) |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + }) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + Context("when OpenShift DNS cluster config does not exist", func() { |
| 113 | + It("should return an error", func() { |
| 114 | + // Try to auto-detect when no DNS config exists |
| 115 | + // This test will work even without OpenShift CRDs because it just checks error handling |
| 116 | + _, err := DiscoverBaseDomain(ctx, k8sClient, "test-namespace") |
| 117 | + Expect(err).To(HaveOccurred()) |
| 118 | + Expect(err.Error()).To(ContainSubstring("failed to auto-detect baseDomain")) |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments