From 8cbceceb38a85658f418c72eb1730941230f6574 Mon Sep 17 00:00:00 2001 From: chinmaym07 Date: Sun, 18 Sep 2022 00:06:02 +0530 Subject: [PATCH] Fixed the Logic for comma-separated splitting Signed-off-by: chinmaym07 --- .../aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go | 6 +++--- .../litmus/azure-disk-loss/lib/azure-disk-loss.go | 7 +++---- .../azure-instance-stop/lib/azure-instance-stop.go | 6 +++--- .../ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go | 7 +++---- .../ec2-terminate-by-id/lib/ec2-terminate-by-id.go | 7 +++---- .../gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go | 11 ++++++++++- pkg/cloud/aws/ebs/ebs-volume-state.go | 5 ++--- pkg/cloud/aws/ec2/ec2-instance-status.go | 5 ++--- pkg/cloud/azure/instance/instance-status.go | 4 ++-- pkg/cloud/gcp/disk-volume-status.go | 9 +++++---- pkg/cloud/gcp/vm-instance-status.go | 10 ++++++---- pkg/cloud/vmware/vm-status.go | 5 ++--- 12 files changed, 44 insertions(+), 38 deletions(-) diff --git a/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go b/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go index 9ca7b8404..29f5c00d6 100644 --- a/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go +++ b/chaoslib/litmus/aws-ssm-chaos/lib/ssm/aws-ssm-chaos-by-id.go @@ -50,11 +50,11 @@ func PrepareAWSSSMChaosByID(experimentsDetails *experimentTypes.ExperimentDetail // watching for the abort signal and revert the chaos go lib.AbortWatcher(experimentsDetails, abort) - //get the instance id or list of instance ids - instanceIDList := strings.Split(experimentsDetails.EC2InstanceID, ",") - if len(instanceIDList) == 0 { + if experimentsDetails.EC2InstanceID == "" { return errors.Errorf("no instance id found for chaos injection") } + //get the instance id or list of instance ids + instanceIDList := strings.Split(experimentsDetails.EC2InstanceID, ",") switch strings.ToLower(experimentsDetails.Sequence) { case "serial": diff --git a/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go b/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go index 867506603..c76ce5abd 100644 --- a/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go +++ b/chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go @@ -44,12 +44,11 @@ func PrepareChaos(experimentsDetails *experimentTypes.ExperimentDetails, clients log.Infof("[Ramp]: Waiting for the %vs ramp time before injecting chaos", experimentsDetails.RampTime) common.WaitForDuration(experimentsDetails.RampTime) } - - //get the disk name or list of disk names - diskNameList := strings.Split(experimentsDetails.VirtualDiskNames, ",") - if len(diskNameList) == 0 { + if experimentsDetails.VirtualDiskNames == "" { return errors.Errorf("no volume names found to detach") } + //get the disk name or list of disk names + diskNameList := strings.Split(experimentsDetails.VirtualDiskNames, ",") instanceNamesWithDiskNames, err := diskStatus.GetInstanceNameForDisks(diskNameList, experimentsDetails.SubscriptionID, experimentsDetails.ResourceGroup) if err != nil { diff --git a/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go b/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go index f15330682..d7208f642 100644 --- a/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go +++ b/chaoslib/litmus/azure-instance-stop/lib/azure-instance-stop.go @@ -42,11 +42,11 @@ func PrepareAzureStop(experimentsDetails *experimentTypes.ExperimentDetails, cli common.WaitForDuration(experimentsDetails.RampTime) } - // get the instance name or list of instance names - instanceNameList := strings.Split(experimentsDetails.AzureInstanceNames, ",") - if len(instanceNameList) == 0 { + if experimentsDetails.AzureInstanceNames == "" { return errors.Errorf("no instance name found to stop") } + // get the instance name or list of instance names + instanceNameList := strings.Split(experimentsDetails.AzureInstanceNames, ",") // watching for the abort signal and revert the chaos go abortWatcher(experimentsDetails, instanceNameList) diff --git a/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go b/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go index a07c8aa5a..81658dcff 100644 --- a/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go +++ b/chaoslib/litmus/ebs-loss/lib/ebs-loss-by-id/lib/ebs-loss-by-id.go @@ -44,12 +44,11 @@ func PrepareEBSLossByID(experimentsDetails *experimentTypes.ExperimentDetails, c // stopping the chaos execution, if abort signal received os.Exit(0) default: - - //get the volume id or list of instance ids - volumeIDList := strings.Split(experimentsDetails.EBSVolumeID, ",") - if len(volumeIDList) == 0 { + if experimentsDetails.EBSVolumeID == "" { return errors.Errorf("no volume id found to detach") } + //get the volume id or list of instance ids + volumeIDList := strings.Split(experimentsDetails.EBSVolumeID, ",") // watching for the abort signal and revert the chaos go ebsloss.AbortWatcher(experimentsDetails, volumeIDList, abort, chaosDetails) diff --git a/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go b/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go index 62c76a2e3..51bbea715 100644 --- a/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go +++ b/chaoslib/litmus/ec2-terminate-by-id/lib/ec2-terminate-by-id.go @@ -41,12 +41,11 @@ func PrepareEC2TerminateByID(experimentsDetails *experimentTypes.ExperimentDetai log.Infof("[Ramp]: Waiting for the %vs ramp time before injecting chaos", experimentsDetails.RampTime) common.WaitForDuration(experimentsDetails.RampTime) } - - //get the instance id or list of instance ids - instanceIDList := strings.Split(experimentsDetails.Ec2InstanceID, ",") - if len(instanceIDList) == 0 { + if experimentsDetails.Ec2InstanceID == "" { return errors.Errorf("no instance id found to terminate") } + //get the instance id or list of instance ids + instanceIDList := strings.Split(experimentsDetails.Ec2InstanceID, ",") // watching for the abort signal and revert the chaos go abortWatcher(experimentsDetails, instanceIDList, chaosDetails) diff --git a/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go b/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go index 454dbabb2..9342e1355 100644 --- a/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go +++ b/chaoslib/litmus/gcp-vm-instance-stop/lib/gcp-vm-instance-stop.go @@ -43,12 +43,21 @@ func PrepareVMStop(computeService *compute.Service, experimentsDetails *experime common.WaitForDuration(experimentsDetails.RampTime) } + if experimentsDetails.VMInstanceName == "" { + return errors.Errorf("no instance name found to stop") + } // get the instance name or list of instance names instanceNamesList := strings.Split(experimentsDetails.VMInstanceName, ",") - + if experimentsDetails.Zones == "" { + return errors.Errorf("no corresponding zones found for the instances") + } // get the zone name or list of corresponding zones for the instances instanceZonesList := strings.Split(experimentsDetails.Zones, ",") + if len(instanceNamesList) != len(instanceZonesList) { + return errors.Errorf("number of instances is not equal to the number of zones") + } + go abortWatcher(computeService, experimentsDetails, instanceNamesList, instanceZonesList, chaosDetails) switch strings.ToLower(experimentsDetails.Sequence) { diff --git a/pkg/cloud/aws/ebs/ebs-volume-state.go b/pkg/cloud/aws/ebs/ebs-volume-state.go index d49bd307b..8cc00dc03 100644 --- a/pkg/cloud/aws/ebs/ebs-volume-state.go +++ b/pkg/cloud/aws/ebs/ebs-volume-state.go @@ -101,11 +101,10 @@ func GetEBSStatus(ebsVolumeID, ec2InstanceID, region string) (string, error) { //EBSStateCheckByID will check the attachment state of the given volume func EBSStateCheckByID(volumeIDs, region string) error { - - volumeIDList := strings.Split(volumeIDs, ",") - if len(volumeIDList) == 0 { + if volumeIDs == "" { return errors.Errorf("no volumeID provided, please provide a volume to detach") } + volumeIDList := strings.Split(volumeIDs, ",") for _, id := range volumeIDList { instanceID, _, err := GetVolumeAttachmentDetails(id, "", region) if err != nil { diff --git a/pkg/cloud/aws/ec2/ec2-instance-status.go b/pkg/cloud/aws/ec2/ec2-instance-status.go index b0f9b051f..3daf88409 100644 --- a/pkg/cloud/aws/ec2/ec2-instance-status.go +++ b/pkg/cloud/aws/ec2/ec2-instance-status.go @@ -41,11 +41,10 @@ func GetEC2InstanceStatus(instanceID, region string) (string, error) { //InstanceStatusCheckByID is used to check the instance status of all the instance under chaos. func InstanceStatusCheckByID(instanceID, region string) error { - - instanceIDList := strings.Split(instanceID, ",") - if len(instanceIDList) == 0 { + if instanceID == "" { return errors.Errorf("no instance id found to terminate") } + instanceIDList := strings.Split(instanceID, ",") log.Infof("[Info]: The instances under chaos(IUC) are: %v", instanceIDList) return InstanceStatusCheck(instanceIDList, region) } diff --git a/pkg/cloud/azure/instance/instance-status.go b/pkg/cloud/azure/instance/instance-status.go index b3039ddb1..e6a568854 100644 --- a/pkg/cloud/azure/instance/instance-status.go +++ b/pkg/cloud/azure/instance/instance-status.go @@ -71,10 +71,10 @@ func GetAzureScaleSetInstanceStatus(subscriptionID, resourceGroup, virtualMachin // InstanceStatusCheckByName is used to check the instance status of all the instance under chaos func InstanceStatusCheckByName(azureInstanceNames, scaleSet, subscriptionID, resourceGroup string) error { - instanceNameList := strings.Split(azureInstanceNames, ",") - if len(instanceNameList) == 0 { + if azureInstanceNames == "" { return errors.Errorf("no instance found to check the status") } + instanceNameList := strings.Split(azureInstanceNames, ",") log.Infof("[Info]: The instance under chaos(IUC) are: %v", instanceNameList) switch scaleSet { case "enable": diff --git a/pkg/cloud/gcp/disk-volume-status.go b/pkg/cloud/gcp/disk-volume-status.go index 4c1ecabd9..ae3b76f15 100644 --- a/pkg/cloud/gcp/disk-volume-status.go +++ b/pkg/cloud/gcp/disk-volume-status.go @@ -103,16 +103,17 @@ func DiskVolumeStateCheck(computeService *compute.Service, experimentsDetails *e return errors.Errorf("no gcp project id provided, please provide the project id") } - diskNamesList := strings.Split(experimentsDetails.DiskVolumeNames, ",") - if len(diskNamesList) == 0 { + if experimentsDetails.DiskVolumeNames == "" { return errors.Errorf("no disk name provided, please provide the name of the disk") } - zonesList := strings.Split(experimentsDetails.Zones, ",") - if len(zonesList) == 0 { + diskNamesList := strings.Split(experimentsDetails.DiskVolumeNames, ",") + if experimentsDetails.Zones == "" { return errors.Errorf("no zone provided, please provide the zone of the disk") } + zonesList := strings.Split(experimentsDetails.Zones, ",") + if len(diskNamesList) != len(zonesList) { return errors.Errorf("unequal number of disk names and zones found, please verify the input details") } diff --git a/pkg/cloud/gcp/vm-instance-status.go b/pkg/cloud/gcp/vm-instance-status.go index bcf091cb6..74da86697 100644 --- a/pkg/cloud/gcp/vm-instance-status.go +++ b/pkg/cloud/gcp/vm-instance-status.go @@ -26,18 +26,20 @@ func GetVMInstanceStatus(computeService *compute.Service, instanceName string, g // InstanceStatusCheckByName is used to check the status of all the VM instances under chaos func InstanceStatusCheckByName(computeService *compute.Service, managedInstanceGroup string, delay, timeout int, check string, instanceNames string, gcpProjectId string, instanceZones string) error { + if instanceNames == "" { + return errors.Errorf("no vm instance name found to stop") + } instanceNamesList := strings.Split(instanceNames, ",") + if instanceZones == "" { + return errors.Errorf("no corresponding zones found for the instances") + } instanceZonesList := strings.Split(instanceZones, ",") if managedInstanceGroup != "enable" && managedInstanceGroup != "disable" { return errors.Errorf("invalid value for MANAGED_INSTANCE_GROUP: %v", managedInstanceGroup) } - if len(instanceNamesList) == 0 { - return errors.Errorf("no vm instance name found to stop") - } - if len(instanceNamesList) != len(instanceZonesList) { return errors.Errorf("the number of vm instance names and the number of regions are not equal") } diff --git a/pkg/cloud/vmware/vm-status.go b/pkg/cloud/vmware/vm-status.go index 0a98b4eee..b461ddf95 100644 --- a/pkg/cloud/vmware/vm-status.go +++ b/pkg/cloud/vmware/vm-status.go @@ -58,11 +58,10 @@ func GetVMStatus(vcenterServer, vmId, cookie string) (string, error) { //VMStatusCheck validates the steady state for the given vm ids func VMStatusCheck(vcenterServer, vmIds, cookie string) error { - - vmIdList := strings.Split(vmIds, ",") - if len(vmIdList) == 0 { + if vmIds == "" { return errors.Errorf("no vm received, please input the target VMMoids") } + vmIdList := strings.Split(vmIds, ",") for _, vmId := range vmIdList {