Skip to content

Commit

Permalink
Refactor tests - decouple test data from test files (#14)
Browse files Browse the repository at this point in the history
* refactor tests

* Decouple Test data from tests
  • Loading branch information
Pearl1594 authored Oct 14, 2021
1 parent 83acc75 commit 78fa5e4
Show file tree
Hide file tree
Showing 61 changed files with 4,130 additions and 3,446 deletions.
173 changes: 14 additions & 159 deletions cloudstack/AccountService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,97 +27,20 @@ import (
)

func TestListAccounts(t *testing.T) {
apiName := "listAccountsResponse"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := `
{
"listaccountsresponse": {
"account": [
{
"accountdetails": {
"key0": "value0",
"key1": "value1"
},
"accounttype": 1,
"cpuavailable": "Unlimited",
"cpulimit": "Unlimited",
"cputotal": 1,
"domain": "ROOT",
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
"groups": [],
"id": "62f61a73-5f5a-11e5-bc86-0242ac11180a",
"ipavailable": "Unlimited",
"iplimit": "Unlimited",
"iptotal": 1,
"isdefault": true,
"memoryavailable": "Unlimited",
"memorylimit": "Unlimited",
"memorytotal": 512,
"name": "admin",
"networkavailable": "Unlimited",
"networklimit": "Unlimited",
"networktotal": 1,
"primarystorageavailable": "Unlimited",
"primarystoragelimit": "Unlimited",
"primarystoragetotal": 2,
"projectavailable": "Unlimited",
"projectlimit": "Unlimited",
"projecttotal": 1,
"receivedbytes": 100,
"secondarystorageavailable": "Unlimited",
"secondarystoragelimit": "Unlimited",
"secondarystoragetotal": 0,
"sentbytes": 100,
"snapshotavailable": "Unlimited",
"snapshotlimit": "Unlimited",
"snapshottotal": 0,
"state": "enabled",
"templateavailable": "Unlimited",
"templatelimit": "Unlimited",
"templatetotal": 0,
"user": [
{
"account": "admin",
"accountid": "62f61a73-5f5a-11e5-bc86-0242ac11180a",
"accounttype": 1,
"apikey": "BFCyYeY0HhhrvNWC7FBwHTlso52ow3XWZUzNuiv03jmJOQn-5UH5BTVRVheiBnaRfYFE9yOzjwiqkbahq9P-Rw",
"created": "2015-09-20T05:42:31+0000",
"domain": "ROOT",
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
"email": "admin@mailprovider.com",
"firstname": "Admin",
"id": "62f6394b-5f5a-11e5-bc86-0242ac11180a",
"iscallerchilddomain": false,
"isdefault": true,
"lastname": "User",
"secretkey": "Em1UPdKTYEDaUxPXKtpqy_1uYluHWU2ZHotPi-VnBi_-MOEUAANoFHg3ycDProluDzYAXmT3YRssk367ylr_4A",
"state": "enabled",
"username": "admin"
}
],
"vmavailable": "Unlimited",
"vmlimit": "Unlimited",
"vmrunning": 1,
"vmtotal": 1,
"volumeavailable": "Unlimited",
"volumelimit": "Unlimited",
"volumetotal": 1,
"vpcavailable": "Unlimited",
"vpclimit": "Unlimited",
"vpctotal": 0
}
],
"count": 1
}
}
`
fmt.Fprintln(w, resp)
resp, err := ReadData(apiName, "AccountService")
if err != nil {
t.Errorf("Failed to read response data, due to: %v", err)
return
}
fmt.Fprintln(w, resp[apiName])
}))
defer server.Close()

client := newClient(server.URL, "APIKEY", "SECRETKEY", true, true)
p := client.Account.NewListAccountsParams()
acc, _ := client.Account.ListAccounts(p)

