PageRenderTime 3346ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/openstack/identity/v2/user_test.go

https://gitlab.com/JamesClonk/machine
Go | 127 lines | 93 code | 33 blank | 1 comment | 5 complexity | 55a2e87f8198f9ba5b762750fb1aa84a MD5 | raw file
  1. // +build acceptance identity
  2. package v2
  3. import (
  4. "strconv"
  5. "testing"
  6. "github.com/rackspace/gophercloud"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
  9. "github.com/rackspace/gophercloud/openstack/identity/v2/users"
  10. "github.com/rackspace/gophercloud/pagination"
  11. th "github.com/rackspace/gophercloud/testhelper"
  12. )
  13. func TestUsers(t *testing.T) {
  14. client := authenticatedClient(t)
  15. tenantID := findTenant(t, client)
  16. userID := createUser(t, client, tenantID)
  17. listUsers(t, client)
  18. getUser(t, client, userID)
  19. updateUser(t, client, userID)
  20. listUserRoles(t, client, tenantID, userID)
  21. deleteUser(t, client, userID)
  22. }
  23. func findTenant(t *testing.T, client *gophercloud.ServiceClient) string {
  24. var tenantID string
  25. err := tenants.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
  26. tenantList, err := tenants.ExtractTenants(page)
  27. th.AssertNoErr(t, err)
  28. for _, t := range tenantList {
  29. tenantID = t.ID
  30. break
  31. }
  32. return true, nil
  33. })
  34. th.AssertNoErr(t, err)
  35. return tenantID
  36. }
  37. func createUser(t *testing.T, client *gophercloud.ServiceClient, tenantID string) string {
  38. t.Log("Creating user")
  39. opts := users.CreateOpts{
  40. Name: tools.RandomString("user_", 5),
  41. Enabled: users.Disabled,
  42. TenantID: tenantID,
  43. Email: "new_user@foo.com",
  44. }
  45. user, err := users.Create(client, opts).Extract()
  46. th.AssertNoErr(t, err)
  47. t.Logf("Created user %s on tenant %s", user.ID, tenantID)
  48. return user.ID
  49. }
  50. func listUsers(t *testing.T, client *gophercloud.ServiceClient) {
  51. err := users.List(client).EachPage(func(page pagination.Page) (bool, error) {
  52. userList, err := users.ExtractUsers(page)
  53. th.AssertNoErr(t, err)
  54. for _, user := range userList {
  55. t.Logf("Listing user: ID [%s] Name [%s] Email [%s] Enabled? [%s]",
  56. user.ID, user.Name, user.Email, strconv.FormatBool(user.Enabled))
  57. }
  58. return true, nil
  59. })
  60. th.AssertNoErr(t, err)
  61. }
  62. func getUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  63. _, err := users.Get(client, userID).Extract()
  64. th.AssertNoErr(t, err)
  65. t.Logf("Getting user %s", userID)
  66. }
  67. func updateUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  68. opts := users.UpdateOpts{Name: tools.RandomString("new_name", 5), Email: "new@foo.com"}
  69. user, err := users.Update(client, userID, opts).Extract()
  70. th.AssertNoErr(t, err)
  71. t.Logf("Updated user %s: Name [%s] Email [%s]", userID, user.Name, user.Email)
  72. }
  73. func listUserRoles(t *testing.T, client *gophercloud.ServiceClient, tenantID, userID string) {
  74. count := 0
  75. err := users.ListRoles(client, tenantID, userID).EachPage(func(page pagination.Page) (bool, error) {
  76. count++
  77. roleList, err := users.ExtractRoles(page)
  78. th.AssertNoErr(t, err)
  79. t.Logf("Listing roles for user %s", userID)
  80. for _, r := range roleList {
  81. t.Logf("- %s (%s)", r.Name, r.ID)
  82. }
  83. return true, nil
  84. })
  85. if count == 0 {
  86. t.Logf("No roles for user %s", userID)
  87. }
  88. th.AssertNoErr(t, err)
  89. }
  90. func deleteUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  91. res := users.Delete(client, userID)
  92. th.AssertNoErr(t, res.Err)
  93. t.Logf("Deleted user %s", userID)
  94. }