github.com/waynz0r/controller-tools@v0.4.1-0.20200916220028-16254aeef2d7/pkg/schemapatcher/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 schemapatcher_test
    18  
    19  import (
    20  	"io/ioutil"
    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/genall"
    29  	. "sigs.k8s.io/controller-tools/pkg/schemapatcher"
    30  )
    31  
    32  var _ = Describe("CRD Patching From Parsing to Editing", func() {
    33  	It("should properly generate and patch the test CRDs", func() {
    34  		// TODO(directxman12): I've ported these over from @sttts's tests,
    35  		// but they should really probably not be writing to an actual filesystem
    36  
    37  		By("switching into testdata to appease go modules")
    38  		cwd, err := os.Getwd()
    39  		Expect(err).NotTo(HaveOccurred())
    40  		Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive
    41  		defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
    42  
    43  		By("loading the generation runtime")
    44  		var crdSchemaGen genall.Generator = &Generator{
    45  			ManifestsPath: "./manifests",
    46  		}
    47  		rt, err := genall.Generators{&crdSchemaGen}.ForRoots("./...")
    48  		Expect(err).NotTo(HaveOccurred())
    49  
    50  		outputDir, err := ioutil.TempDir("", "controller-tools-test")
    51  		Expect(err).NotTo(HaveOccurred())
    52  		defer os.RemoveAll(outputDir)
    53  		rt.OutputRules.Default = genall.OutputToDirectory(outputDir)
    54  
    55  		By("running the generator")
    56  		Expect(rt.Run()).To(BeFalse(), "unexpectedly had errors")
    57  
    58  		By("loading the output files")
    59  		expectedFiles, err := ioutil.ReadDir("expected")
    60  		Expect(err).NotTo(HaveOccurred())
    61  
    62  		for _, expectedFile := range expectedFiles {
    63  			By("reading the expected and actual files for " + expectedFile.Name())
    64  			actualContents, err := ioutil.ReadFile(filepath.Join(outputDir, expectedFile.Name()))
    65  			Expect(err).NotTo(HaveOccurred())
    66  
    67  			expectedContents, err := ioutil.ReadFile(filepath.Join("expected", expectedFile.Name()))
    68  			Expect(err).NotTo(HaveOccurred())
    69  
    70  			By("checking that the expected and actual files for " + expectedFile.Name() + " are identical")
    71  			Expect(actualContents).To(Equal(expectedContents), "contents not as expected, check pkg/schemapatcher/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(string(actualContents), string(expectedContents)))
    72  		}
    73  	})
    74  })