github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/dms/v2/instances_test.go (about)

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