-
Notifications
You must be signed in to change notification settings - Fork 70
/
utils_test.go
94 lines (84 loc) · 3.09 KB
/
utils_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
package main
import (
"reflect"
"testing"
)
func TestCreateResourceLabels(t *testing.T) {
var cases = []struct {
url string
want map[string]string
}{
{
"/subscriptions/abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6/resourceGroups/prod-rg-001/providers/Microsoft.Compute/virtualMachines/prod-vm-01/providers/microsoft.insights/metrics",
map[string]string{"resource_group": "prod-rg-001", "resource_name": "prod-vm-01"},
},
{
"/subscriptions/abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6/resourceGroups/prod-rg-002/providers/Microsoft.Sql/servers/sqlprod/databases/prod-db-01/providers/microsoft.insights/metrics",
map[string]string{"resource_group": "prod-rg-002", "resource_name": "sqlprod", "sub_resource_name": "prod-db-01"},
},
}
for _, c := range cases {
got := CreateResourceLabels(c.url)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("doesn't create expected resource labels\ngot: %v\nwant: %v", got, c.want)
}
}
}
func TestCreateAllResourceLabelsFrom(t *testing.T) {
var cases = []struct {
rm resourceMeta
want map[string]string
}{
{
resourceMeta{
resourceURL: "/subscriptions/abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6/resourceGroups/prod-rg-001/providers/Microsoft.Compute/virtualMachines/prod-vm-01/providers/microsoft.insights/metrics",
resource: AzureResource{
ID: "/resourceGroups/prod-rg-001/providers/Microsoft.Compute/virtualMachines/prod-vm-01",
Name: "fxpromdev01",
Location: "canadaeast",
Type: "Microsoft.Compute/virtualMachines",
Tags: map[string]string{"monitoring": "enabled", "department": "secret"},
Subscription: "abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6",
},
},
map[string]string{
"azure_location": "canadaeast",
"azure_subscription": "abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6",
"id": "/resourceGroups/prod-rg-001/providers/Microsoft.Compute/virtualMachines/prod-vm-01",
"managed_by": "",
"resource_group": "prod-rg-001",
"resource_name": "prod-vm-01",
"resource_type": "Microsoft.Compute/virtualMachines",
"tag_department": "secret",
"tag_monitoring": "enabled",
},
},
}
for _, c := range cases {
got := CreateAllResourceLabelsFrom(c.rm)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("doesn't create expected resource labels\ngot: %v\nwant: %v", got, c.want)
}
}
}
func TestCetResourceType(t *testing.T) {
var cases = []struct {
url string
want string
}{
{
"/subscriptions/abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6/resourceGroups/prod-rg-001/providers/Microsoft.Compute/virtualMachines/prod-vm-01/providers/microsoft.insights/metrics",
"Microsoft.Compute/virtualMachines",
},
{
"/subscriptions/abc123d4-e5f6-g7h8-i9j10-a1b2c3d4e5f6/resourceGroups/prod-rg-002/providers/Microsoft.Sql/servers/sqlprod/databases/prod-db-01/providers/microsoft.insights/metrics",
"Microsoft.Sql/servers/databases",
},
}
for _, c := range cases {
got := GetResourceType(c.url)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("doesn't create expected resource type\ngot: %v\nwant: %v", got, c.want)
}
}
}