Identity Acceptance Test Cleanup (#49)
diff --git a/acceptance/openstack/identity/v3/endpoint_test.go b/acceptance/openstack/identity/v3/endpoint_test.go
index f575649..6a52f26 100644
--- a/acceptance/openstack/identity/v3/endpoint_test.go
+++ b/acceptance/openstack/identity/v3/endpoint_test.go
@@ -6,106 +6,80 @@
"testing"
"github.com/gophercloud/gophercloud"
- endpoints3 "github.com/gophercloud/gophercloud/openstack/identity/v3/endpoints"
- services3 "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
- "github.com/gophercloud/gophercloud/pagination"
+ "github.com/gophercloud/gophercloud/acceptance/clients"
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/endpoints"
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
)
-func TestListEndpoints(t *testing.T) {
- // Create a service client.
- serviceClient := createAuthenticatedClient(t)
- if serviceClient == nil {
- return
+func TestEndpointsList(t *testing.T) {
+ client, err := clients.NewIdentityV3Client()
+ if err != nil {
+ t.Fatalf("Unable to obtain an identity client: %v")
}
- // Use the service to list all available endpoints.
- pager := endpoints3.List(serviceClient, endpoints3.ListOpts{})
- err := pager.EachPage(func(page pagination.Page) (bool, error) {
- t.Logf("--- Page ---")
-
- endpoints, err := endpoints3.ExtractEndpoints(page)
- if err != nil {
- t.Fatalf("Error extracting endpoings: %v", err)
- }
-
- for _, endpoint := range endpoints {
- t.Logf("Endpoint: %8s %10s %9s %s",
- endpoint.ID,
- endpoint.Availability,
- endpoint.Name,
- endpoint.URL)
- }
-
- return true, nil
- })
+ allPages, err := endpoints.List(client, nil).AllPages()
if err != nil {
- t.Errorf("Unexpected error while iterating endpoint pages: %v", err)
+ t.Fatalf("Unable to list endpoints: %v", err)
+ }
+
+ allEndpoints, err := endpoints.ExtractEndpoints(allPages)
+ if err != nil {
+ t.Fatalf("Unable to extract endpoints: %v", err)
+ }
+
+ for _, endpoint := range allEndpoints {
+ PrintEndpoint(t, &endpoint)
}
}
-func TestNavigateCatalog(t *testing.T) {
- // Create a service client.
- client := createAuthenticatedClient(t)
- if client == nil {
- return
+func TestEndpointsNavigateCatalog(t *testing.T) {
+ client, err := clients.NewIdentityV3Client()
+ if err != nil {
+ t.Fatalf("Unable to obtain an identity client: %v")
}
- var compute *services3.Service
- var endpoint *endpoints3.Endpoint
-
// Discover the service we're interested in.
- servicePager := services3.List(client, services3.ListOpts{ServiceType: "compute"})
- err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
- part, err := services3.ExtractServices(page)
- if err != nil {
- return false, err
- }
- if compute != nil {
- t.Fatalf("Expected one service, got more than one page")
- return false, nil
- }
- if len(part) != 1 {
- t.Fatalf("Expected one service, got %d", len(part))
- return false, nil
- }
+ serviceListOpts := services.ListOpts{
+ ServiceType: "compute",
+ }
- compute = &part[0]
- return true, nil
- })
+ allPages, err := services.List(client, serviceListOpts).AllPages()
if err != nil {
- t.Fatalf("Unexpected error iterating pages: %v", err)
+ t.Fatalf("Unable to lookup compute service: %v", err)
}
- if compute == nil {
- t.Fatalf("No compute service found.")
+ allServices, err := services.ExtractServices(allPages)
+ if err != nil {
+ t.Fatalf("Unable to extract service: %v")
}
+ if len(allServices) != 1 {
+ t.Fatalf("Expected one service, got %d", len(allServices))
+ }
+
+ computeService := allServices[0]
+ PrintService(t, &computeService)
+
// Enumerate the endpoints available for this service.
- computePager := endpoints3.List(client, endpoints3.ListOpts{
+ endpointListOpts := endpoints.ListOpts{
Availability: gophercloud.AvailabilityPublic,
- ServiceID: compute.ID,
- })
- err = computePager.EachPage(func(page pagination.Page) (bool, error) {
- part, err := endpoints3.ExtractEndpoints(page)
- if err != nil {
- return false, err
- }
- if endpoint != nil {
- t.Fatalf("Expected one endpoint, got more than one page")
- return false, nil
- }
- if len(part) != 1 {
- t.Fatalf("Expected one endpoint, got %d", len(part))
- return false, nil
- }
-
- endpoint = &part[0]
- return true, nil
- })
-
- if endpoint == nil {
- t.Fatalf("No endpoint found.")
+ ServiceID: computeService.ID,
}
- t.Logf("Success. The compute endpoint is at %s.", endpoint.URL)
+ allPages, err = endpoints.List(client, endpointListOpts).AllPages()
+ if err != nil {
+ t.Fatalf("Unable to lookup compute endpoint: %v", err)
+ }
+
+ allEndpoints, err := endpoints.ExtractEndpoints(allPages)
+ if err != nil {
+ t.Fatalf("Unable to extract endpoint: %v")
+ }
+
+ if len(allEndpoints) != 1 {
+ t.Fatalf("Expected one endpoint, got %d", len(allEndpoints))
+ }
+
+ PrintEndpoint(t, &allEndpoints[0])
+
}
diff --git a/acceptance/openstack/identity/v3/identity.go b/acceptance/openstack/identity/v3/identity.go
new file mode 100644
index 0000000..268fd79
--- /dev/null
+++ b/acceptance/openstack/identity/v3/identity.go
@@ -0,0 +1,33 @@
+package v3
+
+import (
+ "testing"
+
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/endpoints"
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
+)
+
+// PrintEndpoint will print an endpoint and all of its attributes.
+func PrintEndpoint(t *testing.T, endpoint *endpoints.Endpoint) {
+ t.Logf("ID: %s", endpoint.ID)
+ t.Logf("Availability: %s", endpoint.Availability)
+ t.Logf("Name: %s", endpoint.Name)
+ t.Logf("Region: %s", endpoint.Region)
+ t.Logf("ServiceID: %s", endpoint.ServiceID)
+ t.Logf("URL: %s", endpoint.URL)
+}
+
+// PrintService will print a service and all of its attributes.
+func PrintService(t *testing.T, service *services.Service) {
+ t.Logf("ID: %s", service.ID)
+ t.Logf("Description: %s", service.Description)
+ t.Logf("Name: %s", service.Name)
+ t.Logf("Type: %s", service.Type)
+}
+
+// PrintToken will print a token and all of its attributes.
+func PrintToken(t *testing.T, token *tokens.Token) {
+ t.Logf("ID: %s", token.ID)
+ t.Logf("ExpiresAt: %v", token.ExpiresAt)
+}
diff --git a/acceptance/openstack/identity/v3/identity_test.go b/acceptance/openstack/identity/v3/identity_test.go
deleted file mode 100644
index 6974ad0..0000000
--- a/acceptance/openstack/identity/v3/identity_test.go
+++ /dev/null
@@ -1,39 +0,0 @@
-// +build acceptance
-
-package v3
-
-import (
- "testing"
-
- "github.com/gophercloud/gophercloud"
- "github.com/gophercloud/gophercloud/openstack"
- th "github.com/gophercloud/gophercloud/testhelper"
-)
-
-func createAuthenticatedClient(t *testing.T) *gophercloud.ServiceClient {
- // Obtain credentials from the environment.
- ao, err := openstack.AuthOptionsFromEnv()
- th.AssertNoErr(t, err)
-
- // Trim out unused fields.
- ao.Username, ao.TenantID, ao.TenantName = "", "", ""
-
- if ao.UserID == "" {
- t.Logf("Skipping identity v3 tests because no OS_USERID is present.")
- return nil
- }
-
- // Create a client and manually authenticate against v3.
- providerClient, err := openstack.NewClient(ao.IdentityEndpoint)
- if err != nil {
- t.Fatalf("Unable to instantiate client: %v", err)
- }
-
- err = openstack.AuthenticateV3(providerClient, ao)
- if err != nil {
- t.Fatalf("Unable to authenticate against identity v3: %v", err)
- }
-
- // Create a service client.
- return openstack.NewIdentityV3(providerClient)
-}
diff --git a/acceptance/openstack/identity/v3/service_test.go b/acceptance/openstack/identity/v3/service_test.go
index b39ba7f..364f69b 100644
--- a/acceptance/openstack/identity/v3/service_test.go
+++ b/acceptance/openstack/identity/v3/service_test.go
@@ -5,32 +5,28 @@
import (
"testing"
- services3 "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
- "github.com/gophercloud/gophercloud/pagination"
+ "github.com/gophercloud/gophercloud/acceptance/clients"
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
)
-func TestListServices(t *testing.T) {
- // Create a service client.
- serviceClient := createAuthenticatedClient(t)
- if serviceClient == nil {
- return
- }
-
- // Use the client to list all available services.
- pager := services3.List(serviceClient, services3.ListOpts{})
- err := pager.EachPage(func(page pagination.Page) (bool, error) {
- parts, err := services3.ExtractServices(page)
- if err != nil {
- return false, err
- }
-
- t.Logf("--- Page ---")
- for _, service := range parts {
- t.Logf("Service: %32s %15s %10s %s", service.ID, service.Type, service.Name, *service.Description)
- }
- return true, nil
- })
+func TestServicesList(t *testing.T) {
+ client, err := clients.NewIdentityV3Client()
if err != nil {
- t.Errorf("Unexpected error traversing pages: %v", err)
+ t.Fatalf("Unable to obtain an identity client: %v")
}
+
+ allPages, err := services.List(client, nil).AllPages()
+ if err != nil {
+ t.Fatalf("Unable to list services: %v", err)
+ }
+
+ allServices, err := services.ExtractServices(allPages)
+ if err != nil {
+ t.Fatalf("Unable to extract services: %v", err)
+ }
+
+ for _, service := range allServices {
+ PrintService(t, &service)
+ }
+
}
diff --git a/acceptance/openstack/identity/v3/token_test.go b/acceptance/openstack/identity/v3/token_test.go
index 5340ead..13b38f8 100644
--- a/acceptance/openstack/identity/v3/token_test.go
+++ b/acceptance/openstack/identity/v3/token_test.go
@@ -5,38 +5,32 @@
import (
"testing"
+ "github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/openstack"
- tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
)
func TestGetToken(t *testing.T) {
- // Obtain credentials from the environment.
+ client, err := clients.NewIdentityV3UnauthenticatedClient()
+ if err != nil {
+ t.Fatalf("Unable to obtain an identity client: %v")
+ }
+
ao, err := openstack.AuthOptionsFromEnv()
if err != nil {
- t.Fatalf("Unable to acquire credentials: %v", err)
+ t.Fatalf("Unable to obtain environment auth options: %v", err)
}
- // Trim out unused fields. Skip if we don't have a UserID.
- ao.Username, ao.TenantID, ao.TenantName = "", "", ""
- if ao.UserID == "" {
- t.Logf("Skipping identity v3 tests because no OS_USERID is present.")
- return
+ authOptions := tokens.AuthOptions{
+ Username: ao.Username,
+ Password: ao.Password,
+ DomainName: "default",
}
- // Create an unauthenticated client.
- provider, err := openstack.NewClient(ao.IdentityEndpoint)
- if err != nil {
- t.Fatalf("Unable to instantiate client: %v", err)
- }
-
- // Create a service client.
- service := openstack.NewIdentityV3(provider)
-
- // Use the service to create a token.
- token, err := tokens3.Create(service, ao, nil).Extract()
+ token, err := tokens.Create(client, &authOptions).Extract()
if err != nil {
t.Fatalf("Unable to get token: %v", err)
}
- t.Logf("Acquired token: %s", token.ID)
+ PrintToken(t, token)
}