Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/azure/azure_test.go
4095 views
1
package azure
2
3
import (
4
"net/url"
5
"testing"
6
"time"
7
8
"github.com/grafana/agent/component/common/config"
9
"github.com/grafana/agent/pkg/river"
10
"github.com/prometheus/common/model"
11
"github.com/stretchr/testify/require"
12
"gotest.tools/assert"
13
)
14
15
func TestRiverUnmarshal(t *testing.T) {
16
riverCfg := `
17
environment = "AzureTestCloud"
18
port = 8080
19
subscription_id = "subid"
20
refresh_interval = "10m"
21
resource_group = "test"
22
oauth {
23
client_id = "clientid"
24
tenant_id = "tenantid"
25
client_secret = "clientsecret"
26
}
27
enable_http2 = true
28
follow_redirects = false
29
proxy_url = "http://example:8080"`
30
31
var args Arguments
32
err := river.Unmarshal([]byte(riverCfg), &args)
33
require.NoError(t, err)
34
35
assert.Equal(t, "AzureTestCloud", args.Environment)
36
assert.Equal(t, 8080, args.Port)
37
assert.Equal(t, "subid", args.SubscriptionID)
38
assert.Equal(t, 10*time.Minute, args.RefreshInterval)
39
assert.Equal(t, "test", args.ResourceGroup)
40
assert.Equal(t, "clientid", args.OAuth.ClientID)
41
assert.Equal(t, "tenantid", args.OAuth.TenantID)
42
assert.Equal(t, "clientsecret", string(args.OAuth.ClientSecret))
43
assert.Equal(t, true, args.EnableHTTP2)
44
assert.Equal(t, false, args.FollowRedirects)
45
assert.Equal(t, "http://example:8080", args.ProxyURL.String())
46
}
47
48
func TestRiverUnmarshal_OAuthRequiredFields(t *testing.T) {
49
riverCfg := `
50
environment = "AzureTestCloud"
51
port = 8080
52
subscription_id = "subid"
53
refresh_interval = "10m"
54
resource_group = "test"
55
oauth {
56
client_id = "clientid"
57
}`
58
var args Arguments
59
err := river.Unmarshal([]byte(riverCfg), &args)
60
require.Error(t, err)
61
}
62
63
func TestValidate(t *testing.T) {
64
noAuth := `
65
environment = "AzureTestCloud"
66
port = 8080
67
subscription_id = "subid"
68
refresh_interval = "10m"
69
resource_group = "test"`
70
71
var args Arguments
72
err := river.Unmarshal([]byte(noAuth), &args)
73
require.ErrorContains(t, err, "exactly one of oauth or managed_identity must be specified")
74
75
bothAuth := `
76
environment = "AzureTestCloud"
77
port = 8080
78
subscription_id = "subid"
79
refresh_interval = "10m"
80
resource_group = "test"
81
oauth {
82
client_id = "clientid"
83
tenant_id = "tenantid"
84
client_secret = "clientsecret"
85
}
86
managed_identity {
87
client_id = "clientid"
88
}`
89
var args2 Arguments
90
err = river.Unmarshal([]byte(bothAuth), &args2)
91
require.ErrorContains(t, err, "exactly one of oauth or managed_identity must be specified")
92
93
invalidTLS := `
94
environment = "AzureTestCloud"
95
port = 8080
96
subscription_id = "subid"
97
refresh_interval = "10m"
98
resource_group = "test"
99
managed_identity {
100
client_id = "clientid"
101
}
102
tls_config {
103
cert_file = "certfile"
104
cert_pem = "certpem"
105
}`
106
var args3 Arguments
107
err = river.Unmarshal([]byte(invalidTLS), &args3)
108
require.ErrorContains(t, err, "at most one of cert_pem and cert_file must be configured")
109
}
110
111
func TestConvert(t *testing.T) {
112
proxyUrl, _ := url.Parse("http://example:8080")
113
riverArgsOAuth := Arguments{
114
Environment: "AzureTestCloud",
115
Port: 8080,
116
SubscriptionID: "subid",
117
RefreshInterval: 10 * time.Minute,
118
ResourceGroup: "test",
119
OAuth: &OAuth{
120
ClientID: "clientid",
121
TenantID: "tenantid",
122
ClientSecret: "clientsecret",
123
},
124
FollowRedirects: false,
125
EnableHTTP2: false,
126
ProxyURL: config.URL{
127
URL: proxyUrl,
128
},
129
}
130
131
promArgs := riverArgsOAuth.Convert()
132
assert.Equal(t, "AzureTestCloud", promArgs.Environment)
133
assert.Equal(t, 8080, promArgs.Port)
134
assert.Equal(t, "subid", promArgs.SubscriptionID)
135
assert.Equal(t, model.Duration(10*time.Minute), promArgs.RefreshInterval)
136
assert.Equal(t, "test", promArgs.ResourceGroup)
137
assert.Equal(t, "clientid", promArgs.ClientID)
138
assert.Equal(t, "tenantid", promArgs.TenantID)
139
assert.Equal(t, "clientsecret", string(promArgs.ClientSecret))
140
assert.Equal(t, false, promArgs.HTTPClientConfig.FollowRedirects)
141
assert.Equal(t, false, promArgs.HTTPClientConfig.EnableHTTP2)
142
assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())
143
144
riverArgsManagedIdentity := Arguments{
145
Environment: "AzureTestCloud",
146
Port: 8080,
147
SubscriptionID: "subid",
148
RefreshInterval: 10 * time.Minute,
149
ResourceGroup: "test",
150
ManagedIdentity: &ManagedIdentity{
151
ClientID: "clientid",
152
},
153
FollowRedirects: true,
154
EnableHTTP2: true,
155
ProxyURL: config.URL{
156
URL: proxyUrl,
157
},
158
}
159
160
promArgs = riverArgsManagedIdentity.Convert()
161
assert.Equal(t, "AzureTestCloud", promArgs.Environment)
162
assert.Equal(t, 8080, promArgs.Port)
163
assert.Equal(t, "subid", promArgs.SubscriptionID)
164
assert.Equal(t, model.Duration(10*time.Minute), promArgs.RefreshInterval)
165
assert.Equal(t, "test", promArgs.ResourceGroup)
166
assert.Equal(t, "clientid", promArgs.ClientID)
167
assert.Equal(t, "", promArgs.TenantID)
168
assert.Equal(t, "", string(promArgs.ClientSecret))
169
assert.Equal(t, true, promArgs.HTTPClientConfig.FollowRedirects)
170
assert.Equal(t, true, promArgs.HTTPClientConfig.EnableHTTP2)
171
assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())
172
}
173
174