| Giorgi Lekveishvili | e8b2f01 | 2023-11-30 19:05:03 +0400 | [diff] [blame^] | 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 controllers |
| 18 | |
| 19 | import ( |
| 20 | "path/filepath" |
| 21 | "testing" |
| 22 | |
| 23 | . "github.com/onsi/ginkgo" |
| 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 | "sigs.k8s.io/controller-runtime/pkg/envtest/printer" |
| 31 | logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 33 | |
| 34 | dodocloudv1 "github.com/giolekva/pcloud/core/ns-controller/api/v1" |
| 35 | //+kubebuilder:scaffold:imports |
| 36 | ) |
| 37 | |
| 38 | // These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 39 | // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 40 | |
| 41 | var cfg *rest.Config |
| 42 | var k8sClient client.Client |
| 43 | var testEnv *envtest.Environment |
| 44 | |
| 45 | func TestAPIs(t *testing.T) { |
| 46 | RegisterFailHandler(Fail) |
| 47 | |
| 48 | RunSpecsWithDefaultAndCustomReporters(t, |
| 49 | "Controller Suite", |
| 50 | []Reporter{printer.NewlineReporter{}}) |
| 51 | } |
| 52 | |
| 53 | var _ = BeforeSuite(func() { |
| 54 | logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) |
| 55 | |
| 56 | By("bootstrapping test environment") |
| 57 | testEnv = &envtest.Environment{ |
| 58 | CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, |
| 59 | ErrorIfCRDPathMissing: true, |
| 60 | } |
| 61 | |
| 62 | var err error |
| 63 | // cfg is defined in this file globally. |
| 64 | cfg, err = testEnv.Start() |
| 65 | Expect(err).NotTo(HaveOccurred()) |
| 66 | Expect(cfg).NotTo(BeNil()) |
| 67 | |
| 68 | err = dodocloudv1.AddToScheme(scheme.Scheme) |
| 69 | Expect(err).NotTo(HaveOccurred()) |
| 70 | |
| 71 | //+kubebuilder:scaffold:scheme |
| 72 | |
| 73 | k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) |
| 74 | Expect(err).NotTo(HaveOccurred()) |
| 75 | Expect(k8sClient).NotTo(BeNil()) |
| 76 | |
| 77 | }, 60) |
| 78 | |
| 79 | var _ = AfterSuite(func() { |
| 80 | By("tearing down the test environment") |
| 81 | err := testEnv.Stop() |
| 82 | Expect(err).NotTo(HaveOccurred()) |
| 83 | }) |