-
Notifications
You must be signed in to change notification settings - Fork 190
/
azure_test.go
63 lines (56 loc) · 1.57 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
package apmcloudutil
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"go.elastic.co/apm/model"
)
func TestAzureCloudMetadata(t *testing.T) {
srv, client := newAzureMetadataServer()
defer srv.Close()
for _, provider := range []Provider{Auto, Azure} {
var out model.Cloud
var logger testLogger
assert.True(t, provider.getCloudMetadata(context.Background(), client, &logger, &out))
assert.Zero(t, logger)
assert.Equal(t, model.Cloud{
Provider: "azure",
Region: "westus2",
Instance: &model.CloudInstance{
ID: "e11ebedc-019d-427f-84dd-56cd4388d3a8",
Name: "basepi-test",
},
Machine: &model.CloudMachine{
Type: "Standard_D2s_v3",
},
Project: &model.CloudProject{
Name: "basepi-testing",
},
Account: &model.CloudAccount{
ID: "7657426d-c4c3-44ac-88a2-3b2cd59e6dba",
},
}, out)
}
}
func newAzureMetadataServer() (*httptest.Server, *http.Client) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/metadata/instance/compute" {
w.WriteHeader(http.StatusNotFound)
return
}
w.Write([]byte(`{
"location": "westus2",
"name": "basepi-test",
"resourceGroupName": "basepi-testing",
"subscriptionId": "7657426d-c4c3-44ac-88a2-3b2cd59e6dba",
"vmId": "e11ebedc-019d-427f-84dd-56cd4388d3a8",
"vmScaleSetName": "",
"vmSize": "Standard_D2s_v3",
"zone": ""
}`))
}))
client := &http.Client{Transport: newTargetedRoundTripper("169.254.169.254", srv.Listener.Addr().String())}
return srv, client
}