isabl-cli 2.0.2__tar.gz → 2.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. {isabl_cli-2.0.2/isabl_cli.egg-info → isabl_cli-2.0.3}/PKG-INFO +9 -1
  2. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/README.md +8 -0
  3. isabl_cli-2.0.3/isabl_cli/VERSION +1 -0
  4. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/api.py +58 -2
  5. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/exceptions.py +7 -0
  6. {isabl_cli-2.0.2 → isabl_cli-2.0.3/isabl_cli.egg-info}/PKG-INFO +9 -1
  7. isabl_cli-2.0.3/tests/conftest.py +65 -0
  8. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_api.py +87 -1
  9. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_commands.py +19 -0
  10. isabl_cli-2.0.2/isabl_cli/VERSION +0 -1
  11. isabl_cli-2.0.2/tests/conftest.py +0 -26
  12. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/LICENSE.md +0 -0
  13. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/MANIFEST.in +0 -0
  14. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/__init__.py +0 -0
  15. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/__main__.py +0 -0
  16. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/app.py +0 -0
  17. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/batch_systems/__init__.py +0 -0
  18. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/batch_systems/local.py +0 -0
  19. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/batch_systems/lsf.py +0 -0
  20. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/batch_systems/sge.py +0 -0
  21. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/batch_systems/slurm.py +0 -0
  22. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/cli.py +0 -0
  23. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/commands.py +0 -0
  24. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/data/README.md +0 -0
  25. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/data.py +0 -0
  26. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/factories.py +0 -0
  27. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/options.py +0 -0
  28. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/settings.py +0 -0
  29. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/signals.py +0 -0
  30. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/test/__init__.py +0 -0
  31. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/test/utils.py +0 -0
  32. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/utils.py +0 -0
  33. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli/validators.py +0 -0
  34. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli.egg-info/SOURCES.txt +0 -0
  35. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli.egg-info/dependency_links.txt +0 -0
  36. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli.egg-info/entry_points.txt +0 -0
  37. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli.egg-info/requires.txt +0 -0
  38. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/isabl_cli.egg-info/top_level.txt +0 -0
  39. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/setup.cfg +0 -0
  40. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/setup.py +0 -0
  41. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/__init__.py +0 -0
  42. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_app.py +0 -0
  43. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_batch_systems.py +0 -0
  44. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_cli.py +0 -0
  45. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_data.py +0 -0
  46. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_options.py +0 -0
  47. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_signals.py +0 -0
  48. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_testutils.py +0 -0
  49. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_utils.py +0 -0
  50. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_validators.py +0 -0
  51. {isabl_cli-2.0.2 → isabl_cli-2.0.3}/tests/test_version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: isabl-cli
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: isabl_cli command line client.
5
5
  Home-page: https://github.com/papaemmelab/isabl_cli
6
6
  Author: Juan S. Medina
@@ -140,6 +140,14 @@ To skip the build step (faster for subsequent runs):
140
140
 
141
141
  ### Running Tests Directly
142
142
 
143
+ The tests create, change and delete records and replace client settings, so only run them against a throwaway local API that you started for them. Three things stop them reaching any other API, whatever ISABL_API_URL your shell exports:
144
+
145
+ - pytest.ini sets ISABL_API_URL to http://localhost:8000/api/v1/.
146
+ - tests/conftest.py refuses to start, and refuses each test, if the URL isn't localhost.
147
+ - isabl_cli itself refuses to write to any API other than localhost while pytest is loaded. This applies to every test suite that uses isabl_cli, not only this one.
148
+
149
+ To run the tests against a disposable remote test server, change ISABL_API_URL in pytest.ini and set ISABL_ALLOW_TEST_WRITES_TO to the server's URL.
150
+
143
151
  If you have a local Isabl API instance running (e.g., via `docker compose up -d` in the `isabl_api` repository), you can run pytest commands directly:
144
152
 
