Skip to content

Commit

Permalink
Merge branch 'master' into fixed-comma-split
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaym07 authored Sep 24, 2022
2 parents 7813ef9 + a83f346 commit 2797b6a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 39 deletions.
3 changes: 1 addition & 2 deletions chaoslib/litmus/spring-boot-chaos/lib/spring-boot-chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

corev1 "k8s.io/api/core/v1"

"github.com/litmuschaos/litmus-go/pkg/clients"
"github.com/litmuschaos/litmus-go/pkg/events"
"github.com/litmuschaos/litmus-go/pkg/log"
Expand Down
23 changes: 14 additions & 9 deletions chaoslib/litmus/stress-chaos/helper/stress-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (
const (
// ProcessAlreadyFinished contains error code when process is finished
ProcessAlreadyFinished = "os: process already finished"
// ProcessAlreadyKilled contains error code when process is already killed
ProcessAlreadyKilled = "no such process"
)

// Helper injects the stress chaos
Expand Down Expand Up @@ -125,6 +127,8 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c

// launch the stress-ng process on the target container in paused mode
cmd := exec.Command("/bin/bash", "-c", stressCommand)
// enables the process group id
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
var buf bytes.Buffer
cmd.Stdout = &buf
err = cmd.Start()
Expand Down Expand Up @@ -183,16 +187,18 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c
err, ok := err.(*exec.ExitError)
if ok {
status := err.Sys().(syscall.WaitStatus)
if status.Signaled() && status.Signal() == syscall.SIGTERM {
if status.Signaled() && status.Signal() == syscall.SIGKILL {
// wait for the completion of abort handler
time.Sleep(10 * time.Second)
return errors.Errorf("process stopped with SIGTERM signal")
return errors.Errorf("process stopped with SIGKILL signal")
}
}
return errors.Errorf("process exited before the actual cleanup, err: %v", err)
}
log.Info("[Info]: Chaos injection completed")
terminateProcess(cmd.Process.Pid)
if err := terminateProcess(cmd.Process.Pid); err != nil {
return err
}
if err = result.AnnotateChaosResult(resultDetails.Name, chaosDetails.ChaosNamespace, "reverted", "pod", experimentsDetails.TargetPods); err != nil {
return err
}
Expand All @@ -203,12 +209,11 @@ func prepareStressChaos(experimentsDetails *experimentTypes.ExperimentDetails, c

//terminateProcess will remove the stress process from the target container after chaos completion
func terminateProcess(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
return errors.Errorf("unreachable path, err: %v", err)
}
if err = process.Signal(syscall.SIGTERM); err != nil && err.Error() != ProcessAlreadyFinished {
return errors.Errorf("error while killing process, err: %v", err)
if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil {
if strings.Contains(err.Error(), ProcessAlreadyKilled) || strings.Contains(err.Error(), ProcessAlreadyFinished) {
return nil
}
return err
}
log.Info("[Info]: Stress process removed successfully")
return nil
Expand Down
4 changes: 1 addition & 3 deletions pkg/probe/cmdprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,12 @@ func triggerSourceOnChaosCmdProbe(probe v1alpha1.ProbeAttributes, execCommandDet
duration = math.Maximum(0, duration-probe.RunProperties.InitialDelaySeconds)
}

var endTime <-chan time.Time
timeDelay := time.Duration(duration) * time.Second
endTime := time.After(time.Duration(duration) * time.Second)

// it trigger the cmd probe for the entire duration of chaos and it fails, if any err encounter
// it marked the error for the probes, if any
loop:
for {
endTime = time.After(timeDelay)
select {
case <-endTime:
log.Infof("[Chaos]: Time is up for the %v probe", probe.Name)
Expand Down
4 changes: 1 addition & 3 deletions pkg/probe/httpprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,12 @@ func triggerOnChaosHTTPProbe(probe v1alpha1.ProbeAttributes, clients clients.Cli
duration = math.Maximum(0, duration-probe.RunProperties.InitialDelaySeconds)
}

var endTime <-chan time.Time
timeDelay := time.Duration(duration) * time.Second
endTime := time.After(time.Duration(duration) * time.Second)

// it trigger the http probe for the entire duration of chaos and it fails, if any error encounter
// it marked the error for the probes, if any
loop:
for {
endTime = time.After(timeDelay)
select {
case <-endTime:
log.Infof("[Chaos]: Time is up for the %v probe", probe.Name)
Expand Down
6 changes: 2 additions & 4 deletions pkg/probe/k8sprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,12 @@ func triggerOnChaosK8sProbe(probe v1alpha1.ProbeAttributes, clients clients.Clie
duration = math.Maximum(0, duration-probe.RunProperties.InitialDelaySeconds)
}

var endTime <-chan time.Time
timeDelay := time.Duration(duration) * time.Second
endTime := time.After(time.Duration(duration) * time.Second)

// it trigger the k8s probe for the entire duration of chaos and it fails, if any error encounter
// it triggers the k8s probe for the entire duration of chaos and it fails, if any error encounter
// marked the error for the probes, if any
loop:
for {
endTime = time.After(timeDelay)
select {
case <-endTime:
log.Infof("[Chaos]: Time is up for the %v probe", probe.Name)
Expand Down
45 changes: 32 additions & 13 deletions pkg/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@ func RunProbes(chaosDetails *types.ChaosDetails, clients clients.ClientSets, res
}

switch strings.ToLower(phase) {
//execute probes for the prechaos & duringchaos phase
case "prechaos", "duringchaos":
//execute probes for the prechaos phase
case "prechaos":
for _, probe := range probes {
if err := execute(probe, chaosDetails, clients, resultDetails, phase); err != nil {
return err
switch strings.ToLower(probe.Mode) {
case "sot", "edge", "continuous":
if err := execute(probe, chaosDetails, clients, resultDetails, phase); err != nil {
return err
}
}
}
//execute probes for the duringchaos phase
case "duringchaos":
for _, probe := range probes {
if strings.ToLower(probe.Mode) == "onchaos" {
if err := execute(probe, chaosDetails, clients, resultDetails, phase); err != nil {
return err
}
}
}
default:
Expand All @@ -58,8 +70,11 @@ func RunProbes(chaosDetails *types.ChaosDetails, clients clients.ClientSets, res
}
// executes the eot and edge modes
for _, probe := range probes {
if err := execute(probe, chaosDetails, clients, resultDetails, phase); err != nil {
return err
switch strings.ToLower(probe.Mode) {
case "eot", "edge":
if err := execute(probe, chaosDetails, clients, resultDetails, phase); err != nil {
return err
}
}
}
}
Expand Down Expand Up @@ -137,7 +152,6 @@ func InitializeProbesInChaosResultDetails(chaosDetails *types.ChaosDetails, clie
tempProbe.Name = probe.Name
tempProbe.Type = probe.Type
tempProbe.Mode = probe.Mode
tempProbe.Phase = "N/A"
tempProbe.RunCount = 0
tempProbe.Status = v1alpha1.ProbeStatus{
Verdict: "Awaited",
Expand Down Expand Up @@ -204,12 +218,8 @@ func markedVerdictInEnd(err error, resultDetails *types.ResultDetails, probe v1a
// counting the passed probes count to generate the score and mark the verdict as passed
// for edge, probe is marked as Passed if passed in both pre/post chaos checks
switch strings.ToLower(probe.Mode) {
case "edge", "continuous":
if phase != "PreChaos" {
resultDetails.PassedProbeCount++
}
case "onchaos":
if phase != "DuringChaos" {
case "edge":
if phase == "PostChaos" && getProbeVerdict(resultDetails, probe.Name, probe.Type) != v1alpha1.ProbeVerdictFailed {
resultDetails.PassedProbeCount++
}
default:
Expand Down Expand Up @@ -313,3 +323,12 @@ func execute(probe v1alpha1.ProbeAttributes, chaosDetails *types.ChaosDetails, c
}
return nil
}

func getProbeVerdict(resultDetails *types.ResultDetails, name, probeType string) v1alpha1.ProbeVerdict {
for _, probe := range resultDetails.ProbeDetails {
if probe.Name == name && probe.Type == probeType {
return probe.Status.Verdict
}
}
return v1alpha1.ProbeVerdictNA
}
4 changes: 1 addition & 3 deletions pkg/probe/promProbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,12 @@ func triggerOnChaosPromProbe(probe v1alpha1.ProbeAttributes, clients clients.Cli
duration = math.Maximum(0, duration-probe.RunProperties.InitialDelaySeconds)
}

var endTime <-chan time.Time
timeDelay := time.Duration(duration) * time.Second
endTime := time.After(time.Duration(duration) * time.Second)

// it trigger the prom probe for the entire duration of chaos and it fails, if any err encounter
// it marked the error for the probes, if any
loop:
for {
endTime = time.After(timeDelay)
select {
case <-endTime:
log.Infof("[Chaos]: Time is up for the %v probe", probe.Name)
Expand Down
3 changes: 2 additions & 1 deletion pkg/result/chaosresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func GetProbeStatus(resultDetails *types.ResultDetails) (bool, []v1alpha1.ProbeS
probes.Mode = probe.Mode
probes.Status = probe.Status
probeStatus = append(probeStatus, probes)
if probe.Phase == "Failed" {
if probe.Status.Verdict == v1alpha1.ProbeVerdictFailed {
isAllProbePassed = false
}
}
Expand Down Expand Up @@ -165,6 +165,7 @@ func updateResultAttributes(clients clients.ClientSets, chaosDetails *types.Chao
if !isAllProbePassed {
resultDetails.Verdict = "Fail"
result.Status.ExperimentStatus.Verdict = "Fail"
result.Status.ExperimentStatus.FailStep = "Probe execution result didn't met the passing criteria"
}
switch strings.ToLower(string(resultDetails.Verdict)) {
case "pass":
Expand Down
1 change: 0 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type ProbeDetails struct {
Name string
Type string
Mode string
Phase string
Status v1alpha1.ProbeStatus
IsProbeFailedWithError error
RunID string
Expand Down

0 comments on commit 2797b6a

Please sign in to comment.