127 lines
4.9 KiB
Bash
127 lines
4.9 KiB
Bash
#!/usr/bin/env bats
|
|
|
|
setup() {
|
|
source tests/helpers/mock-api.sh
|
|
export DISPATCH_POLL_INTERVAL="0.1"
|
|
}
|
|
|
|
teardown() {
|
|
mock_stop
|
|
}
|
|
|
|
@test "dispatch succeeds: POST 201, poll running x3 then success → exit 0" {
|
|
mock_set_sequence '[
|
|
{"code":201},
|
|
{"code":200,"body":{"workflow_runs":[{"id":1,"status":"running"}]}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"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"}' "http://localhost:18080" "test-token-abc123"
|
|
[ "$status" -eq 0 ]
|
|
}
|
|
|
|
@test "dispatch: poll returns failure conclusion → exit 1" {
|
|
mock_set_sequence '[
|
|
{"code":201},
|
|
{"code":200,"body":{"workflow_runs":[{"id":1,"status":"running"}]}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"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"}' "http://localhost:18080" "test-token-abc123"
|
|
[ "$status" -eq 1 ]
|
|
}
|
|
|
|
@test "dispatch: poll returns cancelled conclusion → exit 1" {
|
|
mock_set_sequence '[
|
|
{"code":201},
|
|
{"code":200,"body":{"workflow_runs":[{"id":1,"status":"running"}]}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"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"}' "http://localhost:18080" "test-token-abc123"
|
|
[ "$status" -eq 1 ]
|
|
}
|
|
|
|
@test "timeout: poll never completes, exceeds timeout_minutes → exit 124" {
|
|
mock_set_sequence '[
|
|
{"code":201},
|
|
{"code":200,"body":{"workflow_runs":[{"id":1,"status":"running"}]}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"code":200,"body":{"id":1,"status":"running"}},
|
|
{"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"}' "http://localhost:18080" "test-token-abc123" "0.001"
|
|
[ "$status" -eq 124 ]
|
|
}
|
|
|
|
@test "dispatch API returns 500 → exit 1" {
|
|
mock_set_sequence '[
|
|
{"code":500}
|
|
]'
|
|
mock_start
|
|
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 ]
|
|
}
|
|
|
|
@test "POST dispatch is called with correct URL and payload" {
|
|
mock_set_sequence '[
|
|
{"code":201},
|
|
{"code":200,"body":{"workflow_runs":[{"id":1,"status":"running"}]}},
|
|
{"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"}' "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"* ]]
|
|
method=$(mock_get_first_request_method)
|
|
[[ "$method" == "POST" ]]
|
|
body=$(mock_get_first_request_body)
|
|
[[ "$body" == *'"ref":"main"'* ]]
|
|
[[ "$body" == *'"inputs"'* ]]
|
|
[[ "$body" == *'"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"* ]]
|
|
}
|
|
|
|
@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"* ]]
|
|
}
|
|
|
|
@test "missing target_repo argument → exit 1" {
|
|
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" '{}' "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" "" '{}' "http://localhost:18080" "test-token-abc123"
|
|
[ "$status" -eq 1 ]
|
|
}
|
|
|
|
@test "missing inputs_json argument → exit 1" {
|
|
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" "" "http://localhost:18080" "test-token-abc123"
|
|
[ "$status" -eq 1 ]
|
|
}
|