2022-03-02 08:41:07 -06:00
|
|
|
package metrics
|
2019-02-09 17:23:12 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-08 10:49:46 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-09 17:23:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestURLBuilder(t *testing.T) {
|
2021-10-25 10:08:24 -05:00
|
|
|
t.Run("AzureMonitor URL Builder", func(t *testing.T) {
|
2022-03-31 10:20:29 -05:00
|
|
|
t.Run("when only resource uri is provided it returns resource/uri/providers/microsoft.insights/metrics", func(t *testing.T) {
|
2019-02-11 06:27:08 -06:00
|
|
|
ub := &urlBuilder{
|
2022-04-08 10:49:46 -05:00
|
|
|
ResourceURI: "/subscriptions/sub/resource/uri",
|
2019-02-09 17:23:12 -06:00
|
|
|
}
|
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/sub/resource/uri/providers/microsoft.insights/metrics", url)
|
2019-05-21 05:28:30 -05:00
|
|
|
})
|
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
t.Run("when resource uri and legacy fields are provided the legacy fields are ignored", func(t *testing.T) {
|
2019-05-21 05:28:30 -05:00
|
|
|
ub := &urlBuilder{
|
2022-04-08 10:49:46 -05:00
|
|
|
ResourceURI: "/subscriptions/sub/resource/uri",
|
2019-05-21 05:28:30 -05:00
|
|
|
DefaultSubscription: "default-sub",
|
|
|
|
ResourceGroup: "rg",
|
2022-03-31 10:20:29 -05:00
|
|
|
MetricDefinition: "Microsoft.NetApp/netAppAccounts/capacityPools/volumes",
|
|
|
|
ResourceName: "rn1/rn2/rn3",
|
2019-05-21 05:28:30 -05:00
|
|
|
}
|
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/sub/resource/uri/providers/microsoft.insights/metrics", url)
|
2019-02-09 17:23:12 -06:00
|
|
|
})
|
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
t.Run("Legacy URL Builder params", func(t *testing.T) {
|
|
|
|
t.Run("when metric definition is in the short form", func(t *testing.T) {
|
|
|
|
ub := &urlBuilder{
|
|
|
|
DefaultSubscription: "default-sub",
|
|
|
|
ResourceGroup: "rg",
|
|
|
|
MetricDefinition: "Microsoft.Compute/virtualMachines",
|
|
|
|
ResourceName: "rn",
|
|
|
|
}
|
2019-02-09 17:23:12 -06:00
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/default-sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/rn/providers/microsoft.insights/metrics", url)
|
2022-03-31 10:20:29 -05:00
|
|
|
})
|
2019-02-09 17:23:12 -06:00
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
t.Run("when metric definition is in the short form and a subscription is defined", func(t *testing.T) {
|
|
|
|
ub := &urlBuilder{
|
|
|
|
DefaultSubscription: "default-sub",
|
|
|
|
Subscription: "specified-sub",
|
|
|
|
ResourceGroup: "rg",
|
|
|
|
MetricDefinition: "Microsoft.Compute/virtualMachines",
|
|
|
|
ResourceName: "rn",
|
|
|
|
}
|
2019-02-09 17:23:12 -06:00
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/specified-sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/rn/providers/microsoft.insights/metrics", url)
|
2022-03-31 10:20:29 -05:00
|
|
|
})
|
2021-06-21 07:05:13 -05:00
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
t.Run("when metric definition is Microsoft.Storage/storageAccounts/blobServices", func(t *testing.T) {
|
|
|
|
ub := &urlBuilder{
|
|
|
|
DefaultSubscription: "default-sub",
|
|
|
|
ResourceGroup: "rg",
|
|
|
|
MetricDefinition: "Microsoft.Storage/storageAccounts/blobServices",
|
|
|
|
ResourceName: "rn1/default",
|
|
|
|
}
|
|
|
|
|
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/default-sub/resourceGroups/rg/providers/Microsoft.Storage/storageAccounts/rn1/blobServices/default/providers/microsoft.insights/metrics", url)
|
2022-03-31 10:20:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when metric definition is Microsoft.Storage/storageAccounts/fileServices", func(t *testing.T) {
|
|
|
|
ub := &urlBuilder{
|
|
|
|
DefaultSubscription: "default-sub",
|
|
|
|
ResourceGroup: "rg",
|
|
|
|
MetricDefinition: "Microsoft.Storage/storageAccounts/fileServices",
|
|
|
|
ResourceName: "rn1/default",
|
|
|
|
}
|
|
|
|
|
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/default-sub/resourceGroups/rg/providers/Microsoft.Storage/storageAccounts/rn1/fileServices/default/providers/microsoft.insights/metrics", url)
|
2022-03-31 10:20:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when metric definition is Microsoft.NetApp/netAppAccounts/capacityPools/volumes", func(t *testing.T) {
|
|
|
|
ub := &urlBuilder{
|
|
|
|
DefaultSubscription: "default-sub",
|
|
|
|
ResourceGroup: "rg",
|
|
|
|
MetricDefinition: "Microsoft.NetApp/netAppAccounts/capacityPools/volumes",
|
|
|
|
ResourceName: "rn1/rn2/rn3",
|
|
|
|
}
|
2021-06-21 07:05:13 -05:00
|
|
|
|
2022-03-31 10:20:29 -05:00
|
|
|
url := ub.BuildMetricsURL()
|
2022-04-08 10:49:46 -05:00
|
|
|
assert.Equal(t, "/subscriptions/default-sub/resourceGroups/rg/providers/Microsoft.NetApp/netAppAccounts/rn1/capacityPools/rn2/volumes/rn3/providers/microsoft.insights/metrics", url)
|
2022-03-31 10:20:29 -05:00
|
|
|
})
|
2021-06-21 07:05:13 -05:00
|
|
|
})
|
2019-02-09 17:23:12 -06:00
|
|
|
})
|
|
|
|
}
|