Merge lp:~jtv/gwacl/management-test-helpers into lp:gwacl

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: 148
Merged at revision: 147
Proposed branch: lp:~jtv/gwacl/management-test-helpers
Merge into: lp:gwacl
Diff against target: 269 lines (+89/-109)
1 file modified
management_test.go (+89/-109)
To merge this branch: bzr merge lp:~jtv/gwacl/management-test-helpers
Reviewer Review Type Date Requested Status
Julian Edwards (community) Approve
Review via email: mp+172123@code.launchpad.net

Commit message

Clean up management_test.go a bit, using some test helpers.

Description of the change

In my branch to create ListAllDeployments I had to add a test to management_test.go, and these tests were pretty long and heavy with fixture. So I extracted some of the repetition. There's a lot of little functions now, as is my wont, but the good news is that you can usually ignore these. The test still shrinks overall, and the actual tests shrink by a lot.

Jeroen

To post a comment you must log in.
148. By Jeroen T. Vermeulen

Kick off fresh diff.

Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

Strange: the ListDeployments/ListAllDeployments split, already landed in trunk, now shows up as part of this diff. That's why it looks as if I'm increasing line count instead of reducing it. I'll see if kicking off a fresh diff helps.

Revision history for this message
Julian Edwards (julian-edwards) wrote :

51 + return []DispatcherResponse{
52 + {
53 + response: &x509Response{
54 + StatusCode: http.StatusOK,
55 + Body: []byte(body),
56 + },
57 + },
58 + }

<whinge type=general>
I wish go fmt would make its mind up whether it prefers ANSI or K&R style braces. This is getting on my nerves.
</whinge>

