blob: 236deae3be9d1d2de3a8a5e5f6e743ad66ca5eee [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
12func TestCreate(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
16 HandleCreateInstanceSuccessfully(t)
17
18 opts := CreateOpts{
19 Name: "json_rack_instance",
20 FlavorRef: "1",
21 Databases: DatabasesOpts{
22 DatabaseOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
23 DatabaseOpts{Name: "nextround"},
24 },
25 Users: UsersOpts{
26 UserOpts{
27 Name: "demouser",
28 Password: "demopassword",
29 Databases: DatabasesOpts{
30 DatabaseOpts{Name: "sampledb"},
31 },
32 },
33 },
34 Size: 2,
35 }
36
37 instance, err := Create(fake.ServiceClient(), opts).Extract()
38
39 expected := &Instance{
40 Created: "2014-02-13T21:47:13",
41 Updated: "2014-02-13T21:47:13",
42 Flavor: Flavor{
43 ID: "1",
44 Links: []gophercloud.Link{
45 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
46 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
47 },
48 },
49 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
50 ID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
51 Links: []gophercloud.Link{
52 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
53 },
54 Name: "json_rack_instance",
55 Status: "BUILD",
56 Volume: Volume{Size: 2},
57 }
58
59 th.AssertNoErr(t, err)
60 th.AssertDeepEquals(t, expected, instance)
61}
Jamie Hannaford90684242015-02-10 12:46:07 +010062
63func TestInstanceList(t *testing.T) {
64 th.SetupHTTP()
65 defer th.TeardownHTTP()
66
67 HandleListInstanceSuccessfully(t)
68
69 expectedInstance := Instance{
70 Flavor: Flavor{
71 ID: "1",
72 Links: []gophercloud.Link{
73 gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
74 gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"},
75 },
76 },
77 ID: "8fb081af-f237-44f5-80cc-b46be1840ca9",
78 Links: []gophercloud.Link{
79 gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", Rel: "self"},
80 },
81 Name: "xml_rack_instance",
82 Status: "ACTIVE",
83 Volume: Volume{Size: 2},
84 }
85
86 pages := 0
87 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
88 pages++
89
90 actual, err := ExtractInstances(page)
91 if err != nil {
92 return false, err
93 }
94
95 if len(actual) != 1 {
96 t.Fatalf("Expected 1 DB instance, got %d", len(actual))
97 }
98 th.CheckDeepEquals(t, expectedInstance, actual[0])
99
100 return true, nil
101 })
102
103 th.AssertNoErr(t, err)
104
105 if pages != 1 {
106 t.Errorf("Expected 1 page, saw %d", pages)
107 }
108}