|
| 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 crd_test |
| 18 | + |
| 19 | +import ( |
| 20 | +"bytes" |
| 21 | +"io" |
| 22 | +"os" |
| 23 | +"path/filepath" |
| 24 | + |
| 25 | +"github.com/google/go-cmp/cmp" |
| 26 | +. "github.com/onsi/ginkgo" |
| 27 | +. "github.com/onsi/gomega" |
| 28 | +"sigs.k8s.io/controller-tools/pkg/crd" |
| 29 | +crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers" |
| 30 | +"sigs.k8s.io/controller-tools/pkg/genall" |
| 31 | +"sigs.k8s.io/controller-tools/pkg/loader" |
| 32 | +"sigs.k8s.io/controller-tools/pkg/markers" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = Describe("CRD Feature Gate Generation", func() { |
| 36 | + var ( |
| 37 | +ctx *genall.GenerationContext |
| 38 | +out *featureGateOutputRule |
| 39 | +featureGateDir string |
| 40 | +originalWorkingDir string |
| 41 | +) |
| 42 | + |
| 43 | + BeforeEach(func() { |
| 44 | + var err error |
| 45 | + originalWorkingDir, err = os.Getwd() |
| 46 | + Expect(err).NotTo(HaveOccurred()) |
| 47 | + |
| 48 | + featureGateDir = filepath.Join(originalWorkingDir, "testdata", "featuregates") |
| 49 | + |
| 50 | + By("switching into featuregates testdata") |
| 51 | + err = os.Chdir(featureGateDir) |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + |
| 54 | + By("loading the roots") |
| 55 | + pkgs, err := loader.LoadRoots(".") |
| 56 | + Expect(err).NotTo(HaveOccurred()) |
| 57 | + Expect(pkgs).To(HaveLen(1)) |
| 58 | + |
| 59 | + out = &featureGateOutputRule{buf: &bytes.Buffer{}} |
| 60 | + ctx = &genall.GenerationContext{ |
| 61 | + Collector: &markers.Collector{Registry: &markers.Registry{}}, |
| 62 | + Roots: pkgs, |
| 63 | + Checker: &loader.TypeChecker{}, |
| 64 | + OutputRule: out, |
| 65 | + } |
| 66 | + Expect(crdmarkers.Register(ctx.Collector.Registry)).To(Succeed()) |
| 67 | + }) |
| 68 | + |
| 69 | + AfterEach(func() { |
| 70 | + By("restoring original working directory") |
| 71 | + err := os.Chdir(originalWorkingDir) |
| 72 | + Expect(err).NotTo(HaveOccurred()) |
| 73 | + }) |
| 74 | + |
| 75 | + It("should not include feature-gated fields when no gates are enabled", func() { |
| 76 | + By("calling the generator") |
| 77 | + gen := &crd.Generator{ |
| 78 | + CRDVersions: []string{"v1"}, |
| 79 | + // No FeatureGates specified |
| 80 | + } |
| 81 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 82 | + |
| 83 | + By("loading the expected YAML") |
| 84 | + expectedFile, err := os.ReadFile("output_none/_featuregatetests.yaml") |
| 85 | + Expect(err).NotTo(HaveOccurred()) |
| 86 | + |
| 87 | + By("comparing the two") |
| 88 | + Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) |
| 89 | + }) |
| 90 | + |
| 91 | + It("should include only alpha-gated fields when alpha gate is enabled", func() { |
| 92 | + By("calling the generator") |
| 93 | + gen := &crd.Generator{ |
| 94 | + CRDVersions: []string{"v1"}, |
| 95 | + FeatureGates: "alpha=true", |
| 96 | + } |
| 97 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 98 | + |
| 99 | + By("loading the expected YAML") |
| 100 | + expectedFile, err := os.ReadFile("output_alpha/_featuregatetests.yaml") |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + |
| 103 | + By("comparing the two") |
| 104 | + Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) |
| 105 | + }) |
| 106 | + |
| 107 | + It("should include only beta-gated fields when beta gate is enabled", func() { |
| 108 | + By("calling the generator") |
| 109 | + gen := &crd.Generator{ |
| 110 | + CRDVersions: []string{"v1"}, |
| 111 | + FeatureGates: "beta=true", |
| 112 | + } |
| 113 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 114 | + |
| 115 | + By("loading the expected YAML") |
| 116 | + expectedFile, err := os.ReadFile("output_beta/_featuregatetests.yaml") |
| 117 | + Expect(err).NotTo(HaveOccurred()) |
| 118 | + |
| 119 | + By("comparing the two") |
| 120 | + Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) |
| 121 | + }) |
| 122 | + |
| 123 | + It("should include both feature-gated fields when both gates are enabled", func() { |
| 124 | + By("calling the generator") |
| 125 | + gen := &crd.Generator{ |
| 126 | + CRDVersions: []string{"v1"}, |
| 127 | + FeatureGates: "alpha=true,beta=true", |
| 128 | + } |
| 129 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 130 | + |
| 131 | + By("loading the expected YAML") |
| 132 | + expectedFile, err := os.ReadFile("output_both/_featuregatetests.yaml") |
| 133 | + Expect(err).NotTo(HaveOccurred()) |
| 134 | + |
| 135 | + By("comparing the two") |
| 136 | + Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile))) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +// Helper types for testing |
| 141 | +type featureGateOutputRule struct { |
| 142 | + buf *bytes.Buffer |
| 143 | +} |
| 144 | + |
| 145 | +func (o *featureGateOutputRule) Open(_ *loader.Package, _ string) (io.WriteCloser, error) { |
| 146 | + return featureGateNopCloser{o.buf}, nil |
| 147 | +} |
| 148 | + |
| 149 | +type featureGateNopCloser struct { |
| 150 | + io.Writer |
| 151 | +} |
| 152 | + |
| 153 | +func (n featureGateNopCloser) Close() error { |
| 154 | + return nil |
| 155 | +} |
0 commit comments