145
153
  ```bash
@@ -90,6 +90,14 @@ To skip the build step (faster for subsequent runs):
90
90
 
91
91
  ### Running Tests Directly
92
92
 
93
+ The tests create, change and delete records and replace client settings, so only run them against a throwaway local API that you started for them. Three things stop them reaching any other API, whatever ISABL_API_URL your shell exports:
94
+
95
+ - pytest.ini sets ISABL_API_URL to http://localhost:8000/api/v1/.
96
+ - tests/conftest.py refuses to start, and refuses each test, if the URL isn't localhost.
97
+ - isabl_cli itself refuses to write to any API other than localhost while pytest is loaded. This applies to every test suite that uses isabl_cli, not only this one.
98
+
99
+ To run the tests against a disposable remote test server, change ISABL_API_URL in pytest.ini and set ISABL_ALLOW_TEST_WRITES_TO to the server's URL.
100
+
93
101
  If you have a local Isabl API instance running (e.g., via `docker compose up -d` in the `isabl_api` repository), you can run pytest commands directly:
94
102
 
95
103
  ```bash
@@ -0,0 +1 @@
1
+ 2.0.3
@@ -194,8 +194,64 @@ def get_api_url(url):
194
194
  return url
195
195
 
196
196
 
197
+ CLI_VERSION_HEADER = "X-Isabl-CLI-Version"
198
+
199
+
200
+ @lru_cache(None)
201
+ def get_cli_headers():
202
+ """Get headers that identify this isabl_cli version to the API."""
203
+ from isabl_cli import __version__ # avoid circular import
204
+
205
+ return {
206
+ CLI_VERSION_HEADER: __version__,
207
+ "User-Agent": f"isabl_cli/{__version__} {requests.utils.default_user_agent()}",
208
+ }
209
+
210
+
211
+ # Test suites create, change and delete records and replace client settings, so a
212
+ # test session only writes to a local API. Set this variable to the URL of an API
213
+ # that is safe to write to from tests, e.g. a disposable remote test server.
214
+ LOCAL_API_HOSTS = frozenset({"localhost", "127.0.0.1", "::1"})
215
+ TEST_WRITES_URL_VARIABLE = "ISABL_ALLOW_TEST_WRITES_TO"
216
+ WRITE_METHODS = frozenset({"post", "put", "patch", "delete"})
217
+
218
+
219
+ def check_test_session_write(method, url):
220
+ """
221
+ Refuse a write from a test session to an API that isn't local.
222
+
223
+ A test session is any process that imported pytest, the same check
224
+ `get_token_headers` uses to log in as the test admin.
225
+
226
+ Raises:
227
+ exceptions.TestSessionWriteError: if the write must not be sent.
228
+ """
229
+ if method.lower() not in WRITE_METHODS or "pytest" not in sys.modules:
230
+ return
231
+
232
+ if urlparse(url).hostname in LOCAL_API_HOSTS:
233
+ return
234
+
235
+ allowed = environ.get(TEST_WRITES_URL_VARIABLE)
236
+ if allowed and url.startswith(allowed):
237
+ return
238
+
239
+ raise exceptions.TestSessionWriteError(
240
+ f"Refusing to {method.upper()} {url} from a test session: tests create, "
241
+ "change and delete records, so they only write to a local Isabl API. Point "
242
+ "ISABL_API_URL at a local test API (e.g. http://localhost:8000/api/v1/), or "
243
+ f"set {TEST_WRITES_URL_VARIABLE} to this API's URL if it is a disposable "
244
+ "test server."
245
+ )
246
+
247
+
197
248
  def retry_request(method, **kwargs):
198
249
  """Retry request operation multiple times."""
250
+ check_test_session_write(method, kwargs.get("url", ""))
251
+
252
+ # build a new dict so cached headers (e.g. get_token_headers) aren't mutated
253
+ kwargs["headers"] = {**get_cli_headers(), **(kwargs.get("headers") or {})}
254
+
199
255
  for i in [0.2, 1, 5, 10, 60, 90, 120, 300, 900]: # attempt some retries
200
256
  try:
201
257
  error = None
@@ -628,13 +684,13 @@ def patch_analysis_status(analysis, status):
628
684
  analysis["status"] = status # make sure that the analysis status is updated
629
685
  _set_analysis_permissions(analysis)
630
686
 
631
- if status in {"FAILED", "SUCCEEDED", "IN_PROGRESS", "REJECTED"}:
687
+ if status in {"FAILED", "SUCCEEDED", "IN_PROGRESS", "FINISHED", "REJECTED"}:
632
688
  data["storage_usage"] = utils.get_tree_size(storage_url)
