Skip to content

Commit

Permalink
Merge pull request #4021 from chandankumar4/parameters-unit-tests
Browse files Browse the repository at this point in the history
Add Unit Tests for all Parameters() of the service libraries that don't have them.
  • Loading branch information
k8s-ci-robot authored Oct 6, 2023
2 parents 44cb72a + 30bf98a commit 5aba3bb
Show file tree
Hide file tree
Showing 6 changed files with 709 additions and 0 deletions.
119 changes: 119 additions & 0 deletions azure/services/bastionhosts/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bastionhosts

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

var (
fakeSKU = armnetwork.BastionHostSKUName("fake-SKU")
fakeBastionHost = armnetwork.BastionHost{
Location: &fakeAzureBastionSpec.Location,
Name: ptr.To("my-bastion-host"),
SKU: &armnetwork.SKU{Name: &fakeSKU},
}
fakeAzureBastionSpec1 = AzureBastionSpec{
Name: "my-bastion",
ClusterName: "cluster",
Location: "westus",
SubnetID: "my-subnet-id",
PublicIPID: "my-public-ip-id",
Sku: infrav1.BastionHostSkuName("fake-SKU"),
EnableTunneling: false,
}

fakeBastionHostTags = map[string]*string{
"sigs.k8s.io_cluster-api-provider-azure_cluster_cluster": ptr.To("owned"),
"sigs.k8s.io_cluster-api-provider-azure_role": ptr.To("Bastion"),
"Name": ptr.To("my-bastion"),
}
)

func TestAzureBastionSpec_Parameters(t *testing.T) {
testCases := []struct {
name string
spec *AzureBastionSpec
existing interface{}
expect func(g *WithT, result interface{})
expectedError string
}{
{
name: "error when existing host is not of BastionHost type",
spec: &AzureBastionSpec{},
existing: struct{}{},
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "struct {} is not an armnetwork.BastionHost",
},
{
name: "get result as nil when existing BastionHost is present",
spec: &AzureBastionSpec{},
existing: fakeBastionHost,
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "",
},
{
name: "get result as nil when existing BastionHost is present with empty data",
spec: &AzureBastionSpec{},
existing: armnetwork.BastionHost{},
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "",
},
{
name: "get BastionHost when all values are present",
spec: &fakeAzureBastionSpec1,
existing: nil,
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeAssignableToTypeOf(armnetwork.BastionHost{}))
g.Expect(result.(armnetwork.BastionHost).Location).To(Equal(ptr.To[string](fakeAzureBastionSpec1.Location)))
g.Expect(result.(armnetwork.BastionHost).Name).To(Equal(ptr.To[string](fakeAzureBastionSpec1.ResourceName())))
g.Expect(result.(armnetwork.BastionHost).SKU.Name).To(Equal(ptr.To(armnetwork.BastionHostSKUName(fakeAzureBastionSpec1.Sku))))
g.Expect(result.(armnetwork.BastionHost).Properties.EnableTunneling).To(Equal(ptr.To(fakeAzureBastionSpec1.EnableTunneling)))
g.Expect(result.(armnetwork.BastionHost).Tags).To(Equal(fakeBastionHostTags))
},
expectedError: "",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
t.Parallel()

result, err := tc.spec.Parameters(context.TODO(), tc.existing)
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
tc.expect(g, result)
})
}
}
123 changes: 123 additions & 0 deletions azure/services/natgateways/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package natgateways

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
)

var (
fakeNatGateway = armnetwork.NatGateway{
ID: ptr.To("/subscriptions/my-sub/resourceGroups/my-rg/providers/Microsoft.Network/natGateways/my-node-natgateway-1"),
Properties: &armnetwork.NatGatewayPropertiesFormat{
PublicIPAddresses: []*armnetwork.SubResource{
{ID: ptr.To("/subscriptions/my-sub/resourceGroups/my-rg/providers/Microsoft.Network/natGateways/pip-node-subnet")},
},
},
}
fakeNatGatewaySpec = NatGatewaySpec{
Name: "my-node-natgateway-1",
ResourceGroup: "my-rg",
SubscriptionID: "my-sub",
Location: "westus",
ClusterName: "cluster",
NatGatewayIP: infrav1.PublicIPSpec{Name: "pip-node-subnet"},
}
fakeNatGatewaysTags = map[string]*string{
"sigs.k8s.io_cluster-api-provider-azure_cluster_cluster": ptr.To("owned"),
"Name": ptr.To("my-node-natgateway-1"),
}
)