accounts := acc.Accounts
if len(accounts) != 1 {
t.Errorf("length: actual %d, expected 1", len(accounts))
Expand All @@ -141,82 +64,14 @@ func TestListAccounts(t *testing.T) {
}

func TestCreateAccounts(t *testing.T) {
apiName := "createAccountsResponse"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := `
{
"createaccountresponse": {
"account": {
"accountdetails": {
"key0": "value0",
"key1": "value1"
},
"accounttype": 2,
"cpuavailable": "40",
"cpulimit": "40",
"cputotal": 0,
"domain": "ROOT",
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
"groups": [],
"id": "c9cb9df8-dcd5-44c5-a40d-2e7d266669a6",
"ipavailable": "20",
"iplimit": "20",
"iptotal": 0,
"isdefault": false,
"memoryavailable": "40960",
"memorylimit": "40960",
"memorytotal": 0,
"name": "user01",
"networkavailable": "20",
"networklimit": "20",
"networktotal": 0,
"primarystorageavailable": "200",
"primarystoragelimit": "200",
"primarystoragetotal": 0,
"projectavailable": "Unlimited",
"projectlimit": "Unlimited",
"projecttotal": 0,
"secondarystorageavailable": "400",
"secondarystoragelimit": "400",
"secondarystoragetotal": 0,
"snapshotavailable": "20",
"snapshotlimit": "20",
"snapshottotal": 0,
"state": "enabled",
"templateavailable": "20",
"templatelimit": "20",
"templatetotal": 0,
"user": [
{
"account": "user01",
"accountid": "c9cb9df8-dcd5-44c5-a40d-2e7d266669a6",
"accounttype": 2,
"created": "2016-07-26T02:17:17+0000",
"domain": "ROOT",
"domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a",
"email": "user01@example.com",
"firstname": "user01",
"id": "1d69ac45-a8f7-469c-ab24-d2b0382fa8be",
"iscallerchilddomain": false,
"isdefault": false,
"lastname": "user01",
"state": "enabled",
"username": "user01"
}
],
"vmavailable": "20",
"vmlimit": "20",
"vmtotal": 0,
"volumeavailable": "20",
"volumelimit": "20",
"volumetotal": 0,
"vpcavailable": "20",
"vpclimit": "20",
"vpctotal": 0
}
}
}
`
fmt.Fprintln(w, resp)
resp, err := ReadData(apiName, "AccountService")
if err != nil {
t.Errorf("Failed to read response data, due to: %v", err)
return
}
fmt.Fprintln(w, resp[apiName])
}))
defer server.Close()

Expand Down
88 changes: 10 additions & 78 deletions cloudstack/AddressService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,60 +27,13 @@ import (
)

func TestAssociateIpAddress(t *testing.T) {
apiName := "associateIpAddress"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
responses := map[string]string{
"associateIpAddress": `
{
"associateipaddressresponse": {
"id": "dc51835f-b0e2-4a2e-91a4-6cccc44bdae3",
"jobid": "8e805516-1729-46cf-a7d0-7289d523871e"
}
}`,
"queryAsyncJobResult": `
{
"queryasyncjobresultresponse": {
"accountid": "27ef5ba2-5fe0-11ea-9a56-1e006800018c",
"userid": "27f2484f-5fe0-11ea-9a56-1e006800018c",
"cmd": "org.apache.cloudstack.api.command.admin.address.AssociateIPAddrCmdByAdmin",
"jobstatus": 1,
"jobprocstatus": 0,
"jobresultcode": 0,
"jobresulttype": "object",
"jobresult": {
"ipaddress": {
"id": "dc51835f-b0e2-4a2e-91a4-6cccc44bdae3",
"ipaddress": "10.70.3.100",
"allocated": "2021-10-01T16:25:11+0000",
"zoneid": "3fc049b0-87ae-4d77-90c1-cce70da17db6",
"zonename": "testAdvZone2",
"issourcenat": true,
"account": "admin",
"domainid": "e4874e10-5fdf-11ea-9a56-1e006800018c",
"domain": "ROOT",
"forvirtualnetwork": true,
"vlanid": "2e86e486-b472-4f12-a9b2-bb73701241e0",
"vlanname": "vlan://untagged",
"isstaticnat": false,
"issystem": false,
"associatednetworkid": "cf8056db-25e1-49d1-b023-f13a717e5ecc",
"associatednetworkname": "test-adv-network",
"networkid": "f17d38db-4810-437e-be28-d38cd30e3034",
"state": "Allocating",
"physicalnetworkid": "0d6c1e76-2d83-459a-bbce-c133fbd732b7",
"tags":[],
"isportable": false,
"fordisplay": true
}
},
"jobinstancetype": "IpAddress",
"jobinstanceid": "dc51835f-b0e2-4a2e-91a4-6cccc44bdae3",
"created": "2021-10-01T16:25:12+0000",
"completed": "2021-10-01T16:25:12+0000",
"jobid": "8e805516-1729-46cf-a7d0-7289d523871e"
}
}`,
response, err := ParseAsyncResponse(apiName, "AddressService", *r)
if err != nil {
t.Errorf("Failed to parse response, due to: %v", err)
}
fmt.Fprintln(w, responses[r.FormValue("command")])
fmt.Fprintln(w, response)
}))

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

