...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "os"
20 "path/filepath"
21 "testing"
22 "time"
23
24 . "github.com/onsi/ginkgo/v2"
25 . "github.com/onsi/gomega"
26 "k8s.io/client-go/kubernetes/scheme"
27 "k8s.io/client-go/rest"
28 "sigs.k8s.io/controller-runtime/pkg/client"
29 "sigs.k8s.io/controller-runtime/pkg/envtest"
30 logf "sigs.k8s.io/controller-runtime/pkg/log"
31 "sigs.k8s.io/controller-runtime/pkg/log/zap"
32 )
33
34
35
36
37 var cfg *rest.Config
38 var k8sClient client.Client
39 var testEnv *envtest.Environment
40
41 func TestAPIs(t *testing.T) {
42 RegisterFailHandler(Fail)
43
44 RunSpecs(t, "v1 Suite")
45 }
46
47 var _ = BeforeSuite(func(ctx SpecContext) {
48
49 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
50
51 By("bootstrapping test environment")
52 t := true
53 if os.Getenv("USE_EXISTING_CLUSTER") == "true" {
54 testEnv = &envtest.Environment{
55 UseExistingCluster: &t,
56 }
57 } else {
58 testEnv = &envtest.Environment{
59 CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
60 }
61 }
62
63 err := SchemeBuilder.AddToScheme(scheme.Scheme)
64 Expect(err).NotTo(HaveOccurred())
65
66 cfg, err = testEnv.Start()
67 Expect(err).ToNot(HaveOccurred())
68 Expect(cfg).ToNot(BeNil())
69
70 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
71 Expect(err).ToNot(HaveOccurred())
72 Expect(k8sClient).ToNot(BeNil())
73
74 }, NodeTimeout(60*time.Second))
75
76 var _ = AfterSuite(func() {
77 By("tearing down the test environment")
78 err := testEnv.Stop()
79 Expect(err).ToNot(HaveOccurred())
80 })
81