sigs.k8s.io/cluster-api@v1.6.3/internal/controllers/clusterclass/suite_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     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 clusterclass
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	. "github.com/onsi/gomega"
    27  	corev1 "k8s.io/api/core/v1"
    28  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/apimachinery/pkg/runtime"
    31  	"k8s.io/apimachinery/pkg/runtime/schema"
    32  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    33  	"k8s.io/component-base/featuregate"
    34  	ctrl "sigs.k8s.io/controller-runtime"
    35  	"sigs.k8s.io/controller-runtime/pkg/client"
    36  	"sigs.k8s.io/controller-runtime/pkg/controller"
    37  
    38  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    39  	"sigs.k8s.io/cluster-api/api/v1beta1/index"
    40  	"sigs.k8s.io/cluster-api/feature"
    41  	"sigs.k8s.io/cluster-api/internal/test/envtest"
    42  )
    43  
    44  var (
    45  	ctx        = ctrl.SetupSignalHandler()
    46  	fakeScheme = runtime.NewScheme()
    47  	env        *envtest.Environment
    48  )
    49  
    50  func init() {
    51  	_ = clientgoscheme.AddToScheme(fakeScheme)
    52  	_ = clusterv1.AddToScheme(fakeScheme)
    53  	_ = apiextensionsv1.AddToScheme(fakeScheme)
    54  }
    55  func TestMain(m *testing.M) {
    56  	if err := feature.Gates.(featuregate.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", feature.ClusterTopology, true)); err != nil {
    57  		panic(fmt.Sprintf("unable to set ClusterTopology feature gate: %v", err))
    58  	}
    59  	setupIndexes := func(ctx context.Context, mgr ctrl.Manager) {
    60  		if err := index.AddDefaultIndexes(ctx, mgr); err != nil {
    61  			panic(fmt.Sprintf("unable to setup index: %v", err))
    62  		}
    63  	}
    64  	setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) {
    65  		unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
    66  			Cache: &client.CacheOptions{
    67  				Reader:       mgr.GetCache(),
    68  				Unstructured: true,
    69  			},
    70  		})
    71  		if err != nil {
    72  			panic(fmt.Sprintf("unable to create unstructuredCachineClient: %v", err))
    73  		}
    74  		if err := (&Reconciler{
    75  			Client:                    mgr.GetClient(),
    76  			APIReader:                 mgr.GetAPIReader(),
    77  			UnstructuredCachingClient: unstructuredCachingClient,
    78  		}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 5}); err != nil {
    79  			panic(fmt.Sprintf("unable to create clusterclass reconciler: %v", err))
    80  		}
    81  	}
    82  	SetDefaultEventuallyPollingInterval(100 * time.Millisecond)
    83  	SetDefaultEventuallyTimeout(30 * time.Second)
    84  	os.Exit(envtest.Run(ctx, envtest.RunInput{
    85  		M:                   m,
    86  		ManagerUncachedObjs: []client.Object{},
    87  		SetupEnv:            func(e *envtest.Environment) { env = e },
    88  		SetupIndexes:        setupIndexes,
    89  		SetupReconcilers:    setupReconcilers,
    90  	}))
    91  }
    92  
    93  func ownerReferenceTo(obj client.Object) *metav1.OwnerReference {
    94  	return &metav1.OwnerReference{
    95  		Kind:       obj.GetObjectKind().GroupVersionKind().Kind,
    96  		Name:       obj.GetName(),
    97  		UID:        obj.GetUID(),
    98  		APIVersion: obj.GetObjectKind().GroupVersionKind().GroupVersion().String(),
    99  	}
   100  }
   101  
   102  // referenceExistsWithCorrectKindAndAPIVersion asserts that the passed ObjectReference is not nil and that it has the correct kind and apiVersion.
   103  func referenceExistsWithCorrectKindAndAPIVersion(reference *corev1.ObjectReference, kind string, apiVersion schema.GroupVersion) error {
   104  	if reference == nil {
   105  		return fmt.Errorf("object reference passed was nil")
   106  	}
   107  	if reference.Kind != kind {
   108  		return fmt.Errorf("object reference kind %v does not match expected %v", reference.Kind, kind)
   109  	}
   110  	if reference.APIVersion != apiVersion.String() {
   111  		return fmt.Errorf("apiVersion %v does not match expected %v", reference.APIVersion, apiVersion.String())
   112  	}
   113  	return nil
   114  }