blob: 2a6ba7da1e7c2502fd983be938d6fd88ecb64d21 [file] [log] [blame]
Ash Wilsonc25ae602014-09-02 14:41:12 -04001// +build acceptance
2
Ash Wilson33c85f42014-09-02 14:09:14 -04003package openstack
4
5import (
Ash Wilson1cd3e692014-09-09 11:01:47 -04006 "os"
Ash Wilson33c85f42014-09-02 14:09:14 -04007 "testing"
Joe Topjian20a9ce12017-05-04 01:24:48 +00008 "time"
Ash Wilson33c85f42014-09-02 14:09:14 -04009
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +020011 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack"
Ash Wilson33c85f42014-09-02 14:09:14 -040012)
13
14func TestAuthenticatedClient(t *testing.T) {
15 // Obtain credentials from the environment.
Jamie Hannaford390555a2014-10-22 17:04:03 +020016 ao, err := openstack.AuthOptionsFromEnv()
Ash Wilson33c85f42014-09-02 14:09:14 -040017 if err != nil {
18 t.Fatalf("Unable to acquire credentials: %v", err)
19 }
20
Ash Wilson33c85f42014-09-02 14:09:14 -040021 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 Wilson1cd3e692014-09-09 11:01:47 -040031
32 // Find the storage service in the service catalog.
Jon Perritt2a7797d2014-10-21 15:08:43 -050033 storage, err := openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{
Jon Perritt509fbb62014-09-10 13:29:56 -050034 Region: os.Getenv("OS_REGION_NAME"),
35 })
Ash Wilson1cd3e692014-09-09 11:01:47 -040036 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 Wilson33c85f42014-09-02 14:09:14 -040041}
Joe Topjian20a9ce12017-05-04 01:24:48 +000042
43func 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}