Skip to content

Commit 1c9ae2b

Browse files
committed
[ci,dx] Add unit tests for cozy-lib
## What this PR does The cozy-lib library package got complicated enough to warrant its own unit tests. Since unit tests are a "good thing" (tm), a somewhat generic framework for running all kinds of unit tests was introduced into the CI pipeline and Makefile targets. For now all it runs is `make test` against the `packages/{library,apps,system,extra}/*` directories, wherever a `test` target is present in the Makefile, and for now this is only for the `cozy-lib` Helm library chart. ### Release note ```release-note [ci,dx] Introduce a scaffold for running unit tests locally and in CI and add the first unit tests for the cozy-lib helper Helm chart. ``` Signed-off-by: Timofei Larkin <[email protected]>
1 parent bdff61e commit 1c9ae2b

11 files changed

Lines changed: 161 additions & 1 deletion

File tree

‎.github/workflows/pull-requests.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
fetch-depth: 0
3434
fetch-tags: true
3535

36+
- name: Run unit tests
37+
run: make unit-tests
38+
3639
- name: Set up Docker config
3740
run: |
3841
if [ -d ~/.docker ]; then

‎Makefile‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: manifests repos assets
1+
.PHONY: manifests repos assets unit-tests helm-unit-tests
22

33
build-deps:
44
@command -V find docker skopeo jq gh helm > /dev/null
@@ -46,6 +46,11 @@ test:
4646
make -C packages/core/testing apply
4747
make -C packages/core/testing test
4848

49+
unit-tests: helm-unit-tests
50+
51+
helm-unit-tests:
52+
hack/helm-unit-tests.sh
53+
4954
prepare-env:
5055
make -C packages/core/testing apply
5156
make -C packages/core/testing prepare-cluster

‎hack/helm-unit-tests.sh‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# Script to run unit tests for all Helm charts.
5+
# It iterates through directories in packages/apps, packages/extra,
6+
# packages/system, and packages/library and runs the 'test' Makefile
7+
# target if it exists.
8+
9+
FAILED_DIRS_FILE="$(mktemp)"
10+
trap 'rm -f "$FAILED_DIRS_FILE"' EXIT
11+
12+
tests_found=0
13+
14+
check_and_run_test() {
15+
dir="$1"
16+
makefile="$dir/Makefile"
17+
18+
if [ ! -f "$makefile" ]; then
19+
return 0
20+
fi
21+
22+
if make -C "$dir" -n test >/dev/null 2>&1; then
23+
echo "Running tests in $dir"
24+
tests_found=$((tests_found + 1))
25+
if ! make -C "$dir" test; then
26+
printf '%s\n' "$dir" >> "$FAILED_DIRS_FILE"
27+
return 1
28+
fi
29+
fi
30+
31+
return 0
32+
}
33+
34+
for package_dir in packages/apps packages/extra packages/system packages/library; do
35+
if [ ! -d "$package_dir" ]; then
36+
echo "Warning: Directory $package_dir does not exist, skipping..." >&2
37+
continue
38+
fi
39+
40+
for dir in "$package_dir"/*; do
41+
[ -d "$dir" ] || continue
42+
check_and_run_test "$dir" || true
43+
done
44+
done
45+
46+
if [ "$tests_found" -eq 0 ]; then
47+
echo "No directories with 'test' Makefile targets found."
48+
exit 0
49+
fi
50+
51+
if [ -s "$FAILED_DIRS_FILE" ]; then
52+
echo "ERROR: Tests failed in the following directories:" >&2
53+
while IFS= read -r dir; do
54+
echo " - $dir" >&2
55+
done < "$FAILED_DIRS_FILE"
56+
exit 1
57+
fi
58+
59+
echo "All Helm unit tests passed."

‎packages/library/cozy-lib/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ include ../../../scripts/package.mk
44
generate:
55
cozyvalues-gen -v values.yaml -s values.schema.json -r README.md
66

7+
test:
8+
$(MAKE) -C ../../tests/cozy-lib-tests/ test
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*.orig
18+
*~
19+
# Various IDEs
20+
.project
21+
.idea/
22+
*.tmproj
23+
.vscode/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v2
2+
name: quota
3+
description: Testing chart for cozy-lib
4+
5+
# A chart can be either an 'application' or a 'library' chart.
6+
#
7+
# Application charts are a collection of templates that can be packaged into versioned archives
8+
# to be deployed.
9+
#
10+
# Library charts provide useful utilities or functions for the chart developer. They're included as
11+
# a dependency of application charts to inject those utilities and functions into the rendering
12+
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
13+
type: application
14+
15+
# This is the chart version. This version number should be incremented each time you make changes
16+
# to the chart and its templates, including the app version.
17+
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18+
version: 0.1.0
19+
20+
# This is the version number of the application being deployed. This version number should be
21+
# incremented each time you make changes to the application. Versions are not expected to
22+
# follow Semantic Versioning. They should reflect the version the application is using.
23+
# It is recommended to use it with quotes.
24+
appVersion: "1.16.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
helm unittest .
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../library/cozy-lib
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ResourceQuota
3+
metadata:
4+
name: {{ .Release.Name }}
5+
spec:
6+
{{- with .Values.quota }}
7+
hard: {{- include "cozy-lib.resources.flatten" (list . $) | nindent 4 }}
8+
{{- end }}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ./tests/quota_test.yaml
2+
suite: quota helper
3+
4+
templates:
5+
- templates/tests/quota.yaml
6+
7+
tests:
8+
- it: correctly interprets special kubernetes quota types
9+
values:
10+
- quota_values.yaml
11+
12+
release:
13+
name: myrelease
14+
namespace: default
15+
revision: 1
16+
isUpgrade: false
17+
18+
asserts:
19+
- equal:
20+
path: spec.hard["limits.cpu"]
21+
value: "2"
22+
23+
- equal:
24+
path: spec.hard["requests.cpu"]
25+
value: "0.2"
26+
27+
- equal:
28+
path: spec.hard["services.loadbalancers"]
29+
value: "2"
30+

0 commit comments

Comments
 (0)