forked from kubernetes-sigs/controller-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_integration_test.go
110 lines (90 loc) · 2.97 KB
/
gen_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
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"
"io"
"io/ioutil"
"os"
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-tools/pkg/crd"
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
)
var _ = Describe("CRD Generation proper defaulting", func() {
var ctx *genall.GenerationContext
var out *outputRule
BeforeEach(func() {
By("switching into testdata to appease go modules")
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir("./testdata/gen")).To(Succeed()) // go modules are directory-sensitive
defer func() { Expect(os.Chdir(cwd)).To(Succeed()) }()
By("loading the roots")
pkgs, err := loader.LoadRoots(".")
Expect(err).NotTo(HaveOccurred())
Expect(pkgs).To(HaveLen(1))
By("settup up the context")
reg := &markers.Registry{}
Expect(crdmarkers.Register(reg)).To(Succeed())
out = &outputRule{
buf: &bytes.Buffer{},
}
ctx = &genall.GenerationContext{
Collector: &markers.Collector{Registry: reg},
Roots: pkgs,
Checker: &loader.TypeChecker{},
OutputRule: out,
}
})
It("should strip v1beta1 CRDs of default fields", func() {
By("calling Generate")
gen := &crd.Generator{
CRDVersions: []string{"v1beta1"},
}
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
By("loading the desired YAML")
expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1beta1.yaml")
Expect(err).NotTo(HaveOccurred())
By("comparing the two")
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile)))
})
It("should not strip v1 CRDs of default fields", func() {
By("calling Generate")
gen := &crd.Generator{
CRDVersions: []string{"v1"},
}
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
By("loading the desired YAML")
expectedFile, err := ioutil.ReadFile("./testdata/gen/foo_crd_v1.yaml")
Expect(err).NotTo(HaveOccurred())
By("comparing the two")
Expect(out.buf.String()).To(Equal(string(expectedFile)), cmp.Diff(out.buf.String(), string(expectedFile)))
})
})
type outputRule struct {
buf *bytes.Buffer
}
func (o *outputRule) Open(_ *loader.Package, itemPath string) (io.WriteCloser, error) {
return nopCloser{o.buf}, nil
}
type nopCloser struct {
io.Writer
}
func (n nopCloser) Close() error {
return nil
}