blob: 9e28df436c77d230bea3c21edd5bd50a2ea4c765 [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)
Jamie Hannaford821015f2015-02-10 12:58:36 +010012
Jamie Hannaford9fdda582015-02-10 12:15:43 +010013func TestCreate(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010016 HandleCreate(t)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010017
18 opts := CreateOpts{
19 Name: "json_rack_instance",
20 FlavorRef: "1",
Jamie Hannaford85f10332015-02-12 11:51:37 +010021 Databases: db.BatchCreateOpts{
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010022 db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
23 db.CreateOpts{Name: "nextround"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010024 },
Jamie Hannaford3aba0b12015-02-13 14:33:39 +010025 Users: users.BatchCreateOpts{
26 users.CreateOpts{
Jamie Hannaford9fdda582015-02-10 12:15:43 +010027 Name: "demouser",
28 Password: "demopassword",
Jamie Hannaford85f10332015-02-12 11:51:37 +010029 Databases: db.BatchCreateOpts{
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010030 db.CreateOpts{Name: "sampledb"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010031 },
32 },
33 },
34 Size: 2,
35 }
36
37 instance, err := Create(fake.ServiceClient(), opts).Extract()
38
Jamie Hannaford9fdda582015-02-10 12:15:43 +010039 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010040 th.AssertDeepEquals(t, &expectedInstance, instance)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010041}
Jamie Hannaford90684242015-02-10 12:46:07 +010042
43func TestInstanceList(t *testing.T) {
44 th.SetupHTTP()
45 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010046 HandleList(t)
Jamie Hannaford90684242015-02-10 12:46:07 +010047
48 pages := 0
49 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
50 pages++
51
52 actual, err := ExtractInstances(page)
53 if err != nil {
54 return false, err
55 }
56
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010057 th.CheckDeepEquals(t, []Instance{expectedInstance}, actual)
Jamie Hannaford90684242015-02-10 12:46:07 +010058 return true, nil
59 })
60
61 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010062 th.AssertEquals(t, 1, pages)
Jamie Hannaford90684242015-02-10 12:46:07 +010063}
Jamie Hannaford821015f2015-02-10 12:58:36 +010064
65func TestGetInstance(t *testing.T) {
66 th.SetupHTTP()
67 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010068 HandleGet(t)
Jamie Hannaford821015f2015-02-10 12:58:36 +010069
70 instance, err := Get(fake.ServiceClient(), instanceID).Extract()
71
72 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010073 th.AssertDeepEquals(t, &expectedInstance, instance)
Jamie Hannaford821015f2015-02-10 12:58:36 +010074}
Jamie Hannaford5b16b632015-02-10 13:36:23 +010075
76func TestDeleteInstance(t *testing.T) {
77 th.SetupHTTP()
78 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010079 HandleDelete(t)
Jamie Hannaford5b16b632015-02-10 13:36:23 +010080
81 res := Delete(fake.ServiceClient(), instanceID)
82 th.AssertNoErr(t, res.Err)
83}
Jamie Hannaford94164fa2015-02-10 13:58:45 +010084
85func TestEnableRootUser(t *testing.T) {
86 th.SetupHTTP()
87 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010088 HandleEnableRoot(t)
Jamie Hannaford94164fa2015-02-10 13:58:45 +010089
Jamie Hannaford3aba0b12015-02-13 14:33:39 +010090 expected := &users.User{Name: "root", Password: "secretsecret"}
Jamie Hannaford94164fa2015-02-10 13:58:45 +010091 user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010092
Jamie Hannaford94164fa2015-02-10 13:58:45 +010093 th.AssertNoErr(t, err)
94 th.AssertDeepEquals(t, expected, user)
95}
Jamie Hannaforda74d4252015-02-10 15:35:01 +010096
97func TestIsRootEnabled(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100100 HandleIsRootEnabled(t)
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100101
102 isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID)
103
104 th.AssertNoErr(t, err)
105 th.AssertEquals(t, true, isEnabled)
106}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100107
108func TestRestartService(t *testing.T) {
109 th.SetupHTTP()
110 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100111 HandleRestart(t)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100112
113 res := RestartService(fake.ServiceClient(), instanceID)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100114 th.AssertNoErr(t, res.Err)
115}
116
117func TestResizeInstance(t *testing.T) {
118 th.SetupHTTP()
119 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100120 HandleResize(t)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100121
122 res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
Jamie Hannaford219ca592015-02-10 15:59:05 +0100123 th.AssertNoErr(t, res.Err)
124}
125
126func TestResizeVolume(t *testing.T) {
127 th.SetupHTTP()
128 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100129 HandleResizeVol(t)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100130
131 res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100132 th.AssertNoErr(t, res.Err)
133}