client_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // +build acceptance
  2. package openstack
  3. import (
  4. "os"
  5. "testing"
  6. "time"
  7. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack"
  9. )
  10. func TestAuthenticatedClient(t *testing.T) {
  11. // Obtain credentials from the environment.
  12. ao, err := openstack.AuthOptionsFromEnv()
  13. if err != nil {
  14. t.Fatalf("Unable to acquire credentials: %v", err)
  15. }
  16. client, err := openstack.AuthenticatedClient(ao)
  17. if err != nil {
  18. t.Fatalf("Unable to authenticate: %v", err)
  19. }
  20. if client.TokenID == "" {
  21. t.Errorf("No token ID assigned to the client")
  22. }
  23. t.Logf("Client successfully acquired a token: %v", client.TokenID)
  24. // Find the storage service in the service catalog.
  25. storage, err := openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{
  26. Region: os.Getenv("OS_REGION_NAME"),
  27. })
  28. if err != nil {
  29. t.Errorf("Unable to locate a storage service: %v", err)
  30. } else {
  31. t.Logf("Located a storage service at endpoint: [%s]", storage.Endpoint)
  32. }
  33. }
  34. func TestReauth(t *testing.T) {
  35. ao, err := openstack.AuthOptionsFromEnv()
  36. if err != nil {
  37. t.Fatalf("Unable to obtain environment auth options: %v", err)
  38. }
  39. // Allow reauth
  40. ao.AllowReauth = true
  41. provider, err := openstack.NewClient(ao.IdentityEndpoint)
  42. if err != nil {
  43. t.Fatalf("Unable to create provider: %v", err)
  44. }
  45. err = openstack.Authenticate(provider, ao)
  46. if err != nil {
  47. t.Fatalf("Unable to authenticate: %v", err)
  48. }
  49. t.Logf("Creating a compute client")
  50. _, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
  51. Region: os.Getenv("OS_REGION_NAME"),
  52. })
  53. if err != nil {
  54. t.Fatalf("Unable to create compute client: %v", err)
  55. }
  56. t.Logf("Sleeping for 1 second")
  57. time.Sleep(1 * time.Second)
  58. t.Logf("Attempting to reauthenticate")
  59. err = provider.ReauthFunc()
  60. if err != nil {
  61. t.Fatalf("Unable to reauthenticate: %v", err)
  62. }
  63. t.Logf("Creating a compute client")
  64. _, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
  65. Region: os.Getenv("OS_REGION_NAME"),
  66. })
  67. if err != nil {
  68. t.Fatalf("Unable to create compute client: %v", err)
  69. }
  70. }