Skip to content

Commit 78fa5e4

Browse files
authored
Refactor tests - decouple test data from test files (#14)
* refactor tests * Decouple Test data from tests
1 parent 83acc75 commit 78fa5e4

61 files changed

Lines changed: 4130 additions & 3446 deletions

File tree

Some content is hidden

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

cloudstack/AccountService_test.go

Lines changed: 14 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -27,97 +27,20 @@ import (
2727
)
2828

2929
func TestListAccounts(t *testing.T) {
30+
apiName := "listAccountsResponse"
3031
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31-
resp := `
32-
{
33-
"listaccountsresponse": {
34-
"account": [
35-
{
36-
"accountdetails": {
37-
"key0": "value0",
38-
"key1": "value1"
39-
},
40-
"accounttype": 1,
41-
"cpuavailable": "Unlimited",
42-
"cpulimit": "Unlimited",
43-
"cputotal": 1,
44-
"domain": "ROOT",
45-
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
46-
"groups": [],
47-
"id": "62f61a73-5f5a-11e5-bc86-0242ac11180a",
48-
"ipavailable": "Unlimited",
49-
"iplimit": "Unlimited",
50-
"iptotal": 1,
51-
"isdefault": true,
52-
"memoryavailable": "Unlimited",
53-
"memorylimit": "Unlimited",
54-
"memorytotal": 512,
55-
"name": "admin",
56-
"networkavailable": "Unlimited",
57-
"networklimit": "Unlimited",
58-
"networktotal": 1,
59-
"primarystorageavailable": "Unlimited",
60-
"primarystoragelimit": "Unlimited",
61-
"primarystoragetotal": 2,
62-
"projectavailable": "Unlimited",
63-
"projectlimit": "Unlimited",
64-
"projecttotal": 1,
65-
"receivedbytes": 100,
66-
"secondarystorageavailable": "Unlimited",
67-
"secondarystoragelimit": "Unlimited",
68-
"secondarystoragetotal": 0,
69-
"sentbytes": 100,
70-
"snapshotavailable": "Unlimited",
71-
"snapshotlimit": "Unlimited",
72-
"snapshottotal": 0,
73-
"state": "enabled",
74-
"templateavailable": "Unlimited",
75-
"templatelimit": "Unlimited",
76-
"templatetotal": 0,
77-
"user": [
78-
{
79-
"account": "admin",
80-
"accountid": "62f61a73-5f5a-11e5-bc86-0242ac11180a",
81-
"accounttype": 1,
82-
"apikey": "BFCyYeY0HhhrvNWC7FBwHTlso52ow3XWZUzNuiv03jmJOQn-5UH5BTVRVheiBnaRfYFE9yOzjwiqkbahq9P-Rw",
83-
"created": "2015-09-20T05:42:31+0000",
84-
"domain": "ROOT",
85-
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
86-
"email": "admin@mailprovider.com",
87-
"firstname": "Admin",
88-
"id": "62f6394b-5f5a-11e5-bc86-0242ac11180a",
89-
"iscallerchilddomain": false,
90-
"isdefault": true,
91-
"lastname": "User",
92-
"secretkey": "Em1UPdKTYEDaUxPXKtpqy_1uYluHWU2ZHotPi-VnBi_-MOEUAANoFHg3ycDProluDzYAXmT3YRssk367ylr_4A",
93-
"state": "enabled",
94-
"username": "admin"
95-
}
96-
],
97-
"vmavailable": "Unlimited",
98-
"vmlimit": "Unlimited",
99-
"vmrunning": 1,
100-
"vmtotal": 1,
101-
"volumeavailable": "Unlimited",
102-
"volumelimit": "Unlimited",
103-
"volumetotal": 1,
104-
"vpcavailable": "Unlimited",
105-
"vpclimit": "Unlimited",
106-
"vpctotal": 0
107-
}
108-
],
109-
"count": 1
110-
}
111-
}
112-
`
113-
fmt.Fprintln(w, resp)
32+
resp, err := ReadData(apiName, "AccountService")
33+
if err != nil {
34+
t.Errorf("Failed to read response data, due to: %v", err)
35+
return
36+
}
37+
fmt.Fprintln(w, resp[apiName])
11438
}))
11539
defer server.Close()
11640

