Adding Cluster Role for RIC
[ric-plt/ric-dep.git] / depRicKubernetesOperator / internal / controller / suite_test.go
1 /*
2 Copyright 2023.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package controller
18
19 import (
20         "path/filepath"
21         "testing"
22
23         . "github.com/onsi/ginkgo/v2"
24         . "github.com/onsi/gomega"
25
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         ricdeployv1 "ricdeploy/api/v1"
34         //+kubebuilder:scaffold:imports
35 )
36
37 // These tests use Ginkgo (BDD-style Go testing framework). Refer to
38 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
39
40 var cfg *rest.Config
41 var k8sClient client.Client
42 var testEnv *envtest.Environment
43
44 func TestControllers(t *testing.T) {
45         RegisterFailHandler(Fail)
46
47         RunSpecs(t, "Controller Suite")
48 }
49
50 var _ = BeforeSuite(func() {
51         logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
52
53         By("bootstrapping test environment")
54         testEnv = &envtest.Environment{
55                 CRDDirectoryPaths:     []string{filepath.Join("..", "..", "config", "crd", "bases")},
56                 ErrorIfCRDPathMissing: true,
57         }
58
59         var err error
60         // cfg is defined in this file globally.
61         cfg, err = testEnv.Start()
62         Expect(err).NotTo(HaveOccurred())
63         Expect(cfg).NotTo(BeNil())
64
65         err = ricdeployv1.AddToScheme(scheme.Scheme)
66         Expect(err).NotTo(HaveOccurred())
67
68         //+kubebuilder:scaffold:scheme
69
70         k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
71         Expect(err).NotTo(HaveOccurred())
72         Expect(k8sClient).NotTo(BeNil())
73
74 })
75
76 var _ = AfterSuite(func() {
77         By("tearing down the test environment")
78         err := testEnv.Stop()
79         Expect(err).NotTo(HaveOccurred())
80 })