-
Notifications
You must be signed in to change notification settings - Fork 413
/
azure_test.go
175 lines (155 loc) · 6.72 KB
/
azure_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// -------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// --------------------------------------------------------------------------------------------
// +build unittest
package azure
import (
"encoding/json"
"fmt"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Azure Suite")
}
var _ = Describe("Azure", func() {
Describe("Testing `azure` helpers", func() {
Context("ensure ParseResourceID works as expected", func() {
It("should parse appgw resourceId correctly", func() {
subID := SubscriptionID("xxxx")
resGp := ResourceGroup("yyyy")
resName := ResourceName("zzzz")
resourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s", subID, resGp, resName)
outSubID, outResGp, outResName := ParseResourceID(resourceID)
Expect(outSubID).To(Equal(subID))
Expect(resGp).To(Equal(outResGp))
Expect(resName).To(Equal(outResName))
})
})
Context("ensure ConvertToClusterResourceGroup works as expected", func() {
It("should parse empty infra resourse group correctly", func() {
subID := SubscriptionID("xxxx")
resGp := ResourceGroup("")
_, err := ConvertToClusterResourceGroup(subID, resGp, nil)
Ω(err).To(HaveOccurred(), "this call should have failed in parsing the resource group")
})
It("should parse valid infra resourse group correctly", func() {
subID := SubscriptionID("xxxx")
resGp := ResourceGroup("MC_resgp_resName_location")
Expect(ConvertToClusterResourceGroup(subID, resGp, nil)).To(Equal("/subscriptions/xxxx/resourcegroups/resgp/providers/Microsoft.ContainerService/managedClusters/resName"))
subID = SubscriptionID("xxxx")
resGp = ResourceGroup("mc_resgp_resName_location")
Expect(ConvertToClusterResourceGroup(subID, resGp, nil)).To(Equal("/subscriptions/xxxx/resourcegroups/resgp/providers/Microsoft.ContainerService/managedClusters/resName"))
})
})
Context("test getAuthorizer", func() {
It("should try and get some authorizer", func() {
authorizer, err := getAuthorizer("", false, nil)
Ω(authorizer).ToNot(BeNil())
Ω(err).ToNot(HaveOccurred())
})
})
Context("test getAuthorizerWithRetry", func() {
It("should try and get some authorizer", func() {
authorizer, err := GetAuthorizerWithRetry("", false, nil, 0, time.Duration(10))
Ω(authorizer).ToNot(BeNil())
Ω(err).ToNot(HaveOccurred())
})
})
Context("test AzContext struct", func() {
cpConfigFile := `{
"cloud": "xxxx",
"tenantId": "t",
"subscriptionId": "s",
"aadClientId": "c",
"aadClientSecret": "cs",
"resourceGroup": "r",
"location": "l",
"vmType": "xxxx",
"subnetName": "xxxx",
"securityGroupName": "xxxx",
"vnetName": "xxxx",
"vnetResourceGroup": "xxxx",
"routeTableName": "xxxx",
"primaryAvailabilitySetName": "xxxx",
"primaryScaleSetName": "xxxx",
"cloudProviderBackoff": "xxxx",
"cloudProviderBackoffRetries": "xxxx",
"cloudProviderBackoffExponent": "xxxx",
"cloudProviderBackoffDuration": "xxxx",
"cloudProviderBackoffJitter": "xxxx",
"cloudProviderRatelimit": "xxxx",
"cloudProviderRateLimitQPS": "xxxx",
"cloudProviderRateLimitBucket": "xxxx",
"useManagedIdentityExtension": "xxxx",
"userAssignedIdentityID": "xxxx",
"useInstanceMetadata": true,
"loadBalancerSku": "xxxx",
"excludeMasterFromStandardLB": "xxxx",
"providerVaultName": "xxxx",
"maximumLoadBalancerRuleCount": "xxxx",
"providerKeyName": "xxxx",
"providerKeyVersion": "xxxx"
}`
It("should deserialize correctly", func() {
var cpConfig CloudProviderConfig
err := json.Unmarshal([]byte(cpConfigFile), &cpConfig)
Ω(err).ToNot(HaveOccurred())
Ω(cpConfig.TenantID).To(Equal("t"))
Ω(cpConfig.Region).To(Equal("l"))
})
})
Context("test RouteTableID func", func() {
It("generate correct route table ID", func() {
expectedRouteTable := "/subscriptions/subID/resourceGroups/resGp/providers/Microsoft.Network/routeTables/rt"
Expect(RouteTableID(SubscriptionID("subID"), ResourceGroup("resGp"), ResourceName("rt"))).To(Equal(expectedRouteTable))
})
})
Context("test ApplicationGatewayID func", func() {
It("generate correct application gateway ID", func() {
expectedGatewayID := "/subscriptions/subID/resourceGroups/resGp/providers/Microsoft.Network/applicationGateways/gateway"
Expect(ApplicationGatewayID(SubscriptionID("subID"), ResourceGroup("resGp"), ResourceName("gateway"))).To(Equal(expectedGatewayID))
})
})
Context("test ResourceGroupID func", func() {
It("generate correct resource group ID", func() {
expectedGroupID := "/subscriptions/subID/resourceGroups/resGp"
Expect(ResourceGroupID(SubscriptionID("subID"), ResourceGroup("resGp"))).To(Equal(expectedGroupID))
})
})
Context("test ParseSubResourceID func", func() {
It("parses sub resource ID correctly", func() {
subResourceID := "/subscriptions/subID/resourceGroups/resGp/providers/Microsoft.Network/applicationGateways/appgw/sslCertificates/cert"
subID, resourceGp, resource, subResource := ParseSubResourceID(subResourceID)
Expect(subID).To(Equal(SubscriptionID("subID")))
Expect(resourceGp).To(Equal(ResourceGroup("resGp")))
Expect(resource).To(Equal(ResourceName("appgw")))
Expect(subResource).To(Equal(ResourceName("cert")))
})
It("should give error if segements are less", func() {
subResourceID := "/subscriptions/subID/resourceGroups/resGp/providers/Microsoft.Network/applicationGateways/appgw"
subID, resourceGp, resource, subResource := ParseSubResourceID(subResourceID)
Expect(subID).To(Equal(SubscriptionID("")))
Expect(resourceGp).To(Equal(ResourceGroup("")))
Expect(resource).To(Equal(ResourceName("")))
Expect(subResource).To(Equal(ResourceName("")))
})
})
Context("test GetOperationIDFromPollingURL func", func() {
It("should be able to parse operationID", func() {
pollingURL := "https://management.azure.com/subscriptions/87654321-abcd-1234-b193-d305572e416f/providers/Microsoft.Network/locations/eastus2/operations/c24da597-9666-4950-a9f7-10bdfa17883d?api-version=2020-05-01"
expectedOperationID := "c24da597-9666-4950-a9f7-10bdfa17883d"
Expect(GetOperationIDFromPollingURL(pollingURL)).To(Equal(expectedOperationID))
})
It("should give empty string when unable to parse", func() {
pollingURL := "random"
expectedOperationID := ""
Expect(GetOperationIDFromPollingURL(pollingURL)).To(Equal(expectedOperationID))
})
})
})
})