forked from kubernetes-sigs/controller-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_suite_test.go
108 lines (89 loc) · 3.01 KB
/
builder_suite_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
/*
Copyright 2018 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 builder
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/testing_frameworks/integration/addr"
)
func TestBuilder(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t, "application Suite", []Reporter{envtest.NewlineReporter{}})
}
var testenv *envtest.Environment
var cfg *rest.Config
var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
testenv = &envtest.Environment{}
addCRDToEnvironment(testenv,
testDefaulterGVK,
testValidatorGVK,
testDefaultValidatorGVK)
var err error
cfg, err = testenv.Start()
Expect(err).NotTo(HaveOccurred())
// Prevent the metrics listener being created
metrics.DefaultBindAddress = "0"
webhook.DefaultPort, _, err = addr.Suggest()
Expect(err).NotTo(HaveOccurred())
close(done)
}, 60)
var _ = AfterSuite(func() {
Expect(testenv.Stop()).To(Succeed())
// Put the DefaultBindAddress back
metrics.DefaultBindAddress = ":8080"
// Change the webhook.DefaultPort back to the original default.
webhook.DefaultPort = 443
})
func addCRDToEnvironment(env *envtest.Environment, gvks ...schema.GroupVersionKind) {
for _, gvk := range gvks {
plural, singular := meta.UnsafeGuessKindToResource(gvk)
crd := &apiextensionsv1beta1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: "apiextensions.k8s.io",
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: plural.Resource + "." + gvk.Group,
},
Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
Group: gvk.Group,
Version: gvk.Version,
Names: apiextensionsv1beta1.CustomResourceDefinitionNames{
Plural: plural.Resource,
Singular: singular.Resource,
Kind: gvk.Kind,
},
Versions: []apiextensionsv1beta1.CustomResourceDefinitionVersion{
{
Name: gvk.Version,
Served: true,
Storage: true,
},
},
},
}
env.CRDInstallOptions.CRDs = append(env.CRDInstallOptions.CRDs, crd)
}
}