132 lines
4.3 KiB
Python
Executable File
132 lines
4.3 KiB
Python
Executable File
"""CLI wiring: parsing, exit codes and table/JSON output."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from proxdomain.cli import build_parser, main
|
|
|
|
CONFIG_TEMPLATE = """
|
|
[general]
|
|
base_domain = "example.com"
|
|
|
|
[proxmox]
|
|
api_url = "https://pve.example.com:8006/api2/json"
|
|
token_id = "automation@pve!proxdomain"
|
|
token_secret = "secret"
|
|
|
|
[cloudflare]
|
|
api_token = "cf-token"
|
|
|
|
[state]
|
|
path = "{state}"
|
|
"""
|
|
|
|
|
|
def write_config(directory: str) -> Path:
|
|
state = Path(directory) / "state.json"
|
|
config = Path(directory) / "proxdomain.toml"
|
|
config.write_text(CONFIG_TEMPLATE.format(state=state.as_posix()), encoding="utf-8")
|
|
return config
|
|
|
|
|
|
def write_state(directory: str) -> Path:
|
|
state = Path(directory) / "state.json"
|
|
state.write_text(
|
|
json.dumps(
|
|
{
|
|
"version": 1,
|
|
"assignments": {
|
|
"web01.example.com": {
|
|
"fqdn": "web01.example.com",
|
|
"vmid": 101,
|
|
"node": "pve",
|
|
"kind": "qemu",
|
|
"name": "web01",
|
|
"ip": "10.0.0.5",
|
|
"port": 80,
|
|
"record_id": "rec-1",
|
|
}
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
return state
|
|
|
|
|
|
class CliTests(unittest.TestCase):
|
|
def test_version(self) -> None:
|
|
with contextlib.redirect_stdout(io.StringIO()) as out:
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
main(["--version"])
|
|
self.assertEqual(ctx.exception.code, 0)
|
|
self.assertIn("proxdomain", out.getvalue())
|
|
|
|
def test_no_command_prints_help(self) -> None:
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(main([]), 2)
|
|
|
|
def test_missing_config(self) -> None:
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
self.assertEqual(main(["list", "-c", "/nonexistent/proxdomain.toml"]), 2)
|
|
|
|
def test_list_empty(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
config = write_config(tmp)
|
|
with contextlib.redirect_stdout(io.StringIO()) as out:
|
|
self.assertEqual(main(["list", "-c", str(config)]), 0)
|
|
self.assertIn("no managed domains", out.getvalue())
|
|
|
|
def test_list_json(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
config = write_config(tmp)
|
|
write_state(tmp)
|
|
with contextlib.redirect_stdout(io.StringIO()) as out:
|
|
self.assertEqual(main(["list", "-c", str(config), "--json"]), 0)
|
|
payload = json.loads(out.getvalue())
|
|
self.assertEqual(payload[0]["fqdn"], "web01.example.com")
|
|
|
|
def test_list_table(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
config = write_config(tmp)
|
|
write_state(tmp)
|
|
with contextlib.redirect_stdout(io.StringIO()) as out:
|
|
self.assertEqual(main(["list", "-c", str(config)]), 0)
|
|
text = out.getvalue()
|
|
self.assertIn("FQDN", text)
|
|
self.assertIn("web01.example.com", text)
|
|
self.assertIn("10.0.0.5", text)
|
|
|
|
def test_remove_without_assignment_fails(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
config = write_config(tmp)
|
|
with contextlib.redirect_stdout(io.StringIO()) as out:
|
|
code = main(["remove", "--fqdn", "nope.example.com", "--yes", "-c", str(config)])
|
|
self.assertEqual(code, 1)
|
|
self.assertIn("FAILED", out.getvalue())
|
|
|
|
def test_parser_requires_a_target(self) -> None:
|
|
parser = build_parser()
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
with self.assertRaises(SystemExit):
|
|
parser.parse_args(["assign"])
|
|
|
|
def test_global_option_before_and_after_subcommand(self) -> None:
|
|
parser = build_parser()
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
config = write_config(tmp)
|
|
before = parser.parse_args(["-c", str(config), "list"])
|
|
after = parser.parse_args(["list", "-c", str(config)])
|
|
self.assertEqual(before.config, str(config))
|
|
self.assertEqual(after.config, str(config))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|