blob: 6c0825ba43f21a0d6d9107d037e504394f7d7b99 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud"
Jamie Hannaford90684242015-02-10 12:46:07 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01008 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
Jamie Hannaford821015f2015-02-10 12:58:36 +010012var instanceID = "d4603f69-ec7e-4e9b-803f-600b9205576f"
13
14var expectedInstance = &Instance{
15 Created: "2014-02-13T21:47:13",
16 Updated: "2014-02-13T21:47:13",
17 Flavor: Flavor{
18 ID: "1",
19 Links: []gophercloud.Link{
20 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
21 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
22 },
23 },
24 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
25 ID: instanceID,
26 Links: []gophercloud.Link{
27 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
28 },
29 Name: "json_rack_instance",
30 Status: "BUILD",
31 Volume: Volume{Size: 2},
32}
33
Jamie Hannaford9fdda582015-02-10 12:15:43 +010034func TestCreate(t *testing.T) {
35 th.SetupHTTP()
36 defer th.TeardownHTTP()
37
38 HandleCreateInstanceSuccessfully(t)
39
40 opts := CreateOpts{
41 Name: "json_rack_instance",
42 FlavorRef: "1",
43 Databases: DatabasesOpts{
44 DatabaseOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
45 DatabaseOpts{Name: "nextround"},
46 },
47 Users: UsersOpts{
48 UserOpts{
49 Name: "demouser",
50 Password: "demopassword",
51 Databases: DatabasesOpts{
52 DatabaseOpts{Name: "sampledb"},
53 },
54 },
55 },
56 Size: 2,
57 }
58
59 instance, err := Create(fake.ServiceClient(), opts).Extract()
60
Jamie Hannaford9fdda582015-02-10 12:15:43 +010061 th.AssertNoErr(t, err)
Jamie Hannaford821015f2015-02-10 12:58:36 +010062 th.AssertDeepEquals(t, expectedInstance, instance)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010063}
Jamie Hannaford90684242015-02-10 12:46:07 +010064
65func TestInstanceList(t *testing.T) {
66 th.SetupHTTP()
67 defer th.TeardownHTTP()
68
69 HandleListInstanceSuccessfully(t)
70
71 expectedInstance := Instance{
72 Flavor: Flavor{
73 ID: "1",
74 Links: []gophercloud.Link{
75 gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
76 gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"},
77 },
78 },
79 ID: "8fb081af-f237-44f5-80cc-b46be1840ca9",
80 Links: []gophercloud.Link{
81 gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", Rel: "self"},
82 },
83 Name: "xml_rack_instance",
84 Status: "ACTIVE",
85 Volume: Volume{Size: 2},
86 }
87
88 pages := 0
89 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
90 pages++
91
92 actual, err := ExtractInstances(page)
93 if err != nil {
94 return false, err
95 }
96
97 if len(actual) != 1 {
98 t.Fatalf("Expected 1 DB instance, got %d", len(actual))
99 }
100 th.CheckDeepEquals(t, expectedInstance, actual[0])
101
102 return true, nil
103 })
104
105 th.AssertNoErr(t, err)
106
107 if pages != 1 {
108 t.Errorf("Expected 1 page, saw %d", pages)
109 }
110}
Jamie Hannaford821015f2015-02-10 12:58:36 +0100111
112func TestGetInstance(t *testing.T) {
113 th.SetupHTTP()
114 defer th.TeardownHTTP()
115
116 HandleGetInstanceSuccessfully(t, instanceID)
117
118 instance, err := Get(fake.ServiceClient(), instanceID).Extract()
119
120 th.AssertNoErr(t, err)
121 th.AssertDeepEquals(t, instance, expectedInstance)
122}