github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/db/v1/db.go (about) 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 5 import ( 6 "context" 7 "fmt" 8 "testing" 9 10 "github.com/vnpaycloud-console/gophercloud/v2" 11 "github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/clients" 12 "github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools" 13 "github.com/vnpaycloud-console/gophercloud/v2/openstack/db/v1/databases" 14 "github.com/vnpaycloud-console/gophercloud/v2/openstack/db/v1/instances" 15 "github.com/vnpaycloud-console/gophercloud/v2/openstack/db/v1/users" 16 ) 17 18 // CreateDatabase will create a database with a randomly generated name. 19 // An error will be returned if the database was unable to be created. 20 func CreateDatabase(t *testing.T, client *gophercloud.ServiceClient, instanceID string) error { 21 name := tools.RandomString("ACPTTEST", 8) 22 t.Logf("Attempting to create database: %s", name) 23 24 createOpts := databases.BatchCreateOpts{ 25 databases.CreateOpts{ 26 Name: name, 27 }, 28 } 29 30 return databases.Create(context.TODO(), client, instanceID, createOpts).ExtractErr() 31 } 32 33 // CreateInstance will create an instance with a randomly generated name. 34 // The flavor of the instance will be the value of the OS_FLAVOR_ID 35 // environment variable. The Datastore will be pulled from the 36 // OS_DATASTORE_TYPE_ID environment variable. 37 // An error will be returned if the instance was unable to be created. 38 func CreateInstance(t *testing.T, client *gophercloud.ServiceClient) (*instances.Instance, error) { 39 if testing.Short() { 40 t.Skip("Skipping test that requires instance creation in short mode.") 41 } 42 43 choices, err := clients.AcceptanceTestChoicesFromEnv() 44 if err != nil { 45 return nil, err 46 } 47 48 name := tools.RandomString("ACPTTEST", 8) 49 t.Logf("Attempting to create instance: %s", name) 50 51 createOpts := instances.CreateOpts{ 52 FlavorRef: choices.FlavorID, 53 Size: 1, 54 Name: name, 55 Datastore: &instances.DatastoreOpts{ 56 Type: choices.DBDatastoreType, 57 Version: choices.DBDatastoreVersion, 58 }, 59 } 60 61 instance, err := instances.Create(context.TODO(), client, createOpts).Extract() 62 if err != nil { 63 return instance, err 64 } 65 66 if err := WaitForInstanceStatus(client, instance, "ACTIVE"); err != nil { 67 return instance, err 68 } 69 70 return instances.Get(context.TODO(), client, instance.ID).Extract() 71 } 72 73 // CreateUser will create a user with a randomly generated name. 74 // An error will be returned if the user was unable to be created. 75 func CreateUser(t *testing.T, client *gophercloud.ServiceClient, instanceID string) error { 76 name := tools.RandomString("ACPTTEST", 8) 77 password := tools.RandomString("", 8) 78 t.Logf("Attempting to create user: %s", name) 79 80 createOpts := users.BatchCreateOpts{ 81 users.CreateOpts{ 82 Name: name, 83 Password: password, 84 }, 85 } 86 87 return users.Create(context.TODO(), client, instanceID, createOpts).ExtractErr() 88 } 89 90 // DeleteDatabase deletes a database. A fatal error will occur if the database 91 // failed to delete. This works best when used as a deferred function. 92 func DeleteDatabase(t *testing.T, client *gophercloud.ServiceClient, instanceID, name string) { 93 t.Logf("Attempting to delete database: %s", name) 94 err := databases.Delete(context.TODO(), client, instanceID, name).ExtractErr() 95 if err != nil { 96 t.Fatalf("Unable to delete database %s: %s", name, err) 97 } 98 99 t.Logf("Deleted database: %s", name) 100 } 101 102 // DeleteInstance deletes an instance. A fatal error will occur if the instance 103 // failed to delete. This works best when used as a deferred function. 104 func DeleteInstance(t *testing.T, client *gophercloud.ServiceClient, id string) { 105 t.Logf("Attempting to delete instance: %s", id) 106 err := instances.Delete(context.TODO(), client, id).ExtractErr() 107 if err != nil { 108 t.Fatalf("Unable to delete instance %s: %s", id, err) 109 } 110 111 t.Logf("Deleted instance: %s", id) 112 } 113 114 // DeleteUser deletes a user. A fatal error will occur if the user 115 // failed to delete. This works best when used as a deferred function. 116 func DeleteUser(t *testing.T, client *gophercloud.ServiceClient, instanceID, name string) { 117 t.Logf("Attempting to delete user: %s", name) 118 err := users.Delete(context.TODO(), client, instanceID, name).ExtractErr() 119 if err != nil { 120 t.Fatalf("Unable to delete users %s: %s", name, err) 121 } 122 123 t.Logf("Deleted users: %s", name) 124 } 125 126 // WaitForInstanceState will poll an instance's status until it either matches 127 // the specified status or the status becomes ERROR. 128 func WaitForInstanceStatus( 129 client *gophercloud.ServiceClient, instance *instances.Instance, status string) error { 130 return tools.WaitFor(func(ctx context.Context) (bool, error) { 131 latest, err := instances.Get(ctx, client, instance.ID).Extract() 132 if err != nil { 133 return false, err 134 } 135 136 if latest.Status == status { 137 return true, nil 138 } 139 140 if latest.Status == "ERROR" { 141 return false, fmt.Errorf("Instance in ERROR state") 142 } 143 144 return false, nil 145 }) 146 }