sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/index/node_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  
    27  func TestIndexNodeByProviderID(t *testing.T) {
    28  	validProviderID := "aws://region/zone/id"
    29  
    30  	testCases := []struct {
    31  		name     string
    32  		object   client.Object
    33  		expected []string
    34  	}{
    35  		{
    36  			name:     "Node has no providerID",
    37  			object:   &corev1.Node{},
    38  			expected: nil,
    39  		},
    40  		{
    41  			name: "Node has invalid providerID",
    42  			object: &corev1.Node{
    43  				Spec: corev1.NodeSpec{
    44  					ProviderID: "",
    45  				},
    46  			},
    47  			expected: nil,
    48  		},
    49  		{
    50  			name: "Node has valid providerID",
    51  			object: &corev1.Node{
    52  				Spec: corev1.NodeSpec{
    53  					ProviderID: validProviderID,
    54  				},
    55  			},
    56  			expected: []string{validProviderID},
    57  		},
    58  	}
    59  
    60  	for _, tc := range testCases {
    61  		t.Run(tc.name, func(t *testing.T) {
    62  			g := NewWithT(t)
    63  			got := NodeByProviderID(tc.object)
    64  			g.Expect(got).To(BeEquivalentTo(tc.expected))
    65  		})
    66  	}
    67  }