blob: bdda1f69a4f48a5322a2d2ed3695393c42e7cea0 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud"
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +01007 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
Jamie Hannaford3aba0b12015-02-13 14:33:39 +01008 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +01009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010010 th "github.com/rackspace/gophercloud/testhelper"
11 fake "github.com/rackspace/gophercloud/testhelper/client"
12)
13
Jamie Hannaford821015f2015-02-10 12:58:36 +010014var instanceID = "d4603f69-ec7e-4e9b-803f-600b9205576f"
15
16var expectedInstance = &Instance{
17 Created: "2014-02-13T21:47:13",
18 Updated: "2014-02-13T21:47:13",
19 Flavor: Flavor{
20 ID: "1",
21 Links: []gophercloud.Link{
22 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
23 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
24 },
25 },
26 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
27 ID: instanceID,
28 Links: []gophercloud.Link{
29 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
30 },
31 Name: "json_rack_instance",
32 Status: "BUILD",
33 Volume: Volume{Size: 2},
34}
35
Jamie Hannaford9fdda582015-02-10 12:15:43 +010036func TestCreate(t *testing.T) {
37 th.SetupHTTP()
38 defer th.TeardownHTTP()
39
40 HandleCreateInstanceSuccessfully(t)
41
42 opts := CreateOpts{
43 Name: "json_rack_instance",
44 FlavorRef: "1",
Jamie Hannaford85f10332015-02-12 11:51:37 +010045 Databases: db.BatchCreateOpts{
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010046 db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
47 db.CreateOpts{Name: "nextround"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010048 },
Jamie Hannaford3aba0b12015-02-13 14:33:39 +010049 Users: users.BatchCreateOpts{
50 users.CreateOpts{
Jamie Hannaford9fdda582015-02-10 12:15:43 +010051 Name: "demouser",
52 Password: "demopassword",
Jamie Hannaford85f10332015-02-12 11:51:37 +010053 Databases: db.BatchCreateOpts{
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010054 db.CreateOpts{Name: "sampledb"},
Jamie Hannaford9fdda582015-02-10 12:15:43 +010055 },
56 },
57 },
58 Size: 2,
59 }
60
61 instance, err := Create(fake.ServiceClient(), opts).Extract()
62
Jamie Hannaford9fdda582015-02-10 12:15:43 +010063 th.AssertNoErr(t, err)
Jamie Hannaford821015f2015-02-10 12:58:36 +010064 th.AssertDeepEquals(t, expectedInstance, instance)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010065}
Jamie Hannaford90684242015-02-10 12:46:07 +010066
67func TestInstanceList(t *testing.T) {
68 th.SetupHTTP()
69 defer th.TeardownHTTP()
70
71 HandleListInstanceSuccessfully(t)
72
73 expectedInstance := Instance{
74 Flavor: Flavor{
75 ID: "1",
76 Links: []gophercloud.Link{
77 gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
78 gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"},
79 },
80 },
81 ID: "8fb081af-f237-44f5-80cc-b46be1840ca9",
82 Links: []gophercloud.Link{
83 gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", Rel: "self"},
84 },
85 Name: "xml_rack_instance",
86 Status: "ACTIVE",
87 Volume: Volume{Size: 2},
88 }
89
90 pages := 0
91 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
92 pages++
93
94 actual, err := ExtractInstances(page)
95 if err != nil {
96 return false, err
97 }
98
99 if len(actual) != 1 {
100 t.Fatalf("Expected 1 DB instance, got %d", len(actual))
101 }
102 th.CheckDeepEquals(t, expectedInstance, actual[0])
103
104 return true, nil
105 })
106
107 th.AssertNoErr(t, err)
108
109 if pages != 1 {
110 t.Errorf("Expected 1 page, saw %d", pages)
111 }
112}
Jamie Hannaford821015f2015-02-10 12:58:36 +0100113
114func TestGetInstance(t *testing.T) {
115 th.SetupHTTP()
116 defer th.TeardownHTTP()
117
118 HandleGetInstanceSuccessfully(t, instanceID)
119
120 instance, err := Get(fake.ServiceClient(), instanceID).Extract()
121
122 th.AssertNoErr(t, err)
123 th.AssertDeepEquals(t, instance, expectedInstance)
124}
Jamie Hannaford5b16b632015-02-10 13:36:23 +0100125
126func TestDeleteInstance(t *testing.T) {
127 th.SetupHTTP()
128 defer th.TeardownHTTP()
129
130 HandleDeleteInstanceSuccessfully(t, instanceID)
131
132 res := Delete(fake.ServiceClient(), instanceID)
133 th.AssertNoErr(t, res.Err)
134}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100135
136func TestEnableRootUser(t *testing.T) {
137 th.SetupHTTP()
138 defer th.TeardownHTTP()
139
140 HandleEnableRootUserSuccessfully(t, instanceID)
141
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100142 expected := &users.User{Name: "root", Password: "secretsecret"}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100143
144 user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
145 th.AssertNoErr(t, err)
146 th.AssertDeepEquals(t, expected, user)
147}
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100148
149func TestIsRootEnabled(t *testing.T) {
150 th.SetupHTTP()
151 defer th.TeardownHTTP()
152
153 HandleIsRootEnabledSuccessfully(t, instanceID)
154
155 isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID)
156
157 th.AssertNoErr(t, err)
158 th.AssertEquals(t, true, isEnabled)
159}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100160
161func TestRestartService(t *testing.T) {
162 th.SetupHTTP()
163 defer th.TeardownHTTP()
164
165 HandleRestartSuccessfully(t, instanceID)
166
167 res := RestartService(fake.ServiceClient(), instanceID)
168
169 th.AssertNoErr(t, res.Err)
170}
171
172func TestResizeInstance(t *testing.T) {
173 th.SetupHTTP()
174 defer th.TeardownHTTP()
175
176 HandleResizeInstanceSuccessfully(t, instanceID)
177
178 res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
179
180 th.AssertNoErr(t, res.Err)
181}
182
183func TestResizeVolume(t *testing.T) {
184 th.SetupHTTP()
185 defer th.TeardownHTTP()
186
187 HandleResizeVolSuccessfully(t, instanceID)
188
189 res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
190
191 th.AssertNoErr(t, res.Err)
192}