forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_storage_test.go
186 lines (158 loc) · 5.19 KB
/
block_storage_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
package govultr
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestBlockStorageServiceHandler_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks", func(writer http.ResponseWriter, request *http.Request) {
response := `{"block":{"id":"123456","cost":10,"status":"active","size_gb":100,"region":"ewr","attached_to_instance":"","date_created":"01-01-1960","label":"mylabel", "mount_id": "ewr-123abc", "block_type": "test"}}`
fmt.Fprint(writer, response)
})
blockReq := &BlockStorageCreate{
Region: "ewr",
SizeGB: 100,
Label: "mylabel",
BlockType: "test",
}
blockStorage, err := client.BlockStorage.Create(ctx, blockReq)
if err != nil {
t.Errorf("BlockStorage.Create returned error: %v", err)
}
expected := &BlockStorage{
ID: "123456",
Cost: 10,
Status: "active",
SizeGB: 100,
Region: "ewr",
DateCreated: "01-01-1960",
AttachedToInstance: "",
Label: "mylabel",
MountID: "ewr-123abc",
BlockType: "test",
}
if !reflect.DeepEqual(blockStorage, expected) {
t.Errorf("BlockStorage.Create returned %+v, expected %+v", blockStorage, expected)
}
}
func TestBlockStorageServiceHandler_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks/123456", func(writer http.ResponseWriter, request *http.Request) {
response := `{"block":{"id":"123456","cost":10,"status":"active","size_gb":100,"region":"ewr","attached_to_instance":"","date_created":"01-01-1960","label":"mylabel", "mount_id": "123abc", "block_type": "test"}}`
fmt.Fprint(writer, response)
})
blockStorage, err := client.BlockStorage.Get(ctx, "123456")
if err != nil {
t.Errorf("BlockStorage.Create returned error: %v", err)
}
expected := &BlockStorage{
ID: "123456",
Cost: 10,
Status: "active",
SizeGB: 100,
Region: "ewr",
DateCreated: "01-01-1960",
AttachedToInstance: "",
Label: "mylabel",
MountID: "123abc",
BlockType: "test",
}
if !reflect.DeepEqual(blockStorage, expected) {
t.Errorf("BlockStorage.Create returned %+v, expected %+v", blockStorage, expected)
}
}
func TestBlockStorageServiceHandler_Update(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks/123456", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
blockUpdate := &BlockStorageUpdate{
Label: "unit-test-label-setter",
}
err := client.BlockStorage.Update(ctx, "123456", blockUpdate)
if err != nil {
t.Errorf("BlockStorage.SetLabel returned %+v, expected %+v", err, nil)
}
}
func TestBlockStorageServiceHandler_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks/123456", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
err := client.BlockStorage.Delete(ctx, "123456")
if err != nil {
t.Errorf("BlockStorage.Delete returned %+v, expected %+v", err, nil)
}
}
func TestBlockStorageServiceHandler_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks", func(writer http.ResponseWriter, request *http.Request) {
response := `{"blocks":[{"id":"123456","cost":10,"status":"active","size_gb":100,"region":"ewr","attached_to_instance":"","date_created":"01-01-1960","label":"mylabel", "mount_id": "123abc", "block_type": "test"}],"meta":{"total":1,"links":{"next":"thisismycusror","prev":""}}}`
fmt.Fprint(writer, response)
})
blockStorage, meta, err := client.BlockStorage.List(ctx, nil)
if err != nil {
t.Errorf("BlockStorage.Create returned error: %v", err)
}
expected := []BlockStorage{
{
ID: "123456",
Cost: 10,
Status: "active",
SizeGB: 100,
Region: "ewr",
DateCreated: "01-01-1960",
AttachedToInstance: "",
Label: "mylabel",
MountID: "123abc",
BlockType: "test",
},
}
if !reflect.DeepEqual(blockStorage, expected) {
t.Errorf("BlockStorage.Create returned %+v, expected %+v", blockStorage, expected)
}
expectedMeta := &Meta{
Total: 1,
Links: &Links{
Next: "thisismycusror",
Prev: "",
},
}
if !reflect.DeepEqual(meta, expectedMeta) {
t.Errorf("User.List meta returned %+v, expected %+v", meta, expectedMeta)
}
}
func TestBlockStorageServiceHandler_Attach(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks/12345/attach", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
attach := &BlockStorageAttach{
InstanceID: "1234",
Live: BoolToBoolPtr(true),
}
err := client.BlockStorage.Attach(ctx, "12345", attach)
if err != nil {
t.Errorf("BlockStorage.Attach returned %+v, expected %+v", err, nil)
}
}
func TestBlockStorageServiceHandler_Detach(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blocks/123456/detach", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
detach := &BlockStorageDetach{Live: BoolToBoolPtr(true)}
err := client.BlockStorage.Detach(ctx, "123456", detach)
if err != nil {
t.Errorf("BlockStorage.Detach returned %+v, expected %+v", err, nil)
}
}