sigs.k8s.io/cluster-api@v1.7.1/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  			UnstructuredCachingClient: unstructuredCachingClient,
    77  		}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 5}); err != nil {
    78  			panic(fmt.Sprintf("unable to create clusterclass reconciler: %v", err))
    79  		}
    80  	}
    81  	SetDefaultEventuallyPollingInterval(100 * time.Millisecond)
    82  	SetDefaultEventuallyTimeout(30 * time.Second)
    83  	os.Exit(envtest.Run(ctx, envtest.RunInput{
    84  		M:                   m,
    85  		ManagerUncachedObjs: []client.Object{},
    86  		SetupEnv:            func(e *envtest.Environment) { env = e },
    87  		SetupIndexes:        setupIndexes,
    88  		SetupReconcilers:    setupReconcilers,
    89  	}))
    90  }
    91  
    92  // ownerReferenceTo converts an object to an OwnerReference.
    93  // Note: We pass in gvk explicitly as we can't rely on GVK being set on all objects
    94  // (only on Unstructured).
    95  func ownerReferenceTo(obj client.Object, gvk schema.GroupVersionKind) *metav1.OwnerReference {
    96  	return &metav1.OwnerReference{
    97  		APIVersion: gvk.GroupVersion().String(),
    98  		Kind:       gvk.Kind,
    99  		Name:       obj.GetName(),
   100  		UID:        obj.GetUID(),
   101  	}
   102  }
   103  
   104  // referenceExistsWithCorrectKindAndAPIVersion asserts that the passed ObjectReference is not nil and that it has the correct kind and apiVersion.
   105  func referenceExistsWithCorrectKindAndAPIVersion(reference *corev1.ObjectReference, kind string, apiVersion schema.GroupVersion) error {
   106  	if reference == nil {
   107  		return fmt.Errorf("object reference passed was nil")
   108  	}
   109  	if reference.Kind != kind {
   110  		return fmt.Errorf("object reference kind %v does not match expected %v", reference.Kind, kind)
   111  	}
   112  	if reference.APIVersion != apiVersion.String() {
   113  		return fmt.Errorf("apiVersion %v does not match expected %v", reference.APIVersion, apiVersion.String())
   114  	}
   115  	return nil
   116  }