Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 5477da0

Browse files
committed
Feature PS-9868:
Update logs tests. Prepare Makefile to allow run tests in tox for every available python version.
1 parent cab84b8 commit 5477da0

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
clean-tox:
2+
rm -rf .tox paperspace.egg-info
3+
4+
run-tests: clean-tox
5+
tox

tests/example_responses.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,39 +2908,39 @@
29082908
}, {
29092909
"line": 2,
29102910
"timestamp": "2019-04-03T15:56:35.458Z",
2911-
"message": " File \"generate_figures.py\", line 15, in \u003cmodule\u003e"
2911+
"message": " File \"generate_figures.py\", line 15, in <module>"
29122912
}, {
29132913
"line": 3,
29142914
"timestamp": "2019-04-03T15:56:35.458Z",
29152915
"message": " import dnnlib.tflib as tflib"
29162916
}, {
29172917
"line": 4,
29182918
"timestamp": "2019-04-03T15:56:35.458Z",
2919-
"message": " File \"/paperspace/dnnlib/tflib/__init__.py\", line 8, in \u003cmodule\u003e"
2919+
"message": " File \"/paperspace/dnnlib/tflib/__init__.py\", line 8, in <module>"
29202920
}, {
29212921
"line": 5,
29222922
"timestamp": "2019-04-03T15:56:35.458Z",
29232923
"message": " from . import autosummary"
29242924
}, {
29252925
"line": 6,
29262926
"timestamp": "2019-04-03T15:56:35.458Z",
2927-
"message": " File \"/paperspace/dnnlib/tflib/autosummary.py\", line 31, in \u003cmodule\u003e"
2927+
"message": " File \"/paperspace/dnnlib/tflib/autosummary.py\", line 31, in <module>"
29282928
}, {
29292929
"line": 7,
29302930
"timestamp": "2019-04-03T15:56:35.458Z",
29312931
"message": " from . import tfutil"
29322932
}, {
29332933
"line": 8,
29342934
"timestamp": "2019-04-03T15:56:35.458Z",
2935-
"message": " File \"/paperspace/dnnlib/tflib/tfutil.py\", line 34, in \u003cmodule\u003e"
2935+
"message": " File \"/paperspace/dnnlib/tflib/tfutil.py\", line 34, in <module>"
29362936
}, {
29372937
"line": 9,
29382938
"timestamp": "2019-04-03T15:56:35.458Z",
2939-
"message": " def shape_to_list(shape: Iterable[tf.Dimension]) -\u003e List[Union[int, None]]:"
2939+
"message": " def shape_to_list(shape: Iterable[tf.Dimension]) -> List[Union[int, None]]:"
29402940
}, {
29412941
"line": 10,
29422942
"timestamp": "2019-04-03T15:56:35.458Z",
2943-
"message": "AttributeError: module 'tensorflow' has no attribute 'Dimension'"
2943+
"message": "AttributeError: module \'tensorflow\' has no attribute \'Dimension\'"
29442944
}, {
29452945
"line": 11,
29462946
"timestamp": "2019-04-03T15:56:46.168Z",

tests/functional/test_logs.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@
77

88

99
class TestListLogs(object):
10-
URL = "https://logs.paperspace.io"
10+
URL = "https://logs.paperspace.io/jobs/logs?jobId=some_job_id&line=0"
1111
EXPECTED_HEADERS = default_headers.copy()
12+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY = default_headers.copy()
13+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY["X-API-Key"] = "some_key"
14+
15+
RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"}
1216
EXPECTED_RESPONSE_JSON = example_responses.LIST_OF_LOGS_FOR_JOB
13-
BASIC_COMMAND = ["logs", "list"]
14-
EXPECTED_STDOUT = """+Job jztdeungdkzjv logs------------------------------------------------------------------+
17+
BASIC_COMMAND_WITHOUT_PARAMETERS = ["logs", "list"]
18+
BASIC_COMMAND = ["logs", "list", "--jobId", "some_job_id", "--apiKey", "some_key"]
19+
20+
EXPECTED_STDOUT_WITHOUT_PARAMETERS = """Usage: cli logs list [OPTIONS]
21+
Try "cli logs list --help" for help.
22+
23+
Error: Missing option "--jobId".
24+
"""
25+
26+
EXPECTED_STDOUT = """+Job some_job_id logs--------------------------------------------------------------------+
1527
| LINE | MESSAGE |
1628
+------+---------------------------------------------------------------------------------+
1729
| 1 | Traceback (most recent call last): |
@@ -28,17 +40,57 @@ class TestListLogs(object):
2840
+------+---------------------------------------------------------------------------------+
2941
"""
3042

43+
EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Invalid API token\n"
44+
3145
@mock.patch("paperspace.cli.cli.client.requests.get")
32-
def test_should_send_valid_get_request_and_print_table_with_logs(self, get_patched):
46+
def test_command_should_not_send_request_without_required_parameters(self, get_patched):
47+
cli_runner = CliRunner()
48+
result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITHOUT_PARAMETERS)
49+
print(result)
50+
51+
get_patched.assert_not_called()
52+
assert result.exit_code == 2
53+
assert result.output == self.EXPECTED_STDOUT_WITHOUT_PARAMETERS
54+
55+
@mock.patch("paperspace.cli.cli.client.requests.get")
56+
def test_should_send_valid_get_request_and_print_available_logs(self, get_patched):
3357
get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200)
3458

3559
cli_runner = CliRunner()
3660
result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND)
3761

3862
get_patched.assert_called_with(self.URL,
39-
headers=self.EXPECTED_HEADERS,
63+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
4064
json=None,
4165
params=None)
4266

4367
assert result.output == self.EXPECTED_STDOUT
4468
assert result.exit_code == 0
69+
70+
@mock.patch("paperspace.cli.cli.client.requests.get")
71+
def test_should_send_valid_get_request_when_log_list_was_used_with_wrong_api_key(self, get_patched):
72+
get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400)
73+
74+
cli_runner = CliRunner()
75+
result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND)
76+
77+
get_patched.assert_called_with(self.URL,
78+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
79+
json=None,
80+
params=None)
81+
assert result.output == self.EXPECTED_STDOUT_WITH_WRONG_API_TOKEN
82+
assert result.exit_code == 0
83+
84+
@mock.patch("paperspace.cli.cli.client.requests.get")
85+
def test_should_print_error_message_when_error_status_code_received_but_no_content_was_provided(self, get_patched):
86+
get_patched.return_value = MockResponse(status_code=400)
87+
88+
cli_runner = CliRunner()
89+
result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND)
90+
91+
get_patched.assert_called_with(self.URL,
92+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
93+
json=None,
94+
params=None)
95+
assert result.output == "Error while parsing response data: No JSON\n"
96+
assert result.exit_code == 0

0 commit comments

Comments
 (0)