Path: blob/main/component/discovery/azure/azure_test.go
4095 views
package azure12import (3"net/url"4"testing"5"time"67"github.com/grafana/agent/component/common/config"8"github.com/grafana/agent/pkg/river"9"github.com/prometheus/common/model"10"github.com/stretchr/testify/require"11"gotest.tools/assert"12)1314func TestRiverUnmarshal(t *testing.T) {15riverCfg := `16environment = "AzureTestCloud"17port = 808018subscription_id = "subid"19refresh_interval = "10m"20resource_group = "test"21oauth {22client_id = "clientid"23tenant_id = "tenantid"24client_secret = "clientsecret"25}26enable_http2 = true27follow_redirects = false28proxy_url = "http://example:8080"`2930var args Arguments31err := river.Unmarshal([]byte(riverCfg), &args)32require.NoError(t, err)3334assert.Equal(t, "AzureTestCloud", args.Environment)35assert.Equal(t, 8080, args.Port)36assert.Equal(t, "subid", args.SubscriptionID)37assert.Equal(t, 10*time.Minute, args.RefreshInterval)38assert.Equal(t, "test", args.ResourceGroup)39assert.Equal(t, "clientid", args.OAuth.ClientID)40assert.Equal(t, "tenantid", args.OAuth.TenantID)41assert.Equal(t, "clientsecret", string(args.OAuth.ClientSecret))42assert.Equal(t, true, args.EnableHTTP2)43assert.Equal(t, false, args.FollowRedirects)44assert.Equal(t, "http://example:8080", args.ProxyURL.String())45}4647func TestRiverUnmarshal_OAuthRequiredFields(t *testing.T) {48riverCfg := `49environment = "AzureTestCloud"50port = 808051subscription_id = "subid"52refresh_interval = "10m"53resource_group = "test"54oauth {55client_id = "clientid"56}`57var args Arguments58err := river.Unmarshal([]byte(riverCfg), &args)59require.Error(t, err)60}6162func TestValidate(t *testing.T) {63noAuth := `64environment = "AzureTestCloud"65port = 808066subscription_id = "subid"67refresh_interval = "10m"68resource_group = "test"`6970var args Arguments71err := river.Unmarshal([]byte(noAuth), &args)72require.ErrorContains(t, err, "exactly one of oauth or managed_identity must be specified")7374bothAuth := `75environment = "AzureTestCloud"76port = 808077subscription_id = "subid"78refresh_interval = "10m"79resource_group = "test"80oauth {81client_id = "clientid"82tenant_id = "tenantid"83client_secret = "clientsecret"84}85managed_identity {86client_id = "clientid"87}`88var args2 Arguments89err = river.Unmarshal([]byte(bothAuth), &args2)90require.ErrorContains(t, err, "exactly one of oauth or managed_identity must be specified")9192invalidTLS := `93environment = "AzureTestCloud"94port = 808095subscription_id = "subid"96refresh_interval = "10m"97resource_group = "test"98managed_identity {99client_id = "clientid"100}101tls_config {102cert_file = "certfile"103cert_pem = "certpem"104}`105var args3 Arguments106err = river.Unmarshal([]byte(invalidTLS), &args3)107require.ErrorContains(t, err, "at most one of cert_pem and cert_file must be configured")108}109110func TestConvert(t *testing.T) {111proxyUrl, _ := url.Parse("http://example:8080")112riverArgsOAuth := Arguments{113Environment: "AzureTestCloud",114Port: 8080,115SubscriptionID: "subid",116RefreshInterval: 10 * time.Minute,117ResourceGroup: "test",118OAuth: &OAuth{119ClientID: "clientid",120TenantID: "tenantid",121ClientSecret: "clientsecret",122},123FollowRedirects: false,124EnableHTTP2: false,125ProxyURL: config.URL{126URL: proxyUrl,127},128}129130promArgs := riverArgsOAuth.Convert()131assert.Equal(t, "AzureTestCloud", promArgs.Environment)132assert.Equal(t, 8080, promArgs.Port)133assert.Equal(t, "subid", promArgs.SubscriptionID)134assert.Equal(t, model.Duration(10*time.Minute), promArgs.RefreshInterval)135assert.Equal(t, "test", promArgs.ResourceGroup)136assert.Equal(t, "clientid", promArgs.ClientID)137assert.Equal(t, "tenantid", promArgs.TenantID)138assert.Equal(t, "clientsecret", string(promArgs.ClientSecret))139assert.Equal(t, false, promArgs.HTTPClientConfig.FollowRedirects)140assert.Equal(t, false, promArgs.HTTPClientConfig.EnableHTTP2)141assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())142143riverArgsManagedIdentity := Arguments{144Environment: "AzureTestCloud",145Port: 8080,146SubscriptionID: "subid",147RefreshInterval: 10 * time.Minute,148ResourceGroup: "test",149ManagedIdentity: &ManagedIdentity{150ClientID: "clientid",151},152FollowRedirects: true,153EnableHTTP2: true,154ProxyURL: config.URL{155URL: proxyUrl,156},157}158159promArgs = riverArgsManagedIdentity.Convert()160assert.Equal(t, "AzureTestCloud", promArgs.Environment)161assert.Equal(t, 8080, promArgs.Port)162assert.Equal(t, "subid", promArgs.SubscriptionID)163assert.Equal(t, model.Duration(10*time.Minute), promArgs.RefreshInterval)164assert.Equal(t, "test", promArgs.ResourceGroup)165assert.Equal(t, "clientid", promArgs.ClientID)166assert.Equal(t, "", promArgs.TenantID)167assert.Equal(t, "", string(promArgs.ClientSecret))168assert.Equal(t, true, promArgs.HTTPClientConfig.FollowRedirects)169assert.Equal(t, true, promArgs.HTTPClientConfig.EnableHTTP2)170assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())171}172173174