forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
288 lines (241 loc) · 9.23 KB
/
example_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package linodego_test
/**
* The tests in the examples directory demontrate use and test the library
* in a real-use setting
*
* cd examples && go test -test.v
*/
import (
"context"
"fmt"
"log"
"strings"
"github.com/linode/linodego"
)
func ExampleClient_ListTypes_all() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListTypes_all")
defer teardown()
types, err := linodeClient.ListTypes(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("ID contains class:", strings.Contains(types[0].ID, string(types[0].Class)))
fmt.Println("Plan has Ram:", types[0].Memory > 0)
// Output:
// ID contains class: true
// Plan has Ram: true
}
// ExampleGetType_missing demonstrates the Error type, which allows inspecting
// the request and response. Error codes will be the HTTP status code,
// or sub-100 for errors before the request was issued.
func ExampleClient_GetType_missing() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetType_missing")
defer teardown()
_, err := linodeClient.GetType(context.Background(), "missing-type")
if err != nil {
if v, ok := err.(*linodego.Error); ok {
fmt.Println("Request was:", v.Response.Request.URL)
fmt.Println("Response was:", v.Response.Status)
fmt.Println("Error was:", v)
}
}
// Output:
// Request was: https://api.linode.com/v4/linode/types/missing-type
// Response was: 404 NOT FOUND
// Error was: [404] Not found
}
// ExampleListKernels_all Demonstrates how to list all Linode Kernels. Paginated
// responses are automatically traversed and concatenated when the ListOptions are nil
func ExampleClient_ListKernels_all() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_all")
defer teardown()
kernels, err := linodeClient.ListKernels(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
// The Linode API default pagination size is 100.
fmt.Println("Fetched > 100:", len(kernels) > 100)
// Output:
// Fetched > 100: true
}
func ExampleClient_ListKernels_allWithOpts() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_allWithOpts")
defer teardown()
filterOpt := linodego.NewListOptions(0, "")
kernels, err := linodeClient.ListKernels(context.Background(), filterOpt)
if err != nil {
log.Fatal(err)
}
// The Linode API default pagination size is 100.
fmt.Println("Fetched > 100:", len(kernels) > 100)
fmt.Println("Fetched Results/100 pages:", filterOpt.Pages > filterOpt.Results/100)
fmt.Println("Fetched all results:", filterOpt.Results == len(kernels))
// Output:
// Fetched > 100: true
// Fetched Results/100 pages: true
// Fetched all results: true
}
func ExampleClient_ListKernels_filtered() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_filtered")
defer teardown()
filterOpt := linodego.ListOptions{Filter: "{\"label\":\"Recovery - Finnix (kernel)\"}"}
kernels, err := linodeClient.ListKernels(context.Background(), &filterOpt)
if err != nil {
log.Fatal(err)
}
for _, kern := range kernels {
fmt.Println(kern.ID, kern.Label)
}
// Unordered output:
// linode/finnix Recovery - Finnix (kernel)
// linode/finnix-legacy Recovery - Finnix (kernel)
}
func ExampleClient_ListKernels_page1() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_page1")
defer teardown()
filterOpt := linodego.NewListOptions(1, "")
kernels, err := linodeClient.ListKernels(context.Background(), filterOpt)
if err != nil {
log.Fatal(err)
}
// The Linode API default pagination size is 100.
fmt.Println("Fetched == 100:", len(kernels) == 100)
fmt.Println("Results > 100:", filterOpt.Results > 100)
fmt.Println("Pages > 1:", filterOpt.Pages > 1)
k := kernels[len(kernels)-1]
fmt.Println("Kernel Version in ID:", strings.Contains(k.ID, k.Label))
// Output:
// Fetched == 100: true
// Results > 100: true
// Pages > 1: true
// Kernel Version in ID: true
}
func ExampleClient_GetKernel_specific() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetKernel_specific")
defer teardown()
l32, err := linodeClient.GetKernel(context.Background(), "linode/latest-32bit")
if err == nil {
fmt.Println("Label starts:", l32.Label[0:9])
} else {
log.Fatalln(err)
}
l64, err := linodeClient.GetKernel(context.Background(), "linode/latest-64bit")
if err == nil {
fmt.Println("Label starts:", l64.Label[0:9])
} else {
log.Fatalln(err)
}
// Interference check
fmt.Println("First Label still starts:", l32.Label[0:9])
// Output:
// Label starts: Latest 32
// Label starts: Latest 64
// First Label still starts: Latest 32
}
func ExampleClient_GetImage_missing() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetImage_missing")
defer teardown()
_, err := linodeClient.GetImage(context.Background(), "not-found")
if err != nil {
if v, ok := err.(*linodego.Error); ok {
fmt.Println("Request was:", v.Response.Request.URL)
fmt.Println("Response was:", v.Response.Status)
fmt.Println("Error was:", v)
}
}
// Output:
// Request was: https://api.linode.com/v4/images/not-found
// Response was: 404 NOT FOUND
// Error was: [404] Not found
}
func ExampleClient_ListImages_all() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListImages_all")
defer teardown()
filterOpt := linodego.NewListOptions(0, "")
images, err := linodeClient.ListImages(context.Background(), filterOpt)
if err != nil {
log.Fatal(err)
}
fmt.Println("Fetched Results/100 pages:", filterOpt.Pages > filterOpt.Results/100)
fmt.Println("Fetched all results:", filterOpt.Results == len(images))
// Output:
// Fetched Results/100 pages: true
// Fetched all results: true
}
// ExampleListImages_notfound demonstrates that an empty slice is returned,
// not an error, when a filter matches no results.
func ExampleClient_ListImages_notfound() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListImages_notfound")
defer teardown()
filterOpt := linodego.ListOptions{Filter: "{\"label\":\"not-found\"}"}
images, err := linodeClient.ListImages(context.Background(), &filterOpt)
if err != nil {
log.Fatal(err)
}
fmt.Println("Images with Label 'not-found':", len(images))
// Output:
// Images with Label 'not-found': 0
}
// ExampleListImages_notfound demonstrates that an error is returned by
// the API and linodego when an invalid filter is provided
func ExampleClient_ListImages_badfilter() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListImages_badfilter")
defer teardown()
filterOpt := linodego.ListOptions{Filter: "{\"foo\":\"bar\"}"}
images, err := linodeClient.ListImages(context.Background(), &filterOpt)
if err == nil {
log.Fatal(err)
}
fmt.Println("Error given on bad filter:", err)
fmt.Println("Images on bad filter:", images) // TODO: nil would be better here
// Output:
// Error given on bad filter: [400] [X-Filter] Cannot filter on foo
// Images on bad filter: []
}
func ExampleClient_ListLongviewSubscriptions_page1() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListLongviewSubscriptions_page1")
defer teardown()
pageOpt := linodego.ListOptions{PageOptions: &linodego.PageOptions{Page: 1}}
subscriptions, err := linodeClient.ListLongviewSubscriptions(context.Background(), &pageOpt)
if err != nil {
log.Fatal(err)
}
fmt.Println("Longview Subscription Types:", len(subscriptions))
// Output:
// Longview Subscription Types: 4
}
func ExampleClient_ListStackscripts_page1() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListStackscripts_page1")
defer teardown()
filterOpt := linodego.NewListOptions(1, "")
scripts, err := linodeClient.ListStackscripts(context.Background(), filterOpt)
if err != nil {
log.Fatal(err)
}
// The Linode API default pagination size is 100.
fmt.Println("Fetched == 100:", len(scripts) == 100)
fmt.Println("Results > 100:", filterOpt.Results > 100)
fmt.Println("Pages > 1:", filterOpt.Pages > 1)
s := scripts[len(scripts)-1]
fmt.Println("StackScript Script has shebang:", strings.Contains(s.Script, "#!/"))
fmt.Println("Created is parsed:", s.Created != nil)
// Output:
// Fetched == 100: true
// Results > 100: true
// Pages > 1: true
// StackScript Script has shebang: true
// Created is parsed: true
}