11741
client := newClient(server.URL, "APIKEY", "SECRETKEY", true, true)
11842
p := client.Account.NewListAccountsParams()
11943
acc, _ := client.Account.ListAccounts(p)
120-
12144
accounts := acc.Accounts
12245
if len(accounts) != 1 {
12346
t.Errorf("length: actual %d, expected 1", len(accounts))
@@ -141,82 +64,14 @@ func TestListAccounts(t *testing.T) {
14164
}
14265

14366
func TestCreateAccounts(t *testing.T) {
67+
apiName := "createAccountsResponse"
14468
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
145-
resp := `
146-
{
147-
"createaccountresponse": {
148-
"account": {
149-
"accountdetails": {
150-
"key0": "value0",
151-
"key1": "value1"
152-
},
153-
"accounttype": 2,
154-
"cpuavailable": "40",
155-
"cpulimit": "40",
156-
"cputotal": 0,
157-
"domain": "ROOT",
158-
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
159-
"groups": [],
160-
"id": "c9cb9df8-dcd5-44c5-a40d-2e7d266669a6",
161-
"ipavailable": "20",
162-
"iplimit": "20",
163-
"iptotal": 0,
164-
"isdefault": false,
165-
"memoryavailable": "40960",
166-
"memorylimit": "40960",
167-
"memorytotal": 0,
168-
"name": "user01",
169-
"networkavailable": "20",
170-
"networklimit": "20",
171-
"networktotal": 0,
172-
"primarystorageavailable": "200",
173-
"primarystoragelimit": "200",
174-
"primarystoragetotal": 0,
175-
"projectavailable": "Unlimited",
176-
"projectlimit": "Unlimited",
177-
"projecttotal": 0,
178-
"secondarystorageavailable": "400",
179-
"secondarystoragelimit": "400",
180-
"secondarystoragetotal": 0,
181-
"snapshotavailable": "20",
182-
"snapshotlimit": "20",
183-
"snapshottotal": 0,
184-
"state": "enabled",
185-
"templateavailable": "20",
186-
"templatelimit": "20",
187-
"templatetotal": 0,
188-
"user": [
189-
{
190-
"account": "user01",
191-
"accountid": "c9cb9df8-dcd5-44c5-a40d-2e7d266669a6",
192-
"accounttype": 2,
193-
"created": "2016-07-26T02:17:17+0000",
194-
"domain": "ROOT",
195-
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
196-
"email": "user01@example.com",
197-
"firstname": "user01",
198-
"id": "1d69ac45-a8f7-469c-ab24-d2b0382fa8be",
199-
"iscallerchilddomain": false,
200-
"isdefault": false,
201-
"lastname": "user01",
202-
"state": "enabled",
203-
"username": "user01"
204-
}
205-
],
206-
"vmavailable": "20",
207-
"vmlimit": "20",
208-
"vmtotal": 0,
209-
"volumeavailable": "20",
210-
"volumelimit": "20",
211-
"volumetotal": 0,
212-
"vpcavailable": "20",
213-
"vpclimit": "20",
214-
"vpctotal": 0
215-
}
216-
}
217-
}
218-
`
219-
fmt.Fprintln(w, resp)
69+
resp, err := ReadData(apiName, "AccountService")
70+
if err != nil {
71+
t.Errorf("Failed to read response data, due to: %v", err)
72+
return
73+
}
74+
fmt.Fprintln(w, resp[apiName])
22075
}))
22176
defer server.Close()
22277

cloudstack/AddressService_test.go

Lines changed: 10 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,60 +27,13 @@ import (
2727
)
2828

