blob: b53757a40f381762f5f0f491b116cbaee553300b [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Joe Topjianc9fb21b2015-02-22 05:55:48 +00002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
jrperritt3d966162016-06-06 14:08:54 -05008 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
Jon Perritt27249f42016-02-18 10:35:59 -06009 th "github.com/gophercloud/gophercloud/testhelper"
10 "github.com/gophercloud/gophercloud/testhelper/client"
Joe Topjianc9fb21b2015-02-22 05:55:48 +000011)
12
13// ListOutput is a sample response to a List call.
14const ListOutput = `
15{
16 "server_groups": [
17 {
18 "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
19 "name": "test",
20 "policies": [
21 "anti-affinity"
22 ],
23 "members": [],
24 "metadata": {}
25 },
26 {
27 "id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
28 "name": "test2",
29 "policies": [
30 "affinity"
31 ],
32 "members": [],
33 "metadata": {}
34 }
35 ]
36}
37`
38
39// GetOutput is a sample response to a Get call.
40const GetOutput = `
41{
42 "server_group": {
43 "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
44 "name": "test",
45 "policies": [
46 "anti-affinity"
47 ],
48 "members": [],
49 "metadata": {}
50 }
51}
52`
53
54// CreateOutput is a sample response to a Post call
55const CreateOutput = `
56{
57 "server_group": {
58 "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
59 "name": "test",
60 "policies": [
61 "anti-affinity"
62 ],
63 "members": [],
64 "metadata": {}
65 }
66}
67`
68
69// FirstServerGroup is the first result in ListOutput.
jrperritt3d966162016-06-06 14:08:54 -050070var FirstServerGroup = servergroups.ServerGroup{
Joe Topjianc9fb21b2015-02-22 05:55:48 +000071 ID: "616fb98f-46ca-475e-917e-2563e5a8cd19",
72 Name: "test",
73 Policies: []string{
74 "anti-affinity",
75 },
76 Members: []string{},
77 Metadata: map[string]interface{}{},
78}
79
80// SecondServerGroup is the second result in ListOutput.
jrperritt3d966162016-06-06 14:08:54 -050081var SecondServerGroup = servergroups.ServerGroup{
Joe Topjianc9fb21b2015-02-22 05:55:48 +000082 ID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
83 Name: "test2",
84 Policies: []string{
85 "affinity",
86 },
87 Members: []string{},
88 Metadata: map[string]interface{}{},
89}
90
91// ExpectedServerGroupSlice is the slice of results that should be parsed
92// from ListOutput, in the expected order.
jrperritt3d966162016-06-06 14:08:54 -050093var ExpectedServerGroupSlice = []servergroups.ServerGroup{FirstServerGroup, SecondServerGroup}
Joe Topjianc9fb21b2015-02-22 05:55:48 +000094
95// CreatedServerGroup is the parsed result from CreateOutput.
jrperritt3d966162016-06-06 14:08:54 -050096var CreatedServerGroup = servergroups.ServerGroup{
Joe Topjianc9fb21b2015-02-22 05:55:48 +000097 ID: "616fb98f-46ca-475e-917e-2563e5a8cd19",
98 Name: "test",
99 Policies: []string{
100 "anti-affinity",
101 },
102 Members: []string{},
103 Metadata: map[string]interface{}{},
104}
105
106// HandleListSuccessfully configures the test server to respond to a List request.
107func HandleListSuccessfully(t *testing.T) {
108 th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
109 th.TestMethod(t, r, "GET")
110 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
111
112 w.Header().Add("Content-Type", "application/json")
113 fmt.Fprintf(w, ListOutput)
114 })
115}
116
117// HandleGetSuccessfully configures the test server to respond to a Get request
118// for an existing server group
119func HandleGetSuccessfully(t *testing.T) {
120 th.Mux.HandleFunc("/os-server-groups/4d8c3732-a248-40ed-bebc-539a6ffd25c0", func(w http.ResponseWriter, r *http.Request) {
121 th.TestMethod(t, r, "GET")
122 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
123
124 w.Header().Add("Content-Type", "application/json")
125 fmt.Fprintf(w, GetOutput)
126 })
127}
128
129// HandleCreateSuccessfully configures the test server to respond to a Create request
130// for a new server group
131func HandleCreateSuccessfully(t *testing.T) {
132 th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
133 th.TestMethod(t, r, "POST")
134 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
135 th.TestJSONRequest(t, r, `
136{
137 "server_group": {
138 "name": "test",
139 "policies": [
140 "anti-affinity"
141 ]
142 }
143}
144`)
145
146 w.Header().Add("Content-Type", "application/json")
147 fmt.Fprintf(w, CreateOutput)
148 })
149}
150
151// HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
152// an existing server group
153func HandleDeleteSuccessfully(t *testing.T) {
154 th.Mux.HandleFunc("/os-server-groups/616fb98f-46ca-475e-917e-2563e5a8cd19", func(w http.ResponseWriter, r *http.Request) {
155 th.TestMethod(t, r, "DELETE")
156 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
157
158 w.WriteHeader(http.StatusAccepted)
159 })
160}