Skip to content

Commit 7eb8864

Browse files
klinch0kvaps
andauthored
Add tests refactor (#451)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced a new script for managing Helm releases in Kubernetes, including installation and status monitoring. - Added a configuration file for tenant settings, enabling monitoring and SeaweedFS. - Enhanced PostgreSQL initialization script to manage database roles and privileges dynamically. - Added a new local pre-commit hook for version map checks. - **Bug Fixes** - Updated pre-commit hooks for consistent formatting. - **Tests** - Improved testing capabilities for applications in a Kubernetes environment with new Makefile targets. - Enhanced Docker image with tools for YAML and JSON processing. - Updated testing image to the latest version for improved performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Andrei Kvapil <[email protected]> Co-authored-by: Andrei Kvapil <[email protected]>
1 parent 7642faa commit 7eb8864

9 files changed

Lines changed: 170 additions & 9 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ repos:
1212
- repo: https://github.com/igorshubovych/markdownlint-cli
1313
rev: v0.41.0
1414
hooks:
15-
- id: markdownlint
16-
args: [--fix, --disable, MD013, MD041, --]
15+
- id: markdownlint
16+
args: [--fix, --disable, MD013, MD041, --]
17+
- repo: local
18+
hooks:
19+
- id: gen-versions-map
20+
name: Generate versions map and check for changes
21+
entry: sh -c 'make -C packages/apps check-version-map && make -C packages/extra check-version-map'
22+
language: system
23+
types: [file]
24+
pass_filenames: false
25+
description: Run the script and fail if it generates changes

‎hack/e2e.application.sh‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/bin/bash
2+
3+
RED='\033[0;31m'
4+
GREEN='\033[0;32m'
5+
RESET='\033[0m'
6+
YELLOW='\033[0;33m'
7+
8+
9+
ROOT_NS="tenant-root"
10+
TEST_TENANT="tenant-e2e"
11+
12+
function clean() {
13+
kubectl delete helmrelease.helm.toolkit.fluxcd.io $TEST_TENANT -n $ROOT_NS
14+
if true; then
15+
echo -e "${GREEN}Cleanup successful!${RESET}"
16+
return 0
17+
else
18+
echo -e "${RED}Cleanup failed!${RESET}"
19+
return 1
20+
fi
21+
}
22+
23+
function install_helmrelease() {
24+
local release_name="$1"
25+
local namespace="$2"
26+
local chart_path="$3"
27+
local repo_name="$4"
28+
local repo_ns="$5"
29+
local values_file="$6"
30+
31+
if [[ -z "$release_name" ]]; then
32+
echo -e "${RED}Error: Release name is required.${RESET}"
33+
exit 1
34+
fi
35+
36+
if [[ -z "$namespace" ]]; then
37+
echo -e "${RED}Error: Namespace name is required.${RESET}"
38+
exit 1
39+
fi
40+
41+
if [[ -z "$chart_path" ]]; then
42+
echo -e "${RED}Error: Chart path name is required.${RESET}"
43+
exit 1
44+
fi
45+
46+
local helmrelease_file=$(mktemp /tmp/HelmRelease.XXXXXX.yaml)
47+
{
48+
echo "apiVersion: helm.toolkit.fluxcd.io/v2"
49+
echo "kind: HelmRelease"
50+
echo "metadata:"
51+
echo " labels:"
52+
echo " cozystack.io/ui: \"true\""
53+
echo " name: \"$release_name\""
54+
echo " namespace: \"$namespace\""
55+
echo "spec:"
56+
echo " chart:"
57+
echo " spec:"
58+
echo " chart: \"$chart_path\""
59+
echo " reconcileStrategy: Revision"
60+
echo " sourceRef:"
61+
echo " kind: HelmRepository"
62+
echo " name: \"$repo_name\""
63+
echo " namespace: \"$repo_ns\""
64+
echo " version: '*'"
65+
echo " interval: 1m0s"
66+
echo " timeout: 5m0s"
67+
68+
if [[ -n "$values_file" && -f "$values_file" ]]; then
69+
echo " values:"
70+
cat "$values_file" | sed 's/^/ /'
71+
fi
72+
} > "$helmrelease_file"
73+
74+
kubectl apply -f "$helmrelease_file"
75+
76+
rm -f "$helmrelease_file"
77+
}
78+
79+
function install_tenant (){
80+
local release_name="$1"
81+
local namespace="$2"
82+
local values_file="${3:-tenant.yaml}"
83+
local repo_name="cozystack-apps"
84+
local repo_ns="cozy-public"
85+
86+
install_helmrelease "$release_name" "$namespace" "tenant" "$repo_name" "$repo_ns" "$values_file"
87+
}
88+
89+
function check_helmrelease_status() {
90+
local release_name="$1"
91+
local namespace="$2"
92+
local timeout=300 # Timeout in seconds
93+
local interval=5 # Interval between checks in seconds
94+
local elapsed=0
95+
96+
while [[ $elapsed -lt $timeout ]]; do
97+
local status_output
98+
status_output=$(kubectl get helmrelease "$release_name" -n "$namespace" -o json | jq -r '.status.conditions[-1].reason')
99+
100+
if [[ "$status_output" == "InstallSucceeded" ]]; then
101+
echo -e "${GREEN}Helm release '$release_name' is ready.${RESET}"
102+
return 0
103+
elif [[ "$status_output" == "InstallFailed" ]]; then
104+
echo -e "${RED}Helm release '$release_name': InstallFailed${RESET}"
105+
exit 1
106+
else
107+
echo -e "${YELLOW}Helm release '$release_name' is not ready. Current status: $status_output${RESET}"
108+
fi
109+
110+
sleep "$interval"
111+
elapsed=$((elapsed + interval))
112+
done
113+
114+
echo -e "${RED}Timeout reached. Helm release '$release_name' is still not ready after $timeout seconds.${RESET}"
115+
exit 1
116+
}
117+
118+
chart_name="$1"
119+
120+
if [ -z "$chart_name" ]; then
121+
echo -e "${RED}No chart name provided. Exiting...${RESET}"
122+
exit 1
123+
fi
124+
125+
echo "Running tests for chart: $chart_name"
126+
install_tenant $TEST_TENANT $ROOT_NS
127+
check_helmrelease_status $TEST_TENANT $ROOT_NS
128+
129+
repo_name="cozystack-apps"
130+
repo_ns="cozy-public"
131+
132+
release_name="$chart_name-e2e"
133+
install_helmrelease "$release_name" "$TEST_TENANT" "$chart_name" "$repo_name" "$repo_ns"
134+
135+
check_helmrelease_status "$release_name" "$TEST_TENANT"

‎hack/values/tenant.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
host: ""
2+
etcd: false
3+
monitoring: false
4+
ingress: false
5+
seaweedfs: false
6+
isolated: true

‎packages/apps/ferretdb/templates/init-script.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ stringData:
6363
DROP USER $user;
6464
EOT
6565
done
66-
66+
6767
echo "== create roles"
6868
psql -v ON_ERROR_STOP=1 --echo-all <<\EOT
6969
SELECT 'CREATE ROLE app_admin NOINHERIT;'
@@ -83,7 +83,7 @@ stringData:
8383
FOR schema_record IN SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('pg_catalog', 'information_schema') LOOP
8484
-- Changing Schema Ownership
8585
EXECUTE format('ALTER SCHEMA %I OWNER TO %I', schema_record.schema_name, 'app_admin');
86-
86+
8787
-- Add rights for the admin role
8888
EXECUTE format('GRANT ALL ON SCHEMA %I TO %I', schema_record.schema_name, 'app_admin');
8989
EXECUTE format('GRANT ALL ON ALL TABLES IN SCHEMA %I TO %I', schema_record.schema_name, 'app_admin');

‎packages/core/testing/Chart.yaml‎

100644100755
File mode changed.

‎packages/core/testing/Makefile‎

100644100755
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
NAMESPACE=cozy-e2e-tests
22
NAME := sandbox
33
CLEAN := 1
4+
TESTING_APPS := $(shell find ../../apps -maxdepth 1 -mindepth 1 -type d | awk -F/ '{print $$NF}')
45

56
include ../../../scripts/common-envs.mk
67

@@ -32,10 +33,19 @@ image-e2e-sandbox:
3233
yq -i '.e2e.image = strenv(IMAGE)' values.yaml
3334
rm -f images/e2e-sandbox.json
3435

35-
test: wait-for-sandbox ## Run the end-to-end tests in existing sandbox.
36-
cat ../../../hack/e2e.sh | kubectl exec -i -n $(NAMESPACE) deploy/cozystack-e2e-$(NAME) -- sh -c 'cat > /e2e.sh && chmod +x /e2e.sh'
36+
copy-hack-dir:
37+
tar -C ../../../ -cf- hack | kubectl exec -i -n $(NAMESPACE) deploy/cozystack-e2e-$(NAME) -- tar -xf-
38+
39+
test: wait-for-sandbox copy-hack-dir ## Run the end-to-end tests in existing sandbox.
3740
helm template -n cozy-system installer ../installer | kubectl exec -i -n $(NAMESPACE) deploy/cozystack-e2e-$(NAME) -- sh -c 'cat > /cozystack-installer.yaml'
38-
kubectl exec -ti -n $(NAMESPACE) deploy/cozystack-e2e-$(NAME) -- sh -c 'export COZYSTACK_INSTALLER_YAML=$$(cat /cozystack-installer.yaml) && /e2e.sh'
41+
kubectl exec -ti -n $(NAMESPACE) deploy/cozystack-e2e-$(NAME) -- sh -c 'export COZYSTACK_INSTALLER_YAML=$$(cat /cozystack-installer.yaml) && /hack/e2e.sh'
42+
43+
test-applications: wait-for-sandbox copy-hack-dir ## Run the end-to-end tests in existing sandbox for applications.
44+
for app in $(TESTING_APPS); do \
45+
echo "Running tests for $${app}"; \
46+
kubectl exec -ti -n cozy-e2e-tests deploy/cozystack-e2e-sandbox -- bash -c "/hack/e2e.application.sh $${app}"; \
47+
done
48+
kubectl exec -ti -n cozy-e2e-tests deploy/cozystack-e2e-sandbox -- bash -c "kubectl get hr -A | grep -v 'True'"
3949

4050
delete: ## Remove sandbox from existing Kubernetes cluster.
4151
kubectl delete deploy -n $(NAMESPACE) cozystack-e2e-$(NAME)

‎packages/core/testing/images/e2e-sandbox/Dockerfile‎

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ ARG TALOSCTL_VERSION=1.7.6
55
ARG HELM_VERSION=3.15.4
66

77
RUN apt-get update
8-
RUN apt-get -y install genisoimage qemu-kvm qemu-utils iproute2 iptables wget xz-utils netcat curl
8+
RUN apt-get -y install genisoimage qemu-kvm qemu-utils iproute2 iptables wget xz-utils netcat curl jq
99
RUN curl -LO "https://github.com/siderolabs/talos/releases/download/v${TALOSCTL_VERSION}/talosctl-linux-amd64" \
1010
&& chmod +x talosctl-linux-amd64 \
1111
&& mv talosctl-linux-amd64 /usr/local/bin/talosctl
1212
RUN curl -LO "https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl" \
1313
&& chmod +x kubectl \
1414
&& mv kubectl /usr/local/bin/kubectl
1515
RUN curl -sSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash -s - --version "v${HELM_VERSION}"
16+
RUN wget https://github.com/mikefarah/yq/releases/download/v4.44.3/yq_linux_amd64 -O /usr/local/bin/yq && chmod +x /usr/local/bin/yq

‎packages/core/testing/templates/sandbox.yaml‎

100644100755
File mode changed.

‎packages/core/testing/values.yaml‎

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
e2e:
2-
image: ghcr.io/aenix-io/cozystack/e2e-sandbox:v0.17.1@sha256:c3390f0076f4a8445273d0cdeda7725ac6f5110ad35e1a286be4b158708c4402
2+
image: ghcr.io/aenix-io/cozystack/e2e-sandbox:latest@sha256:1a26a511b9e269bcb607e2d80f878d7c2d993b7a2a7a3a2a1042470c8c56b061

0 commit comments

Comments
 (0)