2929
func TestAssociateIpAddress(t *testing.T) {
30+
apiName := "associateIpAddress"
3031
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31-
responses := map[string]string{
32-
"associateIpAddress": `
33-
{
34-
"associateipaddressresponse": {
35-
"id": "dc51835f-b0e2-4a2e-91a4-6cccc44bdae3",
36-
"jobid": "8e805516-1729-46cf-a7d0-7289d523871e"
37-
}
38-
}`,
39-
"queryAsyncJobResult": `
40-
{
41-
"queryasyncjobresultresponse": {
42-
"accountid": "27ef5ba2-5fe0-11ea-9a56-1e006800018c",
43-
"userid": "27f2484f-5fe0-11ea-9a56-1e006800018c",
44-
"cmd": "org.apache.cloudstack.api.command.admin.address.AssociateIPAddrCmdByAdmin",
45-
"jobstatus": 1,
46-
"jobprocstatus": 0,
47-
"jobresultcode": 0,
48-
"jobresulttype": "object",
49-
"jobresult": {
50-
"ipaddress": {
51-
"id": "dc51835f-b0e2-4a2e-91a4-6cccc44bdae3",
52-
"ipaddress": "10.70.3.100",
53-
"allocated": "2021-10-01T16:25:11+0000",
54-
"zoneid": "3fc049b0-87ae-4d77-90c1-cce70da17db6",
55-
"zonename": "testAdvZone2",
56-
"issourcenat": true,
57-
"account": "admin",
58-
"domainid": "e4874e10-5fdf-11ea-9a56-1e006800018c",
59-
"domain": "ROOT",
60-
"forvirtualnetwork": true,
61-
"vlanid": "2e86e486-b472-4f12-a9b2-bb73701241e0",
62-
"vlanname": "vlan://untagged",
63-
"isstaticnat": false,
64-
"issystem": false,
65-
"associatednetworkid": "cf8056db-25e1-49d1-b023-f13a717e5ecc",
66-
"associatednetworkname": "test-adv-network",
67-
"networkid": "f17d38db-4810-437e-be28-d38cd30e3034",
68-
"state": "Allocating",
69-
"physicalnetworkid": "0d6c1e76-2d83-459a-bbce-c133fbd732b7",
70-
"tags":[],
71-
"isportable": false,
72-
"fordisplay": true
73-
}
74-
},
75-
"jobinstancetype": "IpAddress",
76-
"jobinstanceid": "dc51835f-b0e2-4a2e-91a4-6cccc44bdae3",
77-
"created": "2021-10-01T16:25:12+0000",
78-
"completed": "2021-10-01T16:25:12+0000",
79-
"jobid": "8e805516-1729-46cf-a7d0-7289d523871e"
80-
}
81-
}`,
32+
response, err := ParseAsyncResponse(apiName, "AddressService", *r)
33+
if err != nil {
34+
t.Errorf("Failed to parse response, due to: %v", err)
8235
}
83-
fmt.Fprintln(w, responses[r.FormValue("command")])
36+
fmt.Fprintln(w, response)
8437
}))
8538

8639
defer server.Close()
@@ -98,34 +51,13 @@ func TestAssociateIpAddress(t *testing.T) {
9851
}
9952

10053
func TestDisassociateIpAddress(t *testing.T) {
54+
apiName := "disassociateIpAddress"
10155
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
102-
responses := map[string]string{
103-
"disassociateIpAddress": `{
104-
"disassociateipaddressresponse": {
105-
"jobid": "44fc961e-9d57-4313-9f11-7556508b319c"
106-
}
107-
}`,
108-
"queryAsyncJobResult": `{
109-
"queryasyncjobresultresponse": {
110-
"accountid": "27ef5ba2-5fe0-11ea-9a56-1e006800018c",
111-
"userid": "27f2484f-5fe0-11ea-9a56-1e006800018c",
112-
"cmd": "org.apache.cloudstack.api.command.user.address.DisassociateIPAddrCmd",
113-
"jobstatus": 1,
114-
"jobprocstatus": 0,
115-
"jobresultcode": 0,
116-
"jobresulttype": "object",
117-
"jobresult": {
118-
"success": true
119-
},
120-
"jobinstancetype": "IpAddress",
121-
"jobinstanceid": "a767fbe1-ed7a-4d7c-8221-c7d736ca622d",
122-
"created": "2021-10-01T16:42:32+0000",
123-
"completed": "2021-10-01T16:42:33+0000",
124-
"jobid": "44fc961e-9d57-4313-9f11-7556508b319c"
125-
}
126-
}`,
56+
response, err := ParseAsyncResponse(apiName, "AddressService", *request)
57+
if err != nil {
58+
t.Errorf("Failed to parse response, due to: %v", err)
12759
}
128-
fmt.Fprintln(writer, responses[request.FormValue("command")])
60+
fmt.Fprintln(writer, response)
12961
}))
13062

