Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 484e062

Browse files
committed
unit tests - wip
1 parent 150a922 commit 484e062

File tree

5 files changed

+280
-3
lines changed

5 files changed

+280
-3
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
//// +build fixtures
2+
3+
package meters
4+
5+
import (
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"testing"
10+
11+
th "github.com/rackspace/gophercloud/testhelper"
12+
"github.com/rackspace/gophercloud/testhelper/client"
13+
)
14+
15+
// MeterListBody contains the canned body of a meters.List response.
16+
const MeterListBody = `
17+
[
18+
{
19+
"meter_id": "YmQ5NDMxYzEtOGQ2OS00YWQzLTgwM2EtOGQ0YTZiODlmZDM2K2luc3RhbmNl\n",
20+
"name": "instance",
21+
"project_id": "35b17138-b364-4e6a-a131-8f3099c5be68",
22+
"resource_id": "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36",
23+
"source": "openstack",
24+
"type": "gauge",
25+
"unit": "instance",
26+
"user_id": "efd87807-12d2-4b38-9c70-5f5c2ac427ff"
27+
},
28+
{
29+
"user_id": "7ya0f7a33717400b951037d55b929c53",
30+
"name": "cpu_util",
31+
"resource_id": "5b88239b-8ba1-44ff-a154-978cbda23479",
32+
"source": "region2",
33+
"meter_id": "NWI4ODIzOWAtOGJhMS00NGZhLWExNTQtOTc4Y2JkYTIzNDc5K2NwdV91dGls\n",
34+
"project_id": "69e6e7c4ed8b434e92feacbf3d4891fd",
35+
"type": "gauge",
36+
"unit": "%"
37+
}
38+
]
39+
`
40+
41+
// MeterShowBody is the canned body of a Get request on an existing meter.
42+
const MeterShowBody = `
43+
[
44+
{
45+
"counter_name": "instance",
46+
"counter_type": "gauge",
47+
"counter_unit": "instance",
48+
"counter_volume": 1.0,
49+
"message_id": "5460acce-4fd6-480d-ab18-9735ec7b1996",
50+
"project_id": "35b17138-b364-4e6a-a131-8f3099c5be68",
51+
"resource_id": "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36",
52+
"resource_metadata": {
53+
"name1": "value1",
54+
"name2": "value2"
55+
},
56+
"source": "openstack",
57+
"timestamp": "2013-11-21T12:33:08.323533",
58+
"user_id": "efd87807-12d2-4b38-9c70-5f5c2ac427ff"
59+
}
60+
]
61+
`
62+
63+
// MeterStatisticsBody is the canned body of a Get statistics request on an existing meter.
64+
const MeterStatisticsBody = `
65+
[
66+
{
67+
"avg": 4.5,
68+
"count": 10,
69+
"duration": 300.0,
70+
"duration_end": "2013-01-04T16:47:00",
71+
"duration_start": "2013-01-04T16:42:00",
72+
"max": 9.0,
73+
"min": 1.0,
74+
"period": 7200,
75+
"period_end": "2013-01-04T18:00:00",
76+
"period_start": "2013-01-04T16:00:00",
77+
"sum": 45.0,
78+
"unit": "GiB"
79+
},
80+
{
81+
"count": 28162,
82+
"duration_start": "2015-06-27T20:52:08",
83+
"min": 0.06999999999999999,
84+
"max": 10.06799336650083,
85+
"duration_end": "2015-07-27T20:47:02",
86+
"period": 0,
87+
"sum": 44655.782463977856,
88+
"period_end": "2015-07-17T16:43:31",
89+
"duration": 2591694.0,
90+
"period_start": "2015-07-17T16:43:31",
91+
"avg": 1.5856751105737468,
92+
"groupby": null,
93+
"unit": "%"
94+
}
95+
]
96+
`
97+
98+
var (
99+
// MeterHerp is a MeterListResult struct that should correspond to the first result in *[]MeterListResult.
100+
MeterHerp = MeterListResult{
101+
MeterId: "YmQ5NDMxYzEtOGQ2OS00YWQzLTgwM2EtOGQ0YTZiODlmZDM2K2luc3RhbmNl",
102+
Name: "instance",
103+
ProjectId: "35b17138-b364-4e6a-a131-8f3099c5be68",
104+
ResourceId: "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36",
105+
Source: "openstack",
106+
Type: "gauge",
107+
Unit: "instance",
108+
UserId: "efd87807-12d2-4b38-9c70-5f5c2ac427ff",
109+
}
110+
111+
// MeterDerp is a MeterListResult struct that should correspond to the second result in *[]MeterListResult.
112+
MeterDerp = MeterListResult{
113+
MeterId: "NWI4ODIzOWAtOGJhMS00NGZhLWExNTQtOTc4Y2JkYTIzNDc5K2NwdV91dGls",
114+
Name: "cpu_util",
115+
ProjectId: "69e6e7c4ed8b434e92feacbf3d4891fd",
116+
ResourceId: "5b88239b-8ba1-44ff-a154-978cbda23479",
117+
Source: "region2",
118+
Type: "gauge",
119+
Unit: "%",
120+
UserId: "7ya0f7a33717400b951037d55b929c53",
121+
}
122+
123+
// StatisticsHerp is a MeterStatisticsResult struct that should correspond to the first result in *[]MeterStatisticsResult.
124+
StatisticsHerp = MeterStatisticsResult{
125+
Avg: 4.5,
126+
Count: 10,
127+
Duration: 300.0,
128+
DurationEnd: "2013-01-04T16:47:00",
129+
DurationStart: "2013-01-04T16:42:00",
130+
Max: 9.0,
131+
Min: 1.0,
132+
Period: 7200,
133+
PeriodEnd: "2013-01-04T18:00:00",
134+
PeriodStart: "2013-01-04T16:00:00",
135+
Sum: 45.0,
136+
Unit: "GiB",
137+
}
138+
139+
// StatisticsDerp is a MeterStatisticsResult struct that should correspond to the first result in *[]MeterStatisticsResult.
140+
StatisticsDerp = MeterStatisticsResult{
141+
Avg: 1.5856751105737468,
142+
Count: 28162,
143+
Duration: 2591694.0,
144+
DurationEnd: "2015-07-27T20:47:02",
145+
DurationStart: "2015-06-27T20:52:08",
146+
Max: 10.06799336650083,
147+
Min: 0.06999999999999999,
148+
Period: 0,
149+
PeriodEnd: "2015-07-17T16:43:31",
150+
PeriodStart: "2015-07-17T16:43:31",
151+
Sum: 44655.782463977856,
152+
Unit: "%",
153+
}
154+
)
155+
156+
// HandleMeterListSuccessfully sets up the test server to respond to a meters List request.
157+
func HandleMeterListSuccessfully(t *testing.T) {
158+
th.Mux.HandleFunc("/v2/meters", func(w http.ResponseWriter, r *http.Request) {
159+
th.TestMethod(t, r, "GET")
160+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
161+
162+
w.Header().Add("Content-Type", "application/json; charset=UTF-8")
163+
w.WriteHeader(http.StatusOK)
164+
fmt.Fprintf(w, MeterListBody)
165+
})
166+
}
167+
168+
// HandleMeterShowSuccessfully sets up the test server to respond to a show meter request.
169+
func HandleMeterShowSuccessfully(t *testing.T) {
170+
th.Mux.HandleFunc("/v2/meters/instance", func(w http.ResponseWriter, r *http.Request) {
171+
th.TestMethod(t, r, "GET")
172+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
173+
174+
w.Header().Add("Content-Type", "application/json")
175+
w.WriteHeader(http.StatusOK)
176+
fmt.Fprintf(w, MeterShowBody)
177+
})
178+
}
179+
180+
// HandleMeterStatisticsSuccessfully sets up the test server to respond to a show meter request.
181+
func HandleMeterStatisticsSuccessfully(t *testing.T) {
182+
th.Mux.HandleFunc("/v2/meters/memory/statistics", func(w http.ResponseWriter, r *http.Request) {
183+
th.TestMethod(t, r, "GET")
184+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
185+
186+
w.Header().Add("Content-Type", "application/json")
187+
w.WriteHeader(http.StatusOK)
188+
fmt.Fprintf(w, MeterStatisticsBody)
189+
log.Printf("%s\n", MeterStatisticsBody)
190+
})
191+
}

