Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/v2/test_ui_cockpit.py
Original file line number Diff line number Diff line change
@@ -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 == []
4 changes: 2 additions & 2 deletions virl/cli/cockpit/commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import subprocess
import webbrowser

import click

Expand All @@ -12,4 +12,4 @@ def cockpit():
"""
server = VIRLServer()
url = "https://{}:9090".format(server.host)
subprocess.Popen(["open", url])
webbrowser.open(url)
4 changes: 2 additions & 2 deletions virl/cli/ui/commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import subprocess
import webbrowser

import click

Expand All @@ -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)