github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/syncer/suite_test.go (about)

     1  /*
     2  Copyright 2022 Red Hat Inc.
     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 syncer
    18  
    19  import (
    20  	"context"
    21  	"go/build"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/konflux-ci/operator-toolkit/test"
    26  	applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
    27  
    28  	. "github.com/onsi/ginkgo/v2"
    29  	. "github.com/onsi/gomega"
    30  
    31  	"k8s.io/client-go/rest"
    32  	"sigs.k8s.io/controller-runtime/pkg/client"
    33  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    34  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    35  
    36  	appstudiov1alpha1 "github.com/redhat-appstudio/release-service/api/v1alpha1"
    37  	clientsetscheme "k8s.io/client-go/kubernetes/scheme"
    38  
    39  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    40  	//+kubebuilder:scaffold:imports
    41  )
    42  
    43  var (
    44  	cfg       *rest.Config
    45  	k8sClient client.Client
    46  	testEnv   *envtest.Environment
    47  	ctx       context.Context
    48  	cancel    context.CancelFunc
    49  )
    50  
    51  func Test(t *testing.T) {
    52  	RegisterFailHandler(Fail)
    53  	RunSpecs(t, "Syncer Suite")
    54  }
    55  
    56  var _ = BeforeSuite(func() {
    57  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    58  	ctx, cancel = context.WithCancel(context.TODO())
    59  
    60  	testEnv = &envtest.Environment{
    61  		CRDDirectoryPaths: []string{
    62  			filepath.Join("..", "config", "crd", "bases"),
    63  			filepath.Join(
    64  				build.Default.GOPATH,
    65  				"pkg", "mod", test.GetRelativeDependencyPath("application-api"), "config", "crd", "bases",
    66  			),
    67  		},
    68  		ErrorIfCRDPathMissing: true,
    69  	}
    70  
    71  	var err error
    72  	cfg, err = testEnv.Start()
    73  	Expect(err).NotTo(HaveOccurred())
    74  	Expect(cfg).NotTo(BeNil())
    75  
    76  	err = appstudiov1alpha1.AddToScheme(clientsetscheme.Scheme)
    77  	Expect(err).NotTo(HaveOccurred())
    78  
    79  	err = applicationapiv1alpha1.AddToScheme(clientsetscheme.Scheme)
    80  	Expect(err).NotTo(HaveOccurred())
    81  
    82  	//+kubebuilder:scaffold:scheme
    83  	k8sClient, err = client.New(cfg, client.Options{
    84  		Scheme: clientsetscheme.Scheme,
    85  	})
    86  	Expect(err).NotTo(HaveOccurred())
    87  	Expect(k8sClient).NotTo(BeNil())
    88  })
    89  
    90  var _ = AfterSuite(func() {
    91  	cancel()
    92  	By("tearing down the test environment")
    93  	err := testEnv.Stop()
    94  	Expect(err).NotTo(HaveOccurred())
    95  })