13163
defer server.Close()

cloudstack/AffinityGroupService_test.go

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,13 @@ import (
2727
)
2828

2929
func TestCreateAffinityGroup(t *testing.T) {
30+
apiName := "createAffinityGroup"
3031
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
31-
responses := map[string]string{
32-
"createAffinityGroup": `{
33-
"createaffinitygroupresponse": {
34-
"id": "5059d7e3-9213-448d-9314-6ae4e1d95a33",
35-
"jobid": "58e969e8-768b-44d0-b278-fd1b2f236c00"
36-
}
37-
}`,
38-
"queryAsyncJobResult": `{
39-
"queryasyncjobresultresponse": {
40-
"accountid": "27ef5ba2-5fe0-11ea-9a56-1e006800018c",
41-
"userid": "27f2484f-5fe0-11ea-9a56-1e006800018c",
42-
"cmd": "org.apache.cloudstack.api.command.user.affinitygroup.CreateAffinityGroupCmd",
43-
"jobstatus": 1,
44-
"jobprocstatus": 0,
45-
"jobresultcode": 0,
46-
"jobresulttype": "object",
47-
"jobresult": {
48-
"affinitygroup": {
49-
"id": "5059d7e3-9213-448d-9314-6ae4e1d95a33",
50-
"name": "testAffinityGroup",
51-
"description": "testAffinityGroup",
52-
"account": "admin",
53-
"domainid": "e4874e10-5fdf-11ea-9a56-1e006800018c",
54-
"domain": "ROOT",
55-
"type": "host affinity"
56-
}
57-
},
58-
"jobinstancetype": "AffinityGroup",
59-
"created": "2021-10-01T16:59:04+0000",
60-
"completed": "2021-10-01T16:59:04+0000",
61-
"jobid": "58e969e8-768b-44d0-b278-fd1b2f236c00"
62-
}
63-
}`,
32+
responses, err := ParseAsyncResponse(apiName, "AffinityGroupService", *request)
33+
if err != nil {
34+
t.Errorf("Failed to parse response, due to: %v", err)
6435
}
65-
fmt.Fprintf(writer, responses[request.FormValue("command")])
36+
fmt.Fprintln(writer, responses)
6637
}))
6738
defer server.Close()
6839
client := NewAsyncClient(server.URL, "APIKEY", "SECRETKEY", false)

cloudstack/AsyncJobService_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,13 @@ import (
2727
)
2828

2929
func TestListAsyncJobs(t *testing.T) {
30+
apiName := "listAsyncJobs"
3031
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
31-
response := `{
32-
"listasyncjobsresponse": {
33-
"asyncjobs": [
34-
{
35-
"accountid": "bc1b465f-1d18-11ec-9173-50eb7122da94",
36-
"created": "2021-09-29T17:50:11+0530",
37-
"jobid": "6679e6b9-4bf2-4d83-b9f0-235fc1609227",
38-
"jobprocstatus": 0,
39-
"jobresultcode": 0,
40-
"userid": "bc1b60db-1d18-11ec-9173-50eb7122da94"
41-
}
42-
],
43-
"count": 1
44-
}
45-
}`
46-
fmt.Fprintf(writer, response)
32+
response, err := ReadData(apiName, "AsyncJobService")
33+
if err != nil {
34+
t.Errorf("Failed to parse response, due to: %v", err)
35+
}
36+
fmt.Fprintf(writer, response[apiName])
4737
}))
4838

4939
defer server.Close()

0 commit comments

Comments
 (0)