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