1package testing
2
3import (
4	"fmt"
5	"net/http"
6	"testing"
7
8	"github.com/gophercloud/gophercloud"
9	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets"
10	th "github.com/gophercloud/gophercloud/testhelper"
11	"github.com/gophercloud/gophercloud/testhelper/client"
12)
13
14// GetOutput is a sample response to a Get call.
15const GetOutput = `
16{
17   "quota_set" : {
18      "instances" : 25,
19      "security_groups" : 10,
20      "security_group_rules" : 20,
21      "cores" : 200,
22      "injected_file_content_bytes" : 10240,
23      "injected_files" : 5,
24      "metadata_items" : 128,
25      "ram" : 9216000,
26      "key_pairs" : 10,
27      "injected_file_path_bytes" : 255,
28	  "server_groups" : 2,
29	  "server_group_members" : 3
30   }
31}
32`
33
34// GetDetailsOutput is a sample response to a Get call with the detailed option.
35const GetDetailsOutput = `
36{
37   "quota_set" : {
38	  "id": "555544443333222211110000ffffeeee",
39      "instances" : {
40          "in_use": 0,
41          "limit": 25,
42          "reserved": 0
43      },
44      "security_groups" : {
45          "in_use": 0,
46          "limit": 10,
47          "reserved": 0
48      },
49      "security_group_rules" : {
50          "in_use": 0,
51          "limit": 20,
52          "reserved": 0
53      },
54      "cores" : {
55          "in_use": 0,
56          "limit": 200,
57          "reserved": 0
58      },
59      "injected_file_content_bytes" : {
60          "in_use": 0,
61          "limit": 10240,
62          "reserved": 0
63      },
64      "injected_files" : {
65          "in_use": 0,
66          "limit": 5,
67          "reserved": 0
68      },
69      "metadata_items" : {
70          "in_use": 0,
71          "limit": 128,
72          "reserved": 0
73      },
74      "ram" : {
75          "in_use": 0,
76          "limit": 9216000,
77          "reserved": 0
78      },
79      "key_pairs" : {
80          "in_use": 0,
81          "limit": 10,
82          "reserved": 0
83      },
84      "injected_file_path_bytes" : {
85          "in_use": 0,
86          "limit": 255,
87          "reserved": 0
88      },
89      "server_groups" : {
90          "in_use": 0,
91          "limit": 2,
92          "reserved": 0
93      },
94      "server_group_members" : {
95          "in_use": 0,
96          "limit": 3,
97          "reserved": 0
98      }
99   }
100}
101`
102const FirstTenantID = "555544443333222211110000ffffeeee"
103
104// FirstQuotaSet is the first result in ListOutput.
105var FirstQuotaSet = quotasets.QuotaSet{
106	FixedIPs:                 0,
107	FloatingIPs:              0,
108	InjectedFileContentBytes: 10240,
109	InjectedFilePathBytes:    255,
110	InjectedFiles:            5,
111	KeyPairs:                 10,
112	MetadataItems:            128,
113	RAM:                      9216000,
114	SecurityGroupRules:       20,
115	SecurityGroups:           10,
116	Cores:                    200,
117	Instances:                25,
118	ServerGroups:             2,
119	ServerGroupMembers:       3,
120}
121
122// FirstQuotaDetailsSet is the first result in ListOutput.
123var FirstQuotaDetailsSet = quotasets.QuotaDetailSet{
124	ID:                       FirstTenantID,
125	InjectedFileContentBytes: quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 10240},
126	InjectedFilePathBytes:    quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 255},
127	InjectedFiles:            quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 5},
128	KeyPairs:                 quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 10},
129	MetadataItems:            quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 128},
130	RAM:                      quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 9216000},
131	SecurityGroupRules:       quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 20},
132	SecurityGroups:           quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 10},
133	Cores:                    quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 200},
134	Instances:                quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 25},
135	ServerGroups:             quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 2},
136	ServerGroupMembers:       quotasets.QuotaDetail{InUse: 0, Reserved: 0, Limit: 3},
137}
138
139//The expected update Body. Is also returned by PUT request
140const UpdateOutput = `{"quota_set":{"cores":200,"fixed_ips":0,"floating_ips":0,"injected_file_content_bytes":10240,"injected_file_path_bytes":255,"injected_files":5,"instances":25,"key_pairs":10,"metadata_items":128,"ram":9216000,"security_group_rules":20,"security_groups":10,"server_groups":2,"server_group_members":3}}`
141
142//The expected partialupdate Body. Is also returned by PUT request
143const PartialUpdateBody = `{"quota_set":{"cores":200, "force":true}}`
144
145//Result of Quota-update
146var UpdatedQuotaSet = quotasets.UpdateOpts{
147	FixedIPs:                 gophercloud.IntToPointer(0),
148	FloatingIPs:              gophercloud.IntToPointer(0),
149	InjectedFileContentBytes: gophercloud.IntToPointer(10240),
150	InjectedFilePathBytes:    gophercloud.IntToPointer(255),
151	InjectedFiles:            gophercloud.IntToPointer(5),
152	KeyPairs:                 gophercloud.IntToPointer(10),
153	MetadataItems:            gophercloud.IntToPointer(128),
154	RAM:                      gophercloud.IntToPointer(9216000),
155	SecurityGroupRules:       gophercloud.IntToPointer(20),
156	SecurityGroups:           gophercloud.IntToPointer(10),
157	Cores:                    gophercloud.IntToPointer(200),
158	Instances:                gophercloud.IntToPointer(25),
159	ServerGroups:             gophercloud.IntToPointer(2),
160	ServerGroupMembers:       gophercloud.IntToPointer(3),
161}
162
163// HandleGetSuccessfully configures the test server to respond to a Get request for sample tenant
164func HandleGetSuccessfully(t *testing.T) {
165	th.Mux.HandleFunc("/os-quota-sets/"+FirstTenantID, func(w http.ResponseWriter, r *http.Request) {
166		th.TestMethod(t, r, "GET")
167		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
168
169		w.Header().Add("Content-Type", "application/json")
170		fmt.Fprintf(w, GetOutput)
171	})
172}
173
174// HandleGetDetailSuccessfully configures the test server to respond to a Get Details request for sample tenant
175func HandleGetDetailSuccessfully(t *testing.T) {
176	th.Mux.HandleFunc("/os-quota-sets/"+FirstTenantID+"/detail", func(w http.ResponseWriter, r *http.Request) {
177		th.TestMethod(t, r, "GET")
178		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
179		w.Header().Add("Content-Type", "application/json")
180		fmt.Fprintf(w, GetDetailsOutput)
181	})
182}
183
184// HandlePutSuccessfully configures the test server to respond to a Put request for sample tenant
185func HandlePutSuccessfully(t *testing.T) {
186	th.Mux.HandleFunc("/os-quota-sets/"+FirstTenantID, func(w http.ResponseWriter, r *http.Request) {
187		th.TestMethod(t, r, "PUT")
188		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
189		th.TestJSONRequest(t, r, UpdateOutput)
190		w.Header().Add("Content-Type", "application/json")
191		fmt.Fprintf(w, UpdateOutput)
192	})
193}
194
195// HandlePartialPutSuccessfully configures the test server to respond to a Put request for sample tenant that only containes specific values
196func HandlePartialPutSuccessfully(t *testing.T) {
197	th.Mux.HandleFunc("/os-quota-sets/"+FirstTenantID, func(w http.ResponseWriter, r *http.Request) {
198		th.TestMethod(t, r, "PUT")
199		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
200		th.TestJSONRequest(t, r, PartialUpdateBody)
201		w.Header().Add("Content-Type", "application/json")
202		fmt.Fprintf(w, UpdateOutput)
203	})
204}
205
206// HandleDeleteSuccessfully configures the test server to respond to a Delete request for sample tenant
207func HandleDeleteSuccessfully(t *testing.T) {
208	th.Mux.HandleFunc("/os-quota-sets/"+FirstTenantID, func(w http.ResponseWriter, r *http.Request) {
209		th.TestMethod(t, r, "DELETE")
210		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
211		th.TestBody(t, r, "")
212		w.Header().Add("Content-Type", "application/json")
213		w.WriteHeader(202)
214	})
215}
216