diff --git a/Makefile b/Makefile index 74d7eebc..aa1249f7 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ # Makefile for building Litmus and its tools # Reference Guide - https://www.gnu.org/software/make/manual/make.html -REGISTRY ?= litmuschaos -IMG_NAME ?= chaos-runner -PACKAGE_VERSION ?= ci +DOCKER_REGISTRY ?= docker.io +DOCKER_REPO ?= litmuschaos +DOCKER_IMAGE ?= chaos-runner +DOCKER_TAG ?= ci IS_DOCKER_INSTALLED = $(shell which docker >> /dev/null 2>&1; echo $$?) HOME = $(shell echo $$HOME) @@ -57,20 +58,20 @@ dockerops: @echo "------------------" @echo "--> Build Chaos-runner image..." @echo "------------------" - @docker buildx build --file build/Dockerfile --progress plane --platform linux/arm64,linux/amd64 --tag $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) . + @docker buildx build --file build/Dockerfile --progress plane --platform linux/arm64,linux/amd64 --tag $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) . .PHONY: dockerops-amd64 dockerops-amd64: @echo "--------------------------------------------" @echo "--> Build chaos-runner amd-64 docker image" @echo "--------------------------------------------" - sudo docker build --file build/Dockerfile --tag $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) . --build-arg TARGETARCH=amd64 + sudo docker build --file build/Dockerfile --tag $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) . --build-arg TARGETARCH=amd64 @echo "--------------------------------------------" @echo "--> Push chaos-runner amd-64 docker image" @echo "--------------------------------------------" - sudo docker push $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) + sudo docker push $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) .PHONY: push push: - @docker buildx build --file build/Dockerfile --progress plane --push --platform linux/arm64,linux/amd64 --tag $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) . + @docker buildx build --file build/Dockerfile --progress plane --push --platform linux/arm64,linux/amd64 --tag $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) . diff --git a/pkg/utils/builders.go b/pkg/utils/builders.go index f80a685e..63e85f66 100644 --- a/pkg/utils/builders.go +++ b/pkg/utils/builders.go @@ -23,7 +23,7 @@ func buildContainerSpec(experiment *ExperimentDetails, envVars []corev1.EnvVar) containerSpec := container.NewBuilder(). WithName(experiment.JobName). WithImage(experiment.ExpImage). - WithCommandNew([]string{"/bin/bash"}). + WithCommandNew(experiment.ExpCommand). WithArgumentsNew(experiment.ExpArgs). WithImagePullPolicy(experiment.ExpImagePullPolicy). WithEnvsNew(envVars) diff --git a/pkg/utils/experimentHelper.go b/pkg/utils/experimentHelper.go index 75814e71..4647b322 100644 --- a/pkg/utils/experimentHelper.go +++ b/pkg/utils/experimentHelper.go @@ -84,6 +84,7 @@ func (expDetails *ExperimentDetails) SetDefaultAttributeValuesFromChaosExperimen expDetails.SetImage(experimentSpec). SetImagePullPolicy(experimentSpec). SetArgs(experimentSpec). + SetCommand(experimentSpec). SetLabels(experimentSpec, engine). SetSecurityContext(experimentSpec). SetHostPID(experimentSpec) @@ -120,6 +121,12 @@ func (expDetails *ExperimentDetails) SetArgs(experimentSpec *litmuschaosv1alpha1 return expDetails } +// SetCommand to execute inside the experiment image. +func (expDetails *ExperimentDetails) SetCommand(experimentSpec *litmuschaosv1alpha1.ChaosExperiment) *ExperimentDetails { + expDetails.ExpCommand = experimentSpec.Spec.Definition.Command + return expDetails +} + // SetSecurityContext sets the security context, in Experiment Structure func (expDetails *ExperimentDetails) SetSecurityContext(experimentSpec *litmuschaosv1alpha1.ChaosExperiment) *ExperimentDetails { expDetails.SecurityContext = experimentSpec.Spec.Definition.SecurityContext diff --git a/pkg/utils/experimentHelper_test.go b/pkg/utils/experimentHelper_test.go index b6711f9b..5c1bddc9 100644 --- a/pkg/utils/experimentHelper_test.go +++ b/pkg/utils/experimentHelper_test.go @@ -173,6 +173,7 @@ func TestSetDefaultAttributeValuesFromChaosExperiment(t *testing.T) { envMap: map[string]v1.EnvVar{}, ExpLabels: map[string]string{}, ExpArgs: []string{}, + ExpCommand: []string{}, ConfigMaps: []v1alpha1.ConfigMap{}, Secrets: []v1alpha1.Secret{}, ExpImage: "", @@ -637,6 +638,61 @@ func TestSetArgs(t *testing.T) { } } +func TestSetCommand(t *testing.T) { + fakeExperimentCommand := "fake-exp-command" + experiment := ExperimentDetails{ + Name: "Fake-Exp-Name", + Namespace: "Fake NameSpace", + JobName: "fake-job-name", + StatusCheckTimeout: 10, + envMap: map[string]v1.EnvVar{}, + ExpLabels: map[string]string{}, + ExpArgs: []string{}, + } + + tests := map[string]struct { + chaosexperiment *v1alpha1.ChaosExperiment + }{ + "Test Positive-1": { + chaosexperiment: &v1alpha1.ChaosExperiment{ + ObjectMeta: metav1.ObjectMeta{ + Name: experiment.Name, + Namespace: experiment.Namespace, + }, + Spec: v1alpha1.ChaosExperimentSpec{ + Definition: v1alpha1.ExperimentDef{ + Command: []string{ + fakeExperimentCommand, + }, + }, + }, + }, + }, + } + + for name, mock := range tests { + t.Run(name, func(t *testing.T) { + client := CreateFakeClient(t) + + _, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosExperiments(mock.chaosexperiment.Namespace).Create(mock.chaosexperiment) + if err != nil { + t.Fatalf("experiment not created for %v test, err: %v", name, err) + } + experimentSpec, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosExperiments(mock.chaosexperiment.Namespace).Get(mock.chaosexperiment.Name, metav1.GetOptions{}) + if err != nil { + t.Fatalf("fail to get the chaosexperiment for %v test, err: %v", name, err) + } + + expDetails := experiment.SetCommand(experimentSpec) + expectedResult := expDetails.ExpCommand + actualResult := mock.chaosexperiment.Spec.Definition.Command + if !reflect.DeepEqual(expectedResult, actualResult) { + t.Fatalf("Test %q failed to set the command from experiment", name) + } + }) + } +} + func TestSetSecurityContext(t *testing.T) { experiment := ExperimentDetails{ Name: "Fake-Exp-Name", diff --git a/pkg/utils/types.go b/pkg/utils/types.go index 3b62a62f..3a57e146 100644 --- a/pkg/utils/types.go +++ b/pkg/utils/types.go @@ -39,6 +39,7 @@ type ExperimentDetails struct { ExpImage string ExpImagePullPolicy v1.PullPolicy ExpArgs []string + ExpCommand []string JobName string Namespace string ConfigMaps []v1alpha1.ConfigMap diff --git a/tests/runner_test.go b/tests/runner_test.go index c9768639..dfacc529 100644 --- a/tests/runner_test.go +++ b/tests/runner_test.go @@ -18,6 +18,7 @@ limitations under the License. import ( "flag" + "fmt" "os" "os/exec" "regexp" @@ -243,39 +244,54 @@ var _ = Describe("BDD on chaos-runner", func() { When("Check if the Job is spawned by chaos-runner", func() { It("Should create a Pod delete Job", func() { - var jobName string - jobs, _ := clients.KubeClient.BatchV1().Jobs("litmus").List(metav1.ListOptions{}) - - for _, job := range jobs.Items { - matched, _ := regexp.MatchString("pod-delete-.*", job.Name) - if matched { - jobName = job.Name - break - } - } + err := retry. + Times(uint(180 / 2)). + Wait(time.Duration(2) * time.Second). + Try(func(attempt uint) error { + var jobName string + jobs, err := clients.KubeClient.BatchV1().Jobs("litmus").List(metav1.ListOptions{}) + if err != nil { + return err + } + regExpr, err := regexp.Compile("pod-delete-.*") + if err != nil { + return err + } + for _, job := range jobs.Items { + matched := regExpr.MatchString(job.Name) + if matched { + jobName = job.Name + break + } + } + if jobName == "" { + return fmt.Errorf("unable to get the job, might be something wrong with chaos-runner") + } + return nil + }) - Expect(jobName).To( - Not(BeEmpty()), - "unable to get the job, might be something wrong with chaos-runner", + Expect(err).To( + BeNil(), + "while listing experiment job in namespace litmus", ) }) }) }) -// This is a workaround to prevent a condition where operator expects presence of chaosresult to update target revert status -// Also, this minikube is a transient cluster brought up in the pipeline VM, so we cna skip the cleanup -/* - //Deleting all unused resources var _ = AfterSuite(func() { - By("Deleting all CRDs") - crdDeletion := exec.Command("kubectl", "delete", "-f", "https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/chaos_crds.yaml").Run() + + By("Deleting chaosengine CRD") + ceDeleteCRDs := exec.Command("kubectl", "delete", "crds", "chaosengines.litmuschaos.io").Run() + Expect(ceDeleteCRDs).To(BeNil()) + + By("Deleting other CRDs") + crdDeletion := exec.Command("kubectl", "delete", "crds", "chaosresults.litmuschaos.io", "chaosexperiments.litmuschaos.io").Run() Expect(crdDeletion).To(BeNil()) - By("Deleting RBAC Permissions") - rbacDeletion := exec.Command("kubectl", "delete", "-f", "https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/rbac.yaml").Run() + + By("Deleting namespace litmus") + rbacDeletion := exec.Command("kubectl", "delete", "ns", "litmus").Run() Expect(rbacDeletion).To(BeNil()) - log.Info("deleted CRD and RBAC") + log.Info("deleted CRD and Namespace") }) - -*/