Skip to content

Commit da24235

Browse files
committed
feat: Add e2e tests (#3)
Added test for Helm deployment and Sonar CR Jira: EPMDEDP-11699 Change-Id: If3be6c6ff9515fa2d8b7ab793d4b078c71bfb126
1 parent e3af044 commit da24235

11 files changed

+141
-0
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ KIND_CLUSTER_NAME?="sonar-operator"
5858
KUBE_VERSION?=1.26
5959
KIND_CONFIG?=./hack/kind-$(KUBE_VERSION).yaml
6060

61+
E2E_IMAGE_REPOSITORY?="sonar-image"
62+
E2E_IMAGE_TAG?="latest"
63+
6164
.DEFAULT_GOAL:=help
6265
# set default shell
6366
SHELL=/bin/bash -o pipefail -o errexit
@@ -83,6 +86,12 @@ test: fmt vet envtest
8386
TEST_SONAR_PASSWORD=${TEST_SONAR_PASSWORD} \
8487
go test ./... -coverprofile=coverage.out `go list ./...`
8588

89+
## Run e2e tests. Requires kind with running cluster and kuttl tool.
90+
e2e: build
91+
docker build --no-cache -t ${E2E_IMAGE_REPOSITORY}:${E2E_IMAGE_TAG} .
92+
kind load --name $(KIND_CLUSTER_NAME) docker-image ${E2E_IMAGE_REPOSITORY}:${E2E_IMAGE_TAG}
93+
E2E_IMAGE_REPOSITORY=${E2E_IMAGE_REPOSITORY} E2E_IMAGE_TAG=${E2E_IMAGE_TAG} kubectl kuttl test
94+
8695
.PHONY: fmt
8796
fmt: ## Run go fmt
8897
go fmt ./...

kuttl-test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestSuite
3+
testDirs:
4+
- tests/e2e/
5+
skipClusterDelete: false
6+
skipDelete: false
7+
timeout: 100
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: sonar-operator
5+
status:
6+
readyReplicas: 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
commands:
4+
- command: helm install --set image.repository=${E2E_IMAGE_REPOSITORY} --set image.tag=${E2E_IMAGE_TAG} --replace --wait sonar-operator-e2e ../../../deploy-templates
5+
namespaced: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: sonar
5+
status:
6+
readyReplicas: 1
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: sonar
5+
labels:
6+
app: sonar
7+
spec:
8+
ports:
9+
- name: http
10+
port: 9000
11+
targetPort: 9000
12+
selector:
13+
app: sonar
14+
type: ClusterIP
15+
16+
---
17+
apiVersion: apps/v1
18+
kind: Deployment
19+
metadata:
20+
name: sonar
21+
labels:
22+
app: sonar
23+
spec:
24+
replicas: 1
25+
selector:
26+
matchLabels:
27+
app: sonar
28+
template:
29+
metadata:
30+
labels:
31+
app: sonar
32+
spec:
33+
containers:
34+
- name: sonar
35+
image: sonarqube:lts-community
36+
ports:
37+
- name: http
38+
containerPort: 9000
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: change-password-job
5+
status:
6+
succeeded: 1
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: sonar-secret
5+
type: Opaque
6+
data:
7+
user: "YWRtaW4="
8+
password: "bmV3QWRtaW5QYXNzd29yZA==" # newAdminPassword
9+
10+
---
11+
# SonarQube requires a password change on the first login.
12+
apiVersion: batch/v1
13+
kind: Job
14+
metadata:
15+
name: change-password-job
16+
spec:
17+
template:
18+
spec:
19+
containers:
20+
- name: curl-container
21+
image: curlimages/curl:latest
22+
command: ["/bin/sh", "-c"]
23+
args:
24+
- |
25+
max_retries=20
26+
retries=0
27+
while [ $retries -lt $max_retries ]; do
28+
http_code=$(curl -s -o -L /dev/null -w "%{http_code}" -u admin:admin -X POST "http://sonar:9000/api/users/change_password?login=admin&previousPassword=admin&password=newAdminPassword")
29+
if [[ "$http_code" == *"204"* ]]; then
30+
echo "Password change successful (HTTP $http_code)"
31+
exit 0
32+
else
33+
echo "Password change failed (HTTP $http_code), retrying..."
34+
retries=$((retries+1))
35+
sleep 10
36+
fi
37+
done
38+
echo "Max retries reached, password change still unsuccessful."
39+
exit 1
40+
restartPolicy: Never # The Job should not restart
41+
backoffLimit: 0 # No retries on the Job level
42+
43+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: edp.epam.com/v1alpha1
2+
kind: Sonar
3+
metadata:
4+
name: sonar
5+
spec:
6+
secret: sonar-secret
7+
url: http://sonar:9000
8+
status:
9+
connected: true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: edp.epam.com/v1alpha1
2+
kind: Sonar
3+
metadata:
4+
name: sonar
5+
spec:
6+
secret: sonar-secret
7+
url: http://sonar:9000

0 commit comments

Comments
 (0)