Skip to content

Commit 56131b2

Browse files
committed
Added tofu tests to CI
1 parent 4b1f484 commit 56131b2

File tree

3 files changed

+209
-1
lines changed

3 files changed

+209
-1
lines changed

.github/workflows/pr.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ jobs:
3535
functional_tests:
3636
needs: [publish_images]
3737
uses: ./.github/workflows/functional.yaml
38-
if: github.repository == 'stackhpc/coral-credits'
38+
if: github.repository == 'stackhpc/coral-credits'
39+
40+
tofu_tests:
41+
needs: [publish_images]
42+
uses: ./.github/workflows/tofu.yaml
43+
if: github.repository == 'stackhpc/coral-credits'

.github/workflows/tofu.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Tofu tests
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
functional_test:
8+
name: Operator functional tests via tox
9+
timeout-minutes: 10
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Helm
16+
uses: azure/setup-helm@v4
17+
with:
18+
version: v3.11.3
19+
20+
- name: Create k8s Kind Cluster
21+
uses: helm/[email protected]
22+
with:
23+
config: kind-config.yml
24+
25+
- name: Run test
26+
timeout-minutes: 10
27+
run: tofu/tests/setup-and-run-ci-env.sh

tofu/tests/setup-and-run-ci-env.sh

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
6+
7+
PORT=80
8+
METRICS_PORT=8000
9+
SITE=localhost
10+
# Function to check if port is open
11+
check_port() {
12+
nc -z localhost $PORT
13+
return $?
14+
}
15+
16+
# Function to make HTTP request and return status code and content
17+
get_http_response() {
18+
local endpoint="$1"
19+
local port="$2"
20+
local response=$(curl -s -w "\n%{http_code}" "http://$SITE:$port/$endpoint")
21+
echo "$response"
22+
}
23+
24+
# Function to check HTTP status for _status endpoint
25+
check_http_status() {
26+
local response=$(get_http_response "_status/" $PORT)
27+
local status=$(echo "$response" | tail -n1)
28+
local content=$(echo "$response" | sed '$d')
29+
30+
if [ "$status" -eq 204 ]; then
31+
echo "Status check passed. (No content for 204 status)"
32+
return 0
33+
else
34+
echo "Error: Expected HTTP status code 204 for _status, but got $status"
35+
[ -n "$content" ] && echo "Response content: $content"
36+
return 1
37+
fi
38+
}
39+
40+
# Function to check HTTP status for metrics endpoint
41+
check_metrics_status() {
42+
local response=$(get_http_response "metrics/" $METRICS_PORT)
43+
local status=$(echo "$response" | tail -n1)
44+
local content=$(echo "$response" | sed '$d')
45+
46+
if [ "$status" -eq 200 ]; then
47+
echo "Metrics retrieved successfully. Content:"
48+
echo "$content"
49+
return 0
50+
else
51+
echo "Error: Expected HTTP status code 200 for metrics, but got $status"
52+
[ -n "$content" ] && echo "Response content: $content"
53+
return 1
54+
fi
55+
}
56+
57+
# Set variables
58+
CHART_NAME="coral-credits"
59+
RELEASE_NAME=$CHART_NAME
60+
NAMESPACE=$CHART_NAME
61+
TEST_PASSWORD="testpassword"
62+
63+
# Install nginx
64+
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
65+
kubectl wait --namespace ingress-nginx \
66+
--for=condition=ready pod \
67+
--selector=app.kubernetes.io/component=controller \
68+
--timeout=90s
69+
70+
# Install the CaaS operator from the chart we are about to ship
71+
# Make sure to use the images that we just built
72+
helm upgrade $RELEASE_NAME ./charts \
73+
--dependency-update \
74+
--namespace $NAMESPACE \
75+
--create-namespace \
76+
--install \
77+
--wait \
78+
--timeout 3m \
79+
--set-string image.tag=${GITHUB_SHA::7} \
80+
--set settings.superuserPassword=$TEST_PASSWORD \
81+
--set ingress.host=$SITE \
82+
--set ingress.tls.enabled=false \
83+
--set monitoring.enabled=false
84+
85+
# Wait for rollout
86+
kubectl rollout status deployment/$RELEASE_NAME -n $NAMESPACE --timeout=300s -w
87+
88+
# Wait for port to be open
89+
echo "Waiting for port $PORT to be available..."
90+
for i in {1..30}; do
91+
if check_port; then
92+
echo "Port $PORT is now open"
93+
break
94+
fi
95+
if [ $i -eq 30 ]; then
96+
echo "Timeout waiting for port $PORT"
97+
exit 1
98+
fi
99+
sleep 1
100+
done
101+
102+
# Check HTTP status with retries
103+
echo "Checking HTTP status..."
104+
for i in {1..10}; do
105+
if check_http_status; then
106+
echo "Success: HTTP status code is 204."
107+
break
108+
fi
109+
if [ $i -eq 10 ]; then
110+
echo "Failed to get correct HTTP status after 10 attempts"
111+
# Get pod logs on failure
112+
SELECTOR="app.kubernetes.io/name=$CHART_NAME,app.kubernetes.io/instance=$RELEASE_NAME"
113+
POD_NAME=$(kubectl get pods -n $NAMESPACE -l $SELECTOR -o jsonpath="{.items[0].metadata.name}")
114+
kubectl logs -n $NAMESPACE $POD_NAME
115+
exit 1
116+
fi
117+
echo "Attempt $i failed. Retrying in 3 seconds..."
118+
sleep 3
119+
done
120+
121+
echo "Running additional tests..."
122+
123+
# Set up some variables
124+
CONTENT_TYPE="Content-Type: application/json"
125+
TF_VAR_coral_uri=http://$SITE:$PORT
126+
127+
# Get a token
128+
echo "Getting an auth token:"
129+
TF_VAR_auth_token=$(curl -s -X POST -H "$CONTENT_TYPE" -d \
130+
"{
131+
\"username\": \"admin\",
132+
\"password\": \"$TEST_PASSWORD\"
133+
}" \
134+
${TF_VAR_coral_uri}/api-token-auth/ | jq -r '.token')
135+
echo "Auth Token: $TF_VAR_auth_token"
136+
137+
python -m venv venv
138+
source venv/bin/active
139+
pip install -r ${SCRIPT_DIR}/../../requirements.txt
140+
141+
pytest ${SCRIPT_DIR}/tofu_tests.py
142+
143+
# Scrape prometheus metrics:
144+
kubectl port-forward -n $NAMESPACE svc/$RELEASE_NAME $METRICS_PORT:$METRICS_PORT &
145+
# Wait for port to be open
146+
echo "Waiting for port $METRICS_PORT to be available..."
147+
for i in {1..30}; do
148+
if check_port; then
149+
echo "Port $METRICS_PORT is now open"
150+
break
151+
fi
152+
if [ $i -eq 30 ]; then
153+
echo "Timeout waiting for port $METRICS_PORT"
154+
exit 1
155+
fi
156+
sleep 1
157+
done
158+
159+
# Check metrics status with retries
160+
echo "Checking Prometheus status..."
161+
for i in {1..10}; do
162+
if check_metrics_status; then
163+
echo "Success: HTTP status code is 200."
164+
break
165+
fi
166+
if [ $i -eq 10 ]; then
167+
echo "Failed to get correct HTTP status after 10 attempts"
168+
# Get pod logs on failure
169+
SELECTOR="app.kubernetes.io/name=$CHART_NAME,app.kubernetes.io/instance=$RELEASE_NAME"
170+
POD_NAME=$(kubectl get pods -n $NAMESPACE -l $SELECTOR -o jsonpath="{.items[0].metadata.name}")
171+
kubectl logs -n $NAMESPACE $POD_NAME
172+
exit 1
173+
fi
174+
echo "Attempt $i failed. Retrying in 3 seconds..."
175+
sleep 3
176+
done

0 commit comments

Comments
 (0)