Refactoring tests to use helper funcs :ok_hand:
diff --git a/acceptance/openstack/networking/v2/network_test.go b/acceptance/openstack/networking/v2/network_test.go
index 83991e7..4013b71 100644
--- a/acceptance/openstack/networking/v2/network_test.go
+++ b/acceptance/openstack/networking/v2/network_test.go
@@ -4,13 +4,13 @@
import (
"os"
- "reflect"
"testing"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/openstack/utils"
+ th "github.com/rackspace/gophercloud/testhelper"
)
var (
@@ -36,10 +36,7 @@
func Setup(t *testing.T) {
client, err := NewClient()
- if err != nil {
- t.Fatalf("Error creating client: %s", err)
- }
-
+ th.AssertNoErr(err)
Client = client
}
@@ -47,26 +44,12 @@
Client = nil
}
-func Equals(t *testing.T, actual interface{}, expected interface{}) {
- if expected != actual {
- t.Fatalf("Expected %#v but got %#v", expected, actual)
- }
-}
-
-func DeepEquals(t *testing.T, actual, expected interface{}) {
- if !reflect.DeepEqual(actual, expected) {
- t.Fatalf("Expected %#v but got %#v", expected, actual)
- }
-}
-
func TestListAPIVersions(t *testing.T) {
Setup(t)
defer Teardown()
res, err := networks.APIVersions(Client)
- if err != nil {
- t.Fatalf("Failed to list API versions: %v", err)
- }
+ th.AssertNoErr(err)
err = gophercloud.EachPage(res, func(page gophercloud.Collection) bool {
t.Logf("--- Page ---")
@@ -75,9 +58,7 @@
}
return true
})
- if err != nil {
- t.Fatalf("Unexpected error while iterating API versions: %v", err)
- }
+ th.AssertNoErr(err)
}
func TestGetApiInfo(t *testing.T) {
@@ -85,9 +66,7 @@
defer Teardown()
res, err := networks.APIInfo(Client, "v2.0")
- if err != nil {
- t.Fatalf("Failed to list API info for v2: %v", err)
- }
+ th.AssertNoErr(err)
err = gophercloud.EachPage(res, func(page gophercloud.Collection) bool {
t.Logf("--- Page ---")
@@ -96,9 +75,7 @@
}
return true
})
- if err != nil {
- t.Fatalf("Unexpected error while iteratoring API resources: %v", err)
- }
+ th.AssertNoErr(err)
}
func TestListExts(t *testing.T) {
@@ -110,15 +87,13 @@
defer Teardown()
ext, err := networks.GetExtension(Client, "service-type")
- if err != nil {
- t.Fatalf("Unexpected error when getting extension: %#v", err)
- }
+ th.AssertNoErr(err)
- Equals(t, ext.Updated, "2013-01-20T00:00:00-00:00")
- Equals(t, ext.Name, "Neutron Service Type Management")
- Equals(t, ext.Namespace, "http://docs.openstack.org/ext/neutron/service-type/api/v1.0")
- Equals(t, ext.Alias, "service-type")
- Equals(t, ext.Description, "API for retrieving service providers for Neutron advanced services")
+ th.AssertEquals(t, ext.Updated, "2013-01-20T00:00:00-00:00")
+ th.AssertEquals(t, ext.Name, "Neutron Service Type Management")
+ th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/neutron/service-type/api/v1.0")
+ th.AssertEquals(t, ext.Alias, "service-type")
+ th.AssertEquals(t, ext.Description, "API for retrieving service providers for Neutron advanced services")
}
func TestListNetworks(t *testing.T) {
@@ -131,52 +106,36 @@
// Create a network
res, err := networks.Create(Client, networks.NetworkOpts{Name: "sample_network", AdminStateUp: true})
- if err != nil {
- t.Fatalf("Unexpected error when creating network: %#v", err)
- }
-
- Equals(t, res.Name, "sample_network")
- Equals(t, res.AdminStateUp, true)
-
+ th.AssertNoErr(err)
+ th.AssertEquals(t, res.Name, "sample_network")
+ th.AssertEquals(t, res.AdminStateUp, true)
networkID := res.ID
// Get a network
if networkID == "" {
t.Fatalf("In order to retrieve a network, the NetworkID must be set")
}
-
- Setup(t)
- defer Teardown()
-
n, err := networks.Get(Client, networkID)
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
-
- Equals(t, n.Status, "ACTIVE")
- DeepEquals(t, n.Subnets, []interface{}{})
- Equals(t, n.Name, "sample_network")
- Equals(t, n.ProviderPhysicalNetwork, "")
- Equals(t, n.ProviderNetworkType, "local")
- Equals(t, n.ProviderSegmentationID, 0f)
- Equals(t, n.AdminStateUp, true)
- Equals(t, n.RouterExternal, false)
- Equals(t, n.Shared, false)
- Equals(t, n.ID, networkID)
+ th.AssertNoErr(err)
+ th.AssertEquals(t, n.Status, "ACTIVE")
+ th.AssertDeepEquals(t, n.Subnets, []interface{}{})
+ th.AssertEquals(t, n.Name, "sample_network")
+ th.AssertEquals(t, n.ProviderPhysicalNetwork, "")
+ th.AssertEquals(t, n.ProviderNetworkType, "local")
+ th.AssertEquals(t, n.ProviderSegmentationID, 0)
+ th.AssertEquals(t, n.AdminStateUp, true)
+ th.AssertEquals(t, n.RouterExternal, false)
+ th.AssertEquals(t, n.Shared, false)
+ th.AssertEquals(t, n.ID, networkID)
// Update network
n, err = networks.Update(Client, networkID, networks.NetworkOpts{Name: "new_network_name"})
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
-
- Equals(t, n.Name, "new_network_name")
+ th.AssertNoErr(err)
+ th.AssertEquals(t, n.Name, "new_network_name")
// Delete network
err := networks.Delete(Client, networkID)
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
+ th.AssertNoErr(err)
}
func TestCreateMultipleNetworks(t *testing.T) {
diff --git a/openstack/networking/v2/networks/requests_test.go b/openstack/networking/v2/networks/requests_test.go
index ccb6eb9..c1d9c9f 100644
--- a/openstack/networking/v2/networks/requests_test.go
+++ b/openstack/networking/v2/networks/requests_test.go
@@ -3,7 +3,6 @@
import (
"fmt"
"net/http"
- "reflect"
"testing"
"github.com/rackspace/gophercloud"
@@ -21,24 +20,6 @@
}
}
-func Equals(t *testing.T, actual interface{}, expected interface{}) {
- if expected != actual {
- t.Fatalf("Expected %#v but got %#v", expected, actual)
- }
-}
-
-func DeepEquals(t *testing.T, actual, expected interface{}) {
- if !reflect.DeepEqual(actual, expected) {
- t.Fatalf("Expected %#v but got %#v", expected, actual)
- }
-}
-
-func CheckErr(t *testing.T, e error) {
- if e != nil {
- t.Fatalf("An error occurred: %#v", e)
- }
-}
-
func TestListAPIVersions(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -70,11 +51,10 @@
c := ServiceClient()
res, err := APIVersions(c)
- if err != nil {
- t.Fatalf("Error listing API versions: %v", err)
- }
+ th.AssertNoErr(err)
coll, err := gophercloud.AllPages(res)
+ th.AssertNoErr(err)
actual := ToAPIVersions(coll)
@@ -84,10 +64,7 @@
ID: "v2.0",
},
}
-
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("Expected %#v, got %#v", expected, actual)
- }
+ th.AssertDeepEquals(expected, actual)
}
func TestAPIInfo(t *testing.T) {
@@ -139,17 +116,13 @@
`)
})
- c := ServiceClient()
-
- res, err := APIInfo(c, "v2.0")
- if err != nil {
- t.Fatalf("Error getting API info: %v", err)
- }
+ res, err := APIInfo(ServiceClient(), "v2.0")
+ th.AssertNoErr(err)
coll, err := gophercloud.AllPages(res)
+ th.AssertNoErr(err)
actual := ToAPIResource(coll)
-
expected := []APIResource{
APIResource{
Name: "subnet",
@@ -164,10 +137,7 @@
Collection: "ports",
},
}
-
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("Expected %#v, got %#v", expected, actual)
- }
+ th.AssertDeepEquals(expected, actual)
}
func TestListingExtensions(t *testing.T) {
@@ -198,16 +168,14 @@
}
`)
- c := ServiceClient()
+ ext, err := GetExtension(ServiceClient(), "agent")
+ th.AssertNoErr(t, err)
- ext, err := GetExtension(c, "agent")
- CheckErr(t, err)
-
- Equals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
- Equals(t, ext.Name, "agent")
- Equals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
- Equals(t, ext.Alias, "agent")
- Equals(t, ext.Description, "The agent management extension.")
+ th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
+ th.AssertEquals(t, ext.Name, "agent")
+ th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
+ th.AssertEquals(t, ext.Alias, "agent")
+ th.AssertEquals(t, ext.Description, "The agent management extension.")
})
}
@@ -243,24 +211,20 @@
`)
})
- c := ServiceClient()
+ n, err := Get(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+ th.AssertNoErr(err)
- n, err := Get(c, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
-
- Equals(t, n.Status, "ACTIVE")
- DeepEquals(t, n.Subnets, []interface{}{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"})
- Equals(t, n.Name, "private-network")
- Equals(t, n.ProviderPhysicalNetwork, "")
- Equals(t, n.ProviderNetworkType, "local")
- Equals(t, n.ProviderSegmentationID, 0)
- Equals(t, n.AdminStateUp, true)
- Equals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
- Equals(t, n.RouterExternal, true)
- Equals(t, n.Shared, true)
- Equals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+ th.AssertEquals(t, n.Status, "ACTIVE")
+ th.AssertDeepEquals(t, n.Subnets, []interface{}{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"})
+ th.AssertEquals(t, n.Name, "private-network")
+ th.AssertEquals(t, n.ProviderPhysicalNetwork, "")
+ th.AssertEquals(t, n.ProviderNetworkType, "local")
+ th.AssertEquals(t, n.ProviderSegmentationID, 0)
+ th.AssertEquals(t, n.AdminStateUp, true)
+ th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
+ th.AssertEquals(t, n.RouterExternal, true)
+ th.AssertEquals(t, n.Shared, true)
+ th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
}
func TestCreateNetwork(t *testing.T) {
@@ -313,24 +277,21 @@
})
options := NetworkOpts{Name: "sample_network", AdminStateUp: true}
-
n, err := Create(ServiceClient(), options)
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
+ th.AssertNoErr(err)
- Equals(t, n.Status, "ACTIVE")
- DeepEquals(t, n.Subnets, []interface{}{})
- Equals(t, n.Name, "net1")
- Equals(t, n.AdminStateUp, true)
- Equals(t, n.TenantID, "9bacb3c5d39d41a79512987f338cf177")
- DeepEquals(t, n.Segments, []NetworkProvider{
+ th.AssertEquals(t, n.Status, "ACTIVE")
+ th.AssertDeepEquals(t, n.Subnets, []interface{}{})
+ th.AssertEquals(t, n.Name, "net1")
+ th.AssertEquals(t, n.AdminStateUp, true)
+ th.AssertEquals(t, n.TenantID, "9bacb3c5d39d41a79512987f338cf177")
+ th.AssertDeepEquals(t, n.Segments, []NetworkProvider{
{ProviderSegmentationID: 2, ProviderPhysicalNetwork: "8bab8453-1bc9-45af-8c70-f83aa9b50453", ProviderNetworkType: "vlan"},
{ProviderSegmentationID: 0, ProviderPhysicalNetwork: "8bab8453-1bc9-45af-8c70-f83aa9b50453", ProviderNetworkType: "stt"},
})
- Equals(t, n.Shared, false)
- Equals(t, n.PortSecurityEnabled, true)
- Equals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
+ th.AssertEquals(t, n.Shared, false)
+ th.AssertEquals(t, n.PortSecurityEnabled, true)
+ th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
}
func TestCreateNetworkWithOptionalFields(t *testing.T) {
@@ -358,11 +319,8 @@
shared := true
options := NetworkOpts{Name: "sample_network", AdminStateUp: true, Shared: &shared, TenantID: "12345"}
-
_, err := Create(ServiceClient(), options)
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
+ th.AssertNoErr(err)
}
func TestUpdateNetwork(t *testing.T) {
@@ -408,16 +366,13 @@
shared := true
options := NetworkOpts{Name: "new_network_name", AdminStateUp: false, Shared: &shared}
-
n, err := Update(ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options)
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
+ th.AssertNoErr(err)
- Equals(t, n.Name, "new_network_name")
- Equals(t, n.AdminStateUp, false)
- Equals(t, n.Shared, true)
- Equals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
+ th.AssertEquals(t, n.Name, "new_network_name")
+ th.AssertEquals(t, n.AdminStateUp, false)
+ th.AssertEquals(t, n.Shared, true)
+ th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
}
func TestDeleteNetwork(t *testing.T) {
@@ -431,7 +386,5 @@
})
err := Delete(ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
- if err != nil {
- t.Fatalf("Unexpected error: %#v", err)
- }
+ th.AssertNoErr(err)
}
diff --git a/openstack/networking/v2/networks/urls_test.go b/openstack/networking/v2/networks/urls_test.go
index b38a417..8533867 100644
--- a/openstack/networking/v2/networks/urls_test.go
+++ b/openstack/networking/v2/networks/urls_test.go
@@ -4,6 +4,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ th "github.com/rackspace/gophercloud/testhelper"
)
const Endpoint = "http://localhost:57909/"
@@ -15,39 +16,29 @@
func TestAPIVersionsURL(t *testing.T) {
actual := APIVersionsURL(EndpointClient())
expected := Endpoint
- if expected != actual {
- t.Errorf("[%s] does not match expected [%s]", actual, expected)
- }
+ th.AssertEquals(expected, actual)
}
func TestAPIInfoURL(t *testing.T) {
actual := APIInfoURL(EndpointClient(), "v2.0")
expected := Endpoint + "v2.0/"
- if expected != actual {
- t.Fatalf("[%s] does not match expected [%s]", actual, expected)
- }
+ th.AssertEquals(expected, actual)
}
func TestExtensionURL(t *testing.T) {
actual := ExtensionURL(EndpointClient(), "agent")
expected := Endpoint + "v2.0/extensions/agent"
- if expected != actual {
- t.Fatalf("[%s] does not match expected [%s]", actual, expected)
- }
+ th.AssertEquals(expected, actual)
}
func TestNetworkURL(t *testing.T) {
actual := NetworkURL(EndpointClient(), "foo")
expected := Endpoint + "v2.0/networks/foo"
- if expected != actual {
- t.Fatalf("[%s] does not match expected [%s]", actual, expected)
- }
+ th.AssertEquals(expected, actual)
}
func TestCreateURL(t *testing.T) {
actual := CreateURL(EndpointClient())
expected := Endpoint + "v2.0/networks"
- if expected != actual {
- t.Fatalf("[%s] does not match expected [%s]", actual, expected)
- }
+ th.AssertEquals(expected, actual)
}