Ash Wilson | c25ae60 | 2014-09-02 14:41:12 -0400 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 3 | package openstack |
| 4 | |
| 5 | import ( |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 6 | "os" |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 7 | "testing" |
Joe Topjian | 20a9ce1 | 2017-05-04 01:24:48 +0000 | [diff] [blame] | 8 | "time" |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 9 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 11 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack" |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | func TestAuthenticatedClient(t *testing.T) { |
| 15 | // Obtain credentials from the environment. |
Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 16 | ao, err := openstack.AuthOptionsFromEnv() |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 17 | if err != nil { |
| 18 | t.Fatalf("Unable to acquire credentials: %v", err) |
| 19 | } |
| 20 | |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 21 | client, err := openstack.AuthenticatedClient(ao) |
| 22 | if err != nil { |
| 23 | t.Fatalf("Unable to authenticate: %v", err) |
| 24 | } |
| 25 | |
| 26 | if client.TokenID == "" { |
| 27 | t.Errorf("No token ID assigned to the client") |
| 28 | } |
| 29 | |
| 30 | t.Logf("Client successfully acquired a token: %v", client.TokenID) |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 31 | |
| 32 | // Find the storage service in the service catalog. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 33 | storage, err := openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{ |
Jon Perritt | 509fbb6 | 2014-09-10 13:29:56 -0500 | [diff] [blame] | 34 | Region: os.Getenv("OS_REGION_NAME"), |
| 35 | }) |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 36 | if err != nil { |
| 37 | t.Errorf("Unable to locate a storage service: %v", err) |
| 38 | } else { |
| 39 | t.Logf("Located a storage service at endpoint: [%s]", storage.Endpoint) |
| 40 | } |
Ash Wilson | 33c85f4 | 2014-09-02 14:09:14 -0400 | [diff] [blame] | 41 | } |
Joe Topjian | 20a9ce1 | 2017-05-04 01:24:48 +0000 | [diff] [blame] | 42 | |
| 43 | func TestReauth(t *testing.T) { |
| 44 | ao, err := openstack.AuthOptionsFromEnv() |
| 45 | if err != nil { |
| 46 | t.Fatalf("Unable to obtain environment auth options: %v", err) |
| 47 | } |
| 48 | |
| 49 | // Allow reauth |
| 50 | ao.AllowReauth = true |
| 51 | |
| 52 | provider, err := openstack.NewClient(ao.IdentityEndpoint) |
| 53 | if err != nil { |
| 54 | t.Fatalf("Unable to create provider: %v", err) |
| 55 | } |
| 56 | |
| 57 | err = openstack.Authenticate(provider, ao) |
| 58 | if err != nil { |
| 59 | t.Fatalf("Unable to authenticate: %v", err) |
| 60 | } |
| 61 | |
| 62 | t.Logf("Creating a compute client") |
| 63 | _, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ |
| 64 | Region: os.Getenv("OS_REGION_NAME"), |
| 65 | }) |
| 66 | if err != nil { |
| 67 | t.Fatalf("Unable to create compute client: %v", err) |
| 68 | } |
| 69 | |
| 70 | t.Logf("Sleeping for 1 second") |
| 71 | time.Sleep(1 * time.Second) |
| 72 | t.Logf("Attempting to reauthenticate") |
| 73 | |
| 74 | err = provider.ReauthFunc() |
| 75 | if err != nil { |
| 76 | t.Fatalf("Unable to reauthenticate: %v", err) |
| 77 | } |
| 78 | |
| 79 | t.Logf("Creating a compute client") |
| 80 | _, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ |
| 81 | Region: os.Getenv("OS_REGION_NAME"), |
| 82 | }) |
| 83 | if err != nil { |
| 84 | t.Fatalf("Unable to create compute client: %v", err) |
| 85 | } |
| 86 | } |