Skip to content

Commit

Permalink
Merge pull request #153 from abdullah-sl/feature/PLAT-392-add-unit-te…
Browse files Browse the repository at this point in the history
…sts-for-cronjob-chart

Add unit tests for cronjob chart
  • Loading branch information
neal-sl authored Jul 24, 2024
2 parents 794dede + 75a7877 commit f23ee3c
Show file tree
Hide file tree
Showing 12 changed files with 733 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/test-cronjob-chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
name: Test cronjob chart

on:
push:
paths:
- cronjob/**
- .github/workflows/**
pull_request:
paths:
- cronjob/**
- .github/workflows/**

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
helm-version:
- 3.1.0
- 3.2.3
- 3.2.4
- 3.3.0
- latest
steps:
- uses: actions/checkout@v4

- name: Set up Helm ${{ matrix.helm-version }}
uses: azure/[email protected]
with:
version: ${{ matrix.helm-version }}

- name: Install Helm unittest plugin
run: |
helm plugin install https://github.com/helm-unittest/helm-unittest.git
- name: Run cronjob chart tests
run: |
helm unittest cronjob
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ vi ./example/subChart/requirements.yaml

```bash
helm dependency update ./helm/preview
```

<!-- how to unit test -->
### How to run unit tests

```bash
helm unittest <chart-name>
```
1 change: 1 addition & 0 deletions cronjob/.helmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.github
*.md
*.tgz
tests
74 changes: 74 additions & 0 deletions cronjob/tests/configmaps_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
suite: ConfigMap Test
templates:
- configmaps.yaml
tests:
- it: should render nothing if no configmaps are provided
asserts:
- hasDocuments:
count: 0

- it: should render a configmap
set:
configmaps:
test-configmap:
data:
TEST_KEY: "test-value"
TEST_KEY_2: "test-value-2"
asserts:
- hasDocuments:
count: 1
- isKind:
of: ConfigMap
- equal:
path: data.TEST_KEY
value: "test-value"
- equal:
path: data.TEST_KEY_2
value: "test-value-2"

- it: should render multiple configmaps
set:
configmaps:
test-configmap:
data:
TEST_KEY: "test-value"
test-configmap-2:
data:
TEST_KEY: "test-value"
asserts:
- hasDocuments:
count: 2
- isKind:
of: ConfigMap
documentIndex: 0
- equal:
path: metadata.name
value: test-configmap-2
documentIndex: 1
- equal:
path: data.TEST_KEY
value: "test-value"
documentIndex: 0
- equal:
path: data.TEST_KEY
value: "test-value"
documentIndex: 1

- it: should render a configmap with quoted values
set:
configmaps:
test-configmap:
data:
TEST_KEY: 9527
TEST_KEY_2: true
asserts:
- hasDocuments:
count: 1
- isKind:
of: ConfigMap
- equal:
path: data.TEST_KEY
value: "9527"
- equal:
path: data.TEST_KEY_2
value: "true"
224 changes: 224 additions & 0 deletions cronjob/tests/cronjob_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
suite: CronJob Test
templates:
- cronjob.yaml
tests:
- it: should render a argo cron workflow with default values
values:
- ./values/cronjob/argo.yaml
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronWorkflow
- equal:
path: spec.timezone
value: "Etc/UTC"
- equal:
path: spec.successfulJobsHistoryLimit
value: 3
- equal:
path: spec.failedJobsHistoryLimit
value: 1
- equal:
path: spec.concurrencyPolicy
value: "Allow"
- equal:
path: spec.suspend
value: false
- equal:
path: spec.schedule
value: "1 * * * *"
- equal:
path: spec.workflowSpec.templates[1].container.command
value:
- bundle
- exec
- rails
- test
- equal:
path: spec.workflowSpec.templates[1].container.image
value: "test/testImage:latest"

- it: should render a argo cron workflow with custom values
values:
- ./values/cronjob/argo.yaml
set:
timezone: Asia/Taipei
job:
history:
success: 99
failed: 99
concurrency: Forbid
suspend: true
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronWorkflow
- equal:
path: spec.timezone
value: "Asia/Taipei"
- equal:
path: spec.successfulJobsHistoryLimit
value: 99
- equal:
path: spec.failedJobsHistoryLimit
value: 99
- equal:
path: spec.concurrencyPolicy
value: "Forbid"
- equal:
path: spec.suspend
value: true
- equal:
path: spec.schedule
value: "1 * * * *"
- equal:
path: spec.workflowSpec.templates[1].container.command
value:
- bundle
- exec
- rails
- test
- equal:
path: spec.workflowSpec.templates[1].container.image
value: "test/testImage:latest"

- it: should render a k8s cron job with default values
values:
- ./values/cronjob/k8s.yaml
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronJob
- equal:
path: spec.successfulJobsHistoryLimit
value: 3
- equal:
path: spec.failedJobsHistoryLimit
value: 1
- equal:
path: spec.concurrencyPolicy
value: "Allow"
- equal:
path: spec.suspend
value: false
- equal:
path: spec.schedule
value: "0 * * * *"
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: "test/testImage:latest:v1.0.0"
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].command
value:
- /app/run.sh

- it: should render a k8s cron job with custom values
values:
- ./values/cronjob/k8s.yaml
set:
job:
history:
success: 99
failed: 99
concurrency: Forbid
suspend: true
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronJob
- equal:
path: spec.successfulJobsHistoryLimit
value: 99
- equal:
path: spec.failedJobsHistoryLimit
value: 99
- equal:
path: spec.concurrencyPolicy
value: "Forbid"
- equal:
path: spec.suspend
value: true
- equal:
path: spec.schedule
value: "0 * * * *"
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: "test/testImage:latest:v1.0.0"
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].command
value:
- /app/run.sh

- it: should render a argo cron workflow when exitNotifications.slackApp exists and mention is null
values:
- ./values/cronjob/argo.yaml
set:
exitNotifications:
slackApp:
mention: ~
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronWorkflow

- it: should render a argo cron workflow when exitNotifications.slackApp.mention exists and onSuccess is null
values:
- ./values/cronjob/argo.yaml
set:
exitNotifications:
slackApp:
mention:
onSuccess: ~
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronWorkflow

- it: should render a argo cron workflow when exitNotifications.slackApp.mention exists and onFailure is null
values:
- ./values/cronjob/argo.yaml
set:
exitNotifications:
slackApp:
mention:
onFailure: ~
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronWorkflow

- it: should not render a argo cron workflow when image.repository is null
values:
- ./values/cronjob/argo.yaml
set:
image:
repository: ~
asserts:
- failedTemplate:
errorMessage: image.repository must be provided

- it: should not render a argo cron workflow when image.tag is null
values:
- ./values/cronjob/argo.yaml
set:
image:
tag: ~
asserts:
- failedTemplate:
errorMessage: image.tag must be provided

- it: should render a argo cron workflow with minimal values
values:
- ./values/cronjob/argo_minimal.yaml
asserts:
- hasDocuments:
count: 1
- isKind:
of: CronWorkflow
Loading

0 comments on commit f23ee3c

Please sign in to comment.