Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eed41cd

Browse files
committed
tests: Avoid use of global mocked server
The meat of this changes is in 'testhelper/http_responses.go' and 'testhelper/client/fake.go'. The rest of the change is bulk generated updates to handle those changes: Create a "FakeServer" instance instead of configuring the global fake: sed -i 's/th.SetupHTTP/fakeServer := th.SetupHTTP/g' $(ag 'th.SetupHTTP' -l) Tear down this instance on test completion: sed -i 's/defer th.TeardownHTTP/defer fakeServer.Teardown/g' $(ag 'defer th.TeardownHTTP' -l) Update calls to the global 'Mux' instance with calls to the FakeServer version: sed -i 's/th.Mux.HandleFunc/fakeServer.Mux.HandleFunc/g' $(ag 'th.Mux.HandleFunc' -l) Update calls to ServiceClient to include a 'FakeServer' parameter: sed -i 's/client.ServiceClient()/client.ServiceClient(fakeServer)/' $(ag -w client.ServiceClient -l) Plus alises for the same: sed -i 's/fake.ServiceClient()/fake.ServiceClient(fakeServer)/' $(ag 'fake.ServiceClient' -l) Update definitions and calls to various MockFoo and HandleFoo helpers - plus lowercase, private variants of same - to include a 'FakeServer' parameter: sed -i 's/\(func Mock\w\+(t \*testing.T\)/\1, fakeServer th.FakeServer/' $(ag 'func Mock\w+\(t \*testing.T' -l) sed -i 's/\(\tMock\w\+(t\)/\1, fakeServer/' $(ag '\tMock\w+\(t' -l) sed -i 's/\(func mock\w\+(t \*testing.T\)/\1, fakeServer th.FakeServer/' $(ag 'func mock\w+\(t \*testing.T' -l) sed -i 's/\(\tmock\w\+(t\)/\1, fakeServer/' $(ag '\tmock\w+\(t' -l) sed -i 's/\(func Handle\w\+(t \*testing.T\)/\1, fakeServer th.FakeServer/' $(ag 'func Handle\w+\(t \*testing.T' -l) sed -i 's/\(\tHandle\w\+(t\)/\1, fakeServer/' $(ag '\tHandle\w+\(t' -l) sed -i 's/\(func handle\w\+(t \*testing.T\)/\1, fakeServer th.FakeServer/' $(ag 'func handle\w+\(t \*testing.T' -l) sed -i 's/\(\thandle\w\+(t\)/\1, fakeServer/' $(ag '\thandle\w+\(t' -l) Finally, there were some manual fixes to handle stuff that the regexes missed. Signed-off-by: Stephen Finucane <[email protected]>
1 parent 2f52485 commit eed41cd

File tree

342 files changed

+7196
-7175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

342 files changed

+7196
-7175
lines changed

