|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 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 applyconfiguration |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/fs" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo" |
| 28 | + . "github.com/onsi/ginkgo/extensions/table" |
| 29 | + . "github.com/onsi/gomega" |
| 30 | + "k8s.io/apimachinery/pkg/util/sets" |
| 31 | + |
| 32 | + "sigs.k8s.io/controller-tools/pkg/crd" |
| 33 | + "sigs.k8s.io/controller-tools/pkg/genall" |
| 34 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 35 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + cronjobDir = "./testdata/cronjob" |
| 40 | + applyConfigurationDir = "applyconfiguration" |
| 41 | +) |
| 42 | + |
| 43 | +type outputToMap map[string]*outputFile |
| 44 | + |
| 45 | +// Open implements genall.OutputRule. |
| 46 | +func (m outputToMap) Open(_ *loader.Package, path string) (io.WriteCloser, error) { |
| 47 | + if _, ok := m[path]; !ok { |
| 48 | + m[path] = &outputFile{} |
| 49 | + } |
| 50 | + return m[path], nil |
| 51 | +} |
| 52 | + |
| 53 | +type outputFile struct { |
| 54 | + contents []byte |
| 55 | +} |
| 56 | + |
| 57 | +func (o *outputFile) Write(p []byte) (int, error) { |
| 58 | + o.contents = append(o.contents, p...) |
| 59 | + return len(p), nil |
| 60 | +} |
| 61 | + |
| 62 | +func (o *outputFile) Close() error { |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +var _ = Describe("ApplyConfiguration generation from API types", func() { |
| 67 | + var originalCWD string |
| 68 | + |
| 69 | + BeforeEach(func() { |
| 70 | + var tmpDir string |
| 71 | + |
| 72 | + By("Setting up a temporary directory", func() { |
| 73 | + var err error |
| 74 | + tmpDir, err = os.MkdirTemp("", "applyconfiguration-integration-test") |
| 75 | + Expect(err).NotTo(HaveOccurred(), "Should be able to create a temporary directory") |
| 76 | + |
| 77 | + // Copy the testdata directory, but removed the generated files. |
| 78 | + Expect(os.CopyFS(tmpDir, os.DirFS(cronjobDir))).To(Succeed(), "Should be able to copy source files") |
| 79 | + Expect(os.RemoveAll(filepath.Join(tmpDir, "api/v1", applyConfigurationDir))).To(Succeed(), "Should be able to remove generated file from temp directory") |
| 80 | + }) |
| 81 | + |
| 82 | + By("Switching into testdata to appease go modules", func() { |
| 83 | + cwd, err := os.Getwd() |
| 84 | + Expect(err).NotTo(HaveOccurred()) |
| 85 | + |
| 86 | + originalCWD = cwd |
| 87 | + |
| 88 | + Expect(os.Chdir(tmpDir)).To(Succeed()) // go modules are directory-sensitive |
| 89 | + }) |
| 90 | + |
| 91 | + By(fmt.Sprintf("Completed set up in %s", tmpDir)) |
| 92 | + }) |
| 93 | + |
| 94 | + AfterEach(func() { |
| 95 | + // Reset the working directory |
| 96 | + Expect(os.Chdir(originalCWD)).To(Succeed()) |
| 97 | + }) |
| 98 | + |
| 99 | + DescribeTable("should be able to verify generated ApplyConfiguration types for the CronJob schema", func(outputPackage string) { |
| 100 | + Expect(replaceOutputPkgMarker("./api/v1", outputPackage)).To(Succeed()) |
| 101 | + |
| 102 | + // The output is used to capture the generated CRD file. |
| 103 | + // The output of the applyconfiguration cannot be generated to memory, gengo handles all of the writing to disk directly. |
| 104 | + output := make(outputToMap) |
| 105 | + |
| 106 | + By("Initializing the runtime") |
| 107 | + optionsRegistry := &markers.Registry{} |
| 108 | + Expect(genall.RegisterOptionsMarkers(optionsRegistry)).To(Succeed()) |
| 109 | + Expect(optionsRegistry.Register(markers.Must(markers.MakeDefinition("crd", markers.DescribesPackage, crd.Generator{})))).To(Succeed()) |
| 110 | + Expect(optionsRegistry.Register(markers.Must(markers.MakeDefinition("applyconfiguration", markers.DescribesPackage, Generator{})))).To(Succeed()) |
| 111 | + |
| 112 | + rt, err := genall.FromOptions(optionsRegistry, []string{ |
| 113 | + "crd:allowDangerousTypes=true,ignoreUnexportedFields=true", // Run another generator first to make sure they don't interfere; see also: the comment on cronjob_types.go:UntypedBlob |
| 114 | + "applyconfiguration", |
| 115 | + "paths=./api/v1", |
| 116 | + }) |
| 117 | + Expect(err).NotTo(HaveOccurred()) |
| 118 | + |
| 119 | + rt.OutputRules = genall.OutputRules{Default: output} |
| 120 | + |
| 121 | + originalFS := os.DirFS(filepath.Join(originalCWD, cronjobDir)) |
| 122 | + tmpFS := os.DirFS(".") |
| 123 | + |
| 124 | + By("Running the generator") |
| 125 | + hadErrs := rt.Run() |
| 126 | + |
| 127 | + By("Checking for generation errors") |
| 128 | + Expect(hadErrs).To(BeFalse(), "Generator should run without errors") |
| 129 | + |
| 130 | + filesInOriginal := make(map[string][]byte) |
| 131 | + originalFileNames := sets.New[string]() |
| 132 | + Expect(fs.WalkDir(originalFS, filepath.Join("api/v1", applyConfigurationDir), func(path string, d fs.DirEntry, err error) error { |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + if d.IsDir() { |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + data, err := os.ReadFile(filepath.Join(originalCWD, cronjobDir, path)) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("error reading file %s: %w", path, err) |
| 144 | + } |
| 145 | + |
| 146 | + // Record the path without the path prefix for comparison later. |
| 147 | + path = strings.TrimPrefix(path, filepath.Join("api/v1", applyConfigurationDir)+"/") |
| 148 | + originalFileNames.Insert(path) |
| 149 | + filesInOriginal[path] = data |
| 150 | + return nil |
| 151 | + })).To(Succeed()) |
| 152 | + |
| 153 | + filesInOutput := make(map[string][]byte) |
| 154 | + outputFileNames := sets.New[string]() |
| 155 | + Expect(fs.WalkDir(tmpFS, filepath.Join("api/v1", outputPackage), func(path string, d fs.DirEntry, err error) error { |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + if d.IsDir() { |
| 161 | + return nil |
| 162 | + } |
| 163 | + |
| 164 | + data, err := os.ReadFile(path) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("error reading file %s: %w", path, err) |
| 167 | + } |
| 168 | + |
| 169 | + // Record the path without the path prefix for comparison later. |
| 170 | + path = strings.TrimPrefix(path, filepath.Join("api/v1", outputPackage)+"/") |
| 171 | + outputFileNames.Insert(path) |
| 172 | + filesInOutput[path] = data |
| 173 | + return nil |
| 174 | + })).To(Succeed()) |
| 175 | + |
| 176 | + // // Every file should be in both sets, check for files not in both sets. |
| 177 | + Expect(outputFileNames.UnsortedList()).To(ConsistOf(originalFileNames.UnsortedList()), "Generated files should match the checked in files") |
| 178 | + |
| 179 | + for name, content := range filesInOriginal { |
| 180 | + // If the output package uses a relative path we need to remove the "../" from the package name. |
| 181 | + outputPackageName := strings.ReplaceAll(outputPackage, "../", "") |
| 182 | + |
| 183 | + // Make sure the package string is correct for the newly generated content. |
| 184 | + content = []byte(strings.Replace(string(content), "package applyconfiguration", fmt.Sprintf("package %s", outputPackageName), 1)) |
| 185 | + |
| 186 | + // Make sure the import paths are correct for the newly generated content. |
| 187 | + content = []byte(strings.ReplaceAll(string(content), "testdata/cronjob/api/v1/applyconfiguration", filepath.Join("testdata/cronjob/api/v1", outputPackage))) |
| 188 | + |
| 189 | + Expect(string(filesInOutput[name])).To(BeComparableTo(string(content)), "Generated files should match the checked in files, diff found in %s", name) |
| 190 | + } |
| 191 | + }, |
| 192 | + Entry("with the default applyconfiguration output package", "applyconfiguration"), |
| 193 | + Entry("with the an alternative output package", "other"), |
| 194 | + Entry("with a package outside of the current directory", "../../clients"), |
| 195 | + ) |
| 196 | +}) |
| 197 | + |
| 198 | +func replaceOutputPkgMarker(dir string, newOutputPackage string) error { |
| 199 | + f, err := os.Open(filepath.Join(dir, "groupversion_info.go")) |
| 200 | + if err != nil { |
| 201 | + return fmt.Errorf("error opening groupversion_info.go: %w", err) |
| 202 | + } |
| 203 | + defer f.Close() |
| 204 | + |
| 205 | + data, err := io.ReadAll(f) |
| 206 | + if err != nil { |
| 207 | + return fmt.Errorf("error reading groupversion_info.go: %w", err) |
| 208 | + } |
| 209 | + |
| 210 | + newData := strings.Replace(string(data), "// +kubebuilder:ac:output:package=\"applyconfiguration\"", fmt.Sprintf("// +kubebuilder:ac:output:package=\"%s\"", newOutputPackage), 1) |
| 211 | + |
| 212 | + if err := os.WriteFile(filepath.Join(dir, "groupversion_info.go"), []byte(newData), 0644); err != nil { |
| 213 | + return fmt.Errorf("error writing groupversion_info.go: %w", err) |
| 214 | + } |
| 215 | + |
| 216 | + return nil |
| 217 | +} |
0 commit comments