github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/dms/v1/instances_test.go (about) 1 package v1 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/others" 8 9 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 10 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" 11 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack" 12 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools" 13 "github.com/opentelekomcloud/gophertelekomcloud/openstack/dms/v1/instances" 14 "github.com/opentelekomcloud/gophertelekomcloud/openstack/dms/v1/products" 15 "github.com/opentelekomcloud/gophertelekomcloud/openstack/dms/v1/topics" 16 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 17 ) 18 19 func TestDmsList(t *testing.T) { 20 client, err := clients.NewDmsV1Client() 21 th.AssertNoErr(t, err) 22 23 listOpts := instances.ListDmsInstanceOpts{} 24 dmsAllPages, err := instances.List(client, listOpts).AllPages() 25 th.AssertNoErr(t, err) 26 dmsInstances, err := instances.ExtractDmsInstances(dmsAllPages) 27 th.AssertNoErr(t, err) 28 for _, val := range dmsInstances.Instances { 29 tools.PrintResource(t, val) 30 } 31 } 32 33 func TestDmsLifeCycle(t *testing.T) { 34 client, err := clients.NewDmsV1Client() 35 th.AssertNoErr(t, err) 36 37 instanceID := createDmsInstance(t, client) 38 defer deleteDmsInstance(t, client, instanceID) 39 40 dmsInstance, err := instances.Get(client, instanceID).Extract() 41 th.AssertNoErr(t, err) 42 th.AssertEquals(t, "some interesting description", dmsInstance.Description) 43 44 dmsTopic := createTopic(t, client, instanceID) 45 46 getTopic, err := topics.Get(client, instanceID).Extract() 47 th.AssertNoErr(t, err) 48 th.AssertEquals(t, dmsTopic, getTopic.Topics[0].Name) 49 50 delTopic := deleteTopic(t, client, instanceID, dmsTopic) 51 th.AssertEquals(t, delTopic[0].Name, dmsTopic) 52 53 updateDmsInstance(t, client, instanceID) 54 dmsInstance, err = instances.Get(client, instanceID).Extract() 55 th.AssertNoErr(t, err) 56 th.AssertEquals(t, "", dmsInstance.Description) 57 } 58 59 func createDmsInstance(t *testing.T, client *golangsdk.ServiceClient) string { 60 t.Logf("Attempting to create DMSv1 instance") 61 dmsName := tools.RandomString("dms-acc-", 8) 62 63 vpcID := clients.EnvOS.GetEnv("VPC_ID") 64 subnetID := clients.EnvOS.GetEnv("NETWORK_ID") 65 if vpcID == "" || subnetID == "" { 66 t.Skip("One of OS_VPC_ID or OS_NETWORK_ID env vars is missing but DMS test requires using existing network") 67 } 68 69 defaultSgID := openstack.DefaultSecurityGroup(t) 70 details := getDmsInstanceSpecification(t, client) 71 az := getDmsInstanceAz(t, client) 72 partitionNum, _ := strconv.Atoi(details.PartitionNum) 73 storage, _ := strconv.Atoi(details.Storage) 74 75 createOpts := instances.CreateOpts{ 76 Name: dmsName, 77 Description: "some interesting description", 78 Engine: "kafka", 79 EngineVersion: "2.3.0", 80 StorageSpace: storage, 81 Password: "5ecuredPa55w0rd!", 82 AccessUser: "root", 83 VpcID: vpcID, 84 SecurityGroupID: defaultSgID, 85 SubnetID: subnetID, 86 AvailableZones: []string{az}, 87 ProductID: details.ProductID, 88 PartitionNum: partitionNum, 89 SslEnable: true, 90 Specification: details.VMSpecification, 91 StorageSpecCode: details.IOs[0].StorageSpecCode, 92 } 93 dmsInstance, err := instances.Create(client, createOpts).Extract() 94 th.AssertNoErr(t, err) 95 err = waitForInstanceAvailable(client, 600, dmsInstance.InstanceID) 96 th.AssertNoErr(t, err) 97 t.Logf("DMSv1 instance successfully created: %s", dmsInstance.InstanceID) 98 99 return dmsInstance.InstanceID 100 } 101 102 func deleteDmsInstance(t *testing.T, client *golangsdk.ServiceClient, instanceID string) { 103 t.Logf("Attempting to delete DMSv1 instance: %s", instanceID) 104 105 err := instances.Delete(client, instanceID).ExtractErr() 106 th.AssertNoErr(t, err) 107 108 err = waitForInstanceDelete(client, 600, instanceID) 109 th.AssertNoErr(t, err) 110 t.Logf("DMSv1 instance deleted successfully: %s", instanceID) 111 } 112 113 func updateDmsInstance(t *testing.T, client *golangsdk.ServiceClient, instanceID string) { 114 t.Logf("Attempting to update DMSv1 instance: %s", instanceID) 115 116 emptyDescription := "" 117 updateOpts := instances.UpdateOpts{ 118 Description: &emptyDescription, 119 } 120 121 err := instances.Update(client, instanceID, updateOpts).ExtractErr() 122 th.AssertNoErr(t, err) 123 124 t.Logf("DMSv1 instance updated successfully: %s", instanceID) 125 } 126 127 func getDmsInstanceSpecification(t *testing.T, client *golangsdk.ServiceClient) products.Detail { 128 v, err := products.Get(client, "kafka").Extract() 129 th.AssertNoErr(t, err) 130 productList := v.Hourly 131 132 var filteredPd []products.Detail 133 for _, pd := range productList { 134 if pd.Version != "2.3.0" { 135 continue 136 } 137 for _, value := range pd.Values { 138 if value.Name != "cluster" { 139 continue 140 } 141 142 filteredPd = append(filteredPd, value.Details...) 143 } 144 } 145 146 return filteredPd[0] 147 } 148 149 func getDmsInstanceAz(t *testing.T, client *golangsdk.ServiceClient) string { 150 az, err := others.ListAvailableZones(client) 151 th.AssertNoErr(t, err) 152 153 return az.AvailableZones[0].ID 154 } 155 156 func waitForInstanceAvailable(client *golangsdk.ServiceClient, secs int, instanceID string) error { 157 return golangsdk.WaitFor(secs, func() (bool, error) { 158 dmsInstance, err := instances.Get(client, instanceID).Extract() 159 if err != nil { 160 return false, err 161 } 162 if dmsInstance.Status == "RUNNING" { 163 return true, nil 164 } 165 return false, nil 166 }) 167 } 168 169 func waitForInstanceDelete(client *golangsdk.ServiceClient, secs int, instanceID string) error { 170 return golangsdk.WaitFor(secs, func() (bool, error) { 171 _, err := instances.Get(client, instanceID).Extract() 172 if err != nil { 173 if _, ok := err.(golangsdk.ErrDefault404); ok { 174 return true, nil 175 } 176 return false, err 177 } 178 return false, nil 179 }) 180 } 181 182 func createTopic(t *testing.T, client *golangsdk.ServiceClient, instanceId string) string { 183 t.Logf("Attempting to create DMSv1 Topic") 184 topicName := tools.RandomString("dms-topic-", 8) 185 186 createOpts := topics.CreateOpts{ 187 Name: topicName, 188 Partition: 10, 189 Replication: 2, 190 SyncReplication: true, 191 RetentionTime: 100, 192 SyncMessageFlush: true, 193 } 194 dmsTopic, err := topics.Create(client, createOpts, instanceId).Extract() 195 th.AssertNoErr(t, err) 196 t.Logf("DMSv1 Topic successfully created: %s", dmsTopic.Name) 197 198 return dmsTopic.Name 199 } 200 201 func deleteTopic(t *testing.T, client *golangsdk.ServiceClient, instanceId string, name string) []topics.TopicDelete { 202 t.Logf("Attempting to delete DMSv1 Topic") 203 204 deleteOpts := topics.DeleteOpts{ 205 Topics: []string{ 206 name, 207 }, 208 } 209 dmsTopic, err := topics.Delete(client, deleteOpts, instanceId).Extract() 210 th.AssertNoErr(t, err) 211 th.AssertEquals(t, dmsTopic[0].Success, true) 212 213 getTopic, err := topics.Get(client, instanceId).Extract() 214 th.AssertNoErr(t, err) 215 th.AssertEquals(t, getTopic.Size, 0) 216 217 t.Logf("DMSv1 Topic successfully deleted") 218 219 return dmsTopic 220 }