github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/dcs/v1/helpers.go (about)

     1  package v1
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/others"
     8  
     9  	"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/dcs/v1/lifecycle"
    14  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    15  )
    16  
    17  func createDCSInstance(t *testing.T, client *golangsdk.ServiceClient) *lifecycle.Instance {
    18  	t.Logf("Attempting to create DCSv1 instance")
    19  
    20  	vpcID := clients.EnvOS.GetEnv("VPC_ID")
    21  	networkID := clients.EnvOS.GetEnv("NETWORK_ID")
    22  	if vpcID == "" || networkID == "" {
    23  		t.Skip("OS_VPC_ID or OS_NETWORK_ID is missing but test requires using existing network")
    24  	}
    25  
    26  	availabilityZone, err := others.ListAvailableZones(client)
    27  	th.AssertNoErr(t, err)
    28  	var az string
    29  	for _, v := range availabilityZone.AvailableZones {
    30  		if v.ResourceAvailability != "true" {
    31  			continue
    32  		}
    33  		az = v.ID
    34  	}
    35  	if az == "" {
    36  		t.Skip("Availability Zone ID wasn't found")
    37  	}
    38  
    39  	productList, err := others.GetProducts(client)
    40  	th.AssertNoErr(t, err)
    41  
    42  	var specCode string
    43  	for _, v := range productList {
    44  		if v.SpecCode == "redis.ha.xu1.tiny.r2.128" {
    45  			specCode = v.SpecCode
    46  		}
    47  	}
    48  	if specCode == "" {
    49  		t.Skip("Product ID wasn't found")
    50  	}
    51  
    52  	plan := lifecycle.InstanceBackupPolicy{
    53  		SaveDays: 1,
    54  		PeriodicalBackupPlan: lifecycle.PeriodicalBackupPlan{
    55  			BeginAt:    "00:00-01:00",
    56  			PeriodType: "weekly",
    57  			BackupAt:   []int{1, 2, 3, 4, 5, 6, 7},
    58  		},
    59  	}
    60  	instanceID, err := lifecycle.Create(client, lifecycle.CreateOps{
    61  		Name:                 tools.RandomString("dcs-instance-", 3),
    62  		Description:          "some test DCSv1 instance",
    63  		Engine:               "Redis",
    64  		EngineVersion:        "5.0",
    65  		Capacity:             0.125,
    66  		Password:             "Qwerty123!",
    67  		VPCId:                vpcID,
    68  		SubnetID:             networkID,
    69  		AvailableZones:       []string{az},
    70  		SpecCode:             specCode,
    71  		SecurityGroupID:      openstack.DefaultSecurityGroup(t),
    72  		InstanceBackupPolicy: &plan,
    73  		Tags: []tags.ResourceTag{
    74  			{
    75  				Key:   "muh",
    76  				Value: "kuh",
    77  			},
    78  			{
    79  				Key:   "muh2",
    80  				Value: "kuh2",
    81  			},
    82  		},
    83  	})
    84  	th.AssertNoErr(t, err)
    85  	t.Cleanup(func() {
    86  		deleteDCSInstance(t, client, instanceID)
    87  	})
    88  
    89  	err = waitForInstanceAvailable(client, 100, instanceID)
    90  	th.AssertNoErr(t, err)
    91  
    92  	t.Logf("DCSv1 instance successfully created: %s", instanceID)
    93  
    94  	ins, err := lifecycle.Get(client, instanceID)
    95  	th.AssertNoErr(t, err)
    96  
    97  	th.AssertEquals(t, plan.SaveDays, ins.InstanceBackupPolicy.Policy.SaveDays)
    98  	th.AssertEquals(t, plan.PeriodicalBackupPlan.BeginAt, ins.InstanceBackupPolicy.Policy.PeriodicalBackupPlan.BeginAt)
    99  	th.AssertEquals(t, plan.PeriodicalBackupPlan.PeriodType, ins.InstanceBackupPolicy.Policy.PeriodicalBackupPlan.PeriodType)
   100  
   101  	return ins
   102  }
   103  
   104  func deleteDCSInstance(t *testing.T, client *golangsdk.ServiceClient, instanceID string) {
   105  	t.Logf("Attempting to delete DCSv1 instance: %s", instanceID)
   106  
   107  	err := lifecycle.Delete(client, instanceID)
   108  	th.AssertNoErr(t, err)
   109  
   110  	err = waitForInstanceDeleted(client, 600, instanceID)
   111  	th.AssertNoErr(t, err)
   112  
   113  	th.AssertNoErr(t, err)
   114  	t.Logf("Deleted DCSv1 instance: %s", instanceID)
   115  }
   116  
   117  func waitForInstanceAvailable(client *golangsdk.ServiceClient, secs int, instanceID string) error {
   118  	return golangsdk.WaitFor(secs, func() (bool, error) {
   119  		dcsInstances, err := lifecycle.Get(client, instanceID)
   120  		if err != nil {
   121  			return false, err
   122  		}
   123  		if dcsInstances.Status == "RUNNING" {
   124  			return true, nil
   125  		}
   126  		return false, nil
   127  	})
   128  }
   129  
   130  func waitForInstanceDeleted(client *golangsdk.ServiceClient, secs int, instanceID string) error {
   131  	return golangsdk.WaitFor(secs, func() (bool, error) {
   132  		_, err := lifecycle.Get(client, instanceID)
   133  		if err != nil {
   134  			if _, ok := err.(golangsdk.ErrDefault404); ok {
   135  				return true, nil
   136  			}
   137  			return false, err
   138  		}
   139  
   140  		return false, nil
   141  	})
   142  }