80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
"""Linux admin settings and SSH host actions."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
from app.models import Host
|
|
from app.models.ui_settings import UI_SETTINGS_ROW_ID, UiSettings
|
|
from app.services.ssh_connect import SshCommandResult, iter_ssh_targets
|
|
|
|
|
|
def test_put_linux_admin_settings_persists(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "")
|
|
from app.config import get_settings
|
|
|
|
get_settings.cache_clear()
|
|
|
|
response = client.put(
|
|
"/api/v1/settings/linux-admin",
|
|
headers=jwt_headers,
|
|
json={"user": "root", "password": "secret-pass"},
|
|
)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["configured"] is True
|
|
assert body["user"] == "root"
|
|
assert body["source"] == "db"
|
|
|
|
row = db_session.get(UiSettings, UI_SETTINGS_ROW_ID)
|
|
assert row is not None
|
|
assert row.linux_admin_user == "root"
|
|
assert row.linux_admin_password == "secret-pass"
|
|
|
|
|
|
def test_iter_ssh_targets_hostname_before_ip():
|
|
host = Host(
|
|
hostname="ubabuba",
|
|
os_family="linux",
|
|
product="ssh-monitor",
|
|
ipv4="10.0.0.5",
|
|
)
|
|
targets = iter_ssh_targets(host)
|
|
assert targets[0] == "ubabuba"
|
|
assert targets[-1] == "10.0.0.5"
|
|
|
|
|
|
def test_host_agent_update_via_ssh(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "root")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "pw")
|
|
from app.config import get_settings
|
|
|
|
get_settings.cache_clear()
|
|
|
|
host = Host(
|
|
hostname="ubabuba",
|
|
os_family="linux",
|
|
product="ssh-monitor",
|
|
ipv4="10.0.0.5",
|
|
)
|
|
db_session.add(host)
|
|
db_session.commit()
|
|
db_session.refresh(host)
|
|
|
|
with patch("app.api.v1.hosts.run_ssh_monitor_update") as mock_update:
|
|
mock_update.return_value = SshCommandResult(
|
|
ok=True,
|
|
message="SSH OK",
|
|
target="ubabuba",
|
|
stdout="updated",
|
|
exit_code=0,
|
|
)
|
|
response = client.post(
|
|
f"/api/v1/hosts/{host.id}/actions/agent-update",
|
|
headers=jwt_headers,
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["ok"] is True
|
|
assert body["target"] == "ubabuba"
|