107 lines
3.7 KiB
Bash
107 lines
3.7 KiB
Bash
#!/usr/bin/env sh
|
|
set -eu
|
|
|
|
api_base="${CONFIGCENTER_API_BASE:-http://127.0.0.1:8080}"
|
|
app_code="config-smoke-$(date +%s)-$$"
|
|
app_id=""
|
|
|
|
json_data_field() {
|
|
field="$1"
|
|
python3 -c 'import json,sys; payload=json.load(sys.stdin)["data"]; print(payload[sys.argv[1]])' "$field"
|
|
}
|
|
|
|
cleanup() {
|
|
if [ -n "$app_id" ]; then
|
|
curl -fsS -X DELETE -H 'X-User: integration-smoke' "$api_base/v1/applications/$app_id" >/dev/null || true
|
|
fi
|
|
}
|
|
trap cleanup EXIT INT TERM
|
|
|
|
curl -fsS "$api_base/health/ready" >/dev/null
|
|
|
|
app_response="$(curl -fsS -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-H 'X-User: integration-smoke' \
|
|
-d "{\"code\":\"$app_code\",\"name\":\"Integration Smoke\"}" \
|
|
"$api_base/v1/applications")"
|
|
app_id="$(printf '%s' "$app_response" | json_data_field id)"
|
|
|
|
env_id="$(curl -fsS "$api_base/v1/environments" | python3 -c '
|
|
import json,sys
|
|
for item in json.load(sys.stdin)["data"]:
|
|
if item["code"] == "DEV":
|
|
print(item["id"])
|
|
break
|
|
else:
|
|
raise SystemExit("DEV environment not found")
|
|
')"
|
|
|
|
namespace_response="$(curl -fsS -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-H 'X-User: integration-smoke' \
|
|
-d "{\"appId\":$app_id,\"name\":\"application\",\"type\":\"properties\"}" \
|
|
"$api_base/v1/namespaces")"
|
|
namespace_id="$(printf '%s' "$namespace_response" | json_data_field id)"
|
|
|
|
curl -fsS -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-H 'X-User: integration-smoke' \
|
|
-d "{\"appId\":$app_id,\"nsId\":$namespace_id,\"envId\":$env_id,\"key\":\"smoke.enabled\",\"value\":\"true\"}" \
|
|
"$api_base/v1/config-items" >/dev/null
|
|
|
|
release_response="$(curl -fsS -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-H 'X-User: integration-smoke' \
|
|
-d "{\"appId\":$app_id,\"nsId\":$namespace_id,\"envId\":$env_id,\"comment\":\"integration smoke\"}" \
|
|
"$api_base/v1/publish")"
|
|
release_id="$(printf '%s' "$release_response" | json_data_field id)"
|
|
|
|
attempt=0
|
|
status="pending"
|
|
while [ "$attempt" -lt 50 ]; do
|
|
release_response="$(curl -fsS "$api_base/v1/releases/$release_id")"
|
|
status="$(printf '%s' "$release_response" | json_data_field status)"
|
|
if [ "$status" = "applied" ]; then
|
|
break
|
|
fi
|
|
if [ "$status" = "failed" ]; then
|
|
printf 'release failed: %s\n' "$release_response" >&2
|
|
exit 1
|
|
fi
|
|
attempt=$((attempt + 1))
|
|
sleep 0.2
|
|
done
|
|
|
|
if [ "$status" != "applied" ]; then
|
|
printf 'release did not become applied (last status: %s)\n' "$status" >&2
|
|
exit 1
|
|
fi
|
|
|
|
runtime_response="$(curl -fsS "$api_base/v1/config?env=DEV&app=$app_code&namespace=application")"
|
|
printf '%s' "$runtime_response" | python3 -c '
|
|
import json,sys
|
|
data=json.load(sys.stdin)["data"]
|
|
assert data["items"]["smoke.enabled"] == "true", data
|
|
assert data["releaseVersion"] == 1, data
|
|
assert data["revision"] > 0, data
|
|
'
|
|
|
|
gray_response="$(curl -fsS -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-H 'X-User: integration-smoke' \
|
|
-d "{\"appId\":$app_id,\"nsId\":$namespace_id,\"envId\":$env_id,\"ruleType\":\"instance\",\"ruleValue\":{\"instances\":[\"smoke-1\"]},\"overrides\":{\"smoke.enabled\":\"gray\"},\"enabled\":true,\"priority\":10,\"desc\":\"smoke rule\"}" \
|
|
"$api_base/v1/gray-rules")"
|
|
gray_id="$(printf '%s' "$gray_response" | json_data_field id)"
|
|
|
|
gray_runtime_response="$(curl -fsS "$api_base/v1/config?env=DEV&app=$app_code&namespace=application&instance=smoke-1")"
|
|
printf '%s' "$gray_runtime_response" | python3 -c '
|
|
import json,sys
|
|
data=json.load(sys.stdin)["data"]
|
|
assert data["items"]["smoke.enabled"] == "gray", data
|
|
assert len(data["grayRuleIds"]) == 1, data
|
|
'
|
|
|
|
curl -fsS "$api_base/metrics" | grep -q '^configcenter_gray_rule_matches_total 1$'
|
|
|
|
printf 'integration smoke passed: release=%s gray_rule=%s status=%s\n' "$release_id" "$gray_id" "$status"
|