github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/drs/v3/tasks_test.go (about)

     1  package v3
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     9  	networking "github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/networking/v1"
    10  	rds "github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/rds/v3"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    12  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
    13  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/drs/v3/public"
    14  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/instances"
    15  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    16  )
    17  
    18  func TestDrsTaskLifecycle(t *testing.T) {
    19  	if os.Getenv("RUN_DRS_LIFECYCLE") == "" {
    20  		t.Skip("too slow to run in zuul")
    21  	}
    22  	subnetId := os.Getenv("OS_SUBNET_ID")
    23  
    24  	client, err := clients.NewDrsV3Client()
    25  	th.AssertNoErr(t, err)
    26  
    27  	rdsClient, err := clients.NewRdsV3()
    28  	th.AssertNoErr(t, err)
    29  
    30  	netClient, err := clients.NewNetworkV1Client()
    31  	th.AssertNoErr(t, err)
    32  
    33  	cc, err := clients.CloudAndClient()
    34  	th.AssertNoErr(t, err)
    35  
    36  	instanceName := tools.RandomString("drs-task-", 8)
    37  	netType := "eip"
    38  	dbType := "postgresql"
    39  
    40  	rdsChan1 := make(chan *instances.Instance)
    41  	rdsChan2 := make(chan *instances.Instance)
    42  
    43  	go func() {
    44  		defer close(rdsChan1)
    45  		source := rds.CreateRDS(t, rdsClient, cc.RegionName)
    46  		rdsChan1 <- source
    47  	}()
    48  
    49  	go func() {
    50  		defer close(rdsChan2)
    51  		target := rds.CreateRDS(t, rdsClient, cc.RegionName)
    52  		rdsChan2 <- target
    53  	}()
    54  
    55  	rdsSource := <-rdsChan1
    56  	rdsTarget := <-rdsChan2
    57  
    58  	t.Cleanup(func() { rds.DeleteRDS(t, rdsClient, rdsSource.Id) })
    59  	t.Cleanup(func() { rds.DeleteRDS(t, rdsClient, rdsTarget.Id) })
    60  
    61  	elasticIP := networking.CreateEip(t, netClient, 100)
    62  	t.Cleanup(func() {
    63  		networking.DeleteEip(t, netClient, elasticIP.ID)
    64  	})
    65  
    66  	t.Log("AttachEip")
    67  
    68  	err = instances.AttachEip(rdsClient, instances.AttachEipOpts{
    69  		InstanceId: rdsSource.Id,
    70  		PublicIp:   elasticIP.PublicAddress,
    71  		PublicIpId: elasticIP.ID,
    72  		IsBind:     pointerto.Bool(true),
    73  	})
    74  	th.AssertNoErr(t, err)
    75  	t.Cleanup(func() {
    76  		err = instances.AttachEip(rdsClient, instances.AttachEipOpts{
    77  			InstanceId: rdsSource.Id,
    78  			IsBind:     pointerto.Bool(false),
    79  		})
    80  		th.AssertNoErr(t, err)
    81  	})
    82  
    83  	createJobOpts := public.BatchCreateTaskOpts{
    84  		Jobs: []public.CreateJobOpts{{
    85  			Name:              instanceName,
    86  			DbUseType:         "sync",
    87  			EngineType:        dbType,
    88  			JobDirection:      "up",
    89  			CustomizeSubnetId: subnetId,
    90  			NetType:           netType,
    91  			BindEip:           pointerto.Bool(true),
    92  			NodeType:          "high",
    93  			TaskType:          "FULL_TRANS",
    94  			SourceEndpoint: public.Endpoint{
    95  				DbType: "postgresql",
    96  				Region: cc.RegionName,
    97  				InstId: rdsSource.Id,
    98  			},
    99  			TargetEndpoint: public.Endpoint{
   100  				DbType: "postgresql",
   101  				Region: cc.RegionName,
   102  				InstId: rdsTarget.Id,
   103  			},
   104  		}},
   105  	}
   106  
   107  	task, err := public.BatchCreateTasks(client, createJobOpts)
   108  	th.AssertNoErr(t, err)
   109  
   110  	defer func() {
   111  		deleteJob, err := public.BatchDeleteTasks(client, public.BatchDeleteTasksOpts{
   112  			Jobs: []public.DeleteJobReq{{
   113  				JobId:      task.Results[0].Id,
   114  				DeleteType: "force_terminate",
   115  			}},
   116  		})
   117  		th.AssertNoErr(t, err)
   118  		th.AssertNoErr(t, waitForTaskComplete(client, deleteJob.Results[0].Id, "RELEASE_RESOURCE_COMPLETE", 600))
   119  		_, err = public.BatchDeleteTasks(client, public.BatchDeleteTasksOpts{
   120  			Jobs: []public.DeleteJobReq{{
   121  				JobId:      task.Results[0].Id,
   122  				DeleteType: "delete",
   123  			}},
   124  		})
   125  		th.AssertNoErr(t, err)
   126  	}()
   127  
   128  	th.AssertNoErr(t, waitForTaskComplete(client, task.Results[0].Id, "CONFIGURATION", 600))
   129  
   130  	taskList, err := public.BatchListTaskDetails(client, public.BatchQueryTaskOpts{
   131  		Jobs: []string{
   132  			task.Results[0].Id,
   133  		},
   134  	})
   135  	th.AssertNoErr(t, err)
   136  	tools.PrintResource(t, taskList)
   137  
   138  	taskStatus, err := public.BatchListTaskStatus(client, public.BatchQueryTaskOpts{
   139  		Jobs: []string{
   140  			task.Results[0].Id,
   141  		},
   142  	})
   143  	th.AssertNoErr(t, err)
   144  	tools.PrintResource(t, taskStatus)
   145  
   146  	preCheck, err := public.BatchCheckTasks(client, public.BatchPreCheckReq{
   147  		Jobs: []public.PreCheckInfo{
   148  			{
   149  				JobId:        task.Results[0].Id,
   150  				PreCheckMode: "forStartJob",
   151  			},
   152  		},
   153  	})
   154  	th.AssertNoErr(t, err)
   155  	th.AssertEquals(t, preCheck.Results[0].Status, "success")
   156  
   157  	testConnection, err := public.BatchTestConnections(client, public.BatchTestConnectionOpts{
   158  		Jobs: []public.TestEndPoint{
   159  			{
   160  				Id:           task.Results[0].Id,
   161  				NetType:      netType,
   162  				DbType:       dbType,
   163  				DbPort:       8635,
   164  				Ip:           elasticIP.PublicAddress,
   165  				DbUser:       "root",
   166  				DbPassword:   "acc-test-password1!",
   167  				EndPointType: "so",
   168  			},
   169  		},
   170  	},
   171  	)
   172  	th.AssertNoErr(t, err)
   173  	th.AssertEquals(t, testConnection.Results[0].Status, "failed")
   174  
   175  	taskModify, err := public.BatchUpdateTask(client, public.BatchModifyJobOpts{
   176  		Jobs: []public.ModifyJobReq{
   177  			{
   178  				JobId:       task.Results[0].Id,
   179  				Name:        "updated-task",
   180  				Description: "new_description",
   181  			},
   182  		},
   183  	})
   184  	th.AssertNoErr(t, err)
   185  	th.AssertEquals(t, taskModify.Results[0].Status, "success")
   186  
   187  	setSpeed, err := public.BatchSetSpeed(client, public.BatchLimitSpeedOpts{
   188  		SpeedLimits: []public.LimitSpeedReq{
   189  			{
   190  				JobId: task.Results[0].Id,
   191  				SpeedLimit: []public.SpeedLimitInfo{
   192  					{
   193  						Speed: "15",
   194  						Begin: "16:00",
   195  						End:   "15:59",
   196  					},
   197  				},
   198  			},
   199  		},
   200  	})
   201  	th.AssertNoErr(t, err)
   202  	th.AssertEquals(t, setSpeed.Results[0].Status, "success")
   203  
   204  	startTask, err := public.BatchStartTasks(client, public.BatchStartJobOpts{
   205  		Jobs: []public.StartInfo{
   206  			{
   207  				JobId: task.Results[0].Id,
   208  			},
   209  		},
   210  	})
   211  	th.AssertNoErr(t, err)
   212  	th.AssertEquals(t, startTask.Results[0].Status, "success")
   213  }
   214  
   215  func waitForTaskComplete(c *golangsdk.ServiceClient, id, status string, secs int) error {
   216  	return golangsdk.WaitFor(secs, func() (bool, error) {
   217  		taskList, err := public.BatchListTaskDetails(c, public.BatchQueryTaskOpts{
   218  			Jobs: []string{
   219  				id,
   220  			},
   221  		})
   222  		if err != nil {
   223  			return false, err
   224  		}
   225  
   226  		if taskList.Results[0].Status == status {
   227  			return true, nil
   228  		}
   229  
   230  		return false, nil
   231  	})
   232  }