From d1a7e50695b0376ee9e7ecb3c3567b9bb2492809 Mon Sep 17 00:00:00 2001 From: Yuri Chiucconi Date: Tue, 29 Oct 2024 12:20:34 +0100 Subject: [PATCH 1/5] change cmd and test --- fractal_client/cmd/__init__.py | 15 ++++++++++----- tests/test_client.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/fractal_client/cmd/__init__.py b/fractal_client/cmd/__init__.py index 4960a880..0b88e528 100644 --- a/fractal_client/cmd/__init__.py +++ b/fractal_client/cmd/__init__.py @@ -1,4 +1,5 @@ from httpx import Client +from httpx import ConnectError from ..authclient import AuthClient from ..config import settings @@ -298,16 +299,20 @@ def job( def version(client: Client, **kwargs) -> Interface: - res = client.get(f"{settings.FRACTAL_SERVER}/api/alive/") - data = res.json() + try: + res = client.get(f"{settings.FRACTAL_SERVER}/api/alive/") + data = res.json() + server_str = ( + f"\turl: {settings.FRACTAL_SERVER}" f"\tversion: {data['version']}" + ) + except ConnectError: + server_str = f"\tConnection to '{settings.FRACTAL_SERVER}' refused" return Interface( retcode=0, data=( f"Fractal client\n\tversion: {__VERSION__}\n" - "Fractal server:\n" - f"\turl: {settings.FRACTAL_SERVER}" - f"\tversion: {data['version']}" + f"Fractal server:\n{server_str}" ), ) diff --git a/tests/test_client.py b/tests/test_client.py index 50b2389e..4a459199 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -21,6 +21,19 @@ def test_version(invoke): assert iface.retcode == 0 +def test_version_connect_error(invoke, monkeypatch): + monkeypatch.setattr( + "fractal_client.cmd.__init__.settings.FRACTAL_SERVER", + "http://localhost:9999", + ) + + iface = invoke("version") + debug(iface.data) + assert f"version: {__VERSION__}" in iface.data + assert "refused" in iface.data + assert iface.retcode == 0 + + def test_server(): """ GIVEN a testserver From 505af309601ee306768362cc84fa293bbb8f6620 Mon Sep 17 00:00:00 2001 From: Yuri Chiucconi Date: Tue, 29 Oct 2024 12:24:12 +0100 Subject: [PATCH 2/5] fix testserver startup --- tests/fixtures_testserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures_testserver.py b/tests/fixtures_testserver.py index 39191ea7..93112691 100644 --- a/tests/fixtures_testserver.py +++ b/tests/fixtures_testserver.py @@ -95,7 +95,7 @@ def testserver(tester, tmpdir_factory, request): while True: try: res = handle(shlex.split("fractal version")) - if res.retcode == 0: + if "refused" not in res.data: break else: raise ConnectError("fractal-server not ready") From 8c4f9a34b8169ab46134efdece641f7629a2a3f8 Mon Sep 17 00:00:00 2001 From: Yuri Chiucconi Date: Tue, 29 Oct 2024 12:26:50 +0100 Subject: [PATCH 3/5] changelog [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9497b6b6..dd1c6025 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Add `--new-ssh-settings-json` option to `fractal user edit` (\#715). * Add `--private` option to task-creating commands (\#717). * Drop `task delete` command (\#717). +* Handle missing server in `fractal version` (\#724). * Testing: * Run all tests against a single `fractal-server` instance (\#717). * Run tests in random module order, based on `pytest-randomly` (\#717). From db756ce0aa6e6fa07e6fd6bf5eb0b1c3b1b587d9 Mon Sep 17 00:00:00 2001 From: Yuri Chiucconi Date: Tue, 29 Oct 2024 12:47:11 +0100 Subject: [PATCH 4/5] fix test --- tests/test_client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 4a459199..64af97fc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,6 +6,7 @@ from fractal_client import __VERSION__ from fractal_client.client import handle +from fractal_client.config import Settings def test_debug(invoke): @@ -22,10 +23,10 @@ def test_version(invoke): def test_version_connect_error(invoke, monkeypatch): - monkeypatch.setattr( - "fractal_client.cmd.__init__.settings.FRACTAL_SERVER", - "http://localhost:9999", - ) + + mock_settings = Settings() + mock_settings.FRACTAL_SERVER = "http://localhost:9999" + monkeypatch.setattr("fractal_client.cmd.settings", mock_settings) iface = invoke("version") debug(iface.data) From 982906062f8f26143fff6e4645811e2dd33fda09 Mon Sep 17 00:00:00 2001 From: Yuri Chiucconi Date: Tue, 29 Oct 2024 12:47:44 +0100 Subject: [PATCH 5/5] merge strings --- fractal_client/cmd/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fractal_client/cmd/__init__.py b/fractal_client/cmd/__init__.py index 0b88e528..bed075ff 100644 --- a/fractal_client/cmd/__init__.py +++ b/fractal_client/cmd/__init__.py @@ -303,7 +303,7 @@ def version(client: Client, **kwargs) -> Interface: res = client.get(f"{settings.FRACTAL_SERVER}/api/alive/") data = res.json() server_str = ( - f"\turl: {settings.FRACTAL_SERVER}" f"\tversion: {data['version']}" + f"\turl: {settings.FRACTAL_SERVER}\tversion: {data['version']}" ) except ConnectError: server_str = f"\tConnection to '{settings.FRACTAL_SERVER}' refused"