Skip to content

Commit

Permalink
chore(unit): Adding units to the duration fields
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <shubham.chaudhary@harness.io>
  • Loading branch information
ispeakc0de committed Apr 18, 2023
1 parent 0bbe8e2 commit a1a3b03
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 101 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/aws/aws-sdk-go v1.38.59
github.com/containerd/cgroups v1.0.1
github.com/kyokomi/emoji v2.2.4+incompatible
github.com/litmuschaos/chaos-operator v0.0.0-20230413151351-184224e2d2e9
github.com/litmuschaos/chaos-operator v0.0.0-20230418072131-5ea32522f048
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kyokomi/emoji v2.2.4+incompatible h1:np0woGKwx9LiHAQmwZx79Oc0rHpNw3o+3evou4BEPv4=
github.com/kyokomi/emoji v2.2.4+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA=
github.com/litmuschaos/chaos-operator v0.0.0-20230413151351-184224e2d2e9 h1:g7LFJ2VuERTpv68pR0HeStVtH2sB7rfCKJOjcCIGW34=
github.com/litmuschaos/chaos-operator v0.0.0-20230413151351-184224e2d2e9/go.mod h1:jRA6jKGed6ytLDJ7897yr2Kr2ygg+cuRXJqwvNmE4Bw=
github.com/litmuschaos/chaos-operator v0.0.0-20230418072131-5ea32522f048 h1:JcNUqCgjD+xHxYlTrhaSRLGYSN+0N3OfI++TmP0HXPk=
github.com/litmuschaos/chaos-operator v0.0.0-20230418072131-5ea32522f048/go.mod h1:jRA6jKGed6ytLDJ7897yr2Kr2ygg+cuRXJqwvNmE4Bw=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down
5 changes: 5 additions & 0 deletions pkg/cerrors/custom_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
FailureTypeHttpProbe ErrorType = "HTTP_PROBE_FAILURE"
ErrorTypePromProbe ErrorType = "PROM_PROBE_ERROR"
FailureTypePromProbe ErrorType = "PROM_PROBE_FAILURE"
ErrorTypeTimeout ErrorType = "TIMEOUT"
)

