sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/virtualmachineimages/cache_test.go (about)

     1  /*
     2  Copyright 2022 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 virtualmachineimages
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    24  	. "github.com/onsi/gomega"
    25  	"github.com/pkg/errors"
    26  	"go.uber.org/mock/gomock"
    27  	"k8s.io/utils/ptr"
    28  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachineimages/mock_virtualmachineimages"
    29  )
    30  
    31  func TestCacheGet(t *testing.T) {
    32  	cases := map[string]struct {
    33  		location      string
    34  		publisher     string
    35  		offer         string
    36  		sku           string
    37  		have          armcompute.VirtualMachineImagesClientListResponse
    38  		expectedError error
    39  	}{
    40  		"should find": {
    41  			location: "test", publisher: "foo", offer: "bar", sku: "baz",
    42  			have: armcompute.VirtualMachineImagesClientListResponse{
    43  				VirtualMachineImageResourceArray: []*armcompute.VirtualMachineImageResource{
    44  					{Name: ptr.To("foo")},
    45  				},
    46  			},
    47  			expectedError: nil,
    48  		},
    49  		"should not find": {
    50  			location: "test", publisher: "foo", offer: "bar", sku: "baz",
    51  			have: armcompute.VirtualMachineImagesClientListResponse{
    52  				VirtualMachineImageResourceArray: []*armcompute.VirtualMachineImageResource{},
    53  			},
    54  			expectedError: errors.New("failed to refresh VM images cache"),
    55  		},
    56  	}
    57  
    58  	for name, tc := range cases {
    59  		tc := tc
    60  		t.Run(name, func(t *testing.T) {
    61  			t.Parallel()
    62  
    63  			mockCtrl := gomock.NewController(t)
    64  			defer mockCtrl.Finish()
    65  			mockClient := mock_virtualmachineimages.NewMockClient(mockCtrl)
    66  			mockClient.EXPECT().List(gomock.Any(), tc.location, tc.publisher, tc.offer, tc.sku).Return(tc.have, tc.expectedError)
    67  			c := &Cache{client: mockClient}
    68  
    69  			g := NewWithT(t)
    70  			val, err := c.Get(context.Background(), tc.location, tc.publisher, tc.offer, tc.sku)
    71  			if tc.expectedError != nil {
    72  				g.Expect(err).To(HaveOccurred())
    73  				g.Expect(err.Error()).To(ContainSubstring(tc.expectedError.Error()))
    74  			} else {
    75  				g.Expect(err).NotTo(HaveOccurred())
    76  				g.Expect(val).To(Equal(tc.have))
    77  			}
    78  		})
    79  	}
    80  }