pipeline siivous ja testikattavuuden nosto (#9)
Co-authored-by: moilanik <niko.moilanen@tietoevry.com> Reviewed-on: #9
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup() {
|
||||
export CONF_FILE=$(mktemp)
|
||||
export CI_CONF_FILE="$CONF_FILE"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -f "$CONF_FILE"
|
||||
}
|
||||
|
||||
@test "missing config file → exit 1" {
|
||||
export CI_CONF_FILE="/nonexistent/path/$(date +%s).conf"
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* ]]
|
||||
}
|
||||
|
||||
@test "empty value in config → exit 1" {
|
||||
cat > "$CONF_FILE" <<EOF
|
||||
SOME_KEY=
|
||||
EOF
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"empty"* ]]
|
||||
}
|
||||
|
||||
@test "invalid URL in config → exit 1" {
|
||||
cat > "$CONF_FILE" <<EOF
|
||||
API_URL=not-a-url
|
||||
EOF
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_TOKEN secret → exit 1" {
|
||||
cat > "$CONF_FILE" <<EOF
|
||||
SOME_KEY=ok
|
||||
EOF
|
||||
unset GITEA_TOKEN
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITEA_TOKEN"* ]]
|
||||
}
|
||||
|
||||
@test "missing GIT_PAGES_PUBLISH_TOKEN secret → exit 1" {
|
||||
cat > "$CONF_FILE" <<EOF
|
||||
SOME_KEY=ok
|
||||
EOF
|
||||
export GITEA_TOKEN="sometoken"
|
||||
unset GIT_PAGES_PUBLISH_TOKEN
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GIT_PAGES_PUBLISH_TOKEN"* ]]
|
||||
}
|
||||
|
||||
@test "valid config and all secrets → exit 0" {
|
||||
cat > "$CONF_FILE" <<EOF
|
||||
API_URL=https://example.com
|
||||
ANOTHER=https://test.fi
|
||||
EOF
|
||||
export GITEA_TOKEN="sometoken"
|
||||
export GIT_PAGES_PUBLISH_TOKEN="sometoken"
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "comment and blank lines are ignored → exit 0" {
|
||||
cat > "$CONF_FILE" <<EOF
|
||||
# this is a comment
|
||||
|
||||
VALID_URL=https://example.com
|
||||
EOF
|
||||
export GITEA_TOKEN="sometoken"
|
||||
export GIT_PAGES_PUBLISH_TOKEN="sometoken"
|
||||
run bash scripts/ci-validate.sh
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
setup() {
|
||||
source tests/helpers/mock-api.sh
|
||||
export GITEA_API_URL="http://localhost:18080"
|
||||
export GITEA_TOKEN="test-token-abc123"
|
||||
export DISPATCH_POLL_INTERVAL="0.1"
|
||||
}
|
||||
|
||||
@@ -21,7 +19,7 @@ teardown() {
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"success"}}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@@ -33,7 +31,7 @@ teardown() {
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"failure"}}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@@ -45,7 +43,7 @@ teardown() {
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"cancelled"}}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@@ -63,7 +61,7 @@ teardown() {
|
||||
{"code":200,"body":{"id":1,"status":"running"}}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "0.001"
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" "test-token-abc123" "0.001"
|
||||
[ "$status" -eq 124 ]
|
||||
}
|
||||
|
||||
@@ -72,7 +70,7 @@ teardown() {
|
||||
{"code":500}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@@ -83,7 +81,7 @@ teardown() {
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"success"}}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 0 ]
|
||||
path=$(mock_get_first_request_path)
|
||||
[[ "$path" == *"/api/v1/repos/test-owner/test-repo/actions/workflows/test.yml/dispatches"* ]]
|
||||
@@ -95,36 +93,45 @@ teardown() {
|
||||
[[ "$body" == *'"version":"1.2.3"'* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_API_URL → exit 1 with error message" {
|
||||
unset GITEA_API_URL
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
@test "missing gitea_api_url argument → exit 1 with error message" {
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"GITEA_API_URL"* ]]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"gitea_api_url"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_TOKEN → exit 1 with error message" {
|
||||
unset GITEA_TOKEN
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}'
|
||||
@test "missing gitea_token argument → exit 1 with error message" {
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{"version":"1.2.3"}' "http://localhost:18080" ""
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"GITEA_TOKEN"* ]]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"gitea_token"* ]]
|
||||
}
|
||||
|
||||
@test "missing target_repo argument → exit 1" {
|
||||
run bash scripts/dispatch-workflow.sh "" "test.yml" "main" '{}'
|
||||
run bash scripts/dispatch-workflow.sh "" "test.yml" "main" '{}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "missing workflow_file argument → exit 1" {
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "" "main" '{}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "" "main" '{}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "missing ref argument → exit 1" {
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "" '{}'
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "" '{}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "dispatch: no workflow run found after dispatch → exit 1" {
|
||||
mock_set_sequence '[
|
||||
{"code":201},
|
||||
{"code":200,"body":{"workflow_runs":[]}}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{}' "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* ]]
|
||||
}
|
||||
|
||||
@test "missing inputs_json argument → exit 1" {
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" ""
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" "" "http://localhost:18080" "test-token-abc123"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@@ -26,19 +26,8 @@ function bashQuiet(cmd) {
|
||||
});
|
||||
}
|
||||
|
||||
function envBlock() {
|
||||
return [
|
||||
'export GITEA_API_URL="http://localhost:18080"',
|
||||
'export GITEA_TOKEN="test-token-abc123"',
|
||||
'export GITHUB_REPOSITORY="test-owner/test-repo"',
|
||||
'export GITHUB_SHA="abc123def456789012345678901234567890abcd"',
|
||||
'export GITHUB_SERVER_URL="https://gitea.example.com"',
|
||||
'export GITHUB_RUN_ID="42"',
|
||||
].join('; ');
|
||||
}
|
||||
|
||||
function runReportStatus(args) {
|
||||
return bash(`${envBlock()}; bash "${REPORT_SCRIPT}" ${args}`);
|
||||
return bash(`export GITEA_API_URL="http://localhost:18080" GITEA_TOKEN="test-token-abc123" GIT_PAGES_URL="https://reports.example.com" GITHUB_REPOSITORY="test-owner/test-repo" GITHUB_SHA="abc123def456789012345678901234567890abcd" GITHUB_RUN_ID="42"; bash "${REPORT_SCRIPT}" ${args}`);
|
||||
}
|
||||
|
||||
function getMockBody() {
|
||||
@@ -50,7 +39,7 @@ function getMockPath() {
|
||||
}
|
||||
|
||||
When('a build step starts executing', function () {
|
||||
const r = runReportStatus('pending "Building project" "http://example.com/build/42"');
|
||||
const r = runReportStatus('pending "Building project"');
|
||||
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}: ${r.stderr}`);
|
||||
});
|
||||
|
||||
@@ -61,18 +50,18 @@ Then('the commit shows a pending status with a description of the step', functio
|
||||
});
|
||||
|
||||
When('a build step completes successfully and reports its results', function () {
|
||||
const r = runReportStatus('success "Unit tests OK" "http://example.com/reports/cucumber.html" "unit-test"');
|
||||
const r = runReportStatus('success "Unit tests OK" unit-test cucumber/');
|
||||
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}`);
|
||||
});
|
||||
|
||||
Then('the commit shows a success status with a clickable link to the results', function () {
|
||||
const body = getMockBody();
|
||||
if (!body.includes('"state":"success"')) throw new Error('Expected success status');
|
||||
if (!body.includes('"target_url":"http://example.com/reports/cucumber.html"')) throw new Error('Expected URL');
|
||||
if (!body.includes('"target_url":"https://reports.example.com/test-owner/test-repo/reports/abc123de/cucumber/"')) throw new Error('Expected URL');
|
||||
});
|
||||
|
||||
When('a build step fails', function () {
|
||||
const r = runReportStatus('failure "Tests failed: 3 of 10" "http://example.com/build/42"');
|
||||
const r = runReportStatus('failure "Tests failed: 3 of 10"');
|
||||
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}`);
|
||||
});
|
||||
|
||||
@@ -83,11 +72,11 @@ Then('the commit shows a failure status with a description of what went wrong',
|
||||
});
|
||||
|
||||
When('several build steps each report their own status to the same commit', function () {
|
||||
runReportStatus('pending "Build started" "http://example.com/build/42" "ci-build"');
|
||||
runReportStatus('pending "Build started" ci-build');
|
||||
execSync('sleep 0.3', { stdio: 'ignore' });
|
||||
runReportStatus('success "Unit tests passed" "http://example.com/reports/unit.html" "unit-test"');
|
||||
runReportStatus('success "Unit tests passed" unit-test');
|
||||
execSync('sleep 0.3', { stdio: 'ignore' });
|
||||
runReportStatus('success "Integration tests passed" "http://example.com/reports/integration.html" "integration-test"');
|
||||
runReportStatus('success "Integration tests passed" integration-test');
|
||||
});
|
||||
|
||||
Then('each status appears under a unique label on the commit', function () {
|
||||
@@ -104,7 +93,7 @@ Then('each status appears under a unique label on the commit', function () {
|
||||
});
|
||||
|
||||
When('a deployment finishes for a commit that originated from another repository', function () {
|
||||
const r = runReportStatus('success "Deployed to staging" "http://example.com/deploy/42" "deploy-staging" "rootabc123" "services/temperature-store"');
|
||||
const r = runReportStatus('success "Deployed to staging" deploy-staging');
|
||||
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}`);
|
||||
});
|
||||
|
||||
@@ -114,15 +103,15 @@ Then('the source commit shows the deployment status alongside the build status',
|
||||
if (!body.includes('"context":"deploy-staging"')) throw new Error('Expected deploy-staging context');
|
||||
|
||||
const pathStr = getMockPath();
|
||||
if (!pathStr.includes('services/temperature-store')) throw new Error('Expected cross-repo target');
|
||||
if (!pathStr.includes('rootabc123')) throw new Error('Expected root commit');
|
||||
if (!pathStr.includes('test-owner/test-repo')) throw new Error('Expected default repo target');
|
||||
if (!pathStr.includes('abc123def456789012345678901234567890abcd')) throw new Error('Expected default commit');
|
||||
});
|
||||
|
||||
When('a build step tries to report status but the build system is unavailable', function () {
|
||||
bashQuiet(`source "${MOCK_SCRIPT}" && mock_stop`);
|
||||
execSync('sleep 0.3', { stdio: 'ignore' });
|
||||
bashQuiet(`source "${MOCK_SCRIPT}" && mock_set_response 500 && mock_start`);
|
||||
const r = runReportStatus('success "Should fail" "http://example.com"');
|
||||
const r = runReportStatus('success "Should fail"');
|
||||
this.reportStatusFailed = (r.status !== 0);
|
||||
});
|
||||
|
||||
|
||||
@@ -21,14 +21,6 @@ function bash(cmd) {
|
||||
}
|
||||
}
|
||||
|
||||
function envBlock() {
|
||||
return [
|
||||
'export GITEA_API_URL="http://localhost:18080"',
|
||||
'export GITEA_TOKEN="test-token-abc123"',
|
||||
'export DISPATCH_POLL_INTERVAL="0.1"',
|
||||
].join('; ');
|
||||
}
|
||||
|
||||
function setupMock(seqJson) {
|
||||
execSync('lsof -ti :18080 2>/dev/null | xargs -r kill -9 2>/dev/null || true', { stdio: 'ignore' });
|
||||
execSync('sleep 0.4', { stdio: 'ignore' });
|
||||
@@ -62,7 +54,7 @@ function setupMock(seqJson) {
|
||||
}
|
||||
|
||||
function runDispatch(args) {
|
||||
return bash(`${envBlock()}; bash "${DISPATCH_SCRIPT}" ${args}`);
|
||||
return bash(`export DISPATCH_POLL_INTERVAL="0.1"; bash "${DISPATCH_SCRIPT}" ${args}`);
|
||||
}
|
||||
|
||||
Given('a deployment has completed in the target environment', function () {
|
||||
@@ -77,7 +69,7 @@ When('a test workflow is dispatched to a test project', function () {
|
||||
{ code: 200, body: { workflow_runs: [{ id: 1, status: 'running' }] } },
|
||||
{ code: 200, body: { id: 1, status: 'completed', conclusion: 'success' } },
|
||||
]));
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\'');
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\' "http://localhost:18080" "test-token-abc123"');
|
||||
this.dispatchResult = r.status;
|
||||
});
|
||||
|
||||
@@ -95,7 +87,7 @@ When('a test workflow is dispatched and the tests fail', function () {
|
||||
{ code: 200, body: { workflow_runs: [{ id: 1, status: 'running' }] } },
|
||||
{ code: 200, body: { id: 1, status: 'completed', conclusion: 'failure' } },
|
||||
]));
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\'');
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\' "http://localhost:18080" "test-token-abc123"');
|
||||
this.dispatchResult = r.status;
|
||||
});
|
||||
|
||||
@@ -111,7 +103,7 @@ When('a test workflow is dispatched but does not finish within the allowed time'
|
||||
{ code: 200, body: { id: 1, status: 'running' } },
|
||||
{ code: 200, body: { id: 1, status: 'running' } },
|
||||
]));
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\' "0.001"');
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\' "http://localhost:18080" "test-token-abc123" "0.001"');
|
||||
this.dispatchResult = r.status;
|
||||
});
|
||||
|
||||
|
||||
@@ -13,12 +13,12 @@ _kill_port() {
|
||||
local pids
|
||||
pids=$(lsof -ti ":$MOCK_PORT" 2>/dev/null) || true
|
||||
[ -n "$pids" ] && kill -9 $pids 2>/dev/null || true
|
||||
sleep 0.3
|
||||
sleep 0.5
|
||||
}
|
||||
|
||||
_wait_port_free() {
|
||||
local i=0
|
||||
while lsof -ti ":$MOCK_PORT" >/dev/null 2>&1 && [ $i -lt 30 ]; do
|
||||
while lsof -ti ":$MOCK_PORT" >/dev/null 2>&1 && [ $i -lt 50 ]; do
|
||||
sleep 0.1
|
||||
i=$((i + 1))
|
||||
done
|
||||
|
||||
@@ -46,7 +46,7 @@ class H(http.server.BaseHTTPRequestHandler):
|
||||
def _log_request(self, method):
|
||||
path = self.path
|
||||
content_len = int(self.headers.get('Content-Length', 0))
|
||||
body = self.rfile.read(content_len).decode() if content_len else ''
|
||||
body = self.rfile.read(content_len).decode(errors='replace') if content_len else ''
|
||||
line = f'{method} {path}\n{body}\n'
|
||||
with open(REQ_FILE, 'a') as f:
|
||||
f.write(line)
|
||||
@@ -67,6 +67,14 @@ class H(http.server.BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
self.wfile.write(body.encode())
|
||||
|
||||
def do_PATCH(self):
|
||||
self._log_request('PATCH')
|
||||
code, body = self._get_response()
|
||||
self.send_response(code)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(body.encode())
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup() {
|
||||
source tests/helpers/mock-api.sh
|
||||
export GITEA_API_URL="http://localhost:18080"
|
||||
export GIT_PAGES_URL="http://localhost:18080"
|
||||
export GIT_PAGES_PUBLISH_TOKEN="publish-token-abc"
|
||||
export GITHUB_REPOSITORY="test-owner/test-repo"
|
||||
export GITHUB_SHA="abc123def456789012345678901234567890abcd"
|
||||
|
||||
REPORT_DIR="reports/abc123de/unit-tests"
|
||||
mkdir -p "$REPORT_DIR"
|
||||
echo "<html>test</html>" > "$REPORT_DIR/index.html"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
mock_stop
|
||||
rm -rf "reports/abc123de"
|
||||
}
|
||||
|
||||
@test "missing suite_path argument → exit 1" {
|
||||
run bash scripts/publish-git-pages.sh ""
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_API_URL → exit 1" {
|
||||
unset GITEA_API_URL
|
||||
run bash scripts/publish-git-pages.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITEA_API_URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GIT_PAGES_URL → exit 1" {
|
||||
unset GIT_PAGES_URL
|
||||
run bash scripts/publish-git-pages.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GIT_PAGES_URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GIT_PAGES_PUBLISH_TOKEN → exit 1" {
|
||||
unset GIT_PAGES_PUBLISH_TOKEN
|
||||
run bash scripts/publish-git-pages.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GIT_PAGES_PUBLISH_TOKEN"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITHUB_REPOSITORY → exit 1" {
|
||||
unset GITHUB_REPOSITORY
|
||||
run bash scripts/publish-git-pages.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITHUB_REPOSITORY"* ]]
|
||||
}
|
||||
|
||||
@test "suite path is not a directory → exit 1" {
|
||||
run bash scripts/publish-git-pages.sh "nonexistent"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"not a directory"* ]]
|
||||
}
|
||||
|
||||
@test "valid publish returns report base URL" {
|
||||
mock_set_sequence '[
|
||||
{"code":200,"body":"published"}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/publish-git-pages.sh "unit-tests"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == "http://localhost:18080/test-owner/test-repo/reports/abc123de" ]]
|
||||
}
|
||||
|
||||
@test "publish with suite subpath" {
|
||||
mkdir -p "reports/abc123de/sub/suite"
|
||||
echo "sub" > "reports/abc123de/sub/suite/result.html"
|
||||
mock_set_sequence '[
|
||||
{"code":200,"body":"published"}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/publish-git-pages.sh "sub/suite"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == "http://localhost:18080/test-owner/test-repo/reports/abc123de" ]]
|
||||
}
|
||||
|
||||
@test "git-pages returns HTTP 500 → exit 1" {
|
||||
mock_set_sequence '[
|
||||
{"code":500,"body":"internal error"}
|
||||
]'
|
||||
mock_start
|
||||
run bash scripts/publish-git-pages.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"500"* ]]
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup() {
|
||||
export GITEA_API_URL="http://localhost:18080"
|
||||
export GITEA_TOKEN="test-token-abc"
|
||||
export GIT_PAGES_URL="http://localhost:18080"
|
||||
export GIT_PAGES_PUBLISH_TOKEN="publish-token-abc"
|
||||
export GITHUB_REPOSITORY="test-owner/test-repo"
|
||||
export GITHUB_SHA="abc123def456789012345678901234567890abcd"
|
||||
}
|
||||
|
||||
@test "missing suite_path argument → exit 1" {
|
||||
run bash scripts/publish.sh ""
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_API_URL → exit 1" {
|
||||
unset GITEA_API_URL
|
||||
run bash scripts/publish.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITEA_API_URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_TOKEN → exit 1" {
|
||||
unset GITEA_TOKEN
|
||||
run bash scripts/publish.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITEA_TOKEN"* ]]
|
||||
}
|
||||
|
||||
@test "missing GIT_PAGES_URL → exit 1" {
|
||||
unset GIT_PAGES_URL
|
||||
run bash scripts/publish.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GIT_PAGES_URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GIT_PAGES_PUBLISH_TOKEN → exit 1" {
|
||||
unset GIT_PAGES_PUBLISH_TOKEN
|
||||
run bash scripts/publish.sh "unit-tests"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GIT_PAGES_PUBLISH_TOKEN"* ]]
|
||||
}
|
||||
+31
-51
@@ -4,9 +4,9 @@ setup() {
|
||||
source tests/helpers/mock-api.sh
|
||||
export GITEA_API_URL="http://localhost:18080"
|
||||
export GITEA_TOKEN="test-token-abc123"
|
||||
export GIT_PAGES_URL="https://reports.example.com"
|
||||
export GITHUB_REPOSITORY="test-owner/test-repo"
|
||||
export GITHUB_SHA="abc123def456789012345678901234567890abcd"
|
||||
export GITHUB_SERVER_URL="https://gitea.example.com"
|
||||
export GITHUB_RUN_ID="42"
|
||||
}
|
||||
|
||||
@@ -16,68 +16,40 @@ teardown() {
|
||||
|
||||
@test "pending status is POSTed with correct payload" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh pending "Building project" "http://example.com/build/42"
|
||||
run bash scripts/report-status.sh pending "Building project"
|
||||
[ "$status" -eq 0 ]
|
||||
path=$(mock_get_request_path)
|
||||
[[ "$path" == "/api/v1/repos/test-owner/test-repo/statuses/abc123def456789012345678901234567890abcd" ]]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"state":"pending"'* ]]
|
||||
[[ "$body" == *'"description":"Building project"'* ]]
|
||||
[[ "$body" == *'"target_url":"http://example.com/build/42"'* ]]
|
||||
[[ "$body" == *'"target_url":"http://localhost:18080/test-owner/test-repo/actions/runs/42"'* ]]
|
||||
method=$(mock_get_request_method)
|
||||
[[ "$method" == "POST" ]]
|
||||
}
|
||||
|
||||
@test "success status with url and custom key" {
|
||||
@test "success status with custom key and suite builds report URL" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh success "Unit tests OK" "http://example.com/reports/cucumber.html" "unit-test"
|
||||
run bash scripts/report-status.sh success "Unit tests OK" unit-test cucumber
|
||||
[ "$status" -eq 0 ]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"state":"success"'* ]]
|
||||
[[ "$body" == *'"description":"Unit tests OK"'* ]]
|
||||
[[ "$body" == *'"target_url":"http://example.com/reports/cucumber.html"'* ]]
|
||||
[[ "$body" == *'"context":"unit-test"'* ]]
|
||||
[[ "$body" == *'"target_url":"https://reports.example.com/test-owner/test-repo/reports/abc123de/cucumber/"'* ]]
|
||||
}
|
||||
|
||||
@test "failure status is POSTed correctly" {
|
||||
@test "failure status constructs run URL when no suite" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh failure "Tests failed: 3 of 10" "http://example.com/build/42"
|
||||
run bash scripts/report-status.sh failure "Tests failed: 3 of 10"
|
||||
[ "$status" -eq 0 ]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"state":"failure"'* ]]
|
||||
[[ "$body" == *'"description":"Tests failed: 3 of 10"'* ]]
|
||||
}
|
||||
|
||||
@test "error status is POSTed correctly" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh error "Build timed out" "http://example.com/build/42"
|
||||
[ "$status" -eq 0 ]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"state":"error"'* ]]
|
||||
}
|
||||
|
||||
@test "cross-repo: root_commit and root_repo override target" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh success "Deployed to staging" "http://example.com/deploy/42" "deploy-staging" "rootabc123" "services/temperature-store"
|
||||
[ "$status" -eq 0 ]
|
||||
path=$(mock_get_request_path)
|
||||
[[ "$path" == "/api/v1/repos/services/temperature-store/statuses/rootabc123" ]]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"state":"success"'* ]]
|
||||
[[ "$body" == *'"context":"deploy-staging"'* ]]
|
||||
}
|
||||
|
||||
@test "cross-repo: only root_commit without root_repo is ignored" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh success "Partial cross-repo" "http://example.com" "my-key" "abc"
|
||||
[ "$status" -eq 0 ]
|
||||
path=$(mock_get_request_path)
|
||||
[[ "$path" == "/api/v1/repos/test-owner/test-repo/statuses/abc123def456789012345678901234567890abcd" ]]
|
||||
[[ "$body" == *'"target_url":"http://localhost:18080/test-owner/test-repo/actions/runs/42"'* ]]
|
||||
}
|
||||
|
||||
@test "default key when not provided" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh pending "Build started" "http://example.com/build/42"
|
||||
run bash scripts/report-status.sh pending "Build started"
|
||||
[ "$status" -eq 0 ]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"context":"commit-abc123de"'* ]]
|
||||
@@ -86,38 +58,46 @@ teardown() {
|
||||
@test "API returns 500 causes exit 1" {
|
||||
mock_set_response 500
|
||||
mock_start
|
||||
run bash scripts/report-status.sh success "Should fail" "http://example.com"
|
||||
run bash scripts/report-status.sh success "Should fail"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "cross-repo: ROOT_COMMIT and ROOT_REPO override target" {
|
||||
export ROOT_COMMIT="rootabc123"
|
||||
export ROOT_REPO="services/temperature-store"
|
||||
mock_start
|
||||
run bash scripts/report-status.sh success "Deployed to staging" deploy-staging
|
||||
[ "$status" -eq 0 ]
|
||||
path=$(mock_get_request_path)
|
||||
[[ "$path" == "/api/v1/repos/services/temperature-store/statuses/rootabc123" ]]
|
||||
body=$(mock_get_request_body)
|
||||
[[ "$body" == *'"state":"success"'* ]]
|
||||
[[ "$body" == *'"context":"deploy-staging"'* ]]
|
||||
unset ROOT_COMMIT ROOT_REPO
|
||||
}
|
||||
|
||||
@test "missing GITEA_API_URL causes exit 1 with error message" {
|
||||
unset GITEA_API_URL
|
||||
run bash scripts/report-status.sh pending "Test" "http://example.com"
|
||||
run bash scripts/report-status.sh pending "Test"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* ]] || [[ "$output" == *"GITEA_API_URL"* ]]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"GITEA_API_URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_TOKEN causes exit 1 with error message" {
|
||||
unset GITEA_TOKEN
|
||||
run bash scripts/report-status.sh pending "Test" "http://example.com"
|
||||
run bash scripts/report-status.sh pending "Test"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"ERROR"* ]] || [[ "$output" == *"GITEA_TOKEN"* ]]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"GITEA_TOKEN"* ]]
|
||||
}
|
||||
|
||||
@test "missing required state argument causes exit 1" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh "" "desc" "http://example.com"
|
||||
run bash scripts/report-status.sh "" "desc"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "missing required description argument causes exit 1" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh pending "" "http://example.com"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "missing required url argument causes exit 1" {
|
||||
mock_start
|
||||
run bash scripts/report-status.sh pending "desc" ""
|
||||
run bash scripts/report-status.sh pending ""
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user