type userFriendly interface {
Expand All @@ -46,6 +47,10 @@ func GetErrorType(err error) ErrorType {
if ufe, ok := err.(userFriendly); ok {
return ufe.ErrorType()
}
rootCause := stacktrace.RootCause(err)
if ufe, ok := rootCause.(userFriendly); ok {
return ufe.ErrorType()
}
return ErrorTypeNonUserFriendly
}

Expand Down
70 changes: 40 additions & 30 deletions pkg/probe/cmdprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func prepareCmdProbe(probe v1alpha1.ProbeAttributes, clients clients.ClientSets,

// triggerInlineCmdProbe trigger the cmd probe and storing the output into the out buffer
func triggerInlineCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.ResultDetails) error {
probeTimeout := getProbeTimeouts(probe.Name, resultDetails.ProbeDetails)
var description string

// It parses the templated command and return normal string
Expand All @@ -68,8 +69,8 @@ func triggerInlineCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.
// it contains a timeout per iteration of retry. if the timeout expires without success then it will go to next try
// for a timeout, it will run the command, if it fails wait for the interval and again execute the command until timeout expires
if err := retry.Times(uint(getAttempts(probe.RunProperties.Attempt, probe.RunProperties.Retry))).
Timeout(int64(probe.RunProperties.ProbeTimeout)).
Wait(time.Duration(probe.RunProperties.Interval) * time.Millisecond).
Timeout(probeTimeout.ProbeTimeout).
Wait(probeTimeout.Interval).
TryWithTimeout(func(attempt uint) error {
var out, stdErr bytes.Buffer
// run the inline command probe
Expand Down Expand Up @@ -98,7 +99,7 @@ func triggerInlineCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.
resultDetails.ProbeArtifacts[probe.Name] = probes
return nil
}); err != nil {
return err
return checkProbeTimeoutError(probe.Name, cerrors.FailureTypeCmdProbe, err)
}

setProbeDescription(resultDetails, probe, description)
Expand All @@ -108,6 +109,7 @@ func triggerInlineCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.
// triggerSourceCmdProbe trigger the cmd probe inside the external pod
func triggerSourceCmdProbe(probe v1alpha1.ProbeAttributes, execCommandDetails litmusexec.PodDetails, clients clients.ClientSets, resultDetails *types.ResultDetails) error {
var description string
probeTimeout := getProbeTimeouts(probe.Name, resultDetails.ProbeDetails)

// It parses the templated command and return normal string
// if command doesn't have template, it will return the same command
Expand All @@ -121,8 +123,8 @@ func triggerSourceCmdProbe(probe v1alpha1.ProbeAttributes, execCommandDetails li
// it contains a timeout per iteration of retry. if the timeout expires without success then it will go to next try
// for a timeout, it will run the command, if it fails wait for the interval and again execute the command until timeout expires
if err := retry.Times(uint(getAttempts(probe.RunProperties.Attempt, probe.RunProperties.Retry))).
Timeout(int64(probe.RunProperties.ProbeTimeout)).
Wait(time.Duration(probe.RunProperties.Interval) * time.Millisecond).
Timeout(probeTimeout.ProbeTimeout).
Wait(probeTimeout.Interval).
TryWithTimeout(func(attempt uint) error {
command := append([]string{"/bin/sh", "-c"}, probe.CmdProbeInputs.Command)
// exec inside the external pod to get the o/p of given command
Expand All @@ -148,7 +150,7 @@ func triggerSourceCmdProbe(probe v1alpha1.ProbeAttributes, execCommandDetails li
resultDetails.ProbeArtifacts[probe.Name] = probes
return nil
}); err != nil {
return err
return checkProbeTimeoutError(probe.Name, cerrors.FailureTypeCmdProbe, err)
}

setProbeDescription(resultDetails, probe, description)
Expand Down Expand Up @@ -320,11 +322,13 @@ func deleteProbePod(chaosDetails *types.ChaosDetails, clients clients.ClientSets

// triggerInlineContinuousCmdProbe trigger the inline continuous cmd probes
func triggerInlineContinuousCmdProbe(probe v1alpha1.ProbeAttributes, clients clients.ClientSets, chaosresult *types.ResultDetails, chaosDetails *types.ChaosDetails) {
probeTimeout := getProbeTimeouts(probe.Name, chaosresult.ProbeDetails)

var isExperimentFailed bool
// waiting for initial delay
if probe.RunProperties.InitialDelaySeconds != 0 {
log.Infof("[Wait]: Waiting for %vs before probe execution", probe.RunProperties.InitialDelaySeconds)
time.Sleep(time.Duration(probe.RunProperties.InitialDelaySeconds) * time.Second)
if probeTimeout.InitialDelay != 0 {
log.Infof("[Wait]: Waiting for %v duration before probe execution", probe.RunProperties.InitialDelay)
time.Sleep(probeTimeout.InitialDelay)
}

// it trigger the inline cmd probe for the entire duration of chaos and it fails, if any err encounter
Expand All @@ -346,7 +350,7 @@ loop:
}
}
// waiting for the probe polling interval
time.Sleep(time.Duration(probe.RunProperties.ProbePollingInterval) * time.Second)
time.Sleep(probeTimeout.ProbePollingInterval)
}
// if experiment fails and stopOnfailure is provided as true then it will patch the chaosengine for abort
// if experiment fails but stopOnfailure is provided as false then it will continue the execution
Expand All @@ -360,13 +364,15 @@ loop:

// triggerInlineOnChaosCmdProbe trigger the inline onchaos cmd probes
func triggerInlineOnChaosCmdProbe(probe v1alpha1.ProbeAttributes, clients clients.ClientSets, chaosresult *types.ResultDetails, chaosDetails *types.ChaosDetails) {
probeTimeout := getProbeTimeouts(probe.Name, chaosresult.ProbeDetails)

var isExperimentFailed bool
duration := chaosDetails.ChaosDuration
// waiting for initial delay
if probe.RunProperties.InitialDelaySeconds != 0 {
log.Infof("[Wait]: Waiting for %vs before probe execution", probe.RunProperties.InitialDelaySeconds)
time.Sleep(time.Duration(probe.RunProperties.InitialDelaySeconds) * time.Second)
duration = math.Maximum(0, duration-probe.RunProperties.InitialDelaySeconds)
if probeTimeout.InitialDelay != 0 {
log.Infof("[Wait]: Waiting for %v before probe execution", probe.RunProperties.InitialDelay)
time.Sleep(probeTimeout.InitialDelay)
duration = math.Maximum(0, duration-int(probeTimeout.InitialDelay.Seconds()))
}

var endTime <-chan time.Time
Expand Down Expand Up @@ -397,7 +403,7 @@ loop:
}
}
// waiting for the probe polling interval
time.Sleep(time.Duration(probe.RunProperties.ProbePollingInterval) * time.Second)
time.Sleep(probeTimeout.ProbePollingInterval)
}
}
// if experiment fails and stopOnfailure is provided as true then it will patch the chaosengine for abort
Expand All @@ -412,14 +418,15 @@ loop:

// triggerSourceOnChaosCmdProbe trigger the onchaos cmd probes having need some external source image
func triggerSourceOnChaosCmdProbe(probe v1alpha1.ProbeAttributes, execCommandDetails litmusexec.PodDetails, clients clients.ClientSets, chaosresult *types.ResultDetails, chaosDetails *types.ChaosDetails) {
probeTimeout := getProbeTimeouts(probe.Name, chaosresult.ProbeDetails)

var isExperimentFailed bool
duration := chaosDetails.ChaosDuration
// waiting for initial delay
if probe.RunProperties.InitialDelaySeconds != 0 {
log.Infof("[Wait]: Waiting for %vs before probe execution", probe.RunProperties.InitialDelaySeconds)
time.Sleep(time.Duration(probe.RunProperties.InitialDelaySeconds) * time.Second)
duration = math.Maximum(0, duration-probe.RunProperties.InitialDelaySeconds)
if probeTimeout.InitialDelay != 0 {
log.Infof("[Wait]: Waiting for %v before probe execution", probe.RunProperties.InitialDelay)
time.Sleep(probeTimeout.InitialDelay)
duration = math.Maximum(0, duration-int(probeTimeout.InitialDelay))
}

endTime := time.After(time.Duration(duration) * time.Second)
Expand Down Expand Up @@ -448,7 +455,7 @@ loop:
}
}
// waiting for the probe polling interval
time.Sleep(time.Duration(probe.RunProperties.ProbePollingInterval) * time.Second)
time.Sleep(probeTimeout.ProbePollingInterval)
}
}
// if experiment fails and stopOnfailure is provided as true then it will patch the chaosengine for abort
Expand All @@ -464,12 +471,13 @@ loop:

// triggerSourceContinuousCmdProbe trigger the continuous cmd probes having need some external source image
func triggerSourceContinuousCmdProbe(probe v1alpha1.ProbeAttributes, execCommandDetails litmusexec.PodDetails, clients clients.ClientSets, chaosresult *types.ResultDetails, chaosDetails *types.ChaosDetails) {
probeTimeout := getProbeTimeouts(probe.Name, chaosresult.ProbeDetails)

var isExperimentFailed bool
// waiting for initial delay
if probe.RunProperties.InitialDelaySeconds != 0 {
log.Infof("[Wait]: Waiting for %vs before probe execution", probe.RunProperties.InitialDelaySeconds)
time.Sleep(time.Duration(probe.RunProperties.InitialDelaySeconds) * time.Second)
if probeTimeout.InitialDelay != 0 {
log.Infof("[Wait]: Waiting for %v before probe execution", probe.RunProperties.InitialDelay)
time.Sleep(probeTimeout.InitialDelay)
}

// it trigger the cmd probe for the entire duration of chaos and it fails, if any err encounter
Expand All @@ -491,7 +499,7 @@ loop:
}
}
// waiting for the probe polling interval
time.Sleep(time.Duration(probe.RunProperties.ProbePollingInterval) * time.Second)
time.Sleep(probeTimeout.ProbePollingInterval)
}
// if experiment fails and stopOnfailure is provided as true then it will patch the chaosengine for abort
// if experiment fails but stopOnfailure is provided as false then it will continue the execution
Expand Down Expand Up @@ -535,6 +543,7 @@ func validateResult(comparator v1alpha1.ComparatorInfo, probeName, cmdOutput str

// preChaosCmdProbe trigger the cmd probe for prechaos phase
func preChaosCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.ResultDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails) error {
probeTimeout := getProbeTimeouts(probe.Name, resultDetails.ProbeDetails)

switch probe.Mode {
case "SOT", "Edge":
Expand All @@ -551,9 +560,9 @@ func preChaosCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.Resul
})

