diff --git a/tests/v2/test_ui_cockpit.py b/tests/v2/test_ui_cockpit.py new file mode 100644 index 0000000..43ac4a8 --- /dev/null +++ b/tests/v2/test_ui_cockpit.py @@ -0,0 +1,46 @@ +from click.testing import CliRunner + +from virl.cli.cockpit.commands import cockpit +from virl.cli.ui.commands import ui + + +class FakeServer: + host = "cml.example.local" + + +def test_cockpit_opens_expected_url(monkeypatch): + opened = [] + monkeypatch.setattr("virl.cli.cockpit.commands.VIRLServer", lambda: FakeServer()) + monkeypatch.setattr("virl.cli.cockpit.commands.webbrowser.open", lambda url: opened.append(url)) + + result = CliRunner().invoke(cockpit, []) + + assert result.exit_code == 0 + assert opened == ["https://cml.example.local:9090"] + + +def test_ui_opens_current_lab_when_present(monkeypatch): + opened = [] + monkeypatch.setattr("virl.cli.ui.commands.VIRLServer", lambda: FakeServer()) + monkeypatch.setattr("virl.cli.ui.commands.get_cml_client", lambda _server: object()) + monkeypatch.setattr("virl.cli.ui.commands.get_current_lab", lambda: "lab-123") + monkeypatch.setattr("virl.cli.ui.commands.safe_join_existing_lab", lambda _lab_id, _client: object()) + monkeypatch.setattr("virl.cli.ui.commands.webbrowser.open", lambda url: opened.append(url)) + + result = CliRunner().invoke(ui, []) + + assert result.exit_code == 0 + assert opened == ["https://cml.example.local/lab/lab-123"] + + +def test_ui_does_not_open_when_current_lab_missing(monkeypatch): + opened = [] + monkeypatch.setattr("virl.cli.ui.commands.VIRLServer", lambda: FakeServer()) + monkeypatch.setattr("virl.cli.ui.commands.get_cml_client", lambda _server: object()) + monkeypatch.setattr("virl.cli.ui.commands.get_current_lab", lambda: None) + monkeypatch.setattr("virl.cli.ui.commands.webbrowser.open", lambda url: opened.append(url)) + + result = CliRunner().invoke(ui, []) + + assert result.exit_code == 0 + assert opened == [] diff --git a/virl/cli/cockpit/commands.py b/virl/cli/cockpit/commands.py index 5ebd3a8..aa94e9f 100644 --- a/virl/cli/cockpit/commands.py +++ b/virl/cli/cockpit/commands.py @@ -1,4 +1,4 @@ -import subprocess +import webbrowser import click @@ -12,4 +12,4 @@ def cockpit(): """ server = VIRLServer() url = "https://{}:9090".format(server.host) - subprocess.Popen(["open", url]) + webbrowser.open(url) diff --git a/virl/cli/ui/commands.py b/virl/cli/ui/commands.py index 843a081..d38a6ab 100644 --- a/virl/cli/ui/commands.py +++ b/virl/cli/ui/commands.py @@ -1,4 +1,4 @@ -import subprocess +import webbrowser import click @@ -20,4 +20,4 @@ def ui(): lab = safe_join_existing_lab(current_lab, client) if lab: url = "https://{}/lab/{}".format(server.host, current_lab) - subprocess.Popen(["open", url]) + webbrowser.open(url)