Skip to content

Commit 6ccf559

Browse files
committed
fix tekton integration test.
Signed-off-by: Morven Cao <lcao@redhat.com>
1 parent 7f81be0 commit 6ccf559

File tree

4 files changed

+181
-154
lines changed

4 files changed

+181
-154
lines changed

.tekton/integration-test.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,21 @@ spec:
8484
# Generate mqtt config file
8585
echo '{"brokerHost":"localhost:1883","username":"","password":"","topics":{"sourceEvents":"sources/maestro/consumers/+/sourceevents","agentEvents":"sources/maestro/consumers/+/agentevents"}}' > secrets/mqtt.config
8686
87+
# Generate pubsub config file
88+
echo '{"projectID":"maestro-test","endpoint":"localhost:8085","insecure":true,"topics":{"sourceEvents":"projects/maestro-test/topics/sourceevents","sourceBroadcast":"projects/maestro-test/topics/sourcebroadcast"},"subscriptions":{"agentEvents":"projects/maestro-test/subscriptions/agentevents-maestro","agentBroadcast":"projects/maestro-test/subscriptions/agentbroadcast-maestro"}}' > secrets/pubsub.config
89+
8790
# Generate db.password
8891
echo 'foobar-bizz-buzz' > secrets/db.password
8992
93+
# Wait for the pubsub emulator to be ready
94+
echo "[INFO] Waiting for pubsub emulator to be ready for connection"
95+
timeout 5m bash -c 'until curl -s http://localhost:8085 > /dev/null 2>&1; do sleep 2s; done'
96+
97+
# Initialize pubsub topics and subscriptions
98+
export PUBSUB_EMULATOR_HOST=localhost:8085
99+
export PUBSUB_PROJECT_ID=maestro-test
100+
bash hack/init-pubsub-emulator.sh
101+
90102
# Run integration tests with JSON output
91103
make test-integration integration_test_json_output="$test_output_file"
92104
@@ -129,6 +141,16 @@ spec:
129141
volumeMounts:
130142
- mountPath: /mosquitto/data
131143
name: mqttdata
144+
- image: gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators
145+
name: pubsub-test
146+
command:
147+
- gcloud
148+
- beta
149+
- emulators
150+
- pubsub
151+
- start
152+
- --host-port=0.0.0.0:8085
153+
- --project=maestro-test
132154
volumes:
133155
- name: pgdata
134156
emptyDir: {}

Makefile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ test-integration-mqtt:
274274
.PHONY: test-integration-mqtt
275275

276276
test-integration-pubsub:
277-
MESSAGE_BROKER_TYPE=pubsub MESSAGE_BROKER_CONFIG=$(PWD)/secrets/pubsub.config MAESTRO_ENV=testing gotestsum --jsonfile-timing-events=$(pubsub_integration_test_json_output) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS) \
277+
MESSAGE_BROKER_TYPE=pubsub MESSAGE_BROKER_CONFIG=$(PWD)/secrets/pubsub.config PUBSUB_EMULATOR_HOST=localhost:$(pubsub_port) MAESTRO_ENV=testing gotestsum --jsonfile-timing-events=$(pubsub_integration_test_json_output) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS) \
278278
./test/integration
279279
.PHONY: test-integration-pubsub
280280

@@ -481,12 +481,7 @@ pubsub/teardown:
481481

482482
.PHONY: pubsub/init
483483
pubsub/init: pubsub/setup
484-
@echo "Initializing Pub/Sub emulator topics and subscriptions..."
485-
@echo "Waiting for emulator to be ready..."
486-
@for i in {1..30}; do \
487-
curl -s http://localhost:$(pubsub_port) >/dev/null 2>&1 && break || sleep 1; \
488-
done
489-
@PUBSUB_EMULATOR_HOST=localhost:$(pubsub_port) PUBSUB_PROJECT_ID=$(pubsub_project_id) python3 hack/init-pubsub-emulator.py
484+
@PUBSUB_EMULATOR_HOST=localhost:$(pubsub_port) PUBSUB_PROJECT_ID=$(pubsub_project_id) bash hack/init-pubsub-emulator.sh
490485

491486
crc/login:
492487
@echo "Logging into CRC"

hack/init-pubsub-emulator.py

Lines changed: 0 additions & 147 deletions
This file was deleted.

