|
| 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 crd_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 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 | + 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 Generation golang files", func() { |
| 36 | + var ( |
| 37 | + ctx, ctx2 *genall.GenerationContext |
| 38 | + out *outputRule |
| 39 | + |
| 40 | + genDir = filepath.Join("testdata", "multiple_files") |
| 41 | + ) |
| 42 | + |
| 43 | + BeforeEach(func() { |
| 44 | + By("switching into testdata to appease go modules") |
| 45 | + cwd, err := os.Getwd() |
| 46 | + Expect(err).NotTo(HaveOccurred()) |
| 47 | + Expect(os.Chdir(genDir)).To(Succeed()) // go modules are directory-sensitive |
| 48 | + defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }() |
| 49 | + |
| 50 | + By("loading the roots") |
| 51 | + pkgs, err := loader.LoadRoots("file_one.go") |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + Expect(pkgs).To(HaveLen(1)) |
| 54 | + Expect(pkgs[0].GoFiles).To(HaveLen(1)) |
| 55 | + pkgs2, err := loader.LoadRoots("file_two.go", "file_two_reference.go") |
| 56 | + Expect(err).NotTo(HaveOccurred()) |
| 57 | + Expect(pkgs2).To(HaveLen(1)) |
| 58 | + Expect(pkgs2[0].GoFiles).To(HaveLen(2)) |
| 59 | + |
| 60 | + By("setup up the context") |
| 61 | + reg := &markers.Registry{} |
| 62 | + Expect(crdmarkers.Register(reg)).To(Succeed()) |
| 63 | + out = &outputRule{ |
| 64 | + buf: &bytes.Buffer{}, |
| 65 | + } |
| 66 | + ctx = &genall.GenerationContext{ |
| 67 | + Collector: &markers.Collector{Registry: reg}, |
| 68 | + Roots: pkgs, |
| 69 | + Checker: &loader.TypeChecker{}, |
| 70 | + OutputRule: out, |
| 71 | + } |
| 72 | + ctx2 = &genall.GenerationContext{ |
| 73 | + Collector: &markers.Collector{Registry: reg}, |
| 74 | + Roots: pkgs2, |
| 75 | + Checker: &loader.TypeChecker{}, |
| 76 | + OutputRule: out, |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + It("should have deterministic output for single golang file", func() { |
| 81 | + By("calling Generate on single golang file") |
| 82 | + gen := &crd.Generator{ |
| 83 | + CRDVersions: []string{"v1"}, |
| 84 | + } |
| 85 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 86 | + |
| 87 | + By("loading the desired YAML") |
| 88 | + expectedFileOne, err := os.ReadFile(filepath.Join(genDir, "one.example.com.yaml")) |
| 89 | + Expect(err).NotTo(HaveOccurred()) |
| 90 | + expectedFileOne = fixAnnotations(expectedFileOne) |
| 91 | + expectedOut := string(expectedFileOne) |
| 92 | + Expect(out.buf.String()).To(Equal(expectedOut), cmp.Diff(out.buf.String(), expectedOut)) |
| 93 | + }) |
| 94 | + It("should have deterministic output for multiple golang files referencing other types", func() { |
| 95 | + By("calling Generate on two golang files") |
| 96 | + gen := &crd.Generator{ |
| 97 | + CRDVersions: []string{"v1"}, |
| 98 | + } |
| 99 | + Expect(gen.Generate(ctx2)).NotTo(HaveOccurred()) |
| 100 | + |
| 101 | + By("loading the desired YAML file") |
| 102 | + expectedFileTwo, err := os.ReadFile(filepath.Join(genDir, "two.example.com.yaml")) |
| 103 | + Expect(err).NotTo(HaveOccurred()) |
| 104 | + expectedFileTwo = fixAnnotations(expectedFileTwo) |
| 105 | + expectedOut := string(expectedFileTwo) |
| 106 | + Expect(out.buf.String()).To(Equal(expectedOut), cmp.Diff(out.buf.String(), expectedOut)) |
| 107 | + }) |
| 108 | +}) |
0 commit comments