github.com/matthchr/controller-tools@v0.3.1-0.20200602225425-d33ced351ff8/pkg/webhook/parser_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 webhook_test
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"path"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  	. "github.com/onsi/ginkgo"
    27  	. "github.com/onsi/gomega"
    28  	admissionreg "k8s.io/api/admissionregistration/v1beta1"
    29  	"sigs.k8s.io/yaml"
    30  
    31  	"github.com/matthchr/controller-tools/pkg/genall"
    32  	"github.com/matthchr/controller-tools/pkg/loader"
    33  	"github.com/matthchr/controller-tools/pkg/markers"
    34  	"github.com/matthchr/controller-tools/pkg/webhook"
    35  )
    36  
    37  var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", func() {
    38  	It("should properly generate the webhook definition", func() {
    39  		// TODO(directxman12): test generation across multiple versions (right
    40  		// now, we're trusting k/k's conversion code, though, which is probably
    41  		// fine for the time being)
    42  		By("switching into testdata to appease go modules")
    43  		cwd, err := os.Getwd()
    44  		Expect(err).NotTo(HaveOccurred())
    45  		Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive
    46  		defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
    47  
    48  		By("loading the roots")
    49  		pkgs, err := loader.LoadRoots(".")
    50  		Expect(err).NotTo(HaveOccurred())
    51  		Expect(pkgs).To(HaveLen(1))
    52  
    53  		By("setting up the parser")
    54  		reg := &markers.Registry{}
    55  		Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed())
    56  
    57  		By("requesting that the manifest be generated")
    58  		outputDir, err := ioutil.TempDir("", "webhook-integration-test")
    59  		Expect(err).NotTo(HaveOccurred())
    60  		defer os.RemoveAll(outputDir)
    61  		Expect(webhook.Generator{}.Generate(&genall.GenerationContext{
    62  			Collector:  &markers.Collector{Registry: reg},
    63  			Roots:      pkgs,
    64  			OutputRule: genall.OutputToDirectory(outputDir),
    65  		}))
    66  
    67  		By("loading the generated YAML")
    68  		actualFile, err := ioutil.ReadFile(path.Join(outputDir, "manifests.yaml"))
    69  		Expect(err).NotTo(HaveOccurred())
    70  		actualMutating, actualValidating := unmarshalBoth(actualFile)
    71  
    72  		By("loading the desired YAML")
    73  		expectedFile, err := ioutil.ReadFile("manifests.yaml")
    74  		Expect(err).NotTo(HaveOccurred())
    75  		expectedMutating, expectedValidating := unmarshalBoth(expectedFile)
    76  
    77  		By("comparing the two")
    78  		assertSame := func(actual, expected interface{}) {
    79  			ExpectWithOffset(1, actual).To(Equal(expected), "type not as expected, check pkg/webhook/testdata/README.md for more details.\n\nDiff:\n\n%s", cmp.Diff(actual, expected))
    80  		}
    81  		assertSame(actualMutating, expectedMutating)
    82  		assertSame(actualValidating, expectedValidating)
    83  	})
    84  })
    85  
    86  func unmarshalBoth(in []byte) (mutating admissionreg.MutatingWebhookConfiguration, validating admissionreg.ValidatingWebhookConfiguration) {
    87  	documents := bytes.Split(in, []byte("\n---\n"))[1:]
    88  	ExpectWithOffset(1, documents).To(HaveLen(2), "expected two documents in file, found %d", len(documents))
    89  
    90  	ExpectWithOffset(1, yaml.UnmarshalStrict(documents[0], &mutating)).To(Succeed(), "expected the first document in the file to be a mutating webhook configuration")
    91  	ExpectWithOffset(1, yaml.UnmarshalStrict(documents[1], &validating)).To(Succeed(), "expected the second document in the file to be a validating webhook configuration")
    92  	return
    93  }