openstack/telemetry/v2/meters/requests.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package meters
22

3-
import "github.com/rackspace/gophercloud"
3+
import (
4+
"fmt"
5+
6+
"github.com/rackspace/gophercloud"
7+
)
48

59
// ListOptsBuilder allows extensions to add additional parameters to the
610
// List request.
@@ -82,7 +86,8 @@ func MeterStatistics(client *gophercloud.ServiceClient, n string, opts MeterStat
8286
url += query
8387
}
8488

85-
_, err = client.Get(url, &res.Body, &gophercloud.RequestOpts{})
89+
b, err := client.Get(url, &res.Body, &gophercloud.RequestOpts{})
90+
fmt.Printf("%+v\n%+v\n", res, b)
8691

8792
return
8893
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package meters
2+
3+
import (
4+
"testing"
5+
6+
th "github.com/rackspace/gophercloud/testhelper"
7+
"github.com/rackspace/gophercloud/testhelper/client"
8+
)
9+
10+
func TestListMeters(t *testing.T) {
11+
th.SetupHTTP()
12+
defer th.TeardownHTTP()
13+
HandleMeterListSuccessfully(t)
14+
15+
res, err := List(client.ServiceClient(), ListOpts{})
16+
th.AssertNoErr(t, err)
17+
list, err := res.Extract()
18+
th.AssertNoErr(t, err)
19+
20+
if len(*list) != 2 {
21+
t.Fatalf("Expected 2 meters, got %d", len(*list))
22+
}
23+
th.CheckDeepEquals(t, MeterHerp, (*list)[0])
24+
th.CheckDeepEquals(t, MeterDerp, (*list)[1])
25+
}
26+
27+
func TestMeterStatistics(t *testing.T) {
28+
th.SetupHTTP()
29+
defer th.TeardownHTTP()
30+
HandleMeterStatisticsSuccessfully(t)
31+
32+
res, err := MeterStatistics(client.ServiceClient(), "memory", MeterStatisticsOpts{})
33+
th.AssertNoErr(t, err)
34+
list, err := res.Extract()
35+
th.AssertNoErr(t, err)
36+
37+
if len(*list) != 2 {
38+
t.Fatalf("Expected 2 statistics, got %d", len(*list))
39+
}
40+
th.CheckDeepEquals(t, StatisticsHerp, (*list)[0])
41+
th.CheckDeepEquals(t, StatisticsDerp, (*list)[1])
42+
}

