sigs.k8s.io/controller-tools@v0.15.1-0.20240515195456-85686cb69316/pkg/deepcopy/deepcopy_integration_test.go (about) 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 deepcopy_test 18 19 import ( 20 "io" 21 "os" 22 "sort" 23 24 "github.com/google/go-cmp/cmp" 25 . "github.com/onsi/ginkgo" 26 . "github.com/onsi/gomega" 27 28 "sigs.k8s.io/controller-tools/pkg/crd" 29 "sigs.k8s.io/controller-tools/pkg/deepcopy" 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 type outputToMap map[string]*outputFile 36 37 // Open implements genall.OutputRule. 38 func (m outputToMap) Open(_ *loader.Package, path string) (io.WriteCloser, error) { 39 if _, ok := m[path]; !ok { 40 m[path] = &outputFile{} 41 } 42 return m[path], nil 43 } 44 45 func (m outputToMap) fileList() []string { 46 ret := make([]string, 0, len(m)) 47 for path := range m { 48 ret = append(ret, path) 49 } 50 sort.Strings(ret) 51 return ret 52 } 53 54 type outputFile struct { 55 contents []byte 56 } 57 58 func (o *outputFile) Write(p []byte) (int, error) { 59 o.contents = append(o.contents, p...) 60 return len(p), nil 61 } 62 63 func (o *outputFile) Close() error { 64 return nil 65 } 66 67 var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func() { 68 It("should properly generate and flatten the rewritten CronJob schema", func() { 69 By("switching into testdata to appease go modules") 70 cwd, err := os.Getwd() 71 Expect(err).NotTo(HaveOccurred()) 72 Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive 73 defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }() 74 75 output := make(outputToMap) 76 77 By("initializing the runtime") 78 optionsRegistry := &markers.Registry{} 79 Expect(optionsRegistry.Register(markers.Must(markers.MakeDefinition("crd", markers.DescribesPackage, crd.Generator{})))).To(Succeed()) 80 Expect(optionsRegistry.Register(markers.Must(markers.MakeDefinition("object", markers.DescribesPackage, deepcopy.Generator{})))).To(Succeed()) 81 rt, err := genall.FromOptions(optionsRegistry, []string{ 82 "crd", // Run another generator first to make sure they don't interfere; see also: the comment on cronjob_types.go:UntypedBlob 83 "object", 84 }) 85 Expect(err).NotTo(HaveOccurred()) 86 rt.OutputRules = genall.OutputRules{Default: output} 87 88 By("running the generator and checking for errors") 89 hadErrs := rt.Run() 90 91 By("checking that we got output contents") 92 Expect(output.fileList()).To(ContainElement("zz_generated.deepcopy.go")) // Don't use HaveKey--the output is too verbose to be usable 93 outFile := output["zz_generated.deepcopy.go"] 94 Expect(outFile).NotTo(BeNil()) 95 outContents := outFile.contents 96 Expect(outContents).NotTo(BeNil()) 97 98 By("loading the desired code") 99 expectedFile, err := os.ReadFile("zz_generated.deepcopy.go") 100 Expect(err).NotTo(HaveOccurred()) 101 102 By("comparing the two") 103 Expect(string(outContents)).To(Equal(string(expectedFile)), "generated code not as expected, check pkg/deepcopy/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(outContents, expectedFile)) 104 105 By("checking for errors") 106 Expect(hadErrs).To(BeFalse()) 107 }) 108 })