blob: ac0e6bdb32cf039ed31452d77e51736927ec6823 [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
4 "testing"
5
Jamie Hannaford52dbcee2015-10-06 16:09:56 +02006 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford302c0b62015-02-16 14:12:34 +01007 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010010 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford302c0b62015-02-16 14:12:34 +010011)
12
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010013var (
14 backupID = "{backupID}"
15 _rootURL = "/backups"
16 resURL = _rootURL + "/" + backupID
17)
Jamie Hannaford302c0b62015-02-16 14:12:34 +010018
19func TestCreate(t *testing.T) {
20 th.SetupHTTP()
21 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010022 fixture.SetupHandler(t, _rootURL, "POST", createReq, createResp, 202)
Jamie Hannaford302c0b62015-02-16 14:12:34 +010023
24 opts := CreateOpts{
25 Name: "snapshot",
26 Description: "My Backup",
27 InstanceID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
28 }
29
30 instance, err := Create(fake.ServiceClient(), opts).Extract()
31 th.AssertNoErr(t, err)
32
33 expected := &Backup{
34 Created: "2014-02-13T21:47:16",
35 Description: "My Backup",
36 ID: "61f12fef-edb1-4561-8122-e7c00ef26a82",
37 InstanceID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
38 LocationRef: "",
39 Name: "snapshot",
40 ParentID: "",
41 Size: 100,
42 Status: "NEW",
43 Updated: "2014-02-13T21:47:16",
Jamie Hannaforda50d1352015-02-18 11:38:38 +010044 Datastore: datastores.DatastorePartial{
Jamie Hannafordc1c6bf82015-02-17 16:53:38 +010045 Version: "5.1",
46 Type: "MySQL",
47 VersionID: "20000000-0000-0000-0000-000000000002",
48 },
Jamie Hannaford302c0b62015-02-16 14:12:34 +010049 }
50
51 th.AssertDeepEquals(t, expected, instance)
52}
53
54func TestList(t *testing.T) {
55 th.SetupHTTP()
56 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010057 fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
Jamie Hannaford302c0b62015-02-16 14:12:34 +010058
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010059 pages := 0
Jamie Hannaford302c0b62015-02-16 14:12:34 +010060
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010061 err := List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
62 pages++
Jamie Hannaford302c0b62015-02-16 14:12:34 +010063 actual, err := ExtractBackups(page)
64 th.AssertNoErr(t, err)
65
66 expected := []Backup{
67 Backup{
68 Created: "2014-06-18T21:23:35",
69 Description: "Backup from Restored Instance",
70 ID: "87972694-4be2-40f5-83f8-501656e0032a",
71 InstanceID: "29af2cd9-0674-48ab-b87a-b160f00208e6",
72 LocationRef: "http://localhost/path/to/backup",
73 Name: "restored_backup",
74 ParentID: "",
75 Size: 0.141026,
76 Status: "COMPLETED",
77 Updated: "2014-06-18T21:24:39",
Jamie Hannaforda50d1352015-02-18 11:38:38 +010078 Datastore: datastores.DatastorePartial{
Jamie Hannafordc1c6bf82015-02-17 16:53:38 +010079 Version: "5.1",
80 Type: "MySQL",
81 VersionID: "20000000-0000-0000-0000-000000000002",
82 },
Jamie Hannaford302c0b62015-02-16 14:12:34 +010083 },
84 }
85
86 th.AssertDeepEquals(t, expected, actual)
87
88 return true, nil
89 })
90
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010091 th.AssertNoErr(t, err)
92 th.AssertEquals(t, 1, pages)
Jamie Hannaford302c0b62015-02-16 14:12:34 +010093}
Jamie Hannaforde0524732015-02-16 14:44:13 +010094
95func TestGet(t *testing.T) {
96 th.SetupHTTP()
97 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010098 fixture.SetupHandler(t, resURL, "GET", "", getResp, 200)
Jamie Hannaforde0524732015-02-16 14:44:13 +010099
100 instance, err := Get(fake.ServiceClient(), backupID).Extract()
101 th.AssertNoErr(t, err)
102
103 expected := &Backup{
104 Created: "2014-02-13T21:47:16",
105 Description: "My Backup",
106 ID: "61f12fef-edb1-4561-8122-e7c00ef26a82",
107 InstanceID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
108 LocationRef: "",
109 Name: "snapshot",
110 ParentID: "",
111 Size: 100,
112 Status: "NEW",
113 Updated: "2014-02-13T21:47:16",
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100114 Datastore: datastores.DatastorePartial{
Jamie Hannafordc1c6bf82015-02-17 16:53:38 +0100115 Version: "5.1",
116 Type: "MySQL",
117 VersionID: "20000000-0000-0000-0000-000000000002",
118 },
Jamie Hannaforde0524732015-02-16 14:44:13 +0100119 }
120
121 th.AssertDeepEquals(t, expected, instance)
122}
123
124func TestDelete(t *testing.T) {
125 th.SetupHTTP()
126 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100127 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
Jamie Hannaforde0524732015-02-16 14:44:13 +0100128
129 err := Delete(fake.ServiceClient(), backupID).ExtractErr()
130 th.AssertNoErr(t, err)
131}