blob: e3c81e34c7a19f37c8da5fe5e37797d530a5d859 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jamie Hannaford9fdda582015-02-10 12:15:43 +01002
3import (
4 "testing"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 db "github.com/gophercloud/gophercloud/openstack/db/v1/databases"
jrperritt3d966162016-06-06 14:08:54 -05007 "github.com/gophercloud/gophercloud/openstack/db/v1/instances"
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud/openstack/db/v1/users"
9 "github.com/gophercloud/gophercloud/pagination"
10 th "github.com/gophercloud/gophercloud/testhelper"
11 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010012)
Jamie Hannaford821015f2015-02-10 12:58:36 +010013
Jamie Hannaford9fdda582015-02-10 12:15:43 +010014func TestCreate(t *testing.T) {
15 th.SetupHTTP()
16 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010017 HandleCreate(t)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010018
jrperritt3d966162016-06-06 14:08:54 -050019 opts := instances.CreateOpts{
Jamie Hannaford9fdda582015-02-10 12:15:43 +010020 Name: "json_rack_instance",
21 FlavorRef: "1",
Jamie Hannaford85f10332015-02-12 11:51:37 +010022 Databases: db.BatchCreateOpts{
jrperritt3d966162016-06-06 14:08:54 -050023 {CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
24 {Name: "nextround"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010025 },
Jamie Hannaford3aba0b12015-02-13 14:33:39 +010026 Users: users.BatchCreateOpts{
jrperritt3d966162016-06-06 14:08:54 -050027 {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010028 Name: "demouser",
29 Password: "demopassword",
Jamie Hannaford85f10332015-02-12 11:51:37 +010030 Databases: db.BatchCreateOpts{
jrperritt3d966162016-06-06 14:08:54 -050031 {Name: "sampledb"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010032 },
33 },
34 },
35 Size: 2,
36 }
37
jrperritt3d966162016-06-06 14:08:54 -050038 instance, err := instances.Create(fake.ServiceClient(), opts).Extract()
Jamie Hannaford9fdda582015-02-10 12:15:43 +010039
Jamie Hannaford9fdda582015-02-10 12:15:43 +010040 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010041 th.AssertDeepEquals(t, &expectedInstance, instance)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010042}
Jamie Hannaford90684242015-02-10 12:46:07 +010043
44func TestInstanceList(t *testing.T) {
45 th.SetupHTTP()
46 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010047 HandleList(t)
Jamie Hannaford90684242015-02-10 12:46:07 +010048
49 pages := 0
jrperritt3d966162016-06-06 14:08:54 -050050 err := instances.List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford90684242015-02-10 12:46:07 +010051 pages++
52
jrperritt3d966162016-06-06 14:08:54 -050053 actual, err := instances.ExtractInstances(page)
Jamie Hannaford90684242015-02-10 12:46:07 +010054 if err != nil {
55 return false, err
56 }
57
jrperritt3d966162016-06-06 14:08:54 -050058 th.CheckDeepEquals(t, []instances.Instance{expectedInstance}, actual)
Jamie Hannaford90684242015-02-10 12:46:07 +010059 return true, nil
60 })
61
62 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010063 th.AssertEquals(t, 1, pages)
Jamie Hannaford90684242015-02-10 12:46:07 +010064}
Jamie Hannaford821015f2015-02-10 12:58:36 +010065
66func TestGetInstance(t *testing.T) {
67 th.SetupHTTP()
68 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010069 HandleGet(t)
Jamie Hannaford821015f2015-02-10 12:58:36 +010070
jrperritt3d966162016-06-06 14:08:54 -050071 instance, err := instances.Get(fake.ServiceClient(), instanceID).Extract()
Jamie Hannaford821015f2015-02-10 12:58:36 +010072
73 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010074 th.AssertDeepEquals(t, &expectedInstance, instance)
Jamie Hannaford821015f2015-02-10 12:58:36 +010075}
Jamie Hannaford5b16b632015-02-10 13:36:23 +010076
77func TestDeleteInstance(t *testing.T) {
78 th.SetupHTTP()
79 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010080 HandleDelete(t)
Jamie Hannaford5b16b632015-02-10 13:36:23 +010081
jrperritt3d966162016-06-06 14:08:54 -050082 res := instances.Delete(fake.ServiceClient(), instanceID)
Jamie Hannaford5b16b632015-02-10 13:36:23 +010083 th.AssertNoErr(t, res.Err)
84}
Jamie Hannaford94164fa2015-02-10 13:58:45 +010085
86func TestEnableRootUser(t *testing.T) {
87 th.SetupHTTP()
88 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +010089 HandleEnableRoot(t)
Jamie Hannaford94164fa2015-02-10 13:58:45 +010090
Jamie Hannaford3aba0b12015-02-13 14:33:39 +010091 expected := &users.User{Name: "root", Password: "secretsecret"}
jrperritt3d966162016-06-06 14:08:54 -050092 user, err := instances.EnableRootUser(fake.ServiceClient(), instanceID).Extract()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010093
Jamie Hannaford94164fa2015-02-10 13:58:45 +010094 th.AssertNoErr(t, err)
95 th.AssertDeepEquals(t, expected, user)
96}
Jamie Hannaforda74d4252015-02-10 15:35:01 +010097
98func TestIsRootEnabled(t *testing.T) {
99 th.SetupHTTP()
100 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100101 HandleIsRootEnabled(t)
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100102
jrperritt3d966162016-06-06 14:08:54 -0500103 isEnabled, err := instances.IsRootEnabled(fake.ServiceClient(), instanceID).Extract()
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100104
105 th.AssertNoErr(t, err)
106 th.AssertEquals(t, true, isEnabled)
107}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100108
Jamie Hannaford75e8cc42015-11-16 14:09:25 +0100109func TestRestart(t *testing.T) {
Jamie Hannaford219ca592015-02-10 15:59:05 +0100110 th.SetupHTTP()
111 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100112 HandleRestart(t)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100113
jrperritt3d966162016-06-06 14:08:54 -0500114 res := instances.Restart(fake.ServiceClient(), instanceID)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100115 th.AssertNoErr(t, res.Err)
116}
117
Jamie Hannaford75e8cc42015-11-16 14:09:25 +0100118func TestResize(t *testing.T) {
Jamie Hannaford219ca592015-02-10 15:59:05 +0100119 th.SetupHTTP()
120 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100121 HandleResize(t)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100122
jrperritt3d966162016-06-06 14:08:54 -0500123 res := instances.Resize(fake.ServiceClient(), instanceID, "2")
Jamie Hannaford219ca592015-02-10 15:59:05 +0100124 th.AssertNoErr(t, res.Err)
125}
126
127func TestResizeVolume(t *testing.T) {
128 th.SetupHTTP()
129 defer th.TeardownHTTP()
Jamie Hannaford4a170282015-02-18 14:16:57 +0100130 HandleResizeVol(t)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100131
jrperritt3d966162016-06-06 14:08:54 -0500132 res := instances.ResizeVolume(fake.ServiceClient(), instanceID, 4)
Jamie Hannaford219ca592015-02-10 15:59:05 +0100133 th.AssertNoErr(t, res.Err)
134}