-
Notifications
You must be signed in to change notification settings - Fork 13
/
azure_test.go
185 lines (176 loc) · 4.67 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
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func Test_parseAzureVolumeID(t *testing.T) {
type args struct {
volumeID string
}
tests := []struct {
name string
args args
wantSubscription string
wantResourceGroup string
wantDiskName string
wantErr bool
}{
{
name: "test using a correct volume ID",
args: args{volumeID: "/subscriptions/{subscription}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/disks/{diskname}"},
wantSubscription: "{subscription}",
wantResourceGroup: "{resourceGroup}",
wantDiskName: "{diskname}",
wantErr: false,
},
{
name: "test using a correct volume ID",
args: args{volumeID: "/subscriptions/{subscription}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/disks"},
wantSubscription: "",
wantResourceGroup: "",
wantDiskName: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
gotSubscription, gotResourceGroup, gotDiskName, err := parseAzureVolumeID(tt.args.volumeID)
if (err != nil) != tt.wantErr {
t.Errorf("parseAzureVolumeID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotSubscription != tt.wantSubscription {
t.Errorf("parseAzureVolumeID() gotSubscription = %v, want %v", gotSubscription, tt.wantSubscription)
}
if gotResourceGroup != tt.wantResourceGroup {
t.Errorf("parseAzureVolumeID() gotResourceGroup = %v, want %v", gotResourceGroup, tt.wantResourceGroup)
}
if gotDiskName != tt.wantDiskName {
t.Errorf("parseAzureVolumeID() gotDiskName = %v, want %v", gotDiskName, tt.wantDiskName)
}
})
}
}
func Test_sanitizeKeyForAzure(t *testing.T) {
type args struct {
s string
}
var tests = []struct {
name string
args args
want string
}{
{
name: "the key should be trimmed to 512 characters",
args: args{
s: strings.Repeat("1", 513),
},
want: strings.Repeat("1", 512),
},
{
name: "the key should remove all invalid characters",
args: args{
s: `1<>&\?%/`,
},
want: "1_______",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := sanitizeKeyForAzure(tt.args.s); got != tt.want {
t.Errorf("sanitizeKeyForAzure() = %v, want %v", got, tt.want)
}
})
}
}
func Test_sanitizeValueForAzure(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "a valid value",
args: args{strings.Repeat("1", 256)},
want: strings.Repeat("1", 256),
wantErr: false,
},
{
name: "the max value length is 256 characters",
args: args{strings.Repeat("1", 257)},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := sanitizeValueForAzure(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("sanitizeValueForAzure() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("sanitizeValueForAzure() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_sanitizeLabelsForAzure(t *testing.T) {
t.Run("azure supports up to 50 labels", func(t *testing.T) {
t.Parallel()
tags := map[string]string{}
for x := 0; x < 50; x++ {
v := fmt.Sprintf("%d", x)
tags[v] = v
}
_, err := sanitizeLabelsForAzure(tags)
assert.NoError(t, err)
tags["51"] = "51"
_, err = sanitizeLabelsForAzure(tags)
assert.ErrorIs(t, err, ErrAzureTooManyTags)
})
t.Run("the sanitize labels gives an error when there a duplicated tags after sanitization", func(t *testing.T) {
t.Parallel()
tags := map[string]string{}
tags["Kubernetes/Cluster"] = "foo"
tags["Kubernetes_Cluster"] = "bar"
_, err := sanitizeLabelsForAzure(tags)
assert.ErrorIs(t, err, ErrAzureDuplicatedTags)
})
}
func Test_diskScope(t *testing.T) {
type args struct {
subscription string
resourceGroupName string
diskName string
}
tests := []struct {
name string
args args
want string
}{
{
"it should generate a valid scope for disks",
args{
subscription: "sub",
resourceGroupName: "resource-name",
diskName: "disk-name",
},
"subscriptions/sub/resourceGroups/resource-name/providers/Microsoft.Compute/disks/disk-name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, diskScope(tt.args.subscription, tt.args.resourceGroupName, tt.args.diskName), "diskScope(%v, %v, %v)", tt.args.subscription, tt.args.resourceGroupName, tt.args.diskName)
})
}
}