openstack/telemetry/v2/meters/results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type listResult struct {
2222
gophercloud.Result
2323
}
2424

25-
// Extract interprets any serverResult as a Server, if possible.
25+
// Extract interprets any listResult as an array of MeterListResult
2626
func (r listResult) Extract() (*[]MeterListResult, error) {
2727
if r.Err != nil {
2828
return nil, r.Err
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package meters
2+
3+
import (
4+
"testing"
5+
6+
"github.com/rackspace/gophercloud"
7+
th "github.com/rackspace/gophercloud/testhelper"
8+
)
9+
10+
const endpoint = "http://localhost:57909/"
11+
const meter = "cpu"
12+
13+
func endpointClient() *gophercloud.ServiceClient {
14+
return &gophercloud.ServiceClient{Endpoint: endpoint}
15+
}
16+
17+
func TestListURL(t *testing.T) {
18+
actual := listURL(endpointClient())
19+
expected := endpoint + "v2/meters"
20+
th.CheckEquals(t, expected, actual)
21+
}
22+
23+
func TestShowURL(t *testing.T) {
24+
actual := showURL(endpointClient(), meter)
25+
expected := endpoint + "v2/meters/" + meter
26+
th.CheckEquals(t, expected, actual)
27+
}
28+
29+
func TestCreateURL(t *testing.T) {
30+
actual := createURL(endpointClient(), meter)
31+
expected := endpoint + "v2/meters/" + meter
32+
th.CheckEquals(t, expected, actual)
33+
}
34+
35+
func TestStatisticsURL(t *testing.T) {
36+
actual := statisticsURL(endpointClient(), meter)
37+
expected := endpoint + "v2/meters/" + meter + "/statistics"
38+
th.CheckEquals(t, expected, actual)
39+
}

0 commit comments

Comments
 (0)