633
689
 
634
690
  if status == "STARTED":
635
691
  data["ran_by"] = system_settings.api_username
636
692
 
637
- if status in {"SUCCEEDED", "IN_PROGRESS"}:
693
+ if status in {"SUCCEEDED", "IN_PROGRESS", "FINISHED"}:
638
694
  try:
639
695
  data["results"] = _get_analysis_results(analysis, raise_error=True)
640
696
  except Exception as error: # pragma: no cover
@@ -46,3 +46,10 @@ class MissingDataError(PackageBaseException):
46
46
  class AutomationError(PackageBaseException):
47
47
 
48
48
  """A class to raise when automations fail."""
49
+
50
+
51
+ class TestSessionWriteError(PackageBaseException):
52
+
53
+ """A class to raise when a test session would write to an API that isn't local."""
54
+
55
+ __test__ = False # not a pytest test class
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: isabl-cli
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: isabl_cli command line client.
5
5
  Home-page: https://github.com/papaemmelab/isabl_cli
6
6
  Author: Juan S. Medina
@@ -140,6 +140,14 @@ To skip the build step (faster for subsequent runs):
140
140
 
141
141
  ### Running Tests Directly
142
142
 
143
+ The tests create, change and delete records and replace client settings, so only run them against a throwaway local API that you started for them. Three things stop them reaching any other API, whatever ISABL_API_URL your shell exports:
144
+
145
+ - pytest.ini sets ISABL_API_URL to http://localhost:8000/api/v1/.
146
+ - tests/conftest.py refuses to start, and refuses each test, if the URL isn't localhost.
147
+ - isabl_cli itself refuses to write to any API other than localhost while pytest is loaded. This applies to every test suite that uses isabl_cli, not only this one.
148
+
149
+ To run the tests against a disposable remote test server, change ISABL_API_URL in pytest.ini and set ISABL_ALLOW_TEST_WRITES_TO to the server's URL.
150
+
143
151
  If you have a local Isabl API instance running (e.g., via `docker compose up -d` in the `isabl_api` repository), you can run pytest commands directly:
144
152
 
