github.com/minio/controller-tools@v0.4.7/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  	admissionregv1 "k8s.io/api/admissionregistration/v1"
    29  	admissionregv1beta1 "k8s.io/api/admissionregistration/v1beta1"
    30  	"sigs.k8s.io/yaml"
    31  
    32  	"github.com/minio/controller-tools/pkg/genall"
    33  	"github.com/minio/controller-tools/pkg/loader"
    34  	"github.com/minio/controller-tools/pkg/markers"
    35  	"github.com/minio/controller-tools/pkg/webhook"
    36  )
    37  
    38  var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition", func() {
    39  	assertSame := func(actual, expected interface{}) {
    40  		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))
    41  	}
    42  
    43  	It("should properly generate the webhook definition", func() {
    44  		// TODO(directxman12): test generation across multiple versions (right
    45  		// now, we're trusting k/k's conversion code, though, which is probably
    46  		// fine for the time being)
    47  		By("switching into testdata to appease go modules")
    48  		cwd, err := os.Getwd()
    49  		Expect(err).NotTo(HaveOccurred())
    50  		Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive
    51  		defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
    52  
    53  		By("loading the roots")
    54  		pkgs, err := loader.LoadRoots(".")
    55  		Expect(err).NotTo(HaveOccurred())
    56  		Expect(pkgs).To(HaveLen(1))
    57  
    58  		By("setting up the parser")
    59  		reg := &markers.Registry{}
    60  		Expect(reg.Register(webhook.ConfigDefinition)).To(Succeed())
    61  
    62  		By("requesting that the manifest be generated")
    63  		outputDir, err := ioutil.TempDir("", "webhook-integration-test")
    64  		Expect(err).NotTo(HaveOccurred())
    65  		defer os.RemoveAll(outputDir)
    66  		Expect(webhook.Generator{}.Generate(&genall.GenerationContext{
    67  			Collector:  &markers.Collector{Registry: reg},
    68  			Roots:      pkgs,
    69  			OutputRule: genall.OutputToDirectory(outputDir),
    70  		})).To(Succeed())
    71  
    72  		By("loading the generated v1 YAML")
    73  		actualFile, err := ioutil.ReadFile(path.Join(outputDir, "manifests.yaml"))
    74  		Expect(err).NotTo(HaveOccurred())
    75  		actualMutating, actualValidating := unmarshalBothV1(actualFile)
    76  
    77  		By("loading the desired v1 YAML")
    78  		expectedFile, err := ioutil.ReadFile("manifests.yaml")
    79  		Expect(err).NotTo(HaveOccurred())
    80  		expectedMutating, expectedValidating := unmarshalBothV1(expectedFile)
    81  
    82  		By("comparing the two")
    83  		assertSame(actualMutating, expectedMutating)
    84  		assertSame(actualValidating, expectedValidating)
    85  
    86  		By("loading the generated v1beta1 YAML")
    87  		actualFile, err = ioutil.ReadFile(path.Join(outputDir, "manifests.v1beta1.yaml"))
    88  		Expect(err).NotTo(HaveOccurred())
    89  		actualMutatingV1beta1, actualValidatingV1beta1 := unmarshalBothV1beta1(actualFile)
    90  
    91  		By("loading the desired v1beta1 YAML")
    92  		expectedFile, err = ioutil.ReadFile("manifests.v1beta1.yaml")
    93  		Expect(err).NotTo(HaveOccurred())
    94  		expectedMutatingV1beta1, expectedValidatingV1beta1 := unmarshalBothV1beta1(expectedFile)
    95  
    96  		By("comparing the two")
    97  		assertSame(actualMutatingV1beta1, expectedMutatingV1beta1)
    98  		assertSame(actualValidatingV1beta1, expectedValidatingV1beta1)
    99  	})
   100  })
   101  
   102  func unmarshalBothV1beta1(in []byte) (mutating admissionregv1beta1.MutatingWebhookConfiguration, validating admissionregv1beta1.ValidatingWebhookConfiguration) {
   103  	documents := bytes.Split(in, []byte("\n---\n"))[1:]
   104  	ExpectWithOffset(1, documents).To(HaveLen(2), "expected two documents in file, found %d", len(documents))
   105  
   106  	ExpectWithOffset(1, yaml.UnmarshalStrict(documents[0], &mutating)).To(Succeed(), "expected the first document in the file to be a mutating webhook configuration")
   107  	ExpectWithOffset(1, yaml.UnmarshalStrict(documents[1], &validating)).To(Succeed(), "expected the second document in the file to be a validating webhook configuration")
   108  	return
   109  }
   110  
   111  func unmarshalBothV1(in []byte) (mutating admissionregv1.MutatingWebhookConfiguration, validating admissionregv1.ValidatingWebhookConfiguration) {
   112  	documents := bytes.Split(in, []byte("\n---\n"))[1:]
   113  	ExpectWithOffset(1, documents).To(HaveLen(2), "expected two documents in file, found %d", len(documents))
   114  
   115  	ExpectWithOffset(1, yaml.UnmarshalStrict(documents[0], &mutating)).To(Succeed(), "expected the first document in the file to be a mutating webhook configuration")
   116  	ExpectWithOffset(1, yaml.UnmarshalStrict(documents[1], &validating)).To(Succeed(), "expected the second document in the file to be a validating webhook configuration")
   117  	return
   118  }