sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/index/machinepool_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 index
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	corev1 "k8s.io/api/core/v1"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    27  )
    28  
    29  func TestIndexMachinePoolByNodeName(t *testing.T) {
    30  	testCases := []struct {
    31  		name     string
    32  		object   client.Object
    33  		expected []string
    34  	}{
    35  		{
    36  			name:     "when the machinepool has no NodeRef",
    37  			object:   &expv1.MachinePool{},
    38  			expected: []string{},
    39  		},
    40  		{
    41  			name: "when the machinepool has valid NodeRefs",
    42  			object: &expv1.MachinePool{
    43  				Status: expv1.MachinePoolStatus{
    44  					NodeRefs: []corev1.ObjectReference{
    45  						{
    46  							Name: "node1",
    47  						},
    48  						{
    49  							Name: "node2",
    50  						},
    51  					},
    52  				},
    53  			},
    54  			expected: []string{"node1", "node2"},
    55  		},
    56  	}
    57  
    58  	for _, tc := range testCases {
    59  		t.Run(tc.name, func(t *testing.T) {
    60  			g := NewWithT(t)
    61  			got := MachinePoolByNodeName(tc.object)
    62  			g.Expect(got).To(ConsistOf(tc.expected))
    63  		})
    64  	}
    65  }
    66  
    67  func TestIndexMachinePoolByProviderID(t *testing.T) {
    68  	validProviderID := "aws://region/zone/1"
    69  	otherValidProviderID := "aws://region/zone/2"
    70  
    71  	testCases := []struct {
    72  		name     string
    73  		object   client.Object
    74  		expected []string
    75  	}{
    76  		{
    77  			name:     "MachinePool has no providerID",
    78  			object:   &expv1.MachinePool{},
    79  			expected: nil,
    80  		},
    81  		{
    82  			name: "MachinePool has invalid providerID",
    83  			object: &expv1.MachinePool{
    84  				Spec: expv1.MachinePoolSpec{
    85  					ProviderIDList: []string{""},
    86  				},
    87  			},
    88  			expected: []string{},
    89  		},
    90  		{
    91  			name: "MachinePool has valid providerIDs",
    92  			object: &expv1.MachinePool{
    93  				Spec: expv1.MachinePoolSpec{
    94  					ProviderIDList: []string{validProviderID, otherValidProviderID},
    95  				},
    96  			},
    97  			expected: []string{validProviderID, otherValidProviderID},
    98  		},
    99  	}
   100  
   101  	for _, tc := range testCases {
   102  		t.Run(tc.name, func(t *testing.T) {
   103  			g := NewWithT(t)
   104  			got := machinePoolByProviderID(tc.object)
   105  			g.Expect(got).To(BeEquivalentTo(tc.expected))
   106  		})
   107  	}
   108  }