func TestNatGatewaySpec_Parameters(t *testing.T) {
testCases := []struct {
name string
spec *NatGatewaySpec
existing interface{}
expect func(g *WithT, result interface{})
expectedError string
}{
{
name: "error when existing is not of NatGateway type",
spec: &NatGatewaySpec{},
existing: struct{}{},
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "struct {} is not an armnetwork.NatGateway",
},
{
name: "get result as nil when existing NatGateway is present",
spec: &fakeNatGatewaySpec,
existing: fakeNatGateway,
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "",
},
{
name: "get NatGateway when existing NatGateway is present but PublicIPAddresses is empty",
spec: &fakeNatGatewaySpec,
existing: armnetwork.NatGateway{
Properties: &armnetwork.NatGatewayPropertiesFormat{PublicIPAddresses: []*armnetwork.SubResource{}},
},
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeAssignableToTypeOf(armnetwork.NatGateway{}))
g.Expect(result.(armnetwork.NatGateway).Location).To(Equal(ptr.To[string](fakeNatGatewaySpec.Location)))
g.Expect(result.(armnetwork.NatGateway).Name).To(Equal(ptr.To[string](fakeNatGatewaySpec.ResourceName())))
},
expectedError: "",
},
{
name: "get NatGateway when all values are present",
spec: &fakeNatGatewaySpec,
existing: nil,
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeAssignableToTypeOf(armnetwork.NatGateway{}))
g.Expect(result.(armnetwork.NatGateway).Location).To(Equal(ptr.To[string](fakeNatGatewaySpec.Location)))
g.Expect(result.(armnetwork.NatGateway).Name).To(Equal(ptr.To[string](fakeNatGatewaySpec.ResourceName())))
g.Expect(result.(armnetwork.NatGateway).Properties.PublicIPAddresses[0].ID).To(Equal(ptr.To[string](azure.PublicIPID(fakeNatGatewaySpec.SubscriptionID,
fakeNatGatewaySpec.ResourceGroupName(), fakeNatGatewaySpec.NatGatewayIP.Name))))
g.Expect(result.(armnetwork.NatGateway).Tags).To(Equal(fakeNatGatewaysTags))
},
expectedError: "",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
t.Parallel()

result, err := tc.spec.Parameters(context.TODO(), tc.existing)
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
tc.expect(g, result)
})
}
}
103 changes: 103 additions & 0 deletions azure/services/roleassignments/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package roleassignments

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
)

var (
fakeRoleAssignment = armauthorization.RoleAssignment{
ID: ptr.To("fake-id"),
Name: ptr.To("fake-name"),
Type: ptr.To("fake-type"),
}
fakeRoleAssignmentSpec = RoleAssignmentSpec{
PrincipalID: ptr.To("fake-principal-id"),
RoleDefinitionID: "fake-role-definition-id",
}
)

func TestRoleAssignmentSpec_Parameters(t *testing.T) {
testCases := []struct {
name string
spec *RoleAssignmentSpec
existing interface{}
expect func(g *WithT, result interface{})
expectedError string
}{
{
name: "error when existing is not of RoleAssignment type",
spec: &RoleAssignmentSpec{},
existing: struct{}{},
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "struct {} is not an armauthorization.RoleAssignment",
},
{
name: "get result as nil when existing NatGateway is present",
spec: &fakeRoleAssignmentSpec,
existing: fakeRoleAssignment,
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "",
},
{
name: "get result as nil when existing NatGateway is present with empty data",
spec: &fakeRoleAssignmentSpec,
existing: armauthorization.RoleAssignment{},
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeNil())
},
expectedError: "",
},
{
name: "get RoleAssignment when all values are present",
spec: &fakeRoleAssignmentSpec,
existing: nil,
expect: func(g *WithT, result interface{}) {
g.Expect(result).To(BeAssignableToTypeOf(armauthorization.RoleAssignmentCreateParameters{}))
g.Expect(result.(armauthorization.RoleAssignmentCreateParameters).Properties.RoleDefinitionID).To(Equal(ptr.To[string](fakeRoleAssignmentSpec.RoleDefinitionID)))
g.Expect(result.(armauthorization.RoleAssignmentCreateParameters).Properties.PrincipalID).To(Equal(fakeRoleAssignmentSpec.PrincipalID))
},
expectedError: "",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
t.Parallel()

result, err := tc.spec.Parameters(context.TODO(), tc.existing)
if tc.expectedError != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(tc.expectedError))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
tc.expect(g, result)
})
}
}
Loading

0 comments on commit 5aba3bb

Please sign in to comment.