// waiting for initial delay
if probe.RunProperties.InitialDelaySeconds != 0 {
log.Infof("[Wait]: Waiting for %vs before probe execution", probe.RunProperties.InitialDelaySeconds)
time.Sleep(time.Duration(probe.RunProperties.InitialDelaySeconds) * time.Second)
if probeTimeout.InitialDelay != 0 {
log.Infof("[Wait]: Waiting for %v before probe execution", probe.RunProperties.InitialDelay)
time.Sleep(probeTimeout.InitialDelay)
}

// triggering the cmd probe for the inline mode
Expand Down Expand Up @@ -625,6 +634,7 @@ func preChaosCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.Resul

// postChaosCmdProbe trigger cmd probe for post chaos phase
func postChaosCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.ResultDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails) error {
probeTimeout := getProbeTimeouts(probe.Name, resultDetails.ProbeDetails)

switch probe.Mode {
case "EOT", "Edge":
Expand All @@ -641,9 +651,9 @@ func postChaosCmdProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.Resu
})

// waiting for initial delay
if probe.RunProperties.InitialDelaySeconds != 0 {
log.Infof("[Wait]: Waiting for %vs before probe execution", probe.RunProperties.InitialDelaySeconds)
time.Sleep(time.Duration(probe.RunProperties.InitialDelaySeconds) * time.Second)
if probeTimeout.InitialDelay != 0 {
log.Infof("[Wait]: Waiting for %v before probe execution", probe.RunProperties.InitialDelay)
time.Sleep(probeTimeout.InitialDelay)
}

// triggering the cmd probe for the inline mode
Expand Down
2 changes: 1 addition & 1 deletion pkg/probe/comparator/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (model Model) CompareInt(errorCode cerrors.ErrorType) error {
}
case "==":
if !obj.isEqual() {
return cerrors.Error{ErrorCode: errorCode, Target: model.probeName, Reason: fmt.Sprintf("Actual value: %v. Expected value: should not be equal to %v", obj.a, obj.b)}
return cerrors.Error{ErrorCode: errorCode, Target: model.probeName, Reason: fmt.Sprintf("Actual value: %v. Expected value: should be equal to %v", obj.a, obj.b)}
}
case "!=":
if !obj.isNotEqual() {
Expand Down

0 comments on commit a1a3b03

Please sign in to comment.