docs/contributor-tutorial/.template/testing/requests_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestListResources(t *testing.T) {
1616
HandleListResourcesSuccessfully(t)
1717

1818
count := 0
19-
err := resources.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
19+
err := resources.List(client.ServiceClient(fakeServer), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
2020
count++
2121

2222
actual, err := resources.ExtractResources(page)
@@ -35,7 +35,7 @@ func TestListResourcesAllPages(t *testing.T) {
3535
defer th.TeardownHTTP()
3636
HandleListResourcesSuccessfully(t)
3737

38-
allPages, err := resources.List(client.ServiceClient(), nil).AllPages(context.TODO())
38+
allPages, err := resources.List(client.ServiceClient(fakeServer), nil).AllPages(context.TODO())
3939
th.AssertNoErr(t, err)
4040
actual, err := resources.ExtractResources(allPages)
4141
th.AssertNoErr(t, err)
@@ -47,7 +47,7 @@ func TestGetResource(t *testing.T) {
4747
defer th.TeardownHTTP()
4848
HandleGetResourceSuccessfully(t)
4949

50-
actual, err := resources.Get(context.TODO(), client.ServiceClient(), "9fe1d3").Extract()
50+
actual, err := resources.Get(context.TODO(), client.ServiceClient(fakeServer), "9fe1d3").Extract()
5151
th.AssertNoErr(t, err)
5252
th.AssertDeepEquals(t, SecondResource, *actual)
5353
}
@@ -61,7 +61,7 @@ func TestCreateResource(t *testing.T) {
6161
Name: "resource two",
6262
}
6363

64-
actual, err := resources.Create(context.TODO(), client.ServiceClient(), createOpts).Extract()
64+
actual, err := resources.Create(context.TODO(), client.ServiceClient(fakeServer), createOpts).Extract()
6565
th.AssertNoErr(t, err)
6666
th.AssertDeepEquals(t, SecondResource, *actual)
6767
}
@@ -71,7 +71,7 @@ func TestDeleteResource(t *testing.T) {
7171
defer th.TeardownHTTP()
7272
HandleDeleteResourceSuccessfully(t)
7373

74-
res := resources.Delete(context.TODO(), client.ServiceClient(), "9fe1d3")
74+
res := resources.Delete(context.TODO(), client.ServiceClient(fakeServer), "9fe1d3")
7575
th.AssertNoErr(t, res.Err)
7676
}
7777

@@ -84,7 +84,7 @@ func TestUpdateResource(t *testing.T) {
8484
Description: "Staging Resource",
8585
}
8686

87-
actual, err := resources.Update(context.TODO(), client.ServiceClient(), "9fe1d3", updateOpts).Extract()
87+
actual, err := resources.Update(context.TODO(), client.ServiceClient(fakeServer), "9fe1d3", updateOpts).Extract()
8888
th.AssertNoErr(t, err)
8989
th.AssertDeepEquals(t, SecondResourceUpdated, *actual)
9090
}

openstack/baremetal/apiversions/testing/fixtures_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ var IronicAllAPIVersionResults = apiversions.APIVersions{
8181
},
8282
}
8383

84-
func MockListResponse(t *testing.T) {
85-
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
84+
func MockListResponse(t *testing.T, fakeServer th.FakeServer) {
85+
fakeServer.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
8686
th.TestMethod(t, r, "GET")
8787
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
8888

@@ -93,8 +93,8 @@ func MockListResponse(t *testing.T) {
9393
})
9494
}
9595

