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