sigs.k8s.io/cluster-api@v1.6.3/exp/util/util_test.go (about)

     1  /*
     2  Copyright 2023 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 util implements utility functions.
    18  package util
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  
    24  	. "github.com/onsi/gomega"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    28  
    29  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    30  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    31  	"sigs.k8s.io/cluster-api/util/labels/format"
    32  )
    33  
    34  func TestGetMachinePoolByLabels(t *testing.T) {
    35  	g := NewWithT(t)
    36  
    37  	longMachinePoolName := "this-is-a-very-long-machinepool-name-that-will-turned-into-a-hash-because-it-is-longer-than-63-characters"
    38  	namespace := "default"
    39  
    40  	testcases := []struct {
    41  		name                    string
    42  		labels                  map[string]string
    43  		machinePools            []client.Object
    44  		expectedMachinePoolName string
    45  		expectedError           string
    46  	}{
    47  		{
    48  			name: "returns a MachinePool with matching labels",
    49  			labels: map[string]string{
    50  				clusterv1.MachinePoolNameLabel: "test-pool",
    51  			},
    52  			machinePools: []client.Object{
    53  				&expv1.MachinePool{
    54  					ObjectMeta: metav1.ObjectMeta{
    55  						Name:      "test-pool",
    56  						Namespace: "default",
    57  					},
    58  				},
    59  				&expv1.MachinePool{
    60  					ObjectMeta: metav1.ObjectMeta{
    61  						Name:      "other-pool",
    62  						Namespace: "default",
    63  					},
    64  				},
    65  			},
    66  			expectedMachinePoolName: "test-pool",
    67  		},
    68  		{
    69  			name: "returns a MachinePool with matching labels and cluster name is included",
    70  			labels: map[string]string{
    71  				clusterv1.MachinePoolNameLabel: "test-pool",
    72  				clusterv1.ClusterNameLabel:     "test-cluster",
    73  			},
    74  			machinePools: []client.Object{
    75  				&expv1.MachinePool{
    76  					ObjectMeta: metav1.ObjectMeta{
    77  						Name:      "test-pool",
    78  						Namespace: "default",
    79  						Labels: map[string]string{
    80  							clusterv1.ClusterNameLabel: "test-cluster",
    81  						},
    82  					},
    83  				},
    84  				&expv1.MachinePool{
    85  					ObjectMeta: metav1.ObjectMeta{
    86  						Name:      "other-pool",
    87  						Namespace: "default",
    88  						Labels: map[string]string{
    89  							clusterv1.ClusterNameLabel: "test-cluster",
    90  						},
    91  					},
    92  				},
    93  			},
    94  			expectedMachinePoolName: "test-pool",
    95  		},
    96  		{
    97  			name: "returns a MachinePool where label is a hash",
    98  			labels: map[string]string{
    99  				clusterv1.MachinePoolNameLabel: format.MustFormatValue(longMachinePoolName),
   100  			},
   101  			machinePools: []client.Object{
   102  				&expv1.MachinePool{
   103  					ObjectMeta: metav1.ObjectMeta{
   104  						Name:      longMachinePoolName,
   105  						Namespace: "default",
   106  					},
   107  				},
   108  				&expv1.MachinePool{
   109  					ObjectMeta: metav1.ObjectMeta{
   110  						Name:      "other-pool",
   111  						Namespace: "default",
   112  					},
   113  				},
   114  			},
   115  			expectedMachinePoolName: longMachinePoolName,
   116  		},
   117  		{
   118  			name:          "missing required key",
   119  			labels:        map[string]string{},
   120  			expectedError: fmt.Sprintf("labels missing required key `%s`", clusterv1.MachinePoolNameLabel),
   121  		},
   122  		{
   123  			name: "returns nil when no machine pool matches",
   124  			labels: map[string]string{
   125  				clusterv1.MachinePoolNameLabel: "test-pool",
   126  			},
   127  			machinePools:            []client.Object{},
   128  			expectedMachinePoolName: "",
   129  		},
   130  	}
   131  
   132  	for _, tc := range testcases {
   133  		tc := tc
   134  		t.Run(tc.name, func(t *testing.T) {
   135  			clientFake := fake.NewClientBuilder().
   136  				WithScheme(fakeScheme).
   137  				WithObjects(
   138  					tc.machinePools...,
   139  				).Build()
   140  
   141  			mp, err := GetMachinePoolByLabels(ctx, clientFake, namespace, tc.labels)
   142  			if tc.expectedError != "" {
   143  				g.Expect(err).To(MatchError(tc.expectedError))
   144  			} else {
   145  				g.Expect(err).NotTo(HaveOccurred())
   146  				if tc.expectedMachinePoolName != "" {
   147  					g.Expect(mp).ToNot(BeNil())
   148  					g.Expect(mp.Name).To(Equal(tc.expectedMachinePoolName))
   149  				} else {
   150  					g.Expect(mp).To(BeNil())
   151  				}
   152  			}
   153  		})
   154  	}
   155  }