blob: a1012aa08978be7ec7fb2f3a30fca4924b73c140 [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
10)
11
Jamie Hannaforde0524732015-02-16 14:44:13 +010012var singleBackup = `
13{
14 "backup": {
15 "created": "2014-02-13T21:47:16",
16 "description": "My Backup",
17 "id": "61f12fef-edb1-4561-8122-e7c00ef26a82",
18 "instance_id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
19 "locationRef": null,
20 "name": "snapshot",
21 "parent_id": null,
22 "size": 100,
23 "status": "NEW",
24 "datastore": {
25 "version": "5.1",
26 "type": "MySQL",
27 "version_id": "20000000-0000-0000-0000-000000000002"
28 },
29 "updated": "2014-02-13T21:47:16"
30 }
31}
32`
33
Jamie Hannaford302c0b62015-02-16 14:12:34 +010034func SetupHandler(t *testing.T, url, method, requestBody, responseBody string, status int) {
35 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
36 th.TestMethod(t, r, method)
37 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
38
39 if requestBody != "" {
40 th.TestJSONRequest(t, r, requestBody)
41 }
42
43 if responseBody != "" {
44 w.Header().Add("Content-Type", "application/json")
45 }
46
47 w.WriteHeader(status)
48
49 if responseBody != "" {
50 fmt.Fprintf(w, responseBody)
51 }
52 })
53}
54
55func HandleCreateSuccessfully(t *testing.T) {
56 requestJSON := `
57{
58 "backup": {
59 "description": "My Backup",
60 "instance": "d4603f69-ec7e-4e9b-803f-600b9205576f",
61 "name": "snapshot"
62 }
63}
64`
65
Jamie Hannaforde0524732015-02-16 14:44:13 +010066 SetupHandler(t, "/backups", "POST", requestJSON, singleBackup, 202)
Jamie Hannaford302c0b62015-02-16 14:12:34 +010067}
68
69func HandleListSuccessfully(t *testing.T) {
70 responseJSON := `
71{
72 "backups": [
73 {
74 "status": "COMPLETED",
75 "updated": "2014-06-18T21:24:39",
76 "description": "Backup from Restored Instance",
Jamie Hannaforde0524732015-02-16 14:44:13 +010077 "datastore": {
78 "version": "5.1",
79 "type": "MySQL",
80 "version_id": "20000000-0000-0000-0000-000000000002"
81 },
Jamie Hannaford302c0b62015-02-16 14:12:34 +010082 "id": "87972694-4be2-40f5-83f8-501656e0032a",
83 "size": 0.141026,
84 "name": "restored_backup",
85 "created": "2014-06-18T21:23:35",
86 "instance_id": "29af2cd9-0674-48ab-b87a-b160f00208e6",
87 "parent_id": null,
88 "locationRef": "http://localhost/path/to/backup"
89 }
90 ]
91}
92`
93
94 SetupHandler(t, "/backups", "GET", "", responseJSON, 200)
95}
Jamie Hannaforde0524732015-02-16 14:44:13 +010096
97func HandleGetSuccessfully(t *testing.T, backupID string) {
98 SetupHandler(t, "/backups/"+backupID, "GET", "", singleBackup, 200)
99}
100
101func HandleDeleteSuccessfully(t *testing.T, backupID string) {
102 SetupHandler(t, "/backups/"+backupID, "DELETE", "", "", 202)
103}