Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added check logic before splitting via comma separator #686

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/litmuschaos/litmus-go/pkg/utils/common"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -53,6 +54,9 @@ func PrepareAWSSSMChaosByID(experimentsDetails *experimentTypes.ExperimentDetail
go lib.AbortWatcher(experimentsDetails, abort)

//get the instance id or list of instance ids
if strings.TrimSpace(experimentsDetails.EC2InstanceID) == "" {
return errors.Errorf("no instance id found for chaos injection")
}
instanceIDList := strings.Split(experimentsDetails.EC2InstanceID, ",")
if experimentsDetails.EC2InstanceID == "" || len(instanceIDList) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is redundant now, please check in other places too. Also, we should handle an edge case where the input contains only one or more commas. For example, strings.Split of "," will give us a string slice containing two whitespaces.

return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "no instance id found for chaos injection"}
Expand Down
4 changes: 4 additions & 0 deletions chaoslib/litmus/azure-disk-loss/lib/azure-disk-loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/utils/common"
"github.com/litmuschaos/litmus-go/pkg/utils/retry"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -48,6 +49,9 @@ func PrepareChaos(experimentsDetails *experimentTypes.ExperimentDetails, clients
}

//get the disk name or list of disk names
if strings.TrimSpace(experimentsDetails.VirtualDiskNames) == "" {
return errors.Errorf("no volume names found for chaos injection")
}
diskNameList := strings.Split(experimentsDetails.VirtualDiskNames, ",")
if experimentsDetails.VirtualDiskNames == "" || len(diskNameList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "no volume names found to detach"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/litmuschaos/litmus-go/pkg/utils/common"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

var (
Expand All @@ -45,6 +46,9 @@ func PrepareAzureStop(experimentsDetails *experimentTypes.ExperimentDetails, cli
}

// get the instance name or list of instance names
if strings.TrimSpace(experimentsDetails.AzureInstanceNames) == "" {
return errors.Errorf("no instance name found for chaos injection")
}
instanceNameList := strings.Split(experimentsDetails.AzureInstanceNames, ",")
if experimentsDetails.AzureInstanceNames == "" || len(instanceNameList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "no instance name found to stop"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/litmuschaos/litmus-go/pkg/utils/common"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -48,6 +49,9 @@ func PrepareEBSLossByID(experimentsDetails *experimentTypes.ExperimentDetails, c
default:

//get the volume id or list of instance ids
if strings.TrimSpace(experimentsDetails.EBSVolumeID) == "" {
return errors.Errorf("no volume id found for chaos injection")
}
volumeIDList := strings.Split(experimentsDetails.EBSVolumeID, ",")
if len(volumeIDList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "no volume id found to detach"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/litmuschaos/litmus-go/pkg/utils/common"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -45,6 +46,9 @@ func PrepareEC2TerminateByID(experimentsDetails *experimentTypes.ExperimentDetai
}

//get the instance id or list of instance ids
if strings.TrimSpace(experimentsDetails.Ec2InstanceID) == "" {
return errors.Errorf("no instance id found for chaos injection")
}
instanceIDList := strings.Split(experimentsDetails.Ec2InstanceID, ",")
if experimentsDetails.Ec2InstanceID == "" || len(instanceIDList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeTargetSelection, Reason: "no EC2 instance ID found to terminate"}
Expand Down
6 changes: 6 additions & 0 deletions chaoslib/litmus/gcp-vm-disk-loss/lib/gcp-vm-disk-loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ func PrepareDiskVolumeLoss(computeService *compute.Service, experimentsDetails *
}

//get the disk volume names list
if strings.TrimSpace(experimentsDetails.DiskVolumeNames) == "" {
return errors.Errorf("no disk volume names found for chaos injection")
}
diskNamesList := strings.Split(experimentsDetails.DiskVolumeNames, ",")

//get the disk zones list
if strings.TrimSpace(experimentsDetails.Zones) == "" {
return errors.Errorf("no zones found for chaos injection")
}
diskZonesList := strings.Split(experimentsDetails.Zones, ",")

//get the device names for the given disks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/litmuschaos/litmus-go/pkg/utils/common"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
"google.golang.org/api/compute/v1"
)

Expand Down Expand Up @@ -46,9 +47,15 @@ func PrepareVMStop(computeService *compute.Service, experimentsDetails *experime
}

// get the instance name or list of instance names
if strings.TrimSpace(experimentsDetails.VMInstanceName) == "" {
return errors.Errorf("no instance name found for chaos injection")
}
instanceNamesList := strings.Split(experimentsDetails.VMInstanceName, ",")

// get the zone name or list of corresponding zones for the instances
if strings.TrimSpace(experimentsDetails.Zones) == "" {
return errors.Errorf("no zone name found for chaos injection")
}
instanceZonesList := strings.Split(experimentsDetails.Zones, ",")

go abortWatcher(computeService, experimentsDetails, instanceNamesList, instanceZonesList, chaosDetails)
Expand Down
12 changes: 8 additions & 4 deletions chaoslib/litmus/network-chaos/helper/netem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package helper

import (
"fmt"
"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/events"
"github.com/palantir/stacktrace"
"os"
"os/exec"
"os/signal"
Expand All @@ -13,6 +10,11 @@ import (
"syscall"
"time"

"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/events"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/network-chaos/types"
"github.com/litmuschaos/litmus-go/pkg/log"
Expand Down Expand Up @@ -367,7 +369,9 @@ func getDestIps(serviceMesh string) []string {
if strings.TrimSpace(destIps) == "" {
return nil
}

if strings.TrimSpace(destIps) == "" {
return errors.Errorf("no destination ips found for chaos injection")
}
ips := strings.Split(strings.TrimSpace(destIps), ",")

// removing duplicates ips from the list, if any
Expand Down
13 changes: 10 additions & 3 deletions chaoslib/litmus/node-drain/lib/node-drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package lib
import (
"context"
"fmt"
"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/palantir/stacktrace"
"os"
"os/exec"
"os/signal"
Expand All @@ -13,6 +11,10 @@ import (
"syscall"
"time"

"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
"github.com/litmuschaos/litmus-go/pkg/events"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/node-drain/types"
Expand Down Expand Up @@ -160,7 +162,9 @@ func drainNode(experimentsDetails *experimentTypes.ExperimentDetails, clients cl

// uncordonNode uncordon the application node
func uncordonNode(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails) error {

if strings.TrimSpace(experimentsDetails.TargetNode) == "" {
return errors.Errorf("no node found to uncordon")
}
targetNodes := strings.Split(experimentsDetails.TargetNode, ",")
for _, targetNode := range targetNodes {

Expand Down Expand Up @@ -188,6 +192,9 @@ func uncordonNode(experimentsDetails *experimentTypes.ExperimentDetails, clients
Times(uint(experimentsDetails.Timeout / experimentsDetails.Delay)).
Wait(time.Duration(experimentsDetails.Delay) * time.Second).
Try(func(attempt uint) error {
if strings.TrimSpace(experimentsDetails.TargetNode) == "" {
return errors.Errorf("no node found to uncordon")
}
targetNodes := strings.Split(experimentsDetails.TargetNode, ",")
for _, targetNode := range targetNodes {
nodeSpec, err := clients.KubeClient.CoreV1().Nodes().Get(context.Background(), targetNode, v1.GetOptions{})
Expand Down
14 changes: 12 additions & 2 deletions chaoslib/litmus/pod-network-partition/lib/network-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package lib

import (
"fmt"
"strings"

"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/clients"
"github.com/palantir/stacktrace"
"strings"
"github.com/pkg/errors"

network_chaos "github.com/litmuschaos/litmus-go/chaoslib/litmus/network-chaos/lib"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/pod-network-partition/types"
Expand Down Expand Up @@ -113,6 +115,9 @@ func (np *NetworkPolicy) setPolicy(policy string) *NetworkPolicy {
// setPodSelector sets the pod labels selector
func (np *NetworkPolicy) setPodSelector(podLabel string) *NetworkPolicy {
podSelector := map[string]string{}
if strings.TrimSpace(podLabel) == "" {
return errors.errorf("pod selector cannot be empty")
}
labels := strings.Split(podLabel, ",")
for i := range labels {
key, value := getKeyValue(labels[i])
Expand All @@ -127,6 +132,9 @@ func (np *NetworkPolicy) setPodSelector(podLabel string) *NetworkPolicy {
// setNamespaceSelector sets the namespace labels selector
func (np *NetworkPolicy) setNamespaceSelector(nsLabel string) *NetworkPolicy {
nsSelector := map[string]string{}
if strings.TrimSpace(nsLabel) == "" {
return errors.errorf("namespace selector cannot be empty")
}
labels := strings.Split(nsLabel, ",")
for i := range labels {
key, value := getKeyValue(labels[i])
Expand Down Expand Up @@ -186,7 +194,9 @@ func (np *NetworkPolicy) setExceptIPs(experimentsDetails *experimentTypes.Experi
if err != nil {
return stacktrace.Propagate(err, "could not get destination ips")
}

if strings.TrimSpace(destinationIPs) == "" {
return errors.Errorf("destination ips cannot be empty")
}
ips := strings.Split(destinationIPs, ",")
var uniqueIps []string
// removing all the duplicates and ipv6 ips from the list, if any
Expand Down
4 changes: 4 additions & 0 deletions chaoslib/litmus/vm-poweroff/lib/vm-poweroff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/utils/common"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/vmware/vm-poweroff/types"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

var inject, abort chan os.Signal
Expand All @@ -42,6 +43,9 @@ func InjectVMPowerOffChaos(experimentsDetails *experimentTypes.ExperimentDetails
}

//Fetching the target VM Ids
if strings.TrimSpace(experimentsDetails.VMIds) == "" {
return errors.Errorf("no vm id found for chaos injection")
}
vmIdList := strings.Split(experimentsDetails.VMIds, ",")

// Calling AbortWatcher go routine, it will continuously watch for the abort signal and generate the required events and result
Expand Down
5 changes: 4 additions & 1 deletion pkg/cloud/aws/ebs/ebs-volume-state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/log"
"github.com/litmuschaos/litmus-go/pkg/utils/retry"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -116,7 +117,9 @@ func GetEBSStatus(ebsVolumeID, ec2InstanceID, region string) (string, error) {

// EBSStateCheckByID will check the attachment state of the given volume
func EBSStateCheckByID(volumeIDs, region string) error {

if strings.TrimSpace(volumeIDs) == "" {
return errors.Errorf("no volumeID provided, please provide a volume to detach")
}
volumeIDList := strings.Split(volumeIDs, ",")
if len(volumeIDList) == 0 {
return cerrors.Error{
Expand Down
5 changes: 4 additions & 1 deletion pkg/cloud/aws/ec2/ec2-instance-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/cloud/aws/common"
"github.com/litmuschaos/litmus-go/pkg/log"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
)

// GetEC2InstanceStatus will verify and give the ec2 instance details along with ebs volume idetails.
Expand Down Expand Up @@ -51,7 +52,9 @@ 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 {

if strings.TrimSpace(instanceID) == "" {
return errors.Errorf("no instance id provided to terminate")
}
instanceIDList := strings.Split(instanceID, ",")
if instanceID == "" || len(instanceIDList) == 0 {
return cerrors.Error{
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloud/azure/instance/instance-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/cloud/azure/common"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"

"github.com/litmuschaos/litmus-go/pkg/log"
)
Expand Down Expand Up @@ -97,6 +98,9 @@ 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 {
if strings.TrimSpace(azureInstanceNames) == "" {
return errors.Errorf("no instance provided")
}
instanceNameList := strings.Split(azureInstanceNames, ",")
if azureInstanceNames == "" || len(instanceNameList) == 0 {
return cerrors.Error{
Expand Down
13 changes: 10 additions & 3 deletions pkg/cloud/gcp/disk-volume-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/litmuschaos/litmus-go/pkg/log"
"github.com/litmuschaos/litmus-go/pkg/utils/retry"
"github.com/palantir/stacktrace"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/api/compute/v1"
)
Expand Down Expand Up @@ -104,12 +105,16 @@ func DiskVolumeStateCheck(computeService *compute.Service, experimentsDetails *e
if experimentsDetails.GCPProjectID == "" {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{projectId: %s}", experimentsDetails.GCPProjectID), Reason: "no gcp project id provided, please provide the project id"}
}

if strings.TrimSpace(experimentsDetails.DiskVolumeNames) == "" {
return errors.Errorf("no disk name provided, please provide the name of the disk")
}
diskNamesList := strings.Split(experimentsDetails.DiskVolumeNames, ",")
if len(diskNamesList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{diskNames: %v}", diskNamesList), Reason: "no disk name provided, please provide the name of the disk"}
}

if strings.TrimSpace(experimentsDetails.Zones) == "" {
return errors.Errorf("no zone provided, please provide the zone of the disk")
}
zonesList := strings.Split(experimentsDetails.Zones, ",")
if len(zonesList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{zones: %v}", zonesList), Reason: "no zone provided, please provide the zone of the disk"}
Expand All @@ -131,7 +136,9 @@ func DiskVolumeStateCheck(computeService *compute.Service, experimentsDetails *e

// SetTargetDiskInstanceNames fetches the vm instances to which the disks are attached
func SetTargetDiskInstanceNames(computeService *compute.Service, experimentsDetails *experimentTypes.ExperimentDetails) error {

if strings.TrimSpace(experimentsDetails.DiskVolumeNames) == "" {
return errors.Errorf("no disk name provided, please provide the name of the disk")
}
diskNamesList := strings.Split(experimentsDetails.DiskVolumeNames, ",")
zonesList := strings.Split(experimentsDetails.Zones, ",")

Expand Down
9 changes: 7 additions & 2 deletions pkg/cloud/gcp/vm-instance-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/log"
"github.com/pkg/errors"
"google.golang.org/api/compute/v1"
)

Expand All @@ -26,9 +27,13 @@ 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 strings.TrimSpace(instanceNames) == "" {
return errors.Errorf("no instance provided")
}
instanceNamesList := strings.Split(instanceNames, ",")

if strings.TrimSpace(instanceZones) == "" {
return errors.Errorf("no zone provided")
}
instanceZonesList := strings.Split(instanceZones, ",")

if managedInstanceGroup != "enable" && managedInstanceGroup != "disable" {
Expand Down
Loading