Test and publish container images / images (push) Failing after 1s
Use verified Node HTTPS with bounded timeouts and separate tool, DNS, TLS and HTTP diagnostics. Cover the preflight with regression tests and document explicit Docker job labels for Runner 3.
93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
"""Exercise the actual pre-checkout script without requiring curl or a network."""
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
WORKFLOW = Path(__file__).resolve().parents[2] / ".gitea/workflows/images.yml"
|
|
STEP = " - name: Check Gitea HTTPS connectivity before checkout\n"
|
|
|
|
|
|
class GiteaPreflightTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.bash = shutil.which("bash")
|
|
self.node = shutil.which("node")
|
|
if not self.bash or not self.node:
|
|
self.skipTest("bash and node required")
|
|
block = WORKFLOW.read_text().split(STEP, 1)[1].split("\n - name:", 1)[0]
|
|
lines = block.split(" run: |\n", 1)[1].splitlines()
|
|
self.script = "\n".join(line[10:] if line.startswith(" ") else line for line in lines)
|
|
|
|
def run_check(self, mode="ok", url="https://gitea.example.test", node=True):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
if node:
|
|
(root / "node").symlink_to(self.node)
|
|
stub = root / "https.cjs"
|
|
stub.write_text("""
|
|
const {EventEmitter} = require('node:events');
|
|
const https = require('node:https');
|
|
const assert = require('node:assert/strict');
|
|
https.get = (url, options, callback) => {
|
|
assert.equal(url.pathname, '/api/v1/version');
|
|
assert.equal(options.rejectUnauthorized, true);
|
|
const request = new EventEmitter();
|
|
request.destroy = error => request.emit('error', error);
|
|
process.nextTick(() => {
|
|
if (process.env.CHECK_MODE === 'dns') {
|
|
request.emit('error', {code: 'ENOTFOUND'});
|
|
} else if (process.env.CHECK_MODE === 'tls') {
|
|
request.emit('error', {code: 'CERT_HAS_EXPIRED'});
|
|
} else if (process.env.CHECK_MODE !== 'timeout') {
|
|
callback({statusCode: process.env.CHECK_MODE === 'http' ? 503 : 200, destroy() {}});
|
|
}
|
|
});
|
|
return request;
|
|
};
|
|
if (process.env.CHECK_MODE === 'timeout') {
|
|
const original = global.setTimeout;
|
|
global.setTimeout = (callback, delay) => {
|
|
assert.equal(delay, 15000);
|
|
return original(callback, 1);
|
|
};
|
|
}
|
|
""")
|
|
env = dict(os.environ, PATH=tmp, GITEA_SERVER_URL=url,
|
|
NODE_OPTIONS=f"--require={stub}", CHECK_MODE=mode)
|
|
return subprocess.run([self.bash, "-c", self.script], env=env,
|
|
capture_output=True, text=True, timeout=5)
|
|
|
|
def test_success_without_curl(self):
|
|
result = self.run_check()
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("connectivity OK", result.stdout)
|
|
|
|
def test_missing_node_is_not_a_network_error(self):
|
|
result = self.run_check(node=False)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("Runner tool missing: node", result.stderr)
|
|
self.assertIn("No network check was performed", result.stderr)
|
|
self.assertNotIn("HTTPS check failed", result.stderr)
|
|
|
|
def test_invalid_url(self):
|
|
for url in ["http://gitea.example.test", "bad url", "https://user:pass@gitea.example.test"]:
|
|
with self.subTest(url=url):
|
|
result = self.run_check(url=url)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("Invalid GITEA_SERVER_URL", result.stderr)
|
|
|
|
def test_failure_diagnostics(self):
|
|
for mode, message in [("http", "HTTP 503"), ("dns", "ENOTFOUND"),
|
|
("tls", "CERT_HAS_EXPIRED"), ("timeout", "ETIMEDOUT")]:
|
|
with self.subTest(mode=mode):
|
|
result = self.run_check(mode=mode)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn(message, result.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|