Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ CRD generation integration tests #365

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions pkg/crd/gen_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package crd_test

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/genall"
)

type crdGenParams struct {
// rootPaths is the relative path to the CRD go models
rootPaths string
// expectedFile is the relative path to the expected CRD yaml file
expectedFile string
// generator to use
generator crd.Generator
}

var _ = Describe("CRD generation", func() {

Context("when there is a single api version", func() {
Context("generate cronjob v1", func() {
Context("using crd v1beta1", func() {
testCrdGeneration(crdGenParams{
rootPaths: "./cronjob/v1/...",
expectedFile: "./cronjob/v1/cronjob_crdv1beta1.yaml",
generator: crd.Generator{
CRDVersions: []string{"v1beta1"},
},
})
})
Context("using crd v1", func() {
testCrdGeneration(crdGenParams{
rootPaths: "./cronjob/v1/...",
expectedFile: "./cronjob/v1/cronjob_crdv1.yaml",
generator: crd.Generator{
CRDVersions: []string{"v1"},
},
})
})
})
Context("generate cronjob v2", func() {
Context("using crd v1beta1", func() {
testCrdGeneration(crdGenParams{
rootPaths: "./cronjob/v2/...",
expectedFile: "./cronjob/v2/cronjob_crdv1beta1.yaml",
generator: crd.Generator{
CRDVersions: []string{"v1beta1"},
},
})
})
Context("using crd v1", func() {
testCrdGeneration(crdGenParams{
rootPaths: "./cronjob/v2/...",
expectedFile: "./cronjob/v2/cronjob_crdv1.yaml",
generator: crd.Generator{
CRDVersions: []string{"v1"},
},
})
})
})
})

Context("when there are multiple api versions (cronjob v1 + v2)", func() {
rootPaths := "./cronjob/..."
expectedYamlPath := func(crdVersion string, feature string) string {
if feature != "" {
feature = fmt.Sprintf("_%s", feature)
}
return fmt.Sprintf("./cronjob/cronjob_crd%s%s.yaml", crdVersion, feature)
}

Context("using crd v1", func() {
crdVersion := "v1"
Context("with default options", func() {
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath(crdVersion, ""),
generator: crd.Generator{
CRDVersions: []string{crdVersion},
},
})
})
Context("with maxDescLen=10", func() {
maxDescLen := 10
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath(crdVersion, "maxdesclen"),
generator: crd.Generator{
CRDVersions: []string{crdVersion},
MaxDescLen: &maxDescLen,
},
})
})
})

Context("using crd v1beta1", func() {
crdVersion := "v1beta1"
Context("with default options", func() {
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath(crdVersion, ""),
generator: crd.Generator{
CRDVersions: []string{crdVersion},
},
})
})
Context("with maxDescLen=10", func() {
maxDescLen := 10
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath(crdVersion, "maxdesclen"),
generator: crd.Generator{
CRDVersions: []string{crdVersion},
MaxDescLen: &maxDescLen,
},
})
})
Context("with trivialVersions=true", func() {
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath(crdVersion, "trivial"),
generator: crd.Generator{
CRDVersions: []string{crdVersion},
TrivialVersions: true,
},
})
})
Context("with preserveUnknownFields=false", func() {
preserveUnknownFields := false
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath(crdVersion, "preserve_false"),
generator: crd.Generator{
CRDVersions: []string{crdVersion},
PreserveUnknownFields: &preserveUnknownFields,
},
})
})
})

Context("when crdVersions=v1;v1beta1", func() {
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath("v1v1beta1", ""),
generator: crd.Generator{
CRDVersions: []string{"v1", "v1beta1"},
},
})
})

Context("when crdVersions is empty (default to v1beta1)", func() {
testCrdGeneration(crdGenParams{
rootPaths: rootPaths,
expectedFile: expectedYamlPath("v1beta1", ""),
generator: crd.Generator{
CRDVersions: []string{},
},
})
})
})
})

func testCrdGeneration(params crdGenParams) {
It("should generate the expected CRD", func() {
By("switching into testdata to appease go modules")
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()

By("initializing the generator")
var gen genall.Generator = params.generator
rt, err := genall.Generators{&gen}.ForRoots(params.rootPaths)
Expect(err).NotTo(HaveOccurred())
var buf bytes.Buffer
rt.OutputRules.Default = genall.OutputToBuffer{Buffer: &buf}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that makes sense we can probably adapt other integration tests to also not use temporary files.


By("running the generator")
Expect(rt.Run()).To(BeFalse(), "unexpectedly had errors")

By("loading the actual and expected outputs")
actualStr := buf.String()
expected, err := ioutil.ReadFile(params.expectedFile)
Expect(err).NotTo(HaveOccurred())
expectedStr := string(expected)

By("patching version annotation in the actual content")
// patch the version annotation that appears as "(unknown)" in the test-generated content
Expect(actualStr).To(ContainSubstring("(unknown)"))
actualStr = strings.ReplaceAll(actualStr, "(unknown)", "(devel)")

By("comparing actual and expected CRD output")
Expect(actualStr).To(Equal(expectedStr), "contents not as expected\n\nDiff:\n\n%s", cmp.Diff(actualStr, expectedStr))
})
}
4 changes: 2 additions & 2 deletions pkg/crd/parser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
By("switching into testdata to appease go modules")
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir("./testdata")).To(Succeed()) // go modules are directory-sensitive
Expect(os.Chdir("./testdata/cronjob/v1")).To(Succeed()) // go modules are directory-sensitive
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()

