forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.go
168 lines (153 loc) · 5.23 KB
/
fixtures.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
package testing
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/gophercloud/gophercloud/openstack/baremetal/v1/allocations"
th "github.com/gophercloud/gophercloud/testhelper"
"github.com/gophercloud/gophercloud/testhelper/client"
)
const AllocationListBody = `
{
"allocations": [
{
"candidate_nodes": [],
"created_at": "2019-02-20T09:43:58+00:00",
"extra": {},
"last_error": null,
"links": [
{
"href": "http://127.0.0.1:6385/v1/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
"rel": "self"
},
{
"href": "http://127.0.0.1:6385/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
"rel": "bookmark"
}
],
"name": "allocation-1",
"node_uuid": "6d85703a-565d-469a-96ce-30b6de53079d",
"resource_class": "bm-large",
"state": "active",
"traits": [],
"updated_at": "2019-02-20T09:43:58+00:00",
"uuid": "5344a3e2-978a-444e-990a-cbf47c62ef88"
},
{
"candidate_nodes": [],
"created_at": "2019-02-20T09:43:58+00:00",
"extra": {},
"last_error": "Failed to process allocation eff80f47-75f0-4d41-b1aa-cf07c201adac: no available nodes match the resource class bm-large.",
"links": [
{
"href": "http://127.0.0.1:6385/v1/allocations/eff80f47-75f0-4d41-b1aa-cf07c201adac",
"rel": "self"
},
{
"href": "http://127.0.0.1:6385/allocations/eff80f47-75f0-4d41-b1aa-cf07c201adac",
"rel": "bookmark"
}
],
"name": "allocation-2",
"node_uuid": null,
"resource_class": "bm-large",
"state": "error",
"traits": [
"CUSTOM_GOLD"
],
"updated_at": "2019-02-20T09:43:58+00:00",
"uuid": "eff80f47-75f0-4d41-b1aa-cf07c201adac"
}
]
}
`
const SingleAllocationBody = `
{
"candidate_nodes": ["344a3e2-978a-444e-990a-cbf47c62ef88"],
"created_at": "2019-02-20T09:43:58+00:00",
"extra": {},
"last_error": null,
"links": [
{
"href": "http://127.0.0.1:6385/v1/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
"rel": "self"
},
{
"href": "http://127.0.0.1:6385/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
"rel": "bookmark"
}
],
"name": "allocation-1",
"node_uuid": null,
"resource_class": "baremetal",
"state": "allocating",
"traits": ["foo"],
"updated_at": null,
"uuid": "5344a3e2-978a-444e-990a-cbf47c62ef88"
}`
var (
createdAt, _ = time.Parse(time.RFC3339, "2019-02-20T09:43:58+00:00")
Allocation1 = allocations.Allocation{
UUID: "5344a3e2-978a-444e-990a-cbf47c62ef88",
CandidateNodes: []string{"344a3e2-978a-444e-990a-cbf47c62ef88"},
Name: "allocation-1",
State: "allocating",
ResourceClass: "baremetal",
Traits: []string{"foo"},
Extra: map[string]string{},
CreatedAt: createdAt,
Links: []interface{}{map[string]interface{}{"href": "http://127.0.0.1:6385/v1/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88", "rel": "self"}, map[string]interface{}{"href": "http://127.0.0.1:6385/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88", "rel": "bookmark"}},
}
)
// HandleAllocationListSuccessfully sets up the test server to respond to a allocation List request.
func HandleAllocationListSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Add("Content-Type", "application/json")
r.ParseForm()
marker := r.Form.Get("marker")
switch marker {
case "":
fmt.Fprintf(w, AllocationListBody)
case "eff80f47-75f0-4d41-b1aa-cf07c201adac":
fmt.Fprintf(w, `{ "allocations": [] }`)
default:
t.Fatalf("/allocations invoked with unexpected marker=[%s]", marker)
}
})
}
// HandleAllocationCreationSuccessfully sets up the test server to respond to a allocation creation request
// with a given response.
func HandleAllocationCreationSuccessfully(t *testing.T, response string) {
th.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestJSONRequest(t, r, `{
"name": "allocation-1",
"resource_class": "baremetal",
"candidate_nodes": ["344a3e2-978a-444e-990a-cbf47c62ef88"],
"traits": ["foo"]
}`)
w.WriteHeader(http.StatusAccepted)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, response)
})
}
// HandleAllocationDeletionSuccessfully sets up the test server to respond to a allocation deletion request.
func HandleAllocationDeletionSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.WriteHeader(http.StatusNoContent)
})
}
func HandleAllocationGetSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
fmt.Fprintf(w, SingleAllocationBody)
})
}