Also do you fancy making a branch to s/rig/patch/ everywhere? If you don't I will!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'management_test.go'
2--- management_test.go 2013-06-28 17:02:53 +0000
3+++ management_test.go 2013-06-30 02:20:30 +0000
4@@ -4,6 +4,7 @@
5 package gwacl
6
7 import (
8+ "fmt"
9 . "launchpad.net/gocheck"
10 "net/http"
11 )
12@@ -12,67 +13,90 @@
13
14 var _ = Suite(&managementAPISuite{})
15
16+// makeNamedRoleInstances creates an array of RoleInstance objects, each with
17+// the respective given name.
18+func makeNamedRoleInstances(names ...string) []RoleInstance {
19+ instances := make([]RoleInstance, 0)
20+ for _, name := range names {
21+ instances = append(instances, RoleInstance{RoleName: name})
22+ }
23+ return instances
24+}
25+
26+// makeNamedDeployments creates an array of Deployment objects, each with
27+// the respective given name.
28+func makeNamedDeployments(names ...string) []Deployment {
29+ deployments := make([]Deployment, 0)
30+ for _, name := range names {
31+ deployments = append(deployments, Deployment{Name: name})
32+ }
33+ return deployments
34+}
35+
36+// makeHostedService creates a HostedService with the given deployments.
37+func makeHostedService(deployments []Deployment) HostedService {
38+ return HostedService{
39+ ServiceName: "S1",
40+ Deployments: deployments,
41+ }
42+}
43+
44+// makeOKXMLResponse creates a DispatcherResponse with status code OK, and
45+// an XML-serialized version of the given object.
46+// The response is wrapped in a slice because that's slightly easier for
47+// the callers.
48+func makeOKXMLResponse(c *C, bodyObject AzureObject) []DispatcherResponse {
49+ body, err := bodyObject.Serialize()
50+ c.Assert(err, IsNil)
51+ return []DispatcherResponse{
52+ {
53+ response: &x509Response{
54+ StatusCode: http.StatusOK,
55+ Body: []byte(body),
56+ },
57+ },
58+ }
59+}
60+
61 // TestListInstances goes through the happy path for ListInstances.
62 func (suite *managementAPISuite) TestListInstances(c *C) {
63- // The list of properties for "S1".
64- propertiesS1 := HostedService{
65- ServiceName: "S1",
66- Deployments: []Deployment{
67- {
68- RoleInstanceList: []RoleInstance{
69- {RoleName: "one"},
70- {RoleName: "two"},
71- },
72- },
73- {
74- RoleInstanceList: []RoleInstance{
75- {RoleName: "three"},
76- {RoleName: "four"},
77- },
78- },
79- },
80- }
81- propertiesS1XML, err := propertiesS1.Serialize()
82- c.Assert(err, IsNil)
83- propertiesS1Response := DispatcherResponse{
84- response: &x509Response{
85- Body: []byte(propertiesS1XML),
86- StatusCode: http.StatusOK,
87- },
88- }
89- // Rig the prepared response in.
90+ service := makeHostedService(
91+ []Deployment{
92+ {RoleInstanceList: makeNamedRoleInstances("one", "two")},
93+ {RoleInstanceList: makeNamedRoleInstances("three", "four")},
94+ })
95 record := []*X509Request{}
96- rigRecordingPreparedResponseDispatcher(
97- &record, []DispatcherResponse{propertiesS1Response},
98- )
99- // Finally, exercise ListInstances.
100+ rigRecordingPreparedResponseDispatcher(&record, makeOKXMLResponse(c, service))
101+
102+ // Exercise ListInstances.
103 api := makeAPI(c)
104- request := &ListInstancesRequest{ServiceName: propertiesS1.ServiceName}
105+ request := &ListInstancesRequest{ServiceName: service.ServiceName}
106 instances, err := api.ListInstances(request)
107 c.Assert(err, IsNil)
108+
109+ // We get the expected instances back.
110 c.Check(instances, DeepEquals, []RoleInstance{
111- propertiesS1.Deployments[0].RoleInstanceList[0],
112- propertiesS1.Deployments[0].RoleInstanceList[1],
113- propertiesS1.Deployments[1].RoleInstanceList[0],
114- propertiesS1.Deployments[1].RoleInstanceList[1],
115+ service.Deployments[0].RoleInstanceList[0],
116+ service.Deployments[0].RoleInstanceList[1],
117+ service.Deployments[1].RoleInstanceList[0],
118+ service.Deployments[1].RoleInstanceList[1],
119 })
120- // The only request is for S1's properties
121+
122+ // The only request is for the service's properties
123 c.Assert(len(record), Not(Equals), 0)
124+ expectedURL := fmt.Sprintf(
125+ "%ssubscriptionId/services/hostedservices/%s?embed-detail=true",
126+ AZURE_URL, service.ServiceName)
127 c.Check(record[0], DeepEquals, &X509Request{
128 APIVersion: baseAPIVersion,
129- URL: AZURE_URL + "subscriptionId/services/hostedservices/S1?embed-detail=true",
130+ URL: expectedURL,
131 Method: "GET",
132 })
133 }
134
135 func (suite *managementAPISuite) TestListInstancesFailsGettingDetails(c *C) {
136- propertiesS1Response := DispatcherResponse{
137- response: &x509Response{
138- StatusCode: http.StatusNotFound,
139- },
140- }
141 rigPreparedResponseDispatcher(
142- []DispatcherResponse{propertiesS1Response},
143+ []DispatcherResponse{{response: &x509Response{StatusCode: http.StatusNotFound}}},
144 )
145 api := makeAPI(c)
146 request := &ListInstancesRequest{ServiceName: "SomeService"}
147@@ -84,101 +108,57 @@
148
149 // TestListAllDeployments goes through the happy path for ListAllDeployments.
150 func (suite *managementAPISuite) TestListAllDeployments(c *C) {
151- // The list of properties for "S1".
152- propertiesS1 := HostedService{
153- ServiceName: "S1",
154- Deployments: []Deployment{
155- {Name: "one"}, {Name: "two"},
156- },
157- }
158- propertiesS1XML, err := propertiesS1.Serialize()
159- c.Assert(err, IsNil)
160- propertiesS1Response := DispatcherResponse{
161- response: &x509Response{
162- Body: []byte(propertiesS1XML),
163- StatusCode: http.StatusOK,
164- },
165- }
166- // Rig the prepared response in.
167+ service := makeHostedService(makeNamedDeployments("one", "two"))
168 record := []*X509Request{}
169- rigRecordingPreparedResponseDispatcher(
170- &record, []DispatcherResponse{propertiesS1Response},
171- )
172- // Finally, exercise ListDeployments.
173+ rigRecordingPreparedResponseDispatcher(&record, makeOKXMLResponse(c, service))
174+
175+ // Exercise ListDeployments.
176 api := makeAPI(c)
177- request := &ListAllDeploymentsRequest{ServiceName: propertiesS1.ServiceName}
178+ request := &ListAllDeploymentsRequest{ServiceName: service.ServiceName}
179 deployments, err := api.ListAllDeployments(request)
180 c.Assert(err, IsNil)
181- c.Check(deployments, DeepEquals, propertiesS1.Deployments)
182+
183+ // We get the complete set of deployments back.
184+ c.Check(deployments, DeepEquals, service.Deployments)
185+
186 // Only one request to the API is made.
187 c.Assert(len(record), Equals, 1)
188 }
189
190 // TestListDeployments tests ListDeployments, including filtering by name.
191 func (suite *managementAPISuite) TestListDeployments(c *C) {
192- // The list of properties for "S1".
193- propertiesS1 := HostedService{
194- ServiceName: "S1",
195- Deployments: []Deployment{
196- {Name: "Arthur"}, {Name: "Bobby"},
197- },
198- }
199- propertiesS1XML, err := propertiesS1.Serialize()
200- c.Assert(err, IsNil)
201- propertiesS1Response := DispatcherResponse{
202- response: &x509Response{
203- Body: []byte(propertiesS1XML),
204- StatusCode: http.StatusOK,
205- },
206- }
207- // Rig the prepared response in.
208+ service := makeHostedService(makeNamedDeployments("Arthur", "Bobby"))
209 record := []*X509Request{}
210- rigRecordingPreparedResponseDispatcher(
211- &record, []DispatcherResponse{propertiesS1Response},
212- )
213- // Finally, exercise ListDeployments.
214+ rigRecordingPreparedResponseDispatcher(&record, makeOKXMLResponse(c, service))
215+
216+ // Exercise ListDeployments.
217 api := makeAPI(c)
218 request := &ListDeploymentsRequest{
219- ServiceName: propertiesS1.ServiceName,
220+ ServiceName: service.ServiceName,
221 DeploymentNames: []string{"Arthur"},
222 }
223 deployments, err := api.ListDeployments(request)
224 c.Assert(err, IsNil)
225+
226 // Only the first deployment - named "Arthur" - is returned.
227- c.Check(deployments, DeepEquals, []Deployment{propertiesS1.Deployments[0]})
228+ c.Check(deployments, DeepEquals, []Deployment{service.Deployments[0]})
229 // Only one request to the API is made.
230 c.Assert(len(record), Equals, 1)
231 }
232
233 func (suite *managementAPISuite) TestListDeploymentsWithoutNamesReturnsNothing(c *C) {
234- // The list of properties for "S1".
235- propertiesS1 := HostedService{
236- ServiceName: "S1",
237- Deployments: []Deployment{
238- {Name: "Arthur"}, {Name: "Bobby"},
239- },
240- }
241- propertiesS1XML, err := propertiesS1.Serialize()
242- c.Assert(err, IsNil)
243- propertiesS1Response := DispatcherResponse{
244- response: &x509Response{
245- Body: []byte(propertiesS1XML),
246- StatusCode: http.StatusOK,
247- },
248- }
249- // Rig the prepared response in.
250+ service := makeHostedService(makeNamedDeployments("One", "Two"))
251 record := []*X509Request{}
252- rigRecordingPreparedResponseDispatcher(
253- &record, []DispatcherResponse{propertiesS1Response},
254- )
255- // Finally, exercise ListDeployments.
256+ rigRecordingPreparedResponseDispatcher(&record, makeOKXMLResponse(c, service))
257+ // Exercise ListDeployments.
258 api := makeAPI(c)
259 request := &ListDeploymentsRequest{
260- ServiceName: propertiesS1.ServiceName,
261+ ServiceName: service.ServiceName,
262 DeploymentNames: []string{},
263 }
264 deployments, err := api.ListDeployments(request)
265 c.Assert(err, IsNil)
266+
267 // No deployments are returned.
268 c.Check(len(deployments), Equals, 0)
269 }

Subscribers

People subscribed via source and target branches