github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/tekton/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 tekton
    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  	tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
    38  	clientsetscheme "k8s.io/client-go/kubernetes/scheme"
    39  
    40  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    41  	//+kubebuilder:scaffold:imports
    42  )
    43  
    44  var (
    45  	cfg       *rest.Config
    46  	k8sClient client.Client
    47  	testEnv   *envtest.Environment
    48  	ctx       context.Context
    49  	cancel    context.CancelFunc
    50  )
    51  
    52  func Test(t *testing.T) {
    53  	RegisterFailHandler(Fail)
    54  	RunSpecs(t, "Tekton Suite")
    55  }
    56  
    57  var _ = BeforeSuite(func() {
    58  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    59  	ctx, cancel = context.WithCancel(context.TODO())
    60  
    61  	// adding required CRDs, including tekton for PipelineRun Kind
    62  	testEnv = &envtest.Environment{
    63  		CRDDirectoryPaths: []string{
    64  			filepath.Join("..", "config", "crd", "bases"),
    65  			filepath.Join(
    66  				build.Default.GOPATH,
    67  				"pkg", "mod", test.GetRelativeDependencyPath("tektoncd/pipeline"), "config",
    68  			),
    69  			filepath.Join(
    70  				build.Default.GOPATH,
    71  				"pkg", "mod", test.GetRelativeDependencyPath("application-api"), "config", "crd", "bases",
    72  			),
    73  		},
    74  		ErrorIfCRDPathMissing: true,
    75  	}
    76  
    77  	var err error
    78  	// cfg is defined in this file globally.
    79  	cfg, err = testEnv.Start()
    80  	Expect(err).NotTo(HaveOccurred())
    81  	Expect(cfg).NotTo(BeNil())
    82  
    83  	err = appstudiov1alpha1.AddToScheme(clientsetscheme.Scheme)
    84  	Expect(err).NotTo(HaveOccurred())
    85  
    86  	err = applicationapiv1alpha1.AddToScheme(clientsetscheme.Scheme)
    87  	Expect(err).NotTo(HaveOccurred())
    88  
    89  	err = tektonv1.AddToScheme(clientsetscheme.Scheme)
    90  	Expect(err).NotTo(HaveOccurred())
    91  
    92  	//+kubebuilder:scaffold:scheme
    93  	k8sClient, err = client.New(cfg, client.Options{
    94  		Scheme: clientsetscheme.Scheme,
    95  	})
    96  	Expect(err).NotTo(HaveOccurred())
    97  	Expect(k8sClient).NotTo(BeNil())
    98  })
    99  
   100  var _ = AfterSuite(func() {
   101  	cancel()
   102  	By("tearing down the test environment")
   103  	err := testEnv.Stop()
   104  	Expect(err).NotTo(HaveOccurred())
   105  })