hack/init-pubsub-emulator.sh

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Initialize Google Cloud Pub/Sub emulator with topics and subscriptions for Maestro.
5+
#
6+
# This script creates the necessary topics and subscriptions for the Maestro server
7+
# to communicate with agents using Pub/Sub.
8+
#
9+
# Environment Variables:
10+
# PUBSUB_EMULATOR_HOST: The emulator host (default: localhost:8085)
11+
# PUBSUB_PROJECT_ID: The GCP project ID (default: maestro-test)
12+
# CONSUMER_NAME: Optional consumer name for agent subscriptions
13+
14+
PROJECT_ID="${PUBSUB_PROJECT_ID:-maestro-test}"
15+
EMULATOR_HOST="${PUBSUB_EMULATOR_HOST:-localhost:8085}"
16+
CONSUMER_NAME="${CONSUMER_NAME:-}"
17+
18+
# Base URL for Pub/Sub emulator API
19+
BASE_URL="http://${EMULATOR_HOST}/v1"
20+
21+
echo "Initializing Pub/Sub emulator at ${EMULATOR_HOST}"
22+
echo "Project ID: ${PROJECT_ID}"
23+
24+
# Wait for emulator to be ready
25+
echo "Verifying emulator connectivity..."
26+
max_attempts=30
27+
attempt=0
28+
while [ $attempt -lt $max_attempts ]; do
29+
if curl -s --connect-timeout 2 --max-time 5 "http://${EMULATOR_HOST}" > /dev/null 2>&1; then
30+
echo "Emulator responded! Waiting for API to be fully ready..."
31+
sleep 2
32+
echo "Emulator is ready!"
33+
break
34+
fi
35+
attempt=$((attempt + 1))
36+
if [ $attempt -eq $max_attempts ]; then
37+
echo "Error: Could not connect to Pub/Sub emulator at http://${EMULATOR_HOST}" >&2
38+
echo "Please ensure the emulator is running and accessible." >&2
39+
exit 1
40+
fi
41+
sleep 1
42+
done
43+
44+
# Function to create a topic
45+
create_topic() {
46+
local topic_name=$1
47+
local topic_path="projects/${PROJECT_ID}/topics/${topic_name}"
48+
49+
response=$(curl -s --connect-timeout 5 --max-time 10 -w "\n%{http_code}" -X PUT "${BASE_URL}/${topic_path}" 2>&1)
50+
http_code=$(echo "$response" | tail -n1)
51+
52+
if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then
53+
echo " ✓ Created topic: ${topic_name}"
54+
return 0
55+
elif [ "$http_code" -eq 409 ]; then
56+
echo " - Topic already exists: ${topic_name}"
57+
return 0
58+
else
59+
echo " ✗ Error creating topic ${topic_name}: HTTP ${http_code}" >&2
60+
echo " URL: ${BASE_URL}/${topic_path}" >&2
61+
echo "$response" | head -n-1 >&2
62+
return 1
63+
fi
64+
}
65+
66+
# Function to create a subscription
67+
create_subscription() {
68+
local sub_name=$1
69+
local topic_name=$2
70+
local filter_expr=$3
71+
72+
local sub_path="projects/${PROJECT_ID}/subscriptions/${sub_name}"
73+
local topic_path="projects/${PROJECT_ID}/topics/${topic_name}"
74+
75+
# Build JSON payload using jq for proper JSON escaping
76+
local payload
77+
if [ -n "$filter_expr" ]; then
78+
payload=$(jq -n \
79+
--arg topic "${topic_path}" \
80+
--arg filter "${filter_expr}" \
81+
'{topic: $topic, filter: $filter}')
82+
else
83+
payload=$(jq -n \
84+
--arg topic "${topic_path}" \
85+
'{topic: $topic}')
86+
fi
87+
88+
response=$(curl -s --connect-timeout 5 --max-time 10 -w "\n%{http_code}" -X PUT \
89+
-H "Content-Type: application/json" \
90+
-d "$payload" \
91+
"${BASE_URL}/${sub_path}" 2>&1)
92+
http_code=$(echo "$response" | tail -n1)
93+
94+
if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then
95+
if [ -n "$filter_expr" ]; then
96+
echo " ✓ Created subscription: ${sub_name} (filtered by ${filter_expr})"
97+
else
98+
echo " ✓ Created subscription: ${sub_name}"
99+
fi
100+
return 0
101+
elif [ "$http_code" -eq 409 ]; then
102+
echo " - Subscription already exists: ${sub_name}"
103+
return 0
104+
else
105+
echo " ✗ Error creating subscription ${sub_name}: HTTP ${http_code}" >&2
106+
echo " URL: ${BASE_URL}/${sub_path}" >&2
107+
echo "$response" | head -n-1 >&2
108+
return 1
109+
fi
110+
}
111+
112+
# Create topics
113+
echo "Creating topics..."
114+
topics=(sourceevents sourcebroadcast agentevents agentbroadcast)
115+
for topic in "${topics[@]}"; do
116+
if ! create_topic "$topic"; then
117+
echo "" >&2
118+
echo "✗ Failed to initialize server topics" >&2
119+
exit 1
120+
fi
121+
done
122+
123+
# Create server subscriptions
124+
echo ""
125+
echo "Creating server subscriptions..."
126+
if ! create_subscription "agentevents-maestro" "agentevents" 'attributes."ce-originalsource"="maestro"'; then
127+
echo "" >&2
128+
echo "✗ Failed to initialize server subscriptions" >&2
129+
exit 1
130+
fi
131+
132+
if ! create_subscription "agentbroadcast-maestro" "agentbroadcast" ""; then
133+
echo "" >&2
134+
echo "✗ Failed to initialize server subscriptions" >&2
135+
exit 1
136+
fi
137+
138+
# Create agent subscriptions if consumer name is provided
139+
if [ -n "$CONSUMER_NAME" ]; then
140+
echo ""
141+
echo "Creating agent subscriptions for consumer '${CONSUMER_NAME}'..."
142+
143+
if ! create_subscription "sourceevents-${CONSUMER_NAME}" "sourceevents" "attributes.\"ce-clustername\"=\"${CONSUMER_NAME}\""; then
144+
echo "" >&2
145+
echo "✗ Failed to initialize agent subscriptions for ${CONSUMER_NAME}" >&2
146+
exit 1
147+
fi
148+
149+
if ! create_subscription "sourcebroadcast-${CONSUMER_NAME}" "sourcebroadcast" ""; then
150+
echo "" >&2
151+
echo "✗ Failed to initialize agent subscriptions for ${CONSUMER_NAME}" >&2
152+
exit 1
153+
fi
154+
fi
155+
156+
echo ""
157+
echo "✓ Pub/Sub emulator initialized successfully!"

0 commit comments

Comments
 (0)