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