func TestDisassociateIpAddress(t *testing.T) {
apiName := "disassociateIpAddress"
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
responses := map[string]string{
"disassociateIpAddress": `{
"disassociateipaddressresponse": {
"jobid": "44fc961e-9d57-4313-9f11-7556508b319c"
}
}`,
"queryAsyncJobResult": `{
"queryasyncjobresultresponse": {
"accountid": "27ef5ba2-5fe0-11ea-9a56-1e006800018c",
"userid": "27f2484f-5fe0-11ea-9a56-1e006800018c",
"cmd": "org.apache.cloudstack.api.command.user.address.DisassociateIPAddrCmd",
"jobstatus": 1,
"jobprocstatus": 0,
"jobresultcode": 0,
"jobresulttype": "object",
"jobresult": {
"success": true
},
"jobinstancetype": "IpAddress",
"jobinstanceid": "a767fbe1-ed7a-4d7c-8221-c7d736ca622d",
"created": "2021-10-01T16:42:32+0000",
"completed": "2021-10-01T16:42:33+0000",
"jobid": "44fc961e-9d57-4313-9f11-7556508b319c"
}
}`,
response, err := ParseAsyncResponse(apiName, "AddressService", *request)
if err != nil {
t.Errorf("Failed to parse response, due to: %v", err)
}
fmt.Fprintln(writer, responses[request.FormValue("command")])
fmt.Fprintln(writer, response)
}))

defer server.Close()
Expand Down
39 changes: 5 additions & 34 deletions cloudstack/AffinityGroupService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,13 @@ import (
)

func TestCreateAffinityGroup(t *testing.T) {
apiName := "createAffinityGroup"
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
responses := map[string]string{
"createAffinityGroup": `{
"createaffinitygroupresponse": {
"id": "5059d7e3-9213-448d-9314-6ae4e1d95a33",
"jobid": "58e969e8-768b-44d0-b278-fd1b2f236c00"
}
}`,
"queryAsyncJobResult": `{
"queryasyncjobresultresponse": {
"accountid": "27ef5ba2-5fe0-11ea-9a56-1e006800018c",
"userid": "27f2484f-5fe0-11ea-9a56-1e006800018c",
"cmd": "org.apache.cloudstack.api.command.user.affinitygroup.CreateAffinityGroupCmd",
"jobstatus": 1,
"jobprocstatus": 0,
"jobresultcode": 0,
"jobresulttype": "object",
"jobresult": {
"affinitygroup": {
"id": "5059d7e3-9213-448d-9314-6ae4e1d95a33",
"name": "testAffinityGroup",
"description": "testAffinityGroup",
"account": "admin",
"domainid": "e4874e10-5fdf-11ea-9a56-1e006800018c",
"domain": "ROOT",
"type": "host affinity"
}
},
"jobinstancetype": "AffinityGroup",
"created": "2021-10-01T16:59:04+0000",
"completed": "2021-10-01T16:59:04+0000",
"jobid": "58e969e8-768b-44d0-b278-fd1b2f236c00"
}
}`,
responses, err := ParseAsyncResponse(apiName, "AffinityGroupService", *request)
if err != nil {
t.Errorf("Failed to parse response, due to: %v", err)
}
fmt.Fprintf(writer, responses[request.FormValue("command")])
fmt.Fprintln(writer, responses)
}))
defer server.Close()
client := NewAsyncClient(server.URL, "APIKEY", "SECRETKEY", false)
Expand Down
22 changes: 6 additions & 16 deletions cloudstack/AsyncJobService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,13 @@ import (
)

func TestListAsyncJobs(t *testing.T) {
apiName := "listAsyncJobs"
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
response := `{
"listasyncjobsresponse": {
"asyncjobs": [
{
"accountid": "bc1b465f-1d18-11ec-9173-50eb7122da94",
"created": "2021-09-29T17:50:11+0530",
"jobid": "6679e6b9-4bf2-4d83-b9f0-235fc1609227",
"jobprocstatus": 0,
"jobresultcode": 0,
"userid": "bc1b60db-1d18-11ec-9173-50eb7122da94"
}
],
"count": 1
}
}`
fmt.Fprintf(writer, response)
response, err := ReadData(apiName, "AsyncJobService")
if err != nil {
t.Errorf("Failed to parse response, due to: %v", err)
}
fmt.Fprintf(writer, response[apiName])
}))

defer server.Close()
Expand Down
Loading

0 comments on commit 78fa5e4

Please sign in to comment.