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