/
framework.go
288 lines (236 loc) · 9.44 KB
/
framework.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright 2020 The cert-manager 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 framework
import (
"context"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
api "k8s.io/api/core/v1"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
kscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
apireg "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
gwapi "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
v1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
clientset "github.com/jetstack/cert-manager/pkg/client/clientset/versioned"
certmgrscheme "github.com/jetstack/cert-manager/pkg/client/clientset/versioned/scheme"
"github.com/jetstack/cert-manager/pkg/util/pki"
"github.com/jetstack/cert-manager/test/e2e/framework/addon"
"github.com/jetstack/cert-manager/test/e2e/framework/config"
"github.com/jetstack/cert-manager/test/e2e/framework/helper"
"github.com/jetstack/cert-manager/test/e2e/framework/log"
"github.com/jetstack/cert-manager/test/e2e/framework/util"
"github.com/jetstack/cert-manager/test/e2e/framework/util/errors"
)
// TODO: this really should be done somewhere in cert-manager proper
var Scheme = runtime.NewScheme()
func init() {
kscheme.AddToScheme(Scheme)
certmgrscheme.AddToScheme(Scheme)
apiext.AddToScheme(Scheme)
apireg.AddToScheme(Scheme)
}
// DefaultConfig contains the default shared config the is likely parsed from
// command line arguments.
var DefaultConfig = &config.Config{}
// Framework supports common operations used by e2e tests; it will keep a client & a namespace for you.
type Framework struct {
BaseName string
Config *config.Config
// KubeClientConfig which was used to create the connection.
KubeClientConfig *rest.Config
// Kubernetes API clientsets
KubeClientSet kubernetes.Interface
GWClientSet gwapi.Interface
CertManagerClientSet clientset.Interface
APIExtensionsClientSet apiextcs.Interface
// controller-runtime client for newer controllers
CRClient crclient.Client
// Namespace in which all test resources should reside
Namespace *api.Namespace
// To make sure that this framework cleans up after itself, no matter what,
// we install a Cleanup action before each test and clear it after. If we
// should abort, the AfterSuite hook should run all Cleanup actions.
cleanupHandle CleanupActionHandle
requiredAddons []addon.Addon
helper *helper.Helper
}
// NewDefaultFramework makes a new framework for you, similar to NewFramework.
// It uses the suite-wide 'DefaultConfig' which should be populated by the
// testing harness in test/e2e/e2e_test.go
func NewDefaultFramework(baseName string) *Framework {
return NewFramework(baseName, DefaultConfig)
}
// NewFramework makes a new framework and sets up a BeforeEach/AfterEach for
// you (you can write additional before/after each functions).
// It uses the config provided to it for the duration of the tests.
func NewFramework(baseName string, cfg *config.Config) *Framework {
f := &Framework{
Config: cfg,
BaseName: baseName,
}
f.helper = helper.NewHelper(cfg)
BeforeEach(f.BeforeEach)
AfterEach(f.AfterEach)
return f
}
// BeforeEach gets a client and makes a namespace.
func (f *Framework) BeforeEach() {
f.cleanupHandle = AddCleanupAction(f.AfterEach)
By("Creating a kubernetes client")
kubeConfig, err := util.LoadConfig(f.Config.KubeConfig, f.Config.KubeContext)
Expect(err).NotTo(HaveOccurred())
f.KubeClientConfig = kubeConfig
f.KubeClientSet, err = kubernetes.NewForConfig(kubeConfig)
Expect(err).NotTo(HaveOccurred())
By("Creating an API extensions client")
f.APIExtensionsClientSet, err = apiextcs.NewForConfig(kubeConfig)
Expect(err).NotTo(HaveOccurred())
By("Creating a cert manager client")
f.CertManagerClientSet, err = clientset.NewForConfig(kubeConfig)
Expect(err).NotTo(HaveOccurred())
By("Creating a controller-runtime client")
f.CRClient, err = crclient.New(kubeConfig, crclient.Options{Scheme: Scheme})
Expect(err).NotTo(HaveOccurred())
By("Creating a gateway-api client")
f.GWClientSet, err = gwapi.NewForConfig(kubeConfig)
Expect(err).NotTo(HaveOccurred())
By("Creating a gateway-api class for istio")
f.GWClientSet.NetworkingV1alpha1().GatewayClasses().Create(context.Background(), &gwapiv1alpha1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: "istio",
},
Spec: gwapiv1alpha1.GatewayClassSpec{
Controller: "istio.io/gateway-controller",
},
}, metav1.CreateOptions{})
By("Building a namespace api object")
f.Namespace, err = f.CreateKubeNamespace(f.BaseName)
Expect(err).NotTo(HaveOccurred())
By("Using the namespace " + f.Namespace.Name)
By("Building a ResourceQuota api object")
_, err = f.CreateKubeResourceQuota()
Expect(err).NotTo(HaveOccurred())
f.helper.CMClient = f.CertManagerClientSet
f.helper.KubeClient = f.KubeClientSet
}
// AfterEach deletes the namespace, after reading its events.
func (f *Framework) AfterEach() {
RemoveCleanupAction(f.cleanupHandle)
f.printAddonLogs()
if !f.Config.Cleanup {
return
}
for i := len(f.requiredAddons) - 1; i >= 0; i-- {
a := f.requiredAddons[i]
By("De-provisioning test-scoped addon")
err := a.Deprovision()
Expect(err).NotTo(HaveOccurred())
}
By("Deleting test namespace")
err := f.DeleteKubeNamespace(f.Namespace.Name)
Expect(err).NotTo(HaveOccurred())
}
func (f *Framework) printAddonLogs() {
if CurrentGinkgoTestDescription().Failed {
for _, a := range f.requiredAddons {
if a, ok := a.(loggableAddon); ok {
l, err := a.Logs()
Expect(err).NotTo(HaveOccurred())
for ident, l := range l {
// TODO: replace with writing logs to a file
log.Logf("Got pod logs %q for addon: \n%s", ident, l)
}
}
}
}
}
// RequireGlobalAddon calls Setup on the given addon.
// This should be called in specs or describe blocks that require access to any
// of the global/shared addons in order to ensure their details are available.
// This method should only ever be called with addons defined in the 'addons' that
// are present in the 'globals' variable, as they will not be Provisioned properly
// otherwise.
func (f *Framework) RequireGlobalAddon(a addon.Addon) {
BeforeEach(func() {
By("Setting up access for global shared addon")
err := a.Setup(f.Config)
Expect(err).NotTo(HaveOccurred())
})
}
type loggableAddon interface {
Logs() (map[string]string, error)
}
// RequireAddon calls the Setup and Provision method on the given addon, failing
// the spec if provisioning fails.
func (f *Framework) RequireAddon(a addon.Addon) {
f.requiredAddons = append(f.requiredAddons, a)
BeforeEach(func() {
By("Provisioning test-scoped addon")
err := a.Setup(f.Config)
if errors.IsSkip(err) {
Skipf("Skipping test as addon could not be setup: %v", err)
}
Expect(err).NotTo(HaveOccurred())
err = a.Provision()
Expect(err).NotTo(HaveOccurred())
})
}
func (f *Framework) Helper() *helper.Helper {
return f.helper
}
func (f *Framework) CertificateDurationValid(c *v1.Certificate, duration, fuzz time.Duration) {
By("Verifying TLS certificate exists")
secret, err := f.KubeClientSet.CoreV1().Secrets(f.Namespace.Name).Get(context.TODO(), c.Spec.SecretName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
certBytes, ok := secret.Data[api.TLSCertKey]
if !ok {
Failf("No certificate data found for Certificate %q", c.Name)
}
cert, err := pki.DecodeX509CertificateBytes(certBytes)
Expect(err).NotTo(HaveOccurred())
By("Verifying that the duration is valid")
certDuration := cert.NotAfter.Sub(cert.NotBefore)
if certDuration > (duration+fuzz) || certDuration < duration {
Failf("Expected duration of %s, got %s (fuzz: %s) [NotBefore: %s, NotAfter: %s]", duration, certDuration,
fuzz, cert.NotBefore.Format(time.RFC3339), cert.NotAfter.Format(time.RFC3339))
}
}
func (f *Framework) CertificateRequestDurationValid(c *v1.CertificateRequest, duration, fuzz time.Duration) {
By("Verifying TLS certificate exists")
if len(c.Status.Certificate) == 0 {
Failf("No certificate data found for CertificateRequest %s", c.Name)
}
cert, err := pki.DecodeX509CertificateBytes(c.Status.Certificate)
Expect(err).NotTo(HaveOccurred())
By("Verifying that the duration is valid")
certDuration := cert.NotAfter.Sub(cert.NotBefore)
if certDuration > (duration+fuzz) || certDuration < duration {
Failf("Expected duration of %s, got %s (fuzz: %s) [NotBefore: %s, NotAfter: %s]", duration, certDuration,
fuzz, cert.NotBefore.Format(time.RFC3339), cert.NotAfter.Format(time.RFC3339))
}
}
// CertManagerDescribe is a wrapper function for ginkgo describe. Adds namespacing.
func CertManagerDescribe(text string, body func()) bool {
return Describe("[cert-manager] "+text, body)
}
func ConformanceDescribe(text string, body func()) bool {
return Describe("[Conformance] "+text, body)
}