blob: 4a132868fcff1f018742aff4169dd0861d191c71 [file] [log] [blame]
Jamie Hannafordfac40db2015-02-09 17:27:08 +01001package instances
2
3import (
4 "testing"
5
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford2e817322015-02-16 15:29:17 +01007 osDBs "github.com/rackspace/gophercloud/openstack/db/v1/databases"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01008 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford2e817322015-02-16 15:29:17 +01009 osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaforda50d1352015-02-18 11:38:38 +010010 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannafordfac40db2015-02-09 17:27:08 +010011 th "github.com/rackspace/gophercloud/testhelper"
12 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010013 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannafordfac40db2015-02-09 17:27:08 +010014)
15
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010016var (
17 instanceID = "{instanceID}"
18 _rootURL = "/instances"
19 resURL = "/instances/" + instanceID
20)
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010021
22var expectedInstance = &Instance{
23 Created: "2014-02-13T21:47:13",
24 Updated: "2014-02-13T21:47:13",
Jamie Hannaforda50d1352015-02-18 11:38:38 +010025 Datastore: datastores.DatastorePartial{Type: "mysql", Version: "5.6"},
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010026 Flavor: os.Flavor{
27 ID: "1",
28 Links: []gophercloud.Link{
29 gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
30 gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "bookmark"},
31 },
32 },
33 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
34 ID: instanceID,
35 Links: []gophercloud.Link{
36 gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
37 },
38 Name: "json_rack_instance",
39 Status: "BUILD",
40 Volume: os.Volume{Size: 2},
41}
42
Jamie Hannafordfac40db2015-02-09 17:27:08 +010043func TestCreate(t *testing.T) {
44 th.SetupHTTP()
45 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010046 fixture.SetupHandler(t, _rootURL, "POST", createReq, createResp, 200)
Jamie Hannafordfac40db2015-02-09 17:27:08 +010047
48 opts := CreateOpts{
49 Name: "json_rack_instance",
50 FlavorRef: "1",
Jamie Hannaford2e817322015-02-16 15:29:17 +010051 Databases: osDBs.BatchCreateOpts{
52 osDBs.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
53 osDBs.CreateOpts{Name: "nextround"},
Jamie Hannafordfac40db2015-02-09 17:27:08 +010054 },
Jamie Hannaford2e817322015-02-16 15:29:17 +010055 Users: osUsers.BatchCreateOpts{
56 osUsers.CreateOpts{
Jamie Hannafordfac40db2015-02-09 17:27:08 +010057 Name: "demouser",
58 Password: "demopassword",
Jamie Hannaford2e817322015-02-16 15:29:17 +010059 Databases: osDBs.BatchCreateOpts{
60 osDBs.CreateOpts{Name: "sampledb"},
Jamie Hannafordfac40db2015-02-09 17:27:08 +010061 },
62 },
63 },
64 Size: 2,
65 RestorePoint: "1234567890",
66 }
67
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010068 instance, err := Create(fake.ServiceClient(), opts).Extract()
69
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010070 th.AssertNoErr(t, err)
71 th.AssertDeepEquals(t, expectedInstance, instance)
72}
73
74func TestGet(t *testing.T) {
75 th.SetupHTTP()
76 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010077 fixture.SetupHandler(t, resURL, "GET", "", getResp, 200)
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010078
79 instance, err := Get(fake.ServiceClient(), instanceID).Extract()
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010080
81 th.AssertNoErr(t, err)
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010082 th.AssertDeepEquals(t, expectedInstance, instance)
Jamie Hannafordfac40db2015-02-09 17:27:08 +010083}
Jamie Hannaford1232e042015-02-10 13:36:32 +010084
85func TestDeleteInstance(t *testing.T) {
86 th.SetupHTTP()
87 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010088 os.HandleDelete(t)
Jamie Hannaford1232e042015-02-10 13:36:32 +010089
90 res := Delete(fake.ServiceClient(), instanceID)
91 th.AssertNoErr(t, res.Err)
92}
Jamie Hannafordebcac552015-02-10 13:58:56 +010093
94func TestEnableRootUser(t *testing.T) {
95 th.SetupHTTP()
96 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010097 os.HandleEnableRoot(t)
Jamie Hannafordebcac552015-02-10 13:58:56 +010098
Jamie Hannaford2e817322015-02-16 15:29:17 +010099 expected := &osUsers.User{Name: "root", Password: "secretsecret"}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100100
101 user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
102 th.AssertNoErr(t, err)
103 th.AssertDeepEquals(t, expected, user)
104}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100105
106func TestRestartService(t *testing.T) {
107 th.SetupHTTP()
108 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100109 os.HandleRestart(t)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100110
111 res := RestartService(fake.ServiceClient(), instanceID)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100112 th.AssertNoErr(t, res.Err)
113}
114
115func TestResizeInstance(t *testing.T) {
116 th.SetupHTTP()
117 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100118 os.HandleResize(t)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100119
120 res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100121 th.AssertNoErr(t, res.Err)
122}
123
124func TestResizeVolume(t *testing.T) {
125 th.SetupHTTP()
126 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100127 os.HandleResizeVol(t)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100128
129 res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100130 th.AssertNoErr(t, res.Err)
131}