sigs.k8s.io/cluster-api-provider-azure@v1.17.0/util/azure/azure_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 azure
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  )
    27  
    28  func TestFindParentMachinePool(t *testing.T) {
    29  	client := mockClient{}
    30  
    31  	tests := []struct {
    32  		name    string
    33  		mpName  string
    34  		wantErr bool
    35  	}{
    36  		{
    37  			name:    "valid",
    38  			mpName:  "mock-machinepool-mp-0",
    39  			wantErr: false,
    40  		},
    41  		{
    42  			name:    "invalid",
    43  			mpName:  "mock-machinepool-mp-1",
    44  			wantErr: true,
    45  		},
    46  	}
    47  
    48  	for _, tc := range tests {
    49  		t.Run(tc.name, func(t *testing.T) {
    50  			g := NewWithT(t)
    51  			mp, err := FindParentMachinePool(tc.mpName, client)
    52  			if tc.wantErr {
    53  				g.Expect(err).To(HaveOccurred())
    54  			} else {
    55  				g.Expect(err).NotTo(HaveOccurred())
    56  				g.Expect(mp).NotTo(BeNil())
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestFindParentMachinePoolWithRetry(t *testing.T) {
    63  	client := mockClient{}
    64  
    65  	tests := []struct {
    66  		name        string
    67  		mpName      string
    68  		maxAttempts int
    69  		wantErr     bool
    70  	}{
    71  		{
    72  			name:        "valid",
    73  			mpName:      "mock-machinepool-mp-0",
    74  			maxAttempts: 1,
    75  			wantErr:     false,
    76  		},
    77  		{
    78  			name:        "valid with retries",
    79  			mpName:      "mock-machinepool-mp-0",
    80  			maxAttempts: 5,
    81  			wantErr:     false,
    82  		},
    83  		{
    84  			name:        "invalid",
    85  			mpName:      "mock-machinepool-mp-1",
    86  			maxAttempts: 1,
    87  			wantErr:     true,
    88  		},
    89  		{
    90  			name:        "invalid with retries",
    91  			mpName:      "mock-machinepool-mp-1",
    92  			maxAttempts: 5,
    93  			wantErr:     true,
    94  		},
    95  	}
    96  
    97  	for _, tc := range tests {
    98  		t.Run(tc.name, func(t *testing.T) {
    99  			g := NewWithT(t)
   100  			mp, err := FindParentMachinePoolWithRetry(tc.mpName, client, tc.maxAttempts)
   101  			if tc.wantErr {
   102  				g.Expect(err).To(HaveOccurred())
   103  			} else {
   104  				g.Expect(err).NotTo(HaveOccurred())
   105  				g.Expect(mp).NotTo(BeNil())
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  type mockClient struct {
   112  	client.Client
   113  }
   114  
   115  func (m mockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
   116  	mp := &expv1.MachinePool{}
   117  	mp.Spec.Template.Spec.InfrastructureRef.Name = "mock-machinepool-mp-0"
   118  	list.(*expv1.MachinePoolList).Items = []expv1.MachinePool{*mp}
   119  
   120  	return nil
   121  }
   122  
   123  func TestParseResourceID(t *testing.T) {
   124  	tests := []struct {
   125  		name         string
   126  		id           string
   127  		expectedName string
   128  		errExpected  bool
   129  	}{
   130  		{
   131  			name:         "invalid",
   132  			id:           "invalid",
   133  			expectedName: "",
   134  			errExpected:  true,
   135  		},
   136  		{
   137  			name:         "invalid: must start with slash",
   138  			id:           "subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
   139  			expectedName: "",
   140  			errExpected:  true,
   141  		},
   142  		{
   143  			name:         "invalid: must start with subscriptions or providers",
   144  			id:           "/prescriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
   145  			expectedName: "",
   146  			errExpected:  true,
   147  		},
   148  		{
   149  			name:         "valid",
   150  			id:           "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
   151  			expectedName: "vm",
   152  		},
   153  		{
   154  			name:         "valid with provider prefix",
   155  			id:           "azure:///subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
   156  			expectedName: "vm",
   157  		},
   158  	}
   159  
   160  	for _, tt := range tests {
   161  		t.Run(tt.name, func(t *testing.T) {
   162  			g := NewWithT(t)
   163  			resourceID, err := ParseResourceID(tt.id)
   164  			if tt.errExpected {
   165  				g.Expect(err).To(HaveOccurred())
   166  			} else {
   167  				g.Expect(err).NotTo(HaveOccurred())
   168  				g.Expect(resourceID.Name).To(Equal(tt.expectedName))
   169  			}
   170  		})
   171  	}
   172  }