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

Fix build pipeline and test cases for chaos-operator and litmus-go #407

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
4 changes: 2 additions & 2 deletions apps/nginx/nginx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ spec:
- containerPort: 80
resources:
requests:
ephemeral-storage: "1Gi"
limits:
ephemeral-storage: "2Gi"
limits:
ephemeral-storage: "3Gi"
---
apiVersion: v1
kind: Service
Expand Down
9 changes: 7 additions & 2 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ RUN apk --update add \
sshpass\
build-base

# Update pip version
RUN python3 -m pip install pygithub
# Update pip version and install pygithub in a virtual environment
RUN python3 -m venv /venv \
&& source /venv/bin/activate \
&& pip install --upgrade pip \
&& pip install pygithub \
&& deactivate

# change default shell from ash to bash
RUN sed -i -e "s/bin\/ash/bin\/bash/" /etc/passwd

Expand Down
8 changes: 4 additions & 4 deletions components/operator/admin-mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestAdminModeTest(t *testing.T) {
RunSpecs(t, "BDD test")
}

//BDD Tests for testing admin mode functionality
//Checking for the creation of runner pod for application in same namespace
// BDD Tests for testing admin mode functionality
// Checking for the creation of runner pod for application in same namespace
var _ = Describe("BDD of operator reconcile resiliency check", func() {

// BDD TEST CASE 1
Expand Down Expand Up @@ -75,7 +75,7 @@ var _ = Describe("BDD of operator reconcile resiliency check", func() {
//Installing Chaos Engine for container-kill
By("[Install]: Installing chaos engine")
testsDetails.ChaosServiceAccount = "litmus-admin"
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, clients)
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, types.SetupAppInfoIfEmpty, clients)
Expect(err).To(BeNil(), "Failed to install chaosengine due to {%v}", err)

//Checking runner pod running state
Expand Down Expand Up @@ -144,7 +144,7 @@ var _ = Describe("BDD of operator reconcile resiliency check", func() {
//Installing Chaos Engine for container-kill
By("[Install]: Installing chaos engine")
testsDetails.ChaosServiceAccount = "litmus-admin"
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, clients)
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, types.SetupAppInfoIfEmpty, clients)
Expect(err).To(BeNil(), "Failed to install chaos experiment due to {%v}", err)

//Checking runner pod running state
Expand Down
10 changes: 5 additions & 5 deletions components/operator/reconcile-resiliency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func TestReconcileResiliency(t *testing.T) {
RunSpecs(t, "BDD test")
}

//BDD Tests for operator reconcile resiliency
//Checking for the creation of runner pod for application in same namespace
// BDD Tests for operator reconcile resiliency
// Checking for the creation of runner pod for application in same namespace
var _ = Describe("BDD of operator reconcile resiliency check", func() {

// BDD TEST CASE 1
Expand Down Expand Up @@ -66,7 +66,7 @@ var _ = Describe("BDD of operator reconcile resiliency check", func() {
Expect(err).To(BeNil(), "create testapp2 deployment due to {%v}", err)

//Waiting for deployment to get ready
err = pkg.DeploymentStatusCheck(&testsDetails, "testapp2", "default", clients)
err = pkg.DeploymentStatusCheck(&testsDetails, "testapp2", "litmus", clients)
Expect(err).To(BeNil(), "Error Timeout due to {%v}", err)

////////////////////////////////////////////////////////
Expand Down Expand Up @@ -109,7 +109,7 @@ var _ = Describe("BDD of operator reconcile resiliency check", func() {
//Creating Chaos-Engine for container-kill
By("[Install]: Install Chaos Engine for container-kill")
testsDetails.AppLabel = "run=testapp2"
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, clients)
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, types.SetupAppInfoIfNotEmpty, clients)
Expect(err).To(BeNil(), "Failed to install chaosengine due to {%v}", err)

//Checking the runner pod status
Expand All @@ -128,7 +128,7 @@ var _ = Describe("BDD of operator reconcile resiliency check", func() {
By("[Install]: Install Chaos Engine for pod-delete")
testsDetails.AppLabel = "run=testapp1"
testsDetails.AppNS = "default"
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, clients)
err = pkg.InstallGoChaosEngine(&testsDetails, &chaosEngine, testsDetails.ChaosNamespace, types.SetupAppInfoIfNotEmpty, clients)
Expect(err).To(BeNil(), "Failed to install chaosengine due to {%v}", err)

// Checking the runner pod status
Expand Down
4 changes: 2 additions & 2 deletions pkg/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Kubectl(command ...string) error {
return nil
}

//PrepareChaos install all the Chaos resourses like rbac, experiment and engine
// PrepareChaos install all the Chaos resourses like rbac, experiment and engine
func PrepareChaos(testsDetails *types.TestDetails, chaosExperiment *v1alpha1.ChaosExperiment, chaosEngine *v1alpha1.ChaosEngine, clients environment.ClientSets, annotation bool) error {

testsDetails.AnnotationCheck = strconv.FormatBool(annotation)
Expand All @@ -60,7 +60,7 @@ func PrepareChaos(testsDetails *types.TestDetails, chaosExperiment *v1alpha1.Cha

//Installing Chaos Engine
log.Info("[Install]: Installing chaos engine")
err = InstallGoChaosEngine(testsDetails, chaosEngine, testsDetails.ChaosNamespace, clients)
err = InstallGoChaosEngine(testsDetails, chaosEngine, testsDetails.ChaosNamespace, types.SetupAppInfoIfEmpty, clients)
if err != nil {
return errors.Errorf("Failed to install chaosengine due to {%v}", err)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
types "github.com/litmuschaos/litmus-e2e/pkg/types"
)

//GetENV fetches all the env variables from the runner pod
// GetENV fetches all the env variables from the runner pod
func GetENV(testDetails *types.TestDetails, expName, engineName string) {
testDetails.ExperimentName = expName
testDetails.EngineName = engineName
testDetails.OperatorName = Getenv("OPERATOR_NAME", "chaos-operator-ce")
testDetails.ChaosNamespace = Getenv("CHAOS_NAMESPACE", "litmus")
testDetails.AppNS = Getenv("APP_NS", "litmus")
testDetails.AppLabel = Getenv("APP_LABEL", "run=nginx")
testDetails.Appkind = Getenv("APP_KIND", "deployment")
testDetails.JobCleanUpPolicy = Getenv("JOB_CLEANUP_POLICY", "retain")
testDetails.AnnotationCheck = Getenv("ANNOTATION_CHECK", "false")
testDetails.ApplicationNodeName = Getenv("APPLICATION_NODE_NAME", "")
Expand Down Expand Up @@ -60,9 +61,9 @@ func GetENV(testDetails *types.TestDetails, expName, engineName string) {
testDetails.LibImage = Getenv("LIB_IMAGE", "")

// All Links for running chaos testing
testDetails.RbacPath = Getenv("RBAC_PATH", "https://hub.litmuschaos.io/api/chaos/master?file=charts/generic/"+expName+"/rbac.yaml")
testDetails.ExperimentPath = Getenv("EXPERIMENT_PATH", "https://hub.litmuschaos.io/api/chaos/master?file=charts/generic/"+expName+"/experiment.yaml")
testDetails.EnginePath = Getenv("ENGINE_PATH", "https://raw.githubusercontent.com/litmuschaos/chaos-charts/master/charts/generic/"+expName+"/engine.yaml")
testDetails.RbacPath = Getenv("RBAC_PATH", "https://litmuschaos.github.io/litmus/litmus-admin-rbac.yaml")
testDetails.ExperimentPath = Getenv("EXPERIMENT_PATH", "https://hub.litmuschaos.io/api/chaos/master?file=faults/kubernetes/"+expName+"/fault.yaml")
testDetails.EnginePath = Getenv("ENGINE_PATH", "https://raw.githubusercontent.com/litmuschaos/chaos-charts/master/faults/kubernetes/"+expName+"/engine.yaml")
testDetails.AnsibleRbacPath = Getenv("ANSIBLE_RBAC_PATH", "https://raw.githubusercontent.com/litmuschaos/chaos-charts/master/charts/generic/"+expName+"/ansible/rbac.yaml")
testDetails.AnsibleExperimentPath = Getenv("ANSIBLE_EXPERIMENT_PATH", "https://raw.githubusercontent.com/litmuschaos/chaos-charts/master/charts/generic/"+expName+"/ansible/experiment.yaml")
testDetails.AnsibleEnginePath = Getenv("ANSIBLE_ENGINE_PATH", "https://raw.githubusercontent.com/litmuschaos/chaos-charts/master/charts/generic/"+expName+"/ansible/engine.yaml")
Expand Down
36 changes: 22 additions & 14 deletions pkg/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

var err error

//CreateChaosResource creates litmus components with given inputs
// CreateChaosResource creates litmus components with given inputs
func CreateChaosResource(testsDetails *types.TestDetails, fileData []byte, namespace string, clients environment.ClientSets) error {

decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(fileData), 100)
Expand Down Expand Up @@ -105,7 +105,7 @@ func UpdateEngine(testsDetails *types.TestDetails, clients environment.ClientSet
engine.Spec.EngineState = v1alpha1.EngineStateActive

// Set all environments
setEngineVar(engine, testsDetails)
setEngineVar(engine, testsDetails, types.SetupAppInfoIfNotEmpty)

_, err = clients.LitmusClient.ChaosEngines(testsDetails.ChaosNamespace).Update(engine)
if err != nil {
Expand All @@ -131,7 +131,7 @@ func UpdateExperiment(testsDetails *types.TestDetails, clients environment.Clien
return nil
}

//InstallGoRbac installs and configure rbac for running go based chaos
// InstallGoRbac installs and configure rbac for running go based chaos
func InstallGoRbac(testsDetails *types.TestDetails, rbacNamespace string) error {

//Fetch RBAC file
Expand All @@ -141,11 +141,15 @@ func InstallGoRbac(testsDetails *types.TestDetails, rbacNamespace string) error
}
//Modify Namespace field of the RBAC
if rbacNamespace != "" {
err = EditFile("/tmp/"+testsDetails.ExperimentName+"-sa.yaml", "namespace: default", "namespace: "+rbacNamespace)
err = EditFile("/tmp/"+testsDetails.ExperimentName+"-sa.yaml", "namespace: litmus", "namespace: "+rbacNamespace)
if err != nil {
return errors.Errorf("Failed to Modify rbac file due to %v", err)
}
}
err = EditFile("/tmp/"+testsDetails.ExperimentName+"-sa.yaml", "litmus-admin", testsDetails.ExperimentName+"-sa")
if err != nil {
return errors.Errorf("Failed to Modify rbac file due to %v", err)
}
log.Info("[RBAC]: Installing RBAC...")
//Creating rbac
command := []string{"apply", "-f", "/tmp/" + testsDetails.ExperimentName + "-sa.yaml", "-n", rbacNamespace}
Expand Down Expand Up @@ -214,7 +218,7 @@ func setExperimentVar(chaosExperiment *v1alpha1.ChaosExperiment, testsDetails *t
}
}

//InstallGoChaosExperiment installs the given go based chaos experiment
// InstallGoChaosExperiment installs the given go based chaos experiment
func InstallGoChaosExperiment(testsDetails *types.TestDetails, chaosExperiment *v1alpha1.ChaosExperiment, experimentNamespace string, clients environment.ClientSets) error {

//Fetch Experiment file
Expand Down Expand Up @@ -257,7 +261,7 @@ func InstallGoChaosExperiment(testsDetails *types.TestDetails, chaosExperiment *
}

// setEngineVar setting up variables of engine
func setEngineVar(chaosEngine *v1alpha1.ChaosEngine, testsDetails *types.TestDetails) {
func setEngineVar(chaosEngine *v1alpha1.ChaosEngine, testsDetails *types.TestDetails, setAppInfo bool) {

// contains all the envs
envDetails := ENVDetails{
Expand All @@ -275,9 +279,10 @@ func setEngineVar(chaosEngine *v1alpha1.ChaosEngine, testsDetails *types.TestDet
chaosEngine.ObjectMeta.Namespace = testsDetails.ChaosNamespace

// If ChaosEngine contain App Info then update it
if chaosEngine.Spec.Appinfo.Appns != "" && chaosEngine.Spec.Appinfo.Applabel != "" {
if setAppInfo {
chaosEngine.Spec.Appinfo.Appns = testsDetails.AppNS
chaosEngine.Spec.Appinfo.Applabel = testsDetails.AppLabel
chaosEngine.Spec.Appinfo.AppKind = testsDetails.Appkind
}
if testsDetails.ChaosServiceAccount != "" {
chaosEngine.Spec.ChaosServiceAccount = testsDetails.ChaosServiceAccount
Expand Down Expand Up @@ -377,8 +382,8 @@ func setEngineVar(chaosEngine *v1alpha1.ChaosEngine, testsDetails *types.TestDet
}
}

//InstallGoChaosEngine installs the given go based chaos engine
func InstallGoChaosEngine(testsDetails *types.TestDetails, chaosEngine *v1alpha1.ChaosEngine, engineNamespace string, clients environment.ClientSets) error {
// InstallGoChaosEngine installs the given go based chaos engine
func InstallGoChaosEngine(testsDetails *types.TestDetails, chaosEngine *v1alpha1.ChaosEngine, engineNamespace string, setAppInfo bool, clients environment.ClientSets) error {

//Fetch Engine file
res, err := http.Get(testsDetails.EnginePath)
Expand All @@ -399,7 +404,7 @@ func InstallGoChaosEngine(testsDetails *types.TestDetails, chaosEngine *v1alpha1
}

// Initialise engine
setEngineVar(chaosEngine, testsDetails)
setEngineVar(chaosEngine, testsDetails, setAppInfo)

// Marshal serializes the values provided into a YAML document.
fileData, err := json.Marshal(chaosEngine)
Expand All @@ -416,23 +421,23 @@ func InstallGoChaosEngine(testsDetails *types.TestDetails, chaosEngine *v1alpha1
return nil
}

//InstallLitmus installs the latest version of litmus
// InstallLitmus installs the latest version of litmus
func InstallLitmus(testsDetails *types.TestDetails) error {

log.Info("Installing Litmus ...")
if err := DownloadFile("install-litmus.yaml", testsDetails.InstallLitmus); err != nil {
return errors.Errorf("Failed to fetch litmus operator file due to %v", err)
}
log.Info("Updating ChaosOperator Image ...")
if err := EditFile("install-litmus.yaml", "image: litmuschaos/chaos-operator:latest", "image: "+testsDetails.OperatorImage); err != nil {
if err := EditFile("install-litmus.yaml", "image: litmuschaos.docker.scarf.sh/litmuschaos/chaos-operator:latest", "image: "+testsDetails.OperatorImage); err != nil {
return errors.Errorf("Unable to update operator image due to %v", err)

}
if err = EditKeyValue("install-litmus.yaml", " - chaos-operator", "imagePullPolicy: Always", "imagePullPolicy: "+testsDetails.ImagePullPolicy); err != nil {
return errors.Errorf("Unable to update image pull policy due to %v", err)
}
log.Info("Updating Chaos Runner Image ...")
if err := EditKeyValue("install-litmus.yaml", "CHAOS_RUNNER_IMAGE", "value: \"litmuschaos/chaos-runner:latest\"", "value: '"+testsDetails.RunnerImage+"'"); err != nil {
if err := EditKeyValue("install-litmus.yaml", "CHAOS_RUNNER_IMAGE", "value: \"litmuschaos.docker.scarf.sh/litmuschaos/chaos-runner:latest\"", "value: '"+testsDetails.RunnerImage+"'"); err != nil {
return errors.Errorf("Unable to update runner image due to %v", err)
}
//Creating engine
Expand All @@ -446,7 +451,7 @@ func InstallLitmus(testsDetails *types.TestDetails) error {
return nil
}

//InstallAdminRbac installs admin rbac to run chaos
// InstallAdminRbac installs admin rbac to run chaos
func InstallAdminRbac(testsDetails *types.TestDetails) error {

//Fetch RBAC file
Expand All @@ -471,3 +476,6 @@ func InstallAdminRbac(testsDetails *types.TestDetails) error {

return nil
}



27 changes: 18 additions & 9 deletions pkg/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/klog"
)

//RunnerPodStatus will check the runner pod running state
// RunnerPodStatus will check the runner pod running state
func RunnerPodStatus(testsDetails *types.TestDetails, runnerNamespace string, clients environment.ClientSets) error {

//Fetching the runner pod and Checking if it gets in Running state or not
Expand Down Expand Up @@ -47,7 +47,7 @@ func RunnerPodStatus(testsDetails *types.TestDetails, runnerNamespace string, cl
// CheckRunnerPodCreation will check for the create of runner pod
func CheckRunnerPodCreation(engineName, runnerNS string, clients environment.ClientSets) error {
err := retry.
Times(uint(10 / 2)).
Times(uint(20 / 2)).
Wait(time.Duration(2) * time.Second).
Try(func(attempt uint) error {
runner, err := clients.KubeClient.CoreV1().Pods(runnerNS).Get(engineName+"-runner", metav1.GetOptions{})
Expand All @@ -65,7 +65,7 @@ func CheckRunnerPodCreation(engineName, runnerNS string, clients environment.Cli
return err
}

//DeploymentStatusCheck checks running status of deployment with deployment name and namespace
// DeploymentStatusCheck checks running status of deployment with deployment name and namespace
func DeploymentStatusCheck(testsDetails *types.TestDetails, deploymentName, deploymentNS string, clients environment.ClientSets) error {

sampleApp, _ := clients.KubeClient.AppsV1().Deployments(deploymentNS).Get(deploymentName, metav1.GetOptions{})
Expand All @@ -85,7 +85,7 @@ func DeploymentStatusCheck(testsDetails *types.TestDetails, deploymentName, depl
return nil
}

//OperatorStatusCheck checks the running status of chaos operator
// OperatorStatusCheck checks the running status of chaos operator
func OperatorStatusCheck(testsDetails *types.TestDetails, clients environment.ClientSets) error {
sampleApp, _ := clients.KubeClient.AppsV1().Deployments(testsDetails.ChaosNamespace).Get(testsDetails.OperatorName, metav1.GetOptions{})

Expand All @@ -106,7 +106,7 @@ func OperatorStatusCheck(testsDetails *types.TestDetails, clients environment.Cl
return nil
}

//DeploymentCleanupCheck checks the termination of deployment
// DeploymentCleanupCheck checks the termination of deployment
func DeploymentCleanupCheck(testsDetails *types.TestDetails, deploymentName string, clients environment.ClientSets) error {

sampleApp, _ := clients.KubeClient.AppsV1().Deployments(testsDetails.AppNS).Get(deploymentName, metav1.GetOptions{})
Expand All @@ -127,7 +127,7 @@ func DeploymentCleanupCheck(testsDetails *types.TestDetails, deploymentName stri
return nil
}

//PodStatusCheck checks the pod running status
// PodStatusCheck checks the pod running status
func PodStatusCheck(testsDetails *types.TestDetails, clients environment.ClientSets) error {
PodList, err := clients.KubeClient.CoreV1().Pods(testsDetails.AppNS).List(metav1.ListOptions{LabelSelector: testsDetails.AppLabel})
if err != nil {
Expand Down Expand Up @@ -200,7 +200,7 @@ func ChaosPodStatus(testsDetails *types.TestDetails, clients environment.ClientS
return nil
}

//WaitForEngineStatus waits for engine state to get completed
// WaitForEngineStatus waits for engine state to get completed
func WaitForEngineStatus(testsDetails *types.TestDetails, clients environment.ClientSets, status string) error {
err := retry.
Times(uint(testsDetails.Duration / testsDetails.Delay)).
Expand All @@ -222,7 +222,7 @@ func WaitForEngineStatus(testsDetails *types.TestDetails, clients environment.Cl
return err
}

//WaitForRunnerCompletion waits for runner pod completion
// WaitForRunnerCompletion waits for runner pod completion
func WaitForRunnerCompletion(testsDetails *types.TestDetails, clients environment.ClientSets) error {
err := retry.
Times(uint(testsDetails.Duration / testsDetails.Delay)).
Expand All @@ -245,8 +245,9 @@ func WaitForRunnerCompletion(testsDetails *types.TestDetails, clients environmen
return err
}

//WaitForChaosResultCompletion waits for chaosresult state to get completed
// WaitForChaosResultCompletion waits for chaosresult state to get completed
func WaitForChaosResultCompletion(testsDetails *types.TestDetails, clients environment.ClientSets) error {
isError := false
err := retry.
Times(uint(testsDetails.Duration / testsDetails.Delay)).
Wait(time.Duration(testsDetails.Delay) * time.Second).
Expand All @@ -260,10 +261,18 @@ func WaitForChaosResultCompletion(testsDetails *types.TestDetails, clients envir
klog.Infof("ChaosResult status is %v", chaosResult.Status.ExperimentStatus.Phase)
return errors.Errorf("ChaosResult is not yet completed")
}
if string(chaosResult.Status.ExperimentStatus.Phase) == "Error" {
isError = true
return nil
}
klog.Infof("ChaosResult status is %v", chaosResult.Status.ExperimentStatus.Phase)

return nil
})

if isError {
return errors.Errorf("the chaos result status is 'Error'")
}

return err
}
Loading
Loading