blob: d44220d183d510d1a133904f5d65c5cd07203dc5 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "testing"
5
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +01006 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
Jamie Hannaford3aba0b12015-02-13 14:33:39 +01007 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01009 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010011 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010012)
13
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010014var (
15 instanceID = "{instanceID}"
16 rootURL = "/instances"
17 resURL = rootURL + "/" + instanceID
18 uRootURL = resURL + "/root"
19 aURL = resURL + "/action"
20)
Jamie Hannaford821015f2015-02-10 12:58:36 +010021
Jamie Hannaford9fdda582015-02-10 12:15:43 +010022func TestCreate(t *testing.T) {
23 th.SetupHTTP()
24 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010025 fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010026
27 opts := CreateOpts{
28 Name: "json_rack_instance",
29 FlavorRef: "1",
Jamie Hannaford85f10332015-02-12 11:51:37 +010030 Databases: db.BatchCreateOpts{
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010031 db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
32 db.CreateOpts{Name: "nextround"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010033 },
Jamie Hannaford3aba0b12015-02-13 14:33:39 +010034 Users: users.BatchCreateOpts{
35 users.CreateOpts{
Jamie Hannaford9fdda582015-02-10 12:15:43 +010036 Name: "demouser",
37 Password: "demopassword",
Jamie Hannaford85f10332015-02-12 11:51:37 +010038 Databases: db.BatchCreateOpts{
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010039 db.CreateOpts{Name: "sampledb"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010040 },
41 },
42 },
43 Size: 2,
44 }
45
46 instance, err := Create(fake.ServiceClient(), opts).Extract()
47
Jamie Hannaford9fdda582015-02-10 12:15:43 +010048 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010049 th.AssertDeepEquals(t, &expectedInstance, instance)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010050}
Jamie Hannaford90684242015-02-10 12:46:07 +010051
52func TestInstanceList(t *testing.T) {
53 th.SetupHTTP()
54 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010055 fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
Jamie Hannaford90684242015-02-10 12:46:07 +010056
57 pages := 0
58 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
59 pages++
60
61 actual, err := ExtractInstances(page)
62 if err != nil {
63 return false, err
64 }
65
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010066 th.CheckDeepEquals(t, []Instance{expectedInstance}, actual)
Jamie Hannaford90684242015-02-10 12:46:07 +010067
68 return true, nil
69 })
70
71 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010072 th.AssertEquals(t, 1, pages)
Jamie Hannaford90684242015-02-10 12:46:07 +010073}
Jamie Hannaford821015f2015-02-10 12:58:36 +010074
75func TestGetInstance(t *testing.T) {
76 th.SetupHTTP()
77 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010078 fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
Jamie Hannaford821015f2015-02-10 12:58:36 +010079
80 instance, err := Get(fake.ServiceClient(), instanceID).Extract()
81
82 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010083 th.AssertDeepEquals(t, &expectedInstance, instance)
Jamie Hannaford821015f2015-02-10 12:58:36 +010084}
Jamie Hannaford5b16b632015-02-10 13:36:23 +010085
86func TestDeleteInstance(t *testing.T) {
87 th.SetupHTTP()
88 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010089 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
Jamie Hannaford5b16b632015-02-10 13:36:23 +010090
91 res := Delete(fake.ServiceClient(), instanceID)
92 th.AssertNoErr(t, res.Err)
93}
Jamie Hannaford94164fa2015-02-10 13:58:45 +010094
95func TestEnableRootUser(t *testing.T) {
96 th.SetupHTTP()
97 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010098 fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
Jamie Hannaford94164fa2015-02-10 13:58:45 +010099
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100100 expected := &users.User{Name: "root", Password: "secretsecret"}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100101 user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100102
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100103 th.AssertNoErr(t, err)
104 th.AssertDeepEquals(t, expected, user)
105}
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100106
107func TestIsRootEnabled(t *testing.T) {
108 th.SetupHTTP()
109 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100110 fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100111
112 isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID)
113
114 th.AssertNoErr(t, err)
115 th.AssertEquals(t, true, isEnabled)
116}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100117
118func TestRestartService(t *testing.T) {
119 th.SetupHTTP()
120 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100121 fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100122
123 res := RestartService(fake.ServiceClient(), instanceID)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100124 th.AssertNoErr(t, res.Err)
125}
126
127func TestResizeInstance(t *testing.T) {
128 th.SetupHTTP()
129 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100130 fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100131
132 res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
Jamie Hannaford219ca592015-02-10 15:59:05 +0100133 th.AssertNoErr(t, res.Err)
134}
135
136func TestResizeVolume(t *testing.T) {
137 th.SetupHTTP()
138 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100139 fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100140
141 res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100142 th.AssertNoErr(t, res.Err)
143}