-
Notifications
You must be signed in to change notification settings - Fork 423
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
Changes from all commits
4f8bb15
2d52ffd
8a542f9
40ed028
25fb3dc
48c5c38
e378b3c
52c02bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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} | ||
|
||
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)) | ||
}) | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whenever we generate yaml manifests covering 2 versions (here 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 |
There was a problem hiding this comment.
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.