sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/virtualnetworks/virtualnetworks_test.go (about) 1 /* 2 Copyright 2019 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 virtualnetworks 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 24 asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101" 25 "github.com/Azure/azure-service-operator/v2/pkg/common/labels" 26 "github.com/Azure/azure-service-operator/v2/pkg/genruntime" 27 . "github.com/onsi/gomega" 28 "go.uber.org/mock/gomock" 29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 "k8s.io/apimachinery/pkg/runtime" 31 "k8s.io/utils/ptr" 32 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 33 "sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualnetworks/mock_virtualnetworks" 34 "sigs.k8s.io/controller-runtime/pkg/client" 35 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" 36 ) 37 38 func TestPostCreateOrUpdateResourceHook(t *testing.T) { 39 t.Run("failed to create or update", func(t *testing.T) { 40 g := NewGomegaWithT(t) 41 mockCtrl := gomock.NewController(t) 42 scope := mock_virtualnetworks.NewMockVNetScope(mockCtrl) 43 err := errors.New("an error") 44 g.Expect(postCreateOrUpdateResourceHook(context.Background(), scope, nil, err)).To(MatchError(err)) 45 }) 46 47 t.Run("successfully created or updated", func(t *testing.T) { 48 g := NewGomegaWithT(t) 49 50 mockCtrl := gomock.NewController(t) 51 scope := mock_virtualnetworks.NewMockVNetScope(mockCtrl) 52 53 existing := &asonetworkv1.VirtualNetwork{ 54 ObjectMeta: metav1.ObjectMeta{ 55 Name: "vnet", 56 }, 57 Status: asonetworkv1.VirtualNetwork_STATUS{ 58 Id: ptr.To("id"), 59 Tags: map[string]string{"actual": "tags"}, 60 AddressSpace: &asonetworkv1.AddressSpace_STATUS{ 61 AddressPrefixes: []string{"cidr"}, 62 }, 63 }, 64 } 65 66 vnet := &infrav1.VnetSpec{} 67 scope.EXPECT().Vnet().Return(vnet) 68 69 subnets := []client.Object{ 70 &asonetworkv1.VirtualNetworksSubnet{ 71 ObjectMeta: metav1.ObjectMeta{ 72 Name: "subnet", 73 Labels: map[string]string{ 74 labels.OwnerNameLabel: existing.Name, 75 }, 76 }, 77 Spec: asonetworkv1.VirtualNetworks_Subnet_Spec{ 78 AzureName: "azure-name", 79 }, 80 Status: asonetworkv1.VirtualNetworks_Subnet_STATUS{ 81 AddressPrefixes: []string{"address prefixes"}, 82 }, 83 }, 84 &asonetworkv1.VirtualNetworksSubnet{ 85 ObjectMeta: metav1.ObjectMeta{ 86 Name: "other subnet", 87 }, 88 Spec: asonetworkv1.VirtualNetworks_Subnet_Spec{ 89 Owner: &genruntime.KnownResourceReference{ 90 Name: "not this vnet", 91 }, 92 }, 93 }, 94 } 95 scope.EXPECT().UpdateSubnetCIDRs("azure-name", []string{"address prefixes"}) 96 97 s := runtime.NewScheme() 98 g.Expect(asonetworkv1.AddToScheme(s)).To(Succeed()) 99 c := fakeclient.NewClientBuilder(). 100 WithScheme(s). 101 WithObjects(subnets...). 102 Build() 103 scope.EXPECT().GetClient().Return(c) 104 105 g.Expect(postCreateOrUpdateResourceHook(context.Background(), scope, existing, nil)).To(Succeed()) 106 107 g.Expect(vnet.ID).To(Equal("id")) 108 g.Expect(vnet.Tags).To(Equal(infrav1.Tags{"actual": "tags"})) 109 g.Expect(vnet.CIDRBlocks).To(Equal([]string{"cidr"})) 110 }) 111 }