-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuserdata_test.go
117 lines (88 loc) · 4.29 KB
/
userdata_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package metadata
import (
"context"
"encoding/base64"
"fmt"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
var mockMetadataHost = fmt.Sprintf("%s://%s/%s", APIProto, APIHost, APIVersion)
func SetupMockClient() *http.Client {
// create mock client
mockClient := http.DefaultClient
httpmock.ActivateNonDefault(mockClient)
// Mock out token request
tokenResponder := httpmock.NewStringResponder(200, "[\"token\"]")
httpmock.RegisterResponder("PUT", fmt.Sprintf("%s/token", mockMetadataHost), tokenResponder)
return mockClient
}
func TestGetUserData_Success(t *testing.T) {
mockClient := SetupMockClient()
// Mock out user-data response with the encoded value for "mock-user-data"
instanceResponder := httpmock.NewStringResponder(200, "bW9jay11c2VyLWRhdGE=")
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/user-data", mockMetadataHost), instanceResponder)
newClient, err := NewClient(context.Background(), func(options *clientCreateConfig) {
options.HTTPClient = mockClient
})
assert.NoError(t, err, "Expected no error")
userData, err := newClient.GetUserData(context.Background())
assert.NoError(t, err, "Expected no error")
assert.Equal(t, "mock-user-data", userData, "Unexpected user data")
}
func TestGetUserDataGzip_Success(t *testing.T) {
mockClient := SetupMockClient()
// Mock out user-data response with the gzipped encoded value for "mock-user-data"
instanceResponder := httpmock.NewStringResponder(200, "H4sIAO0n32YAA8vNT87WLS1OLdJNSSxJBACRtuznDgAAAA==")
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/user-data", mockMetadataHost), instanceResponder)
newClient, err := NewClient(context.Background(), func(options *clientCreateConfig) {
options.HTTPClient = mockClient
})
assert.NoError(t, err, "Expected no error")
userData, err := newClient.GetUserData(context.Background())
assert.NoError(t, err, "Expected no error")
assert.Equal(t, "mock-user-data", userData, "Unexpected user data")
}
func TestGetUserDataGzip_Error(t *testing.T) {
mockClient := SetupMockClient()
// Mock out user-data response with the invalid gzip encoded value for "mock-user-data"
invalidGzipData := []byte{0x1F, 0x8B, 0x08, 0x23}
userDataResponse := base64.StdEncoding.EncodeToString(invalidGzipData)
instanceResponder := httpmock.NewStringResponder(200, userDataResponse)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/user-data", mockMetadataHost), instanceResponder)
newClient, err := NewClient(context.Background(), func(options *clientCreateConfig) {
options.HTTPClient = mockClient
})
assert.NoError(t, err, "Expected no error")
userData, err := newClient.GetUserData(context.Background())
assert.EqualErrorf(t, err, "failed to ungzip user-data: unexpected EOF", "Unexpected error message")
assert.Equal(t, "", userData, "expected Empty Userdata")
}
func TestGetUserData_Error(t *testing.T) {
mockClient := SetupMockClient()
instanceResponder := httpmock.NewStringResponder(500, "{\"errors\": [{\"reason\": \"failed to get metadata\"}]}")
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/user-data", mockMetadataHost), instanceResponder)
newClient, err := NewClient(context.Background(), func(options *clientCreateConfig) {
options.HTTPClient = mockClient
})
assert.NoError(t, err, "Expected no error")
userData, err := newClient.GetUserData(context.Background())
assert.Error(t, err, "Expected an error")
assert.Equal(t, "", userData, "Expected empty user data")
assert.EqualErrorf(t, err, "[500] failed to get metadata", "Unexpected error message")
}
func TestGetUserDataDecode_Error(t *testing.T) {
mockClient := SetupMockClient()
// Mock out user-data response with the gzipped encoded value for "mock-user-data"
instanceResponder := httpmock.NewStringResponder(200, "invalid base64")
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/user-data", mockMetadataHost), instanceResponder)
newClient, err := NewClient(context.Background(), func(options *clientCreateConfig) {
options.HTTPClient = mockClient
})
assert.NoError(t, err, "Expected no error")
userData, err := newClient.GetUserData(context.Background())
assert.Error(t, err, "Expected an error")
assert.Equal(t, "", userData, "Expected empty user data")
assert.EqualErrorf(t, err, "failed to decode user-data: illegal base64 data at input byte 7", "Unexpected error message")
}