96-
func MockGetResponse(t *testing.T) {
97-
th.Mux.HandleFunc("/v1/", func(w http.ResponseWriter, r *http.Request) {
96+
func MockGetResponse(t *testing.T, fakeServer th.FakeServer) {
97+
fakeServer.Mux.HandleFunc("/v1/", func(w http.ResponseWriter, r *http.Request) {
9898
th.TestMethod(t, r, "GET")
9999
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
100100

openstack/baremetal/apiversions/testing/requests_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ import (
1010
)
1111

1212
func TestListAPIVersions(t *testing.T) {
13-
th.SetupHTTP()
14-
defer th.TeardownHTTP()
13+
fakeServer := th.SetupHTTP()
14+
defer fakeServer.Teardown()
1515

16-
MockListResponse(t)
16+
MockListResponse(t, fakeServer)
1717

18-
actual, err := apiversions.List(context.TODO(), client.ServiceClient()).Extract()
18+
actual, err := apiversions.List(context.TODO(), client.ServiceClient(fakeServer)).Extract()
1919
th.AssertNoErr(t, err)
2020

2121
th.AssertDeepEquals(t, IronicAllAPIVersionResults, *actual)
2222
}
2323

2424
func TestGetAPIVersion(t *testing.T) {
25-
th.SetupHTTP()
26-
defer th.TeardownHTTP()
25+
fakeServer := th.SetupHTTP()
26+
defer fakeServer.Teardown()
2727

28-
MockGetResponse(t)
28+
MockGetResponse(t, fakeServer)
2929

30-
actual, err := apiversions.Get(context.TODO(), client.ServiceClient(), "v1").Extract()
30+
actual, err := apiversions.Get(context.TODO(), client.ServiceClient(fakeServer), "v1").Extract()
3131
th.AssertNoErr(t, err)
3232

3333
th.AssertDeepEquals(t, IronicAPIVersion1Result, *actual)

openstack/baremetal/v1/allocations/testing/fixtures_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ var (
108108
)
109109

110110
// HandleAllocationListSuccessfully sets up the test server to respond to a allocation List request.
111-
func HandleAllocationListSuccessfully(t *testing.T) {
112-
th.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
111+
func HandleAllocationListSuccessfully(t *testing.T, fakeServer th.FakeServer) {
112+
fakeServer.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
113113
th.TestMethod(t, r, "GET")
114114
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
115115
w.Header().Add("Content-Type", "application/json")
@@ -132,8 +132,8 @@ func HandleAllocationListSuccessfully(t *testing.T) {
132132

133133
// HandleAllocationCreationSuccessfully sets up the test server to respond to a allocation creation request
134134
// with a given response.
135-
func HandleAllocationCreationSuccessfully(t *testing.T, response string) {
136-
th.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
135+
func HandleAllocationCreationSuccessfully(t *testing.T, fakeServer th.FakeServer, response string) {
136+
fakeServer.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
137137
th.TestMethod(t, r, "POST")
138138
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
139139
th.TestJSONRequest(t, r, `{
@@ -150,17 +150,17 @@ func HandleAllocationCreationSuccessfully(t *testing.T, response string) {
150150
}
151151

152152
// HandleAllocationDeletionSuccessfully sets up the test server to respond to a allocation deletion request.
153-
func HandleAllocationDeletionSuccessfully(t *testing.T) {
154-
th.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
153+
func HandleAllocationDeletionSuccessfully(t *testing.T, fakeServer th.FakeServer) {
154+
fakeServer.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
155155
th.TestMethod(t, r, "DELETE")
156156
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
157157

158158
w.WriteHeader(http.StatusNoContent)
159159
})
160160
}
161161

162-
func HandleAllocationGetSuccessfully(t *testing.T) {
163-
th.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
162+
func HandleAllocationGetSuccessfully(t *testing.T, fakeServer th.FakeServer) {
163+
fakeServer.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
164164
th.TestMethod(t, r, "GET")
165165
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
166166
th.TestHeader(t, r, "Accept", "application/json")

openstack/baremetal/v1/allocations/testing/requests_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
func TestListAllocations(t *testing.T) {
14-
th.SetupHTTP()
15-
defer th.TeardownHTTP()
16-
HandleAllocationListSuccessfully(t)
14+
fakeServer := th.SetupHTTP()
15+
defer fakeServer.Teardown()
16+
HandleAllocationListSuccessfully(t, fakeServer)
1717

1818
pages := 0
19-
err := allocations.List(client.ServiceClient(), allocations.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
19+
err := allocations.List(client.ServiceClient(fakeServer), allocations.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
2020
pages++
2121

2222
actual, err := allocations.ExtractAllocations(page)
@@ -41,11 +41,11 @@ func TestListAllocations(t *testing.T) {
4141
}
4242

4343
func TestCreateAllocation(t *testing.T) {
44-
th.SetupHTTP()
45-
defer th.TeardownHTTP()
46-
HandleAllocationCreationSuccessfully(t, SingleAllocationBody)
44+
fakeServer := th.SetupHTTP()
45+
defer fakeServer.Teardown()
46+
HandleAllocationCreationSuccessfully(t, fakeServer, SingleAllocationBody)
4747

48-
actual, err := allocations.Create(context.TODO(), client.ServiceClient(), allocations.CreateOpts{
48+
actual, err := allocations.Create(context.TODO(), client.ServiceClient(fakeServer), allocations.CreateOpts{
4949
Name: "allocation-1",
5050
ResourceClass: "baremetal",
5151
CandidateNodes: []string{"344a3e2-978a-444e-990a-cbf47c62ef88"},
@@ -57,20 +57,20 @@ func TestCreateAllocation(t *testing.T) {
5757
}
5858

5959
func TestDeleteAllocation(t *testing.T) {
60-
th.SetupHTTP()
61-
defer th.TeardownHTTP()
62-
HandleAllocationDeletionSuccessfully(t)
60+
fakeServer := th.SetupHTTP()
61+
defer fakeServer.Teardown()
62+
HandleAllocationDeletionSuccessfully(t, fakeServer)
6363

64-
res := allocations.Delete(context.TODO(), client.ServiceClient(), "344a3e2-978a-444e-990a-cbf47c62ef88")
64+
res := allocations.Delete(context.TODO(), client.ServiceClient(fakeServer), "344a3e2-978a-444e-990a-cbf47c62ef88")
6565
th.AssertNoErr(t, res.Err)
6666
}
6767

6868
func TestGetAllocation(t *testing.T) {
69-
th.SetupHTTP()
70-
defer th.TeardownHTTP()
71-
HandleAllocationGetSuccessfully(t)
69+
fakeServer := th.SetupHTTP()
70+
defer fakeServer.Teardown()
71+
HandleAllocationGetSuccessfully(t, fakeServer)
7272

73-
c := client.ServiceClient()
73+
c := client.ServiceClient(fakeServer)
7474
actual, err := allocations.Get(context.TODO(), c, "344a3e2-978a-444e-990a-cbf47c62ef88").Extract()
7575
if err != nil {
7676
t.Fatalf("Unexpected Get error: %v", err)

openstack/baremetal/v1/conductors/testing/fixtures_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var (
138138
)
139139

140140
// HandleConductorListSuccessfully sets up the test server to respond to a server List request.
141-
func HandleConductorListSuccessfully(t *testing.T) {
142-
th.Mux.HandleFunc("/conductors", func(w http.ResponseWriter, r *http.Request) {
141+
func HandleConductorListSuccessfully(t *testing.T, fakeServer th.FakeServer) {
142+
fakeServer.Mux.HandleFunc("/conductors", func(w http.ResponseWriter, r *http.Request) {
143143
th.TestMethod(t, r, "GET")
144144
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
145145
w.Header().Add("Content-Type", "application/json")
@@ -161,8 +161,8 @@ func HandleConductorListSuccessfully(t *testing.T) {
161161
}
162162

163163
// HandleConductorListDetailSuccessfully sets up the test server to respond to a server List request.
164-
func HandleConductorListDetailSuccessfully(t *testing.T) {
165-
th.Mux.HandleFunc("/conductors", func(w http.ResponseWriter, r *http.Request) {
164+
func HandleConductorListDetailSuccessfully(t *testing.T, fakeServer th.FakeServer) {
165+
fakeServer.Mux.HandleFunc("/conductors", func(w http.ResponseWriter, r *http.Request) {
166166
th.TestMethod(t, r, "GET")
167167
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
168168
w.Header().Add("Content-Type", "application/json")
@@ -174,8 +174,8 @@ func HandleConductorListDetailSuccessfully(t *testing.T) {
174174
})
175175
}
176176

177-
func HandleConductorGetSuccessfully(t *testing.T) {
178-
th.Mux.HandleFunc("/conductors/1234asdf", func(w http.ResponseWriter, r *http.Request) {
177+
func HandleConductorGetSuccessfully(t *testing.T, fakeServer th.FakeServer) {
178+
fakeServer.Mux.HandleFunc("/conductors/1234asdf", func(w http.ResponseWriter, r *http.Request) {
179179
th.TestMethod(t, r, "GET")
180180
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
181181
th.TestHeader(t, r, "Accept", "application/json")

openstack/baremetal/v1/conductors/testing/requests_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
func TestListConductors(t *testing.T) {
14-
th.SetupHTTP()
15-
defer th.TeardownHTTP()
16-
HandleConductorListSuccessfully(t)
14+
fakeServer := th.SetupHTTP()
15+
defer fakeServer.Teardown()
16+
HandleConductorListSuccessfully(t, fakeServer)
1717

1818
pages := 0
19-
err := conductors.List(client.ServiceClient(), conductors.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
19+
err := conductors.List(client.ServiceClient(fakeServer), conductors.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
2020
pages++
2121

2222
actual, err := conductors.ExtractConductors(page)
@@ -41,12 +41,12 @@ func TestListConductors(t *testing.T) {
4141
}
4242

4343
func TestListDetailConductors(t *testing.T) {
44-
th.SetupHTTP()
45-
defer th.TeardownHTTP()
46-
HandleConductorListDetailSuccessfully(t)
44+
fakeServer := th.SetupHTTP()
45+
defer fakeServer.Teardown()
46+
HandleConductorListDetailSuccessfully(t, fakeServer)
4747

4848
pages := 0
49-
err := conductors.List(client.ServiceClient(), conductors.ListOpts{Detail: true}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
49+
err := conductors.List(client.ServiceClient(fakeServer), conductors.ListOpts{Detail: true}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
5050
pages++
5151

5252
actual, err := conductors.ExtractConductors(page)
@@ -93,11 +93,11 @@ func TestListOpts(t *testing.T) {
9393
}
9494

9595
func TestGetConductor(t *testing.T) {
96-
th.SetupHTTP()
97-
defer th.TeardownHTTP()
98-
HandleConductorGetSuccessfully(t)
96+
fakeServer := th.SetupHTTP()
97+
defer fakeServer.Teardown()
98+
HandleConductorGetSuccessfully(t, fakeServer)
9999

100-
c := client.ServiceClient()
100+
c := client.ServiceClient(fakeServer)
101101
actual, err := conductors.Get(context.TODO(), c, "1234asdf").Extract()
102102
if err != nil {
103103
t.Fatalf("Unexpected Get error: %v", err)

openstack/baremetal/v1/drivers/testing/fixtures_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ var (
368368
)
369369

370370
// HandleListDriversSuccessfully sets up the test server to respond to a drivers ListDrivers request.
371-
func HandleListDriversSuccessfully(t *testing.T) {
372-
th.Mux.HandleFunc("/drivers", func(w http.ResponseWriter, r *http.Request) {
371+
func HandleListDriversSuccessfully(t *testing.T, fakeServer th.FakeServer) {
372+
fakeServer.Mux.HandleFunc("/drivers", func(w http.ResponseWriter, r *http.Request) {
373373
th.TestMethod(t, r, "GET")
374374
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
375375
w.Header().Add("Content-Type", "application/json")
@@ -382,8 +382,8 @@ func HandleListDriversSuccessfully(t *testing.T) {
382382
}
383383

384384
// HandleGetDriverDetailsSuccessfully sets up the test server to respond to a drivers GetDriverDetails request.
385-
func HandleGetDriverDetailsSuccessfully(t *testing.T) {
386-
th.Mux.HandleFunc("/drivers/ipmi", func(w http.ResponseWriter, r *http.Request) {
385+
func HandleGetDriverDetailsSuccessfully(t *testing.T, fakeServer th.FakeServer) {
386+
fakeServer.Mux.HandleFunc("/drivers/ipmi", func(w http.ResponseWriter, r *http.Request) {
387387
th.TestMethod(t, r, "GET")
388388
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
389389
th.TestHeader(t, r, "Accept", "application/json")
@@ -393,8 +393,8 @@ func HandleGetDriverDetailsSuccessfully(t *testing.T) {
393393
}
394394

395395
// HandleGetDriverPropertiesSuccessfully sets up the test server to respond to a drivers GetDriverProperties request.
396-
func HandleGetDriverPropertiesSuccessfully(t *testing.T) {
397-
th.Mux.HandleFunc("/drivers/agent_ipmitool/properties", func(w http.ResponseWriter, r *http.Request) {
396+
func HandleGetDriverPropertiesSuccessfully(t *testing.T, fakeServer th.FakeServer) {
397+
fakeServer.Mux.HandleFunc("/drivers/agent_ipmitool/properties", func(w http.ResponseWriter, r *http.Request) {
398398
th.TestMethod(t, r, "GET")
399399
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
400400
th.TestHeader(t, r, "Accept", "application/json")
@@ -404,8 +404,8 @@ func HandleGetDriverPropertiesSuccessfully(t *testing.T) {
404404
}
405405

406406
// HandleGetDriverDiskPropertiesSuccessfully sets up the test server to respond to a drivers GetDriverDiskProperties request.
407-
func HandleGetDriverDiskPropertiesSuccessfully(t *testing.T) {
408-
th.Mux.HandleFunc("/drivers/agent_ipmitool/raid/logical_disk_properties", func(w http.ResponseWriter, r *http.Request) {
407+
func HandleGetDriverDiskPropertiesSuccessfully(t *testing.T, fakeServer th.FakeServer) {
408+
fakeServer.Mux.HandleFunc("/drivers/agent_ipmitool/raid/logical_disk_properties", func(w http.ResponseWriter, r *http.Request) {
409409
th.TestMethod(t, r, "GET")
410410
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
411411
th.TestHeader(t, r, "Accept", "application/json")

0 commit comments

Comments
 (0)