github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/controllers/release/suite_test.go (about) 1 /* 2 Copyright 2022. 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 release 18 19 import ( 20 "context" 21 "go/build" 22 "path/filepath" 23 "testing" 24 25 appstudiov1alpha1 "github.com/redhat-appstudio/release-service/api/v1alpha1" 26 "sigs.k8s.io/controller-runtime/pkg/metrics/server" 27 28 "github.com/konflux-ci/operator-toolkit/test" 29 30 applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" 31 32 "k8s.io/client-go/rest" 33 34 . "github.com/onsi/ginkgo/v2" 35 . "github.com/onsi/gomega" 36 logf "sigs.k8s.io/controller-runtime/pkg/log" 37 "sigs.k8s.io/controller-runtime/pkg/log/zap" 38 39 ecapiv1alpha1 "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1" 40 tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" 41 "k8s.io/client-go/kubernetes/scheme" 42 ctrl "sigs.k8s.io/controller-runtime" 43 "sigs.k8s.io/controller-runtime/pkg/client" 44 "sigs.k8s.io/controller-runtime/pkg/envtest" 45 ) 46 47 const ( 48 testApiVersion = "appstudio.redhat.com/v1alpha1" 49 testNamespace = "default" 50 ) 51 52 var ( 53 cfg *rest.Config 54 k8sClient client.Client 55 testEnv *envtest.Environment 56 ctx context.Context 57 cancel context.CancelFunc 58 ) 59 60 func Test(t *testing.T) { 61 RegisterFailHandler(Fail) 62 RunSpecs(t, "Release Controller Suite") 63 } 64 65 var _ = BeforeSuite(func() { 66 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 67 ctx, cancel = context.WithCancel(context.TODO()) 68 69 // adding required CRDs, including tekton for PipelineRun Kind 70 testEnv = &envtest.Environment{ 71 CRDDirectoryPaths: []string{ 72 filepath.Join("..", "..", "config", "crd", "bases"), 73 filepath.Join( 74 build.Default.GOPATH, 75 "pkg", "mod", test.GetRelativeDependencyPath("tektoncd/pipeline"), "config", 76 ), 77 filepath.Join( 78 build.Default.GOPATH, 79 "pkg", "mod", test.GetRelativeDependencyPath("application-api"), "config", "crd", "bases", 80 ), 81 filepath.Join( 82 build.Default.GOPATH, 83 "pkg", "mod", test.GetRelativeDependencyPath("enterprise-contract-controller"), "config", 84 ), 85 }, 86 ErrorIfCRDPathMissing: true, 87 } 88 89 var err error 90 cfg, err = testEnv.Start() 91 Expect(err).NotTo(HaveOccurred()) 92 Expect(cfg).NotTo(BeNil()) 93 94 Expect(appstudiov1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) 95 Expect(tektonv1.AddToScheme(scheme.Scheme)).To(Succeed()) 96 Expect(ecapiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) 97 Expect(applicationapiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) 98 99 k8sManager, _ := ctrl.NewManager(cfg, ctrl.Options{ 100 Scheme: scheme.Scheme, 101 Metrics: server.Options{ 102 BindAddress: "0", // disables metrics 103 }, 104 LeaderElection: false, 105 }) 106 107 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 108 Expect(err).NotTo(HaveOccurred()) 109 go func() { 110 defer GinkgoRecover() 111 Expect(k8sManager.Start(ctx)).To(Succeed()) 112 }() 113 }) 114 115 var _ = AfterSuite(func() { 116 cancel() 117 By("tearing down the test environment") 118 err := testEnv.Stop() 119 Expect(err).NotTo(HaveOccurred()) 120 })