By("loading the roots")
Expand Down Expand Up @@ -91,7 +91,7 @@ var _ = Describe("CRD Generation From Parsing to CustomResourceDefinition", func
Expect(parser.CustomResourceDefinitions).To(HaveKey(groupKind))

By("loading the desired YAML")
expectedFile, err := ioutil.ReadFile("testdata.kubebuilder.io_cronjobs.yaml")
expectedFile, err := ioutil.ReadFile("cronjob_crdv1.yaml")
Expect(err).NotTo(HaveOccurred())

By("parsing the desired YAML")
Expand Down
27 changes: 27 additions & 0 deletions pkg/crd/testdata/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
GEN?=../../../.run-controller-gen.sh output:stdout

all:
# Generate separate CRDs for cronjob v1 and v2
# - using crd v1
$(GEN) crd:crdVersions=v1 paths=./cronjob/v1 > cronjob/v1/cronjob_crdv1.yaml
$(GEN) crd:crdVersions=v1 paths=./cronjob/v2 > cronjob/v2/cronjob_crdv1.yaml
# - using crd v1beta1
$(GEN) crd:crdVersions=v1beta1 paths=./cronjob/v1 > cronjob/v1/cronjob_crdv1beta1.yaml
$(GEN) crd:crdVersions=v1beta1 paths=./cronjob/v2 > cronjob/v2/cronjob_crdv1beta1.yaml
# Generate a combined CRD of Cronjob v1 & v2
# - using crd v1
$(GEN) crd:crdVersions=v1 paths=./cronjob/... > cronjob/cronjob_crdv1.yaml
Copy link
Author

@sebgl sebgl Nov 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever we generate yaml manifests covering 2 versions (here cronjob/v1 and cronjob/v2), the same content appears twice in the generated output. In most situations the last content replaces the first one, but this is not the case here when using output:stdout.
If you look at the corresponding YAML files you'll see they contain twice the same CRD.

This should be fixed with #350. These tests at least served to prove that :)

# -- with maxDescLen=10
$(GEN) crd:crdVersions=v1,maxDescLen=10 paths=./cronjob/... > cronjob/cronjob_crdv1_maxdesclen.yaml
# - using crd v1beta1
$(GEN) crd:crdVersions=v1beta1 paths=./cronjob/... > cronjob/cronjob_crdv1beta1.yaml
# -- with maxDescLen=10
$(GEN) crd:crdVersions=v1beta1,maxDescLen=10 paths=./cronjob/... > cronjob/cronjob_crdv1beta1_maxdesclen.yaml
# -- with trivialVersions=true
$(GEN) crd:crdVersions=v1beta1,trivialVersions=true paths=./cronjob/... > cronjob/cronjob_crdv1beta1_trivial.yaml
# -- with preserveUnknownFields=false
$(GEN) crd:crdVersions=v1beta1,preserveUnknownFields=false paths=./cronjob/... > cronjob/cronjob_crdv1beta1_preserve_false.yaml
# - using crd v1 and crd v1beta1
$(GEN) "crd:crdVersions=v1;v1beta1" paths=./cronjob/... > cronjob/cronjob_crdv1v1beta1.yaml

.PHONY: all
16 changes: 3 additions & 13 deletions pkg/crd/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,9 @@ on the CronJob tutorial from the [KubeBuilder
Book](https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html), but with added
fields to test additional markers and generation behavior.

If you add a new marker, re-generate the golden output file,
`testdata.kubebuilder.io_cronjobs.yaml`, with (if you have the latest
controller-gen on your path):

```bash
go generate
```

or, if you don't have the latest controller-gen on your path, use:

```bash
$ /path/to/current/build/of/controller-gen crd paths=. output:dir=.
```
It's *highly* unlikely that the generated expected manifests will
ever change from these -- if they do, you've probably broken something.
Nonetheless, you can regenerate output using `make`.

Make sure you review the diff to ensure that it only contains the desired
changes!
Expand Down
Loading