|
| 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 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + "sigs.k8s.io/controller-tools/pkg/crd" |
| 28 | + crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers" |
| 29 | + "sigs.k8s.io/controller-tools/pkg/genall" |
| 30 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 31 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("CRD Generation proper defaulting", func() { |
| 35 | + var ctx *genall.GenerationContext |
| 36 | + var out *outputRule |
| 37 | + BeforeEach(func() { |
| 38 | + By("switching into testdata to appease go modules") |
| 39 | + cwd, err := os.Getwd() |
| 40 | + Expect(err).NotTo(HaveOccurred()) |
| 41 | + Expect(os.Chdir("./testdata/gen")).To(Succeed()) // go modules are directory-sensitive |
| 42 | + defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }() |
| 43 | + |
| 44 | + By("loading the roots") |
| 45 | + pkgs, err := loader.LoadRoots(".") |
| 46 | + Expect(err).NotTo(HaveOccurred()) |
| 47 | + Expect(pkgs).To(HaveLen(1)) |
| 48 | + |
| 49 | + By("settup up the context") |
| 50 | + reg := &markers.Registry{} |
| 51 | + Expect(crdmarkers.Register(reg)).To(Succeed()) |
| 52 | + out = &outputRule{ |
| 53 | + buf: &bytes.Buffer{}, |
| 54 | + } |
| 55 | + ctx = &genall.GenerationContext{ |
| 56 | + Collector: &markers.Collector{Registry: reg}, |
| 57 | + Roots: pkgs, |
| 58 | + Checker: &loader.TypeChecker{}, |
| 59 | + OutputRule: out, |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + It("should strip v1beta1 CRDs of default fields", func() { |
| 64 | + By("calling Generate") |
| 65 | + gen := &crd.Generator{ |
| 66 | + CRDVersions: []string{"v1beta1"}, |
| 67 | + } |
| 68 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 69 | + |
| 70 | + By("loading the desired YAML") |
| 71 | + expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1beta1.yaml") |
| 72 | + Expect(err).NotTo(HaveOccurred()) |
| 73 | + |
| 74 | + By("comparing the two") |
| 75 | + Expect(out.buf.Bytes()).To(Equal(expectedFile)) |
| 76 | + |
| 77 | + }) |
| 78 | + |
| 79 | + It("should not strip v1 CRDs of default fields", func() { |
| 80 | + By("calling Generate") |
| 81 | + gen := &crd.Generator{ |
| 82 | + CRDVersions: []string{"v1"}, |
| 83 | + } |
| 84 | + Expect(gen.Generate(ctx)).NotTo(HaveOccurred()) |
| 85 | + |
| 86 | + By("loading the desired YAML") |
| 87 | + expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1.yaml") |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | + |
| 90 | + By("comparing the two") |
| 91 | + Expect(out.buf.Bytes()).To(Equal(expectedFile)) |
| 92 | + |
| 93 | + }) |
| 94 | +}) |
| 95 | + |
| 96 | +type outputRule struct { |
| 97 | + buf *bytes.Buffer |
| 98 | +} |
| 99 | + |
| 100 | +func (o *outputRule) Open(_ *loader.Package, itemPath string) (io.WriteCloser, error) { |
| 101 | + return nopCloser{o.buf}, nil |
| 102 | +} |
| 103 | + |
| 104 | +type nopCloser struct { |
| 105 | + io.Writer |
| 106 | +} |
| 107 | + |
| 108 | +func (n nopCloser) Close() error { |
| 109 | + return nil |
| 110 | +} |
0 commit comments