PageRenderTime 78ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/db/v1/db.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 145 lines | 101 code | 25 blank | 19 comment | 19 complexity | 60adc1341f75d44d1bc2adb15be65f12 MD5 | raw file
  1. // Package v2 contains common functions for creating db resources for use
  2. // in acceptance tests. See the `*_test.go` files for example usages.
  3. package v1
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/gophercloud/gophercloud"
  8. "github.com/gophercloud/gophercloud/acceptance/clients"
  9. "github.com/gophercloud/gophercloud/acceptance/tools"
  10. "github.com/gophercloud/gophercloud/openstack/db/v1/databases"
  11. "github.com/gophercloud/gophercloud/openstack/db/v1/instances"
  12. "github.com/gophercloud/gophercloud/openstack/db/v1/users"
  13. )
  14. // CreateDatabase will create a database with a randomly generated name.
  15. // An error will be returned if the database was unable to be created.
  16. func CreateDatabase(t *testing.T, client *gophercloud.ServiceClient, instanceID string) error {
  17. name := tools.RandomString("ACPTTEST", 8)
  18. t.Logf("Attempting to create database: %s", name)
  19. createOpts := databases.BatchCreateOpts{
  20. databases.CreateOpts{
  21. Name: name,
  22. },
  23. }
  24. return databases.Create(client, instanceID, createOpts).ExtractErr()
  25. }
  26. // CreateInstance will create an instance with a randomly generated name.
  27. // The flavor of the instance will be the value of the OS_FLAVOR_ID
  28. // environment variable. The Datastore will be pulled from the
  29. // OS_DATASTORE_TYPE_ID environment variable.
  30. // An error will be returned if the instance was unable to be created.
  31. func CreateInstance(t *testing.T, client *gophercloud.ServiceClient) (*instances.Instance, error) {
  32. if testing.Short() {
  33. t.Skip("Skipping test that requires instance creation in short mode.")
  34. }
  35. choices, err := clients.AcceptanceTestChoicesFromEnv()
  36. if err != nil {
  37. return nil, err
  38. }
  39. name := tools.RandomString("ACPTTEST", 8)
  40. t.Logf("Attempting to create instance: %s", name)
  41. createOpts := instances.CreateOpts{
  42. FlavorRef: choices.FlavorID,
  43. Size: 1,
  44. Name: name,
  45. Datastore: &instances.DatastoreOpts{
  46. Type: choices.DBDatastoreType,
  47. Version: choices.DBDatastoreVersion,
  48. },
  49. }
  50. instance, err := instances.Create(client, createOpts).Extract()
  51. if err != nil {
  52. return instance, err
  53. }
  54. if err := WaitForInstanceStatus(client, instance, "ACTIVE"); err != nil {
  55. return instance, err
  56. }
  57. return instances.Get(client, instance.ID).Extract()
  58. }
  59. // CreateUser will create a user with a randomly generated name.
  60. // An error will be returned if the user was unable to be created.
  61. func CreateUser(t *testing.T, client *gophercloud.ServiceClient, instanceID string) error {
  62. name := tools.RandomString("ACPTTEST", 8)
  63. password := tools.RandomString("", 8)
  64. t.Logf("Attempting to create user: %s", name)
  65. createOpts := users.BatchCreateOpts{
  66. users.CreateOpts{
  67. Name: name,
  68. Password: password,
  69. },
  70. }
  71. return users.Create(client, instanceID, createOpts).ExtractErr()
  72. }
  73. // DeleteDatabase deletes a database. A fatal error will occur if the database
  74. // failed to delete. This works best when used as a deferred function.
  75. func DeleteDatabase(t *testing.T, client *gophercloud.ServiceClient, instanceID, name string) {
  76. t.Logf("Attempting to delete database: %s", name)
  77. err := databases.Delete(client, instanceID, name).ExtractErr()
  78. if err != nil {
  79. t.Fatalf("Unable to delete database %s: %s", name, err)
  80. }
  81. t.Logf("Deleted database: %s", name)
  82. }
  83. // DeleteInstance deletes an instance. A fatal error will occur if the instance
  84. // failed to delete. This works best when used as a deferred function.
  85. func DeleteInstance(t *testing.T, client *gophercloud.ServiceClient, id string) {
  86. t.Logf("Attempting to delete instance: %s", id)
  87. err := instances.Delete(client, id).ExtractErr()
  88. if err != nil {
  89. t.Fatalf("Unable to delete instance %s: %s", id, err)
  90. }
  91. t.Logf("Deleted instance: %s", id)
  92. }
  93. // DeleteUser deletes a user. A fatal error will occur if the user
  94. // failed to delete. This works best when used as a deferred function.
  95. func DeleteUser(t *testing.T, client *gophercloud.ServiceClient, instanceID, name string) {
  96. t.Logf("Attempting to delete user: %s", name)
  97. err := users.Delete(client, instanceID, name).ExtractErr()
  98. if err != nil {
  99. t.Fatalf("Unable to delete users %s: %s", name, err)
  100. }
  101. t.Logf("Deleted users: %s", name)
  102. }
  103. // WaitForInstanceState will poll an instance's status until it either matches
  104. // the specified status or the status becomes ERROR.
  105. func WaitForInstanceStatus(
  106. client *gophercloud.ServiceClient, instance *instances.Instance, status string) error {
  107. return tools.WaitFor(func() (bool, error) {
  108. latest, err := instances.Get(client, instance.ID).Extract()
  109. if err != nil {
  110. return false, err
  111. }
  112. if latest.Status == status {
  113. return true, nil
  114. }
  115. if latest.Status == "ERROR" {
  116. return false, fmt.Errorf("Instance in ERROR state")
  117. }
  118. return false, nil
  119. })
  120. }