Skip to content

Commit

Permalink
[ChaosCenter]: Add unit tests to chaos-workflow package in GraphQL se…
Browse files Browse the repository at this point in the history
…rver (#3964)

* feat: add unit tests to chaos-workflow package

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* feat: add unit tests to chaosWorkflowService

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* fix: change package name

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

---------

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>
  • Loading branch information
namkyu1999 committed May 20, 2023
1 parent 40e99c3 commit cf86f7e
Show file tree
Hide file tree
Showing 15 changed files with 2,796 additions and 7 deletions.
1,399 changes: 1,399 additions & 0 deletions litmus-portal/graphql-server/pkg/chaos-workflow/handler/handler_test.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
generateName: nginx-chaos
name: nginx-chaos
namespace: default
spec:
appinfo:
appns: 'default'
applabel: 'app=nginx'
appkind: 'deployment'
# It can be active/stop
engineState: 'active'
chaosServiceAccount: pod-delete-sa
experiments:
- name: pod-delete
spec:
components:
env:
# set chaos duration (in sec) as desired
- name: TOTAL_CHAOS_DURATION
value: '30'

# set chaos interval (in sec) as desired
- name: CHAOS_INTERVAL
value: '10'

# pod failures without '--force' & default terminationGracePeriodSeconds
- name: FORCE
value: 'false'

## percentage of total pods to target
- name: PODS_AFFECTED_PERC
value: ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosSchedule
metadata:
name: schedule-nginx
generateName: schedule-nginx
spec:
schedule:
now: true
engineTemplateSpec:
engineState: 'active'
appinfo:
appns: 'default'
applabel: 'app=nginx'
appkind: 'deployment'
annotationCheck: 'true'
chaosServiceAccount: pod-delete-sa
jobCleanUpPolicy: 'delete'
experiments:
- name: pod-delete
spec:
components:
env:
# set chaos duration (in sec) as desired
- name: TOTAL_CHAOS_DURATION
value: '30'

# set chaos interval (in sec) as desired
- name: CHAOS_INTERVAL
value: '10'

# pod failures without '--force' & default terminationGracePeriodSeconds
- name: FORCE
value: 'false'
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Package mocks ...
package mocks

import (
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
chaosWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow"
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
dbOperationsWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
dbSchemaWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
workflowDBOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson"
)

type ChaosWorkflowService struct {
mock.Mock
}

// ProcessWorkflow mocks the ProcessWorkflow of chaos-workflow service
func (c *ChaosWorkflowService) ProcessWorkflow(workflow *model.ChaosWorkFlowRequest) (*model.ChaosWorkFlowRequest, *dbSchemaWorkflow.ChaosWorkflowType, error) {
args := c.Called(workflow)
return args.Get(0).(*model.ChaosWorkFlowRequest), args.Get(1).(*dbSchemaWorkflow.ChaosWorkflowType), args.Error(2)
}

// ProcessWorkflowCreation mocks the ProcessWorkflowCreation of chaos-workflow service
func (c *ChaosWorkflowService) ProcessWorkflowCreation(input *model.ChaosWorkFlowRequest, username string, wfType *dbSchemaWorkflow.ChaosWorkflowType, r *store.StateData) error {
args := c.Called(input, username, wfType, r)
return args.Error(0)
}

// ProcessWorkflowUpdate mocks the ProcessWorkflowUpdate of chaos-workflow service
func (c *ChaosWorkflowService) ProcessWorkflowUpdate(workflow *model.ChaosWorkFlowRequest, username string, wfType *dbSchemaWorkflow.ChaosWorkflowType, r *store.StateData) error {
args := c.Called(workflow, username, wfType, r)
return args.Error(0)
}

// ProcessWorkflowDelete mocks the ProcessWorkflowDelete of chaos-workflow service
func (c *ChaosWorkflowService) ProcessWorkflowDelete(query bson.D, workflow workflowDBOps.ChaosWorkFlowRequest, username string, r *store.StateData) error {
args := c.Called(query, workflow, username, r)
return args.Error(0)
}

// ProcessWorkflowRunDelete mocks the ProcessWorkflowRunDelete of chaos-workflow service
func (c *ChaosWorkflowService) ProcessWorkflowRunDelete(query bson.D, workflowRunID *string, workflow workflowDBOps.ChaosWorkFlowRequest, username string, r *store.StateData) error {
args := c.Called(query, workflowRunID, workflow, username, r)
return args.Error(0)
}

// ProcessWorkflowRunSync mocks the ProcessWorkflowRunSync of chaos-workflow service
func (c *ChaosWorkflowService) ProcessWorkflowRunSync(workflowID string, workflowRunID *string, workflow workflowDBOps.ChaosWorkFlowRequest, r *store.StateData) error {
args := c.Called(workflowID, workflowRunID, workflow, r)
return args.Error(0)
}

// SendWorkflowEvent mocks the SendWorkflowEvent of chaos-workflow service
func (c *ChaosWorkflowService) SendWorkflowEvent(wfRun model.WorkflowRun, r *store.StateData) {
c.Called(wfRun, r)
}

// ProcessCompletedWorkflowRun mocks the ProcessCompletedWorkflowRun of chaos-workflow service
func (c *ChaosWorkflowService) ProcessCompletedWorkflowRun(execData chaosWorkflow.ExecutionData, wfID string) (chaosWorkflow.WorkflowRunMetrics, error) {
args := c.Called(execData, wfID)
return args.Get(0).(chaosWorkflow.WorkflowRunMetrics), args.Error(1)
}

// GetWorkflow mocks the GetWorkflow of chaos-workflow service
func (c *ChaosWorkflowService) GetWorkflow(query bson.D) (dbOperationsWorkflow.ChaosWorkFlowRequest, error) {
args := c.Called(query)
return args.Get(0).(dbOperationsWorkflow.ChaosWorkFlowRequest), args.Error(1)
}

// GetWorkflows mocks the GetWorkflows of chaos-workflow service
func (c *ChaosWorkflowService) GetWorkflows(query bson.D) ([]dbOperationsWorkflow.ChaosWorkFlowRequest, error) {
args := c.Called(query)
return args.Get(0).([]dbOperationsWorkflow.ChaosWorkFlowRequest), args.Error(1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
kind: Workflow
apiVersion: argoproj.io/v1alpha1
metadata:
name: test-podtato-head-1682669740
namespace: litmus
creationTimestamp: null
labels:
subject: podtato-head_litmus
workflows.argoproj.io/controller-instanceid: 83c20016-f7ec-4c5d-bb84-3a067a7010c2
spec:
templates:
- name: argowf-chaos
inputs: {}
outputs: {}
metadata: {}
steps:
- - name: install-application
template: install-application
arguments: {}
- - name: install-chaos-experiments
template: install-chaos-experiments
arguments: {}
- - name: pod-delete
template: pod-delete
arguments: {}
- - name: revert-chaos
template: revert-chaos
arguments: {}
- name: delete-application
template: delete-application
arguments: {}
- name: install-application
inputs: {}
outputs: {}
metadata: {}
container:
name: ""
image: litmuschaos/litmus-app-deployer:latest
args:
- -namespace={{workflow.parameters.adminModeNamespace}}
- -typeName=resilient
- -operation=apply
- -timeout=400
- -app=podtato-head
- -scope=namespace
resources: {}
- name: install-chaos-experiments
inputs: {}
outputs: {}
metadata: {}
container:
name: ""
image: litmuschaos/k8s:latest
command:
- sh
- -c
args:
- kubectl apply -f
https://hub.litmuschaos.io/api/chaos/3.0.0-beta3?file=charts/generic/experiments.yaml
-n {{workflow.parameters.adminModeNamespace}} ; sleep 30
resources: {}
- name: pod-delete
inputs:
artifacts:
- name: pod-delete
path: /tmp/chaosengine.yaml
raw:
data: >
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
namespace: "{{workflow.parameters.adminModeNamespace}}"
labels:
workflow_run_id: "{{workflow.uid}}"
generateName: podtato-main-pod-delete-chaos
spec:
appinfo:
appns: "{{workflow.parameters.adminModeNamespace}}"
applabel: name=podtato-main
appkind: deployment
engineState: active
chaosServiceAccount: litmus-admin
jobCleanUpPolicy: retain
components:
runner:
imagePullPolicy: Always
experiments:
- name: pod-delete
spec:
probe:
- name: check-podtato-main-access-url
type: httpProbe
httpProbe/inputs:
url: http://podtato-main.{{workflow.parameters.adminModeNamespace}}.svc.cluster.local:9000
insecureSkipVerify: false
method:
get:
criteria: ==
responseCode: "200"
mode: Continuous
runProperties:
probeTimeout: 1
interval: 1
retry: 1
components:
env:
- name: TOTAL_CHAOS_DURATION
value: "30"
- name: CHAOS_INTERVAL
value: "10"
- name: FORCE
value: "false"
outputs: {}
metadata:
labels:
weight: "10"
container:
name: ""
image: litmuschaos/litmus-checker:latest
args:
- -file=/tmp/chaosengine.yaml
- -saveName=/tmp/engine-name
resources: {}
- name: delete-application
inputs: {}
outputs: {}
metadata: {}
container:
name: ""
image: litmuschaos/litmus-app-deployer:latest
args:
- -namespace={{workflow.parameters.adminModeNamespace}}
- -typeName=resilient
- -operation=delete
- -app=podtato-head
resources: {}
- name: revert-chaos
inputs: {}
outputs: {}
metadata: {}
container:
name: ""
image: litmuschaos/k8s:latest
command:
- sh
- -c
args:
- kubectl delete chaosengine -l workflow_run_id={{workflow.uid}} -n
{{workflow.parameters.adminModeNamespace}}
resources: {}
entrypoint: argowf-chaos
arguments:
parameters:
- name: adminModeNamespace
value: litmus
serviceAccountName: argo-chaos
securityContext:
runAsUser: 1000
runAsNonRoot: true
status:
? startedAt
? finishedAt
Loading

0 comments on commit cf86f7e

Please sign in to comment.