blob: 14120b33204baa33a516ff71e759752ec87bad1c [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 "github.com/rackspace/gophercloud/rackspace/db/v1/instances"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10 "github.com/rackspace/gophercloud/testhelper/fixture"
11)
12
13var (
14 configID = "{configID}"
15 _baseURL = "/configurations"
16 resURL = _baseURL + "/" + configID
17)
18
19func TestList(t *testing.T) {
20 th.SetupHTTP()
21 defer th.TeardownHTTP()
22
23 fixture.SetupHandler(t, _baseURL, "GET", "", listConfigsJSON, 200)
24
25 count := 0
26 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
27 count++
28 actual, err := ExtractConfigs(page)
29 th.AssertNoErr(t, err)
30
31 expected := []Config{exampleConfig}
32 th.AssertDeepEquals(t, expected, actual)
33
34 return true, nil
35 })
36
37 th.AssertEquals(t, 1, count)
38 th.AssertNoErr(t, err)
39}
40
41func TestGet(t *testing.T) {
42 th.SetupHTTP()
43 defer th.TeardownHTTP()
44
45 fixture.SetupHandler(t, resURL, "GET", "", getConfigJSON, 200)
46
47 config, err := Get(fake.ServiceClient(), configID).Extract()
48 th.AssertNoErr(t, err)
49 th.AssertDeepEquals(t, &exampleConfig, config)
50}
51
52func TestCreate(t *testing.T) {
53 th.SetupHTTP()
54 defer th.TeardownHTTP()
55
56 fixture.SetupHandler(t, _baseURL, "POST", createReq, createConfigJSON, 201)
57
58 opts := CreateOpts{
59 Datastore: &DatastoreOpts{
60 Type: "a00000a0-00a0-0a00-00a0-000a000000aa",
61 Version: "b00000b0-00b0-0b00-00b0-000b000000bb",
62 },
63 Description: "example description",
64 Name: "example-configuration-name",
65 Values: map[string]interface{}{
66 "collation_server": "latin1_swedish_ci",
67 "connect_timeout": 120,
68 },
69 }
70
71 config, err := Create(fake.ServiceClient(), opts).Extract()
72 th.AssertNoErr(t, err)
73 th.AssertDeepEquals(t, &exampleConfigWithValues, config)
74}
75
76func TestUpdate(t *testing.T) {
77 th.SetupHTTP()
78 defer th.TeardownHTTP()
79
80 fixture.SetupHandler(t, resURL, "PATCH", updateReq, "", 200)
81
82 opts := UpdateOpts{
83 Values: map[string]interface{}{
84 "connect_timeout": 300,
85 },
86 }
87
88 err := Update(fake.ServiceClient(), configID, opts).ExtractErr()
89 th.AssertNoErr(t, err)
90}
91
92func TestReplace(t *testing.T) {
93 th.SetupHTTP()
94 defer th.TeardownHTTP()
95
96 fixture.SetupHandler(t, resURL, "PUT", updateReq, "", 202)
97
98 opts := UpdateOpts{
99 Values: map[string]interface{}{
100 "connect_timeout": 300,
101 },
102 }
103
104 err := Replace(fake.ServiceClient(), configID, opts).ExtractErr()
105 th.AssertNoErr(t, err)
106}
107
108func TestDelete(t *testing.T) {
109 th.SetupHTTP()
110 defer th.TeardownHTTP()
111
112 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
113
114 err := Delete(fake.ServiceClient(), configID).ExtractErr()
115 th.AssertNoErr(t, err)
116}
117
118func TestListInstances(t *testing.T) {
119 th.SetupHTTP()
120 defer th.TeardownHTTP()
121
122 fixture.SetupHandler(t, resURL+"/instances", "GET", "", listInstancesJSON, 200)
123
124 expectedInstance := instances.Instance{
125 ID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
126 Name: "json_rack_instance",
127 }
128
129 pages := 0
130 err := ListInstances(fake.ServiceClient(), configID).EachPage(func(page pagination.Page) (bool, error) {
131 pages++
132
133 actual, err := instances.ExtractInstances(page)
134 if err != nil {
135 return false, err
136 }
137
138 th.AssertDeepEquals(t, actual, []instances.Instance{expectedInstance})
139
140 return true, nil
141 })
142
143 th.AssertNoErr(t, err)
144 th.AssertEquals(t, 1, pages)
145}