github.com/christarazi/controller-tools@v0.3.1-0.20210907042920-aa94049173f8/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/ioutil"
    21  	"os"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	"golang.org/x/tools/go/packages"
    27  
    28  	"sigs.k8s.io/controller-tools/pkg/deepcopy"
    29  	"sigs.k8s.io/controller-tools/pkg/loader"
    30  	"sigs.k8s.io/controller-tools/pkg/markers"
    31  )
    32  
    33  func packageErrors(pkg *loader.Package, filterKinds ...packages.ErrorKind) error {
    34  	toSkip := make(map[packages.ErrorKind]struct{})
    35  	for _, errKind := range filterKinds {
    36  		toSkip[errKind] = struct{}{}
    37  	}
    38  	var outErr error
    39  	packages.Visit([]*packages.Package{pkg.Package}, nil, func(pkgRaw *packages.Package) {
    40  		for _, err := range pkgRaw.Errors {
    41  			if _, skip := toSkip[err.Kind]; skip {
    42  				continue
    43  			}
    44  			outErr = err
    45  		}
    46  	})
    47  	return outErr
    48  }
    49  
    50  var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func() {
    51  	It("should properly generate and flatten the rewritten CronJob schema", func() {
    52  		By("switching into testdata to appease go modules")
    53  		cwd, err := os.Getwd()
    54  		Expect(err).NotTo(HaveOccurred())
    55  		Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive
    56  		defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
    57  
    58  		By("loading the roots")
    59  		pkgs, err := loader.LoadRoots(".")
    60  		Expect(err).NotTo(HaveOccurred())
    61  		Expect(pkgs).To(HaveLen(1))
    62  		cronJobPkg := pkgs[0]
    63  
    64  		By("setting up the generation context")
    65  		collector := &markers.Collector{Registry: &markers.Registry{}}
    66  		Expect(deepcopy.Generator{}.RegisterMarkers(collector.Registry)).To(Succeed())
    67  		checker := &loader.TypeChecker{}
    68  		ctx := &deepcopy.ObjectGenCtx{
    69  			Collector: collector,
    70  			Checker:   checker,
    71  		}
    72  
    73  		By("requesting that types be generated")
    74  		outContents := ctx.GenerateForPackage(cronJobPkg)
    75  
    76  		By("checking that no errors occurred along the way (expect for type errors)")
    77  		Expect(packageErrors(cronJobPkg, packages.TypeError)).NotTo(HaveOccurred())
    78  
    79  		By("checking that we got output contents")
    80  		Expect(outContents).NotTo(BeNil())
    81  
    82  		By("loading the desired code")
    83  		expectedFile, err := ioutil.ReadFile("zz_generated.deepcopy.go")
    84  		Expect(err).NotTo(HaveOccurred())
    85  
    86  		By("comparing the two")
    87  		Expect(outContents).To(Equal(expectedFile), "generated code not as expected, check pkg/deepcopy/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(outContents, expectedFile))
    88  	})
    89  })