This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiles_service_test.go
171 lines (147 loc) · 3.19 KB
/
profiles_service_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package databricks
import (
"bytes"
"context"
"encoding/json"
"net/http"
"testing"
)
func badTransportProfilesHelper(t *testing.T) *ProfilesService {
badTransportClient, err := NewClient(
"test-account",
ClientHTTPClient(BadTransportHTTPClient),
)
if err != nil {
t.Fatal(err)
}
if badTransportClient == nil {
t.Fatalf("NewClient returned nil")
}
profiles := badTransportClient.Profiles()
if profiles == nil {
t.Fatalf("Profiles returned nil")
}
return profiles
}
func non200ProfilesHelper(t *testing.T) *ProfilesService {
non200Client, err := NewClient(
"test-account",
ClientHTTPClient(Non200HTTPClient),
)
if err != nil {
t.Fatal(err)
}
if non200Client == nil {
t.Fatalf("NewClient returned nil")
}
profiles := non200Client.Profiles()
if profiles == nil {
t.Fatalf("Profiles returned nil")
}
return profiles
}
func successProfilesHelper(
t *testing.T,
res []byte,
code int,
) *ProfilesService {
successClient, err := NewClient(
"test-account",
ClientHTTPClient(injectedHTTPClient(
http.Response{
StatusCode: code,
Body: nopCloser{
bytes.NewBuffer(res),
},
},
)),
)
if err != nil {
t.Fatal(err)
}
if successClient == nil {
t.Fatalf("NewClient returned nil")
}
profiles := successClient.Profiles()
if profiles == nil {
t.Fatalf("Profiles returned nil")
}
return profiles
}
func Test_ProfilesService_Add(t *testing.T) {
t.Parallel()
profiles := successProfilesHelper(t, []byte{}, http.StatusOK)
ctx := context.Background()
err := profiles.Add(ctx, "profile-123", false)
if err != nil {
t.Fatal(err)
}
// Non 200 test
profiles = non200ProfilesHelper(t)
err = profiles.Add(ctx, "profile-123", false)
if err == nil {
t.Fatalf("Expected error to not be nil")
}
// Transport error test
profiles = badTransportProfilesHelper(t)
err = profiles.Add(ctx, "profile-123", false)
if err == nil {
t.Fatalf("Expected error to not be nil")
}
}
func Test_ProfilesService_List(t *testing.T) {
t.Parallel()
res, err := json.Marshal(struct {
InstanceProfiles []string
}{
[]string{
"foo",
"bar",
},
})
if err != nil {
t.Fatal(err)
}
profiles := successProfilesHelper(t, res, http.StatusOK)
ctx := context.Background()
profs, err := profiles.List(ctx)
if err != nil {
t.Fatal(err)
}
if len(profs) == 0 {
t.Fatalf("Expected more than 0 profiles")
}
// Non 200 test
profiles = non200ProfilesHelper(t)
_, err = profiles.List(ctx)
if err == nil {
t.Fatalf("Expected error to not be nil")
}
// Transport error test
profiles = badTransportProfilesHelper(t)
_, err = profiles.List(ctx)
if err == nil {
t.Fatalf("Expected error to not be nil")
}
}
func Test_ProfilesService_Remove(t *testing.T) {
t.Parallel()
profiles := successProfilesHelper(t, []byte{}, http.StatusOK)
ctx := context.Background()
err := profiles.Remove(ctx, "profile-123")
if err != nil {
t.Fatal(err)
}
// Non 200 test
profiles = non200ProfilesHelper(t)
err = profiles.Remove(ctx, "profile-123")
if err == nil {
t.Fatalf("Expected error to not be nil")
}
// Transport error test
profiles = badTransportProfilesHelper(t)
err = profiles.Remove(ctx, "profile-123")
if err == nil {
t.Fatalf("Expected error to not be nil")
}
}