blob: 03a482872d5c71937ee3ac0ddb2e71c092471786 [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 Hannaford8803f832015-02-23 10:44:55 +01008 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01009 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford2e817322015-02-16 15:29:17 +010010 osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaforda50d1352015-02-18 11:38:38 +010011 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannafordfac40db2015-02-09 17:27:08 +010012 th "github.com/rackspace/gophercloud/testhelper"
13 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010014 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannafordfac40db2015-02-09 17:27:08 +010015)
16
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010017var (
18 instanceID = "{instanceID}"
19 _rootURL = "/instances"
20 resURL = "/instances/" + instanceID
21)
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010022
23var expectedInstance = &Instance{
24 Created: "2014-02-13T21:47:13",
25 Updated: "2014-02-13T21:47:13",
Jamie Hannaforda50d1352015-02-18 11:38:38 +010026 Datastore: datastores.DatastorePartial{Type: "mysql", Version: "5.6"},
Jamie Hannaford8803f832015-02-23 10:44:55 +010027 Flavor: flavors.Flavor{
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010028 ID: "1",
29 Links: []gophercloud.Link{
30 gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
31 gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "bookmark"},
32 },
33 },
34 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
35 ID: instanceID,
36 Links: []gophercloud.Link{
37 gophercloud.Link{Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/flavors/1", Rel: "self"},
38 },
39 Name: "json_rack_instance",
40 Status: "BUILD",
41 Volume: os.Volume{Size: 2},
42}
43
Jamie Hannafordfac40db2015-02-09 17:27:08 +010044func TestCreate(t *testing.T) {
45 th.SetupHTTP()
46 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010047 fixture.SetupHandler(t, _rootURL, "POST", createReq, createResp, 200)
Jamie Hannafordfac40db2015-02-09 17:27:08 +010048
49 opts := CreateOpts{
50 Name: "json_rack_instance",
51 FlavorRef: "1",
Jamie Hannaford2e817322015-02-16 15:29:17 +010052 Databases: osDBs.BatchCreateOpts{
53 osDBs.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
54 osDBs.CreateOpts{Name: "nextround"},
Jamie Hannafordfac40db2015-02-09 17:27:08 +010055 },
Jamie Hannaford2e817322015-02-16 15:29:17 +010056 Users: osUsers.BatchCreateOpts{
57 osUsers.CreateOpts{
Jamie Hannafordfac40db2015-02-09 17:27:08 +010058 Name: "demouser",
59 Password: "demopassword",
Jamie Hannaford2e817322015-02-16 15:29:17 +010060 Databases: osDBs.BatchCreateOpts{
61 osDBs.CreateOpts{Name: "sampledb"},
Jamie Hannafordfac40db2015-02-09 17:27:08 +010062 },
63 },
64 },
65 Size: 2,
66 RestorePoint: "1234567890",
67 }
68
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010069 instance, err := Create(fake.ServiceClient(), opts).Extract()
70
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010071 th.AssertNoErr(t, err)
72 th.AssertDeepEquals(t, expectedInstance, instance)
73}
74
75func TestGet(t *testing.T) {
76 th.SetupHTTP()
77 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010078 fixture.SetupHandler(t, resURL, "GET", "", getResp, 200)
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010079
80 instance, err := Get(fake.ServiceClient(), instanceID).Extract()
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010081
82 th.AssertNoErr(t, err)
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010083 th.AssertDeepEquals(t, expectedInstance, instance)
Jamie Hannafordfac40db2015-02-09 17:27:08 +010084}
Jamie Hannaford1232e042015-02-10 13:36:32 +010085
86func TestDeleteInstance(t *testing.T) {
87 th.SetupHTTP()
88 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010089 os.HandleDelete(t)
Jamie Hannaford1232e042015-02-10 13:36:32 +010090
91 res := Delete(fake.ServiceClient(), instanceID)
92 th.AssertNoErr(t, res.Err)
93}
Jamie Hannafordebcac552015-02-10 13:58:56 +010094
95func TestEnableRootUser(t *testing.T) {
96 th.SetupHTTP()
97 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010098 os.HandleEnableRoot(t)
Jamie Hannafordebcac552015-02-10 13:58:56 +010099
Jamie Hannaford2e817322015-02-16 15:29:17 +0100100 expected := &osUsers.User{Name: "root", Password: "secretsecret"}
Jamie Hannafordebcac552015-02-10 13:58:56 +0100101
102 user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
103 th.AssertNoErr(t, err)
104 th.AssertDeepEquals(t, expected, user)
105}
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100106
107func TestRestartService(t *testing.T) {
108 th.SetupHTTP()
109 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100110 os.HandleRestart(t)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100111
112 res := RestartService(fake.ServiceClient(), instanceID)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100113 th.AssertNoErr(t, res.Err)
114}
115
116func TestResizeInstance(t *testing.T) {
117 th.SetupHTTP()
118 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100119 os.HandleResize(t)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100120
121 res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100122 th.AssertNoErr(t, res.Err)
123}
124
125func TestResizeVolume(t *testing.T) {
126 th.SetupHTTP()
127 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100128 os.HandleResizeVol(t)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100129
130 res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
Jamie Hannaforde6390d42015-02-10 15:59:28 +0100131 th.AssertNoErr(t, res.Err)
132}