145
153
  ```bash
@@ -0,0 +1,65 @@
1
+ """
2
+ Test configuration.
3
+
4
+ The tests create, change and delete records and replace the settings of the
5
+ `default-backend-settings` client, which configures the whole API. They must only run
6
+ against a disposable local API, see tests/test-with-compose.sh. pytest.ini sets
7
+ ISABL_API_URL to it, and the check below stops the tests before isabl_cli is imported
8
+ if the URL is not local anyway (e.g. pytest-env is not installed).
9
+ """
10
+
11
+ from urllib.parse import urlparse
12
+ import os
13
+
14
+ import pytest
15
+
16
+ LOCAL_API_HOSTS = {"localhost", "127.0.0.1", "::1"}
17
+
18
+
19
+ def check_test_api_url():
20
+ """Raise a UsageError unless the tests would use a local API."""
21
+ url = os.environ.get("ISABL_API_URL", "http://localhost:8000/api/v1/")
22
+
23
+ if urlparse(url).hostname not in LOCAL_API_HOSTS:
24
+ raise pytest.UsageError(
25
+ f"Refusing to run the tests against {url}, they create, change and "
26
+ "delete records and replace client settings. Run them against a local "
27
+ "API, see tests/test-with-compose.sh."
28
+ )
29
+
30
+
31
+ check_test_api_url()
32
+
33
+ # pylint: disable=wrong-import-position
34
+ from isabl_cli.api import create_instance, patch_instance, get_instance
35
+ from isabl_cli.settings import _DEFAULTS
36
+
37
+
38
+ @pytest.hookimpl(tryfirst=True)
39
+ def pytest_runtest_setup(item):
40
+ """Check the API URL again before each test and its fixtures are set up."""
41
+ check_test_api_url()
42
+
43
+
44
+ def pytest_sessionstart():
45
+ """Run before any test starts."""
46
+ check_test_api_url() # again, right before the clients are changed
47
+ client_cli_id = "test-cli-client"
48
+ client_api_id = "default-backend-settings"
49
+ new_settings_cli = {"EXTRA_RAW_DATA_FORMATS": [(r"\.tf(\.gz)?$", "TEST_FORMAT")]}
50
+ new_settings_api = {"EXTRA_RAW_DATA_FORMATS": [("TEST_FORMAT", "TEST_FORMAT")]}
51
+
52
+ # Get or create the test cli client and patch its settings
53
+ client_cli = create_instance("clients", slug=client_cli_id)
54
+ client_cli = patch_instance("clients", client_cli.pk, settings=new_settings_cli)
55
+
56
+ # Get or create the test backend client and patch its settings
57
+ client_api = get_instance("clients", client_api_id)
58
+ client_api = patch_instance("clients", client_api.pk, settings=new_settings_api)
59
+
60
+
61
+ @pytest.fixture(scope="session", autouse=True)
62
+ def set_data_dir(tmpdir_factory):
63
+ """Set data dir for all tests."""
64
+ _DEFAULTS["BASE_STORAGE_DIRECTORY"] = tmpdir_factory.mktemp("data").strpath
65
+
@@ -1,9 +1,12 @@
1
+ import sys
2
+
1
3
  from click.testing import CliRunner
2
4
  import click
3
5
  import pytest
4
6
  import requests
5
7
 
6
8
  from isabl_cli import api
9
+ from isabl_cli import exceptions
7
10
  from isabl_cli import factories
8
11
  from isabl_cli.settings import system_settings
9
12
  from isabl_cli.settings import user_settings
@@ -43,7 +46,90 @@ def test_get_custom_fields():
43
46
  def test_api_request():
44
47
  assert api.api_request("get", url="auth").ok
45
48
  with pytest.raises(requests.HTTPError):
46
- api.api_request("delete", url="http://google.com")
49
+ api.api_request("get", url="http://google.com/isabl-cli-not-found")
50
+
51
+
52
+ @pytest.mark.parametrize("method", ["post", "put", "patch", "delete", "PATCH"])
53
+ def test_test_session_writes_refused_for_remote_api(method):
54
+ url = "https://isabl.example.org/api/v1/projects"
55
+ with pytest.raises(exceptions.TestSessionWriteError, match="from a test session"):
56
+ api.check_test_session_write(method, url)
57
+
58
+
59
+ @pytest.mark.parametrize(
60
+ "method, url",
61
+ [
62
+ ("get", "https://isabl.example.org/api/v1/projects"),
63
+ ("post", "http://localhost:8000/api/v1/projects"),
64
+ ("post", "http://127.0.0.1:8000/api/v1/projects"),
65
+ ("delete", "http://[::1]:8000/api/v1/projects/1"),
66
+ ],
67
+ )
68
+ def test_test_session_reads_and_local_writes_allowed(method, url):
69
+ api.check_test_session_write(method, url)
70
+
71
+
72
+ def test_test_session_writes_allowed_to_named_api(monkeypatch):
73
+ url = "https://isabl-test.example.org/api/v1/projects"
74
+ monkeypatch.setenv(api.TEST_WRITES_URL_VARIABLE, "https://isabl-test.example.org/")
75
+ api.check_test_session_write("post", url)
76
+
77
+ monkeypatch.setenv(api.TEST_WRITES_URL_VARIABLE, "https://other.example.org/")
78
+ with pytest.raises(exceptions.TestSessionWriteError):
79
+ api.check_test_session_write("post", url)
80
+
81
+
82
+ def test_writes_allowed_outside_test_sessions(monkeypatch):
83
+ monkeypatch.delitem(sys.modules, "pytest")
84
+ api.check_test_session_write("post", "https://isabl.example.org/api/v1/projects")
85
+
86
+
87
+ def test_test_session_write_refused_before_sending(monkeypatch):
88
+ sent = []
89
+ monkeypatch.setattr(requests, "post", lambda *args, **kwargs: sent.append(kwargs))
90
+
91
+ with pytest.raises(exceptions.TestSessionWriteError):
92
+ api.retry_request("post", url="https://isabl.example.org/api/v1/projects")
93
+
94
+ assert sent == []
95
+
96
+
97
+ def _spy_requests(monkeypatch, *methods):
98
+ """Record headers sent by `requests.<method>` while still performing the call."""
99
+ sent = []
100
+ for method in methods:
101
+ real = getattr(requests, method)
102
+
103
+ def spy(*args, _real=real, **kwargs):
104
+ sent.append(dict(kwargs.get("headers") or {}))
105
+ return _real(*args, **kwargs)
106
+
107
+ monkeypatch.setattr(requests, method, spy)
108
+ return sent
109
+
110
+
111
+ def test_requests_send_cli_version(monkeypatch):
112
+ from isabl_cli import __version__
113
+
114
+ sent = _spy_requests(monkeypatch, "get")
115
+ api.get_token_headers.cache_clear() # force the /rest-auth/user/ check too
116
+ assert api.api_request("get", url="auth").ok
117
+ assert api.api_request("get", url="/", authenticate=False).ok
118
+
119
+ assert len(sent) == 3 # token check, authenticated get, unauthenticated get
120
+ for headers in sent:
121
+ assert headers[api.CLI_VERSION_HEADER] == __version__
122
+ assert headers["User-Agent"].startswith(f"isabl_cli/{__version__} ")
123
+
124
+ assert "Authorization" in sent[1]
125
+ assert "Authorization" not in sent[2]
126
+
127
+
128
+ def test_cli_headers_do_not_mutate_token_headers():
129
+ token_headers = api.get_token_headers()
130
+ api.api_request("get", url="auth")
131
+ assert api.CLI_VERSION_HEADER not in token_headers
132
+ assert set(token_headers) == {"Authorization"}
47
133
 
48
134
 
49
135
  def test_api_methods():
@@ -324,6 +324,25 @@ def test_force_process_finished_tags(tmpdir):
324
324
  assert analysis["status"] == "SUCCEEDED"
325
325
 
326
326
 
327
+ def test_patch_status_finished_results(tmpdir):
328
+ """Check FINISHED analyses get results and storage usage patched."""
329
+ application = MockApplication()
330
+ analysis = api.create_instance(
331
+ "analyses",
332
+ storage_url=tmpdir.strpath,
333
+ status="STARTED",
334
+ application=application.application,
335
+ targets=[api.create_instance("experiments", **factories.ExperimentFactory())],
336
+ )
337
+ runner = CliRunner()
338
+ args = ["--key", analysis["pk"], "--status", "FINISHED"]
339
+ runner.invoke(commands.patch_status, args, catch_exceptions=False)
340
+ analysis = api.get_instance("analyses", analysis["pk"])
341
+ assert analysis["status"] == "FINISHED"
342
+ assert analysis["results"]["analysis_result_key"] == 1
343
+ assert analysis["storage_usage"] is not None
344
+
345
+
327
346
  def test_rejected_analysis(tmpdir):
328
347
  """Check an analysis can be rejected using the command cli."""
329
348
  analysis = api.create_instance(
@@ -1 +0,0 @@
1
- 2.0.2
@@ -1,26 +0,0 @@
1
- import pytest
2
-
3
- from isabl_cli.api import create_instance, patch_instance, get_instance
4
- from isabl_cli.settings import _DEFAULTS
5
-
6
- def pytest_sessionstart():
7
- """Run before any test starts."""
8
- client_cli_id = "test-cli-client"
9
- client_api_id = "default-backend-settings"
10
- new_settings_cli = {"EXTRA_RAW_DATA_FORMATS": [(r"\.tf(\.gz)?$", "TEST_FORMAT")]}
11
- new_settings_api = {"EXTRA_RAW_DATA_FORMATS": [("TEST_FORMAT", "TEST_FORMAT")]}
12
-
13
- # Get or create the test cli client and patch its settings
14
- client_cli = create_instance("clients", slug=client_cli_id)
15
- client_cli = patch_instance("clients", client_cli.pk, settings=new_settings_cli)
16
-
17
- # Get or create the test backend client and patch its settings
18
- client_api = get_instance("clients", client_api_id)
19
- client_api = patch_instance("clients", client_api.pk, settings=new_settings_api)
20
-
21
-
22
- @pytest.fixture(scope="session", autouse=True)
23
- def set_data_dir(tmpdir_factory):
24
- """Set data dir for all tests."""
25
- _DEFAULTS["BASE_STORAGE_DIRECTORY"] = tmpdir_factory.mktemp("data").strpath
26
-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes