Feature/gitops (#37)
CI Git-Pages Main / Load git-pages.gitea-env.conf to pipeline env (push) Successful in 34s
CI Main / Check existing artifact (push) Successful in 22s
CI Git-Pages Main / Build & Push Helm chart (push) Successful in 48s
CI Main / Bats tests (push) Successful in 1m34s
acc-tests Cucumber test report
CI Main / Cucumber tests (push) Successful in 1m45s
CI Main / Load example-gitea-env.conf to pipeline env (push) Successful in 34s
CI Git-Pages Main / Check existing artifact (push) Successful in 21s
ci-helm-build-push Helm push 0.1.5
unit-tests Bats test report
CI Git-Pages Main / Update chart to the cluster (push) Failing after 0s
ci-docker-build-push Docker push 0.2.25
CI Git-Pages Main / Report Summary (push) Successful in 7s
CI Main / Build & Push Docker (push) Successful in 44s
CI Main / GitOps (push) Failing after 22s
CI Main / Move provider version tag (push) Has been skipped
CI Main / Report Summary (push) Successful in 6s
CI Git-Pages Main / Load git-pages.gitea-env.conf to pipeline env (push) Successful in 34s
CI Main / Check existing artifact (push) Successful in 22s
CI Git-Pages Main / Build & Push Helm chart (push) Successful in 48s
CI Main / Bats tests (push) Successful in 1m34s
acc-tests Cucumber test report
CI Main / Cucumber tests (push) Successful in 1m45s
CI Main / Load example-gitea-env.conf to pipeline env (push) Successful in 34s
CI Git-Pages Main / Check existing artifact (push) Successful in 21s
ci-helm-build-push Helm push 0.1.5
unit-tests Bats test report
CI Git-Pages Main / Update chart to the cluster (push) Failing after 0s
ci-docker-build-push Docker push 0.2.25
CI Git-Pages Main / Report Summary (push) Successful in 7s
CI Main / Build & Push Docker (push) Successful in 44s
CI Main / GitOps (push) Failing after 22s
CI Main / Move provider version tag (push) Has been skipped
CI Main / Report Summary (push) Successful in 6s
Co-authored-by: moilanik <niko.moilanen@tietoevry.com> Reviewed-on: #37
This commit was merged in pull request #37.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
setup() {
|
||||
source tests/helpers/mock-api.sh
|
||||
export DISPATCH_POLL_INTERVAL="0.1"
|
||||
export DISPATCH_ID="test123"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
@@ -12,8 +13,7 @@ teardown() {
|
||||
@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":{"workflow_runs":[{"id":1,"display_title":"POC (test123)","run_number":42,"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"}}
|
||||
@@ -26,7 +26,7 @@ teardown() {
|
||||
@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":{"workflow_runs":[{"id":1,"display_title":"POC (test123)","run_number":42,"status":"running"}]}},
|
||||
{"code":200,"body":{"id":1,"status":"running"}},
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"failure"}}
|
||||
]'
|
||||
@@ -38,7 +38,7 @@ teardown() {
|
||||
@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":{"workflow_runs":[{"id":1,"display_title":"POC (test123)","run_number":42,"status":"running"}]}},
|
||||
{"code":200,"body":{"id":1,"status":"running"}},
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"cancelled"}}
|
||||
]'
|
||||
@@ -47,18 +47,18 @@ teardown() {
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "timeout: poll never completes, exceeds timeout_minutes → exit 124" {
|
||||
@test "timeout: no matching run found, 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"}}
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}},
|
||||
{"code":200,"body":{"workflow_runs":[]}}
|
||||
]'
|
||||
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"
|
||||
@@ -77,7 +77,7 @@ teardown() {
|
||||
@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":{"workflow_runs":[{"id":1,"display_title":"POC (test123)","run_number":42,"status":"running"}]}},
|
||||
{"code":200,"body":{"id":1,"status":"completed","conclusion":"success"}}
|
||||
]'
|
||||
mock_start
|
||||
@@ -91,6 +91,7 @@ teardown() {
|
||||
[[ "$body" == *'"ref":"main"'* ]]
|
||||
[[ "$body" == *'"inputs"'* ]]
|
||||
[[ "$body" == *'"version":"1.2.3"'* ]]
|
||||
[[ "$body" == *'"dispatch_id":"test123"'* ]]
|
||||
}
|
||||
|
||||
@test "missing gitea_api_url argument → exit 1 with error message" {
|
||||
@@ -120,15 +121,15 @@ teardown() {
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "dispatch: no workflow run found after dispatch → exit 1" {
|
||||
@test "dispatch: no workflow run found after dispatch → exit 124 (timeout)" {
|
||||
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"* ]]
|
||||
run bash scripts/dispatch-workflow.sh "test-owner/test-repo" "test.yml" "main" '{}' "http://localhost:18080" "test-token-abc123" "0.001"
|
||||
[ "$status" -eq 124 ]
|
||||
[[ "$output" == *"ERROR"* || "$output" == *"Timeout"* ]]
|
||||
}
|
||||
|
||||
@test "missing inputs_json argument → exit 1" {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
Feature: GitOps update
|
||||
As a GitOps repository
|
||||
I want to update version references and report results to the caller
|
||||
So that the deployment chain is traceable from source to GitOps commit
|
||||
|
||||
Background:
|
||||
Given a project repository exists in Gitea
|
||||
And a commit has been pushed to the repository
|
||||
|
||||
@mock
|
||||
Scenario: Not enough env vars — script fails, no status set
|
||||
Given insufficient environment variables are provided for the GitOps update
|
||||
When the GitOps update script runs
|
||||
Then the script exits with error
|
||||
|
||||
@mock
|
||||
Scenario: GitOps job fails — no status set (SHA not yet known)
|
||||
Given the GitOps repository clone will fail
|
||||
When the GitOps update script runs
|
||||
Then the script exits with error
|
||||
|
||||
@mock
|
||||
Scenario: Everything succeeds — GitOps repo gets success status with link to caller
|
||||
Given a valid GitOps update dispatch
|
||||
When the GitOps update script runs
|
||||
Then the script exits successfully
|
||||
And the GitOps repo commit shows a success status with a link to the caller commit
|
||||
|
||||
@mock
|
||||
Scenario: GitOps push fails — GitOps repo gets failure status
|
||||
Given the GitOps repo push will fail after the version is committed
|
||||
When the GitOps update script runs
|
||||
Then the script exits with error
|
||||
And the GitOps repo commit shows a failure status linking to the caller commit
|
||||
|
||||
@mock
|
||||
Scenario: No changes — GitOps repo gets "no change" status
|
||||
Given the version file already has the target version
|
||||
When the GitOps update script runs
|
||||
Then the script exits successfully
|
||||
And the GitOps repo commit shows a "no change" status
|
||||
And no Git commit or push was performed
|
||||
@@ -0,0 +1,172 @@
|
||||
const { spawnSync, execSync } = require('child_process');
|
||||
const { Before, After, Given, When, Then } = require('@cucumber/cucumber');
|
||||
const path = require('path');
|
||||
|
||||
const PROJECT_ROOT = path.resolve(__dirname, '..', '..', '..');
|
||||
const MOCK_SCRIPT = path.join(PROJECT_ROOT, 'tests', 'helpers', 'mock-api.sh');
|
||||
const GITOPS_SCRIPT = path.join(PROJECT_ROOT, 'scripts', 'gitops-update.sh');
|
||||
const MOCK_HELPERS = path.join(PROJECT_ROOT, 'tests', 'helpers');
|
||||
const REQ_FILE = '/tmp/gitops-mock-requests.log';
|
||||
|
||||
const BASE_ENV = {
|
||||
INPUT_FILE: 'dev/Chart.yaml',
|
||||
YQ_TPL: '(.version) = "{{VERSION}}"',
|
||||
VERSION: '0.2.3',
|
||||
SOURCE_REPO: 'niko/app',
|
||||
SOURCE_COMMIT: 'abc123def456',
|
||||
GITOPS_REPO: 'niko/app-gitops',
|
||||
GITEA_API_URL: 'http://localhost:18080',
|
||||
GITEA_TOKEN: 'test-token',
|
||||
};
|
||||
|
||||
Before({ tags: '@mock' }, function () {
|
||||
process.env.PATH = `${MOCK_HELPERS}:${process.env.PATH}`;
|
||||
try { execSync('rm -f /tmp/gitops-mock-requests.log', { stdio: 'ignore' }); } catch (_) {}
|
||||
// Restart mock with known request file path
|
||||
const result = spawnSync('bash', ['-c', `
|
||||
source "${MOCK_SCRIPT}"
|
||||
mock_stop 2>/dev/null
|
||||
MOCK_REQUEST_FILE="${REQ_FILE}"
|
||||
mock_start
|
||||
sleep 0.5
|
||||
curl -s -o /dev/null -w "%{http_code}" --max-time 3 http://localhost:18080/api/v1/repos/health
|
||||
`], {
|
||||
cwd: PROJECT_ROOT, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe']
|
||||
});
|
||||
const code = result.stdout.trim();
|
||||
if (!code.startsWith('2') && !code.startsWith('4')) {
|
||||
throw new Error(`GitOps mock restart failed: ${result.stderr.substring(0,200)}`);
|
||||
}
|
||||
});
|
||||
|
||||
After({ tags: '@mock' }, function () {
|
||||
spawnSync('bash', ['-c', `source "${MOCK_SCRIPT}" && mock_stop 2>/dev/null`], { stdio: 'ignore' });
|
||||
try { execSync('rm -f /tmp/gitops-mock-requests.log /tmp/gitops-git-calls.log', { stdio: 'ignore' }); } catch (_) {}
|
||||
});
|
||||
|
||||
function bash(cmd) {
|
||||
const result = spawnSync('bash', ['-c', cmd], {
|
||||
cwd: PROJECT_ROOT,
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
});
|
||||
return { status: result.status, stdout: result.stdout || '', stderr: result.stderr || '' };
|
||||
}
|
||||
|
||||
function getFirstBody() {
|
||||
return bash(`grep -A1 '^POST ' "${REQ_FILE}" 2>/dev/null | head -2 | tail -1 || echo ""`).stdout.trim();
|
||||
}
|
||||
|
||||
function getFirstPath() {
|
||||
return bash(`grep '^POST ' "${REQ_FILE}" 2>/dev/null | head -1 | awk '{print $2}' || echo ""`).stdout.trim();
|
||||
}
|
||||
|
||||
function getLastBody() {
|
||||
return bash(`grep -A1 '^POST ' "${REQ_FILE}" 2>/dev/null | grep -v '^POST ' | tail -1 || echo ""`).stdout.trim();
|
||||
}
|
||||
|
||||
function getLastPath() {
|
||||
return bash(`grep '^POST ' "${REQ_FILE}" 2>/dev/null | tail -1 | awk '{print $2}' || echo ""`).stdout.trim();
|
||||
}
|
||||
|
||||
function requestCount() {
|
||||
return parseInt(bash(`grep -c '^POST ' "${REQ_FILE}" 2>/dev/null || echo 0`).stdout.trim(), 10) || 0;
|
||||
}
|
||||
|
||||
function gitCalls() {
|
||||
const callsFile = process.env.GIT_CALLS_FILE || '/dev/null';
|
||||
const out = bash(`cat "${callsFile}" 2>/dev/null || echo ""`).stdout;
|
||||
return out.split('\n').filter(l => l.length > 0);
|
||||
}
|
||||
|
||||
function runScript(envOverrides) {
|
||||
const env = { ...BASE_ENV, ...envOverrides };
|
||||
const scriptPath = `/tmp/gitops-run-${Date.now()}.sh`;
|
||||
const exports = Object.entries(env)
|
||||
.map(([k, v]) => `export ${k}="${v.replace(/"/g, '\\"')}"`)
|
||||
.join('\n');
|
||||
require('fs').writeFileSync(scriptPath, `${exports}\nexport PATH="${MOCK_HELPERS}:$PATH"\nset -euo pipefail\nbash "${GITOPS_SCRIPT}"\nsync\n`, 'utf8');
|
||||
try {
|
||||
return bash(`bash "${scriptPath}"`);
|
||||
} finally {
|
||||
require('fs').unlinkSync(scriptPath);
|
||||
}
|
||||
}
|
||||
|
||||
Given('insufficient environment variables are provided for the GitOps update', function () {
|
||||
this.envOverrides = { INPUT_FILE: '' };
|
||||
});
|
||||
|
||||
Given('the GitOps repository clone will fail', function () {
|
||||
this.envOverrides = { GIT_MOCK_FAIL: '1' };
|
||||
});
|
||||
|
||||
Given('a valid GitOps update dispatch', function () {
|
||||
this.envOverrides = {};
|
||||
});
|
||||
|
||||
Given('the GitOps repo push will fail after the version is committed', function () {
|
||||
this.envOverrides = { GIT_MOCK_FAIL_PUSH: '1' };
|
||||
});
|
||||
|
||||
Given('the version file already has the target version', function () {
|
||||
this.envOverrides = {
|
||||
GIT_MOCK_DIFF_NO_CHANGES: '1',
|
||||
GIT_CALLS_FILE: '/tmp/gitops-git-calls.log',
|
||||
};
|
||||
});
|
||||
|
||||
When('the GitOps update script runs', function () {
|
||||
this.result = runScript(this.envOverrides || {});
|
||||
});
|
||||
|
||||
Then('the script exits with error', function () {
|
||||
if (this.result.status === 0) throw new Error(`Expected non-zero exit, got 0. stderr: ${this.result.stderr.substring(0,200)}`);
|
||||
});
|
||||
|
||||
Then('the script exits successfully', function () {
|
||||
if (this.result.status !== 0) throw new Error(`Expected exit 0, got ${this.result.status}: ${this.result.stderr.substring(0,200)}`);
|
||||
});
|
||||
|
||||
Then('the GitOps repo commit shows a success status with a link to the caller commit', function () {
|
||||
const count = requestCount();
|
||||
if (count < 1) throw new Error(`Expected at least 1 request, got ${count}`);
|
||||
const body = getFirstBody();
|
||||
if (!body.includes('"state":"success"')) throw new Error(`Expected success state, body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('"context":"app ')) throw new Error(`Expected context "app unknown", body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('"description":"Install to dev 0.2.3"')) throw new Error(`Expected description, body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('niko/app/commit/abc123def456')) throw new Error(`Expected link to caller commit, body: ${body.substring(0,200)}`);
|
||||
const pathStr = getFirstPath();
|
||||
if (!pathStr.includes('/repos/niko/app-gitops/statuses/')) throw new Error(`Expected gitops repo path, got: ${pathStr}`);
|
||||
});
|
||||
|
||||
Then('the GitOps repo commit shows a failure status linking to the caller commit', function () {
|
||||
const count = requestCount();
|
||||
if (count < 1) throw new Error(`Expected at least 1 request, got ${count}`);
|
||||
const body = getFirstBody();
|
||||
if (!body.includes('"state":"failure"')) throw new Error(`Expected failure state, body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('"context":"app ')) throw new Error(`Expected context "app unknown", body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('"description":"Install to dev 0.2.3"')) throw new Error(`Expected description, body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('niko/app/commit/abc123def456')) throw new Error(`Expected link to caller commit, body: ${body.substring(0,200)}`);
|
||||
const pathStr = getFirstPath();
|
||||
if (!pathStr.includes('/repos/niko/app-gitops/statuses/')) throw new Error(`Expected gitops repo path, got: ${pathStr}`);
|
||||
});
|
||||
|
||||
Then('the GitOps repo commit shows a "no change" status', function () {
|
||||
const count = requestCount();
|
||||
if (count < 1) throw new Error(`Expected at least 1 request, got ${count}`);
|
||||
const body = getFirstBody();
|
||||
if (!body.includes('"state":"success"')) throw new Error(`Expected success state, body: ${body.substring(0,200)}`);
|
||||
if (!body.includes('"description":"Install to dev 0.2.3 \u2014 no change"')) {
|
||||
throw new Error(`Expected "no change" description, body: ${body.substring(0,200)}`);
|
||||
}
|
||||
const pathStr = getFirstPath();
|
||||
if (!pathStr.includes('/repos/niko/app-gitops/statuses/')) throw new Error(`Expected gitops repo path, got: ${pathStr}`);
|
||||
});
|
||||
|
||||
Then('no Git commit or push was performed', function () {
|
||||
const calls = gitCalls();
|
||||
if (calls.some(l => l.includes(' commit ') || l.includes(' push '))) {
|
||||
throw new Error(`Expected no commit or push, got: ${calls.join(', ')}`);
|
||||
}
|
||||
});
|
||||
@@ -15,7 +15,7 @@ function bash(cmd) {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
});
|
||||
return { status: 0, stdout: out };
|
||||
return { status: 0, stdout: out, stderr: '' };
|
||||
} catch (e) {
|
||||
return { status: e.status, stdout: e.stdout || '', stderr: e.stderr || '' };
|
||||
}
|
||||
@@ -54,7 +54,7 @@ function setupMock(seqJson) {
|
||||
}
|
||||
|
||||
function runDispatch(args) {
|
||||
return bash(`export DISPATCH_POLL_INTERVAL="0.1"; bash "${DISPATCH_SCRIPT}" ${args}`);
|
||||
return bash(`export DISPATCH_ID="test123"; export DISPATCH_POLL_INTERVAL="0.1"; bash "${DISPATCH_SCRIPT}" ${args}`);
|
||||
}
|
||||
|
||||
Given('a deployment has completed in the target environment', function () {
|
||||
@@ -66,7 +66,7 @@ Given('the test project repository exists with test definitions', function () {
|
||||
When('a test workflow is dispatched to a test project', function () {
|
||||
setupMock(JSON.stringify([
|
||||
{ code: 201 },
|
||||
{ code: 200, body: { workflow_runs: [{ id: 1, status: 'running' }] } },
|
||||
{ code: 200, body: { workflow_runs: [{ id: 1, display_title: 'Workflow (test123)', run_number: 42, 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"}\' "http://localhost:18080" "test-token-abc123"');
|
||||
@@ -84,7 +84,7 @@ Then('the pipeline continues only after receiving a success result', function ()
|
||||
When('a test workflow is dispatched and the tests fail', function () {
|
||||
setupMock(JSON.stringify([
|
||||
{ code: 201 },
|
||||
{ code: 200, body: { workflow_runs: [{ id: 1, status: 'running' }] } },
|
||||
{ code: 200, body: { workflow_runs: [{ id: 1, display_title: 'Workflow (test123)', run_number: 42, 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"}\' "http://localhost:18080" "test-token-abc123"');
|
||||
@@ -98,15 +98,19 @@ Then('the calling pipeline reports failure', function () {
|
||||
When('a test workflow is dispatched but does not finish within the allowed time', function () {
|
||||
setupMock(JSON.stringify([
|
||||
{ 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: { workflow_runs: [] } },
|
||||
{ code: 200, body: { workflow_runs: [] } },
|
||||
{ code: 200, body: { workflow_runs: [] } },
|
||||
{ code: 200, body: { workflow_runs: [] } },
|
||||
{ code: 200, body: { workflow_runs: [] } },
|
||||
]));
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\' "http://localhost:18080" "test-token-abc123" "0.001"');
|
||||
const r = runDispatch('"test-owner/test-repo" "test.yml" "main" \'{"version":"1.2.3"}\' "http://localhost:18080" "test-token-abc123" "0.05"');
|
||||
this.dispatchResult = r.status;
|
||||
this.dispatchStderr = r.stderr;
|
||||
});
|
||||
|
||||
Then('the calling pipeline reports a timeout error', function () {
|
||||
if (this.dispatchResult !== 124) throw new Error(`Expected timeout exit 124, got ${this.dispatchResult}`);
|
||||
if (this.dispatchResult !== 124) {
|
||||
throw new Error(`Expected timeout exit 124, got ${this.dispatchResult}. stderr: ${(this.dispatchStderr || '').substring(0,300)}`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup() {
|
||||
export INPUT_FILE=dev/Chart.yaml
|
||||
export YQ_TPL='version = "{{VERSION}}"'
|
||||
export VERSION=1.0.0
|
||||
export SOURCE_REPO=niko/app
|
||||
export SOURCE_COMMIT=abc123def456
|
||||
export GITOPS_REPO=niko/app-gitops
|
||||
export GITEA_TOKEN=test-token
|
||||
export GITEA_API_URL=http://localhost:18080
|
||||
}
|
||||
|
||||
teardown() {
|
||||
if type mock_stop &>/dev/null 2>&1; then
|
||||
mock_stop 2>/dev/null || true
|
||||
fi
|
||||
}
|
||||
|
||||
@test "missing GITEA_API_URL causes exit 1" {
|
||||
unset GITEA_API_URL
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITEA_API_URL"* ]]
|
||||
}
|
||||
|
||||
@test "missing GITEA_TOKEN causes exit 1" {
|
||||
unset GITEA_TOKEN
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITEA_TOKEN"* ]]
|
||||
}
|
||||
|
||||
@test "missing INPUT_FILE causes exit 1" {
|
||||
unset INPUT_FILE
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"INPUT_FILE"* ]]
|
||||
}
|
||||
|
||||
@test "missing YQ_TPL causes exit 1" {
|
||||
unset YQ_TPL
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"YQ_TPL"* ]]
|
||||
}
|
||||
|
||||
@test "missing VERSION causes exit 1" {
|
||||
unset VERSION
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"VERSION"* ]]
|
||||
}
|
||||
|
||||
@test "missing SOURCE_REPO causes exit 1" {
|
||||
unset SOURCE_REPO
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"SOURCE_REPO"* ]]
|
||||
}
|
||||
|
||||
@test "missing SOURCE_COMMIT causes exit 1" {
|
||||
unset SOURCE_COMMIT
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"SOURCE_COMMIT"* ]]
|
||||
}
|
||||
|
||||
@test "_gitops_substitute replaces {{VERSION}}" {
|
||||
run bash -c '
|
||||
source scripts/gitops-update.sh >/dev/null 2>&1
|
||||
_gitops_substitute "(.version) = \"{{VERSION}}\"" "0.2.3"
|
||||
'
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == '(.version) = "0.2.3"' ]]
|
||||
}
|
||||
|
||||
@test "CLONE_URL is constructed correctly from GITEA_API_URL" {
|
||||
export GITEA_API_URL=https://gitea.app.keskikuja.site
|
||||
export GITEA_TOKEN=secret123
|
||||
export GITOPS_REPO=niko/app-gitops
|
||||
run bash -c '
|
||||
source scripts/gitops-update.sh >/dev/null 2>&1
|
||||
echo "$CLONE_URL"
|
||||
'
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "https://secret123@gitea.app.keskikuja.site/niko/app-gitops.git" ]
|
||||
}
|
||||
|
||||
@test "CLONE_URL works with http:// URL" {
|
||||
export GITEA_API_URL=http://localhost:18080
|
||||
export GITEA_TOKEN=token
|
||||
export GITOPS_REPO=owner/repo
|
||||
run bash -c '
|
||||
source scripts/gitops-update.sh >/dev/null 2>&1
|
||||
echo "$CLONE_URL"
|
||||
'
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "https://token@localhost:18080/owner/repo.git" ]
|
||||
}
|
||||
|
||||
@test "_gitops_substitute handles multiple {{VERSION}} occurrences" {
|
||||
run bash -c '
|
||||
source scripts/gitops-update.sh >/dev/null 2>&1
|
||||
_gitops_substitute "version = \"{{VERSION}}\"; tag = \"v{{VERSION}}\"" "1.2.3"
|
||||
'
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == 'version = "1.2.3"; tag = "v1.2.3"' ]]
|
||||
}
|
||||
|
||||
@test "git flow: clone yq add commit push" {
|
||||
source tests/helpers/mock-api.sh
|
||||
mock_set_sequence '[
|
||||
{"code":201},
|
||||
{"code":201}
|
||||
]'
|
||||
mock_start
|
||||
export GIT_CALLS_FILE=$(mktemp)
|
||||
export YQ_CALLS_FILE=$(mktemp)
|
||||
export PATH="${BATS_TEST_DIRNAME}/helpers:$PATH"
|
||||
export INPUT_FILE=dev/Chart.yaml
|
||||
export YQ_TPL='(.version) = "{{VERSION}}"'
|
||||
export VERSION=0.2.3
|
||||
export SOURCE_REPO=niko/app
|
||||
export SOURCE_COMMIT=abc123def456
|
||||
export GITOPS_REPO=niko/app-gitops
|
||||
export GITEA_API_URL=http://localhost:18080
|
||||
export GITEA_TOKEN=test-token
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 0 ]
|
||||
git_calls=$(cat "$GIT_CALLS_FILE")
|
||||
[[ "$git_calls" == *"clone"* ]]
|
||||
[[ "$git_calls" == *"add"* ]]
|
||||
[[ "$git_calls" == *"commit"* ]]
|
||||
[[ "$git_calls" == *"push"* ]]
|
||||
yq_calls=$(cat "$YQ_CALLS_FILE")
|
||||
[[ "$yq_calls" == *"eval -i"* ]]
|
||||
rm -f "$GIT_CALLS_FILE" "$YQ_CALLS_FILE"
|
||||
mock_stop
|
||||
}
|
||||
|
||||
@test "one commit-status call: gitops-repo only" {
|
||||
source tests/helpers/mock-api.sh
|
||||
mock_set_sequence '[
|
||||
{"code":201}
|
||||
]'
|
||||
mock_start
|
||||
export GIT_CALLS_FILE=$(mktemp)
|
||||
export YQ_CALLS_FILE=$(mktemp)
|
||||
export PATH="${BATS_TEST_DIRNAME}/helpers:$PATH"
|
||||
export INPUT_FILE=dev/Chart.yaml
|
||||
export YQ_TPL='(.version) = "{{VERSION}}"'
|
||||
export VERSION=0.2.3
|
||||
export SOURCE_REPO=niko/app
|
||||
export SOURCE_COMMIT=abc123def456
|
||||
export GITOPS_REPO=niko/app-gitops
|
||||
export GITEA_API_URL=http://localhost:18080
|
||||
export GITEA_TOKEN=test-token
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 0 ]
|
||||
path=$(mock_get_first_request_path)
|
||||
body=$(mock_get_first_request_body)
|
||||
[[ "$path" == *"/repos/niko/app-gitops/statuses/"* ]]
|
||||
[[ "$body" == *'"context":"app '* ]]
|
||||
[[ "$body" == *'"description":"Install to dev 0.2.3"'* ]]
|
||||
[[ "$body" == *'"state":"success"'* ]]
|
||||
rm -f "$GIT_CALLS_FILE" "$YQ_CALLS_FILE"
|
||||
mock_stop
|
||||
}
|
||||
|
||||
@test "missing GITOPS_REPO causes exit 1" {
|
||||
unset GITOPS_REPO
|
||||
run bash scripts/gitops-update.sh
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"GITOPS_REPO"* ]]
|
||||
}
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
echo "git $*" >> "${GIT_CALLS_FILE:-/dev/null}"
|
||||
|
||||
[ -z "${GIT_MOCK_FAIL:-}" ] || { echo "git: mock forced failure" >&2; exit 1; }
|
||||
|
||||
if [ "${1:-}" = "push" ] && [ -n "${GIT_MOCK_FAIL_PUSH:-}" ]; then
|
||||
echo "git: mock push failure" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Skip -c config arguments
|
||||
while [ "${1:-}" = "-c" ]; do
|
||||
shift 2
|
||||
done
|
||||
|
||||
case "$1" in
|
||||
clone)
|
||||
TARGET_DIR="${@: -1}"
|
||||
mkdir -p "${TARGET_DIR}/$(dirname "$INPUT_FILE")"
|
||||
echo 'version: 0.1.0' > "${TARGET_DIR}/${INPUT_FILE}"
|
||||
echo "Cloning into '$TARGET_DIR'..."
|
||||
;;
|
||||
add|commit|push|config|init)
|
||||
;;
|
||||
diff)
|
||||
# Default: exit 1 = has changes → proceed to commit
|
||||
# GIT_MOCK_DIFF_NO_CHANGES=1 → exit 0 = no changes → "no change" path
|
||||
if [ -n "${GIT_MOCK_DIFF_NO_CHANGES:-}" ]; then
|
||||
exit 0
|
||||
fi
|
||||
exit 1
|
||||
;;
|
||||
rev-parse)
|
||||
echo "mock-sha-9876543210fedcba9876543210fedcba98765432"
|
||||
;;
|
||||
*)
|
||||
echo "git: unknown command: $*" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -46,7 +46,7 @@ mock_clear_sequence() {
|
||||
|
||||
mock_start() {
|
||||
MOCK_RESPONSE_CODE="${MOCK_RESPONSE_CODE:-201}"
|
||||
MOCK_REQUEST_FILE=$(mktemp)
|
||||
MOCK_REQUEST_FILE="${MOCK_REQUEST_FILE:-$(mktemp)}"
|
||||
echo "$MOCK_REQUEST_FILE" > "$MOCK_STATE_FILE"
|
||||
MOCK_CONFIG_FILE=$(mktemp)
|
||||
|
||||
|
||||
Executable
+2
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
echo "yq $*" >> "${YQ_CALLS_FILE:-/dev/null}"
|
||||
Reference in New Issue
Block a user