sigs.k8s.io/cluster-api@v1.7.1/internal/controllers/topology/cluster/util_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 cluster
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    28  	. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
    29  
    30  	"sigs.k8s.io/cluster-api/internal/contract"
    31  	"sigs.k8s.io/cluster-api/internal/test/builder"
    32  )
    33  
    34  func TestGetReference(t *testing.T) {
    35  	fakeControlPlaneTemplateCRDv99 := builder.GenericControlPlaneTemplateCRD.DeepCopy()
    36  	fakeControlPlaneTemplateCRDv99.Labels = map[string]string{
    37  		"cluster.x-k8s.io/v1beta1": "v1beta1_v99",
    38  	}
    39  	crds := []client.Object{
    40  		fakeControlPlaneTemplateCRDv99,
    41  		builder.GenericBootstrapConfigTemplateCRD,
    42  	}
    43  
    44  	controlPlaneTemplate := builder.ControlPlaneTemplate(metav1.NamespaceDefault, "controlplanetemplate1").Build()
    45  	controlPlaneTemplatev99 := controlPlaneTemplate.DeepCopy()
    46  	controlPlaneTemplatev99.SetAPIVersion(builder.ControlPlaneGroupVersion.Group + "/v99")
    47  
    48  	workerBootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "workerbootstraptemplate1").Build()
    49  
    50  	tests := []struct {
    51  		name    string
    52  		ref     *corev1.ObjectReference
    53  		objects []client.Object
    54  		want    *unstructured.Unstructured
    55  		wantRef *corev1.ObjectReference
    56  		wantErr bool
    57  	}{
    58  		{
    59  			name:    "Get object fails: ref is nil",
    60  			ref:     nil,
    61  			wantErr: true,
    62  		},
    63  		{
    64  			name: "Get object",
    65  			ref:  contract.ObjToRef(workerBootstrapTemplate),
    66  			objects: []client.Object{
    67  				workerBootstrapTemplate,
    68  			},
    69  			want:    workerBootstrapTemplate,
    70  			wantRef: contract.ObjToRef(workerBootstrapTemplate),
    71  		},
    72  		{
    73  			name:    "Get object fails: object does not exist",
    74  			ref:     contract.ObjToRef(workerBootstrapTemplate),
    75  			objects: []client.Object{},
    76  			wantErr: true,
    77  		},
    78  		{
    79  			name: "Get object fails: object does not exist with this apiVersion",
    80  			ref:  contract.ObjToRef(controlPlaneTemplate),
    81  			objects: []client.Object{
    82  				controlPlaneTemplatev99,
    83  			},
    84  			wantErr: true,
    85  		},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			g := NewWithT(t)
    90  
    91  			objs := []client.Object{}
    92  			objs = append(objs, crds...)
    93  			objs = append(objs, tt.objects...)
    94  
    95  			fakeClient := fake.NewClientBuilder().
    96  				WithScheme(fakeScheme).
    97  				WithObjects(objs...).
    98  				Build()
    99  
   100  			r := &Reconciler{
   101  				Client:                    fakeClient,
   102  				UnstructuredCachingClient: fakeClient,
   103  				patchHelperFactory:        dryRunPatchHelperFactory(fakeClient),
   104  			}
   105  			got, err := r.getReference(ctx, tt.ref)
   106  			if tt.wantErr {
   107  				g.Expect(err).To(HaveOccurred())
   108  				return
   109  			}
   110  			g.Expect(err).ToNot(HaveOccurred())
   111  
   112  			g.Expect(got).To(EqualObject(tt.want))
   113  			g.Expect(tt.ref).To(EqualObject(tt.wantRef))
   114  		})
   115  	}
   116  }