@@ -167,3 +167,93 @@ def test_should_print_proper_message_when_jobs_list_was_used_with_mutually_exclu
167167 params = None )
168168 assert result .output == self .EXPECTED_STDOUT_WHEN_MUTUALLY_EXCLUSIVE_FILTERS
169169 assert result .exit_code == 0
170+
171+
172+ class TestJobLogs (object ):
173+ URL = "https://logs.paperspace.io/jobs/logs?jobId=some_job_id&line=0"
174+ EXPECTED_HEADERS = default_headers .copy ()
175+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY = default_headers .copy ()
176+ EXPECTED_HEADERS_WITH_CHANGED_API_KEY ["X-API-Key" ] = "some_key"
177+
178+ RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status" : 400 , "message" : "Invalid API token" }
179+ EXPECTED_RESPONSE_JSON = example_responses .LIST_OF_LOGS_FOR_JOB
180+ BASIC_COMMAND_WITHOUT_PARAMETERS = ["jobs" , "log" ]
181+ BASIC_COMMAND = ["jobs" , "log" , "--jobId" , "some_job_id" , "--apiKey" , "some_key" ]
182+
183+ EXPECTED_STDOUT_WITHOUT_PARAMETERS = """Usage: cli jobs log [OPTIONS]
184+ Try "cli jobs log --help" for help.
185+
186+ Error: Missing option "--jobId".
187+ """
188+
189+ EXPECTED_STDOUT = """+Job some_job_id logs--------------------------------------------------------------------+
190+ | LINE | MESSAGE |
191+ +------+---------------------------------------------------------------------------------+
192+ | 1 | Traceback (most recent call last): |
193+ | 2 | File "generate_figures.py", line 15, in <module> |
194+ | 3 | import dnnlib.tflib as tflib |
195+ | 4 | File "/paperspace/dnnlib/tflib/__init__.py", line 8, in <module> |
196+ | 5 | from . import autosummary |
197+ | 6 | File "/paperspace/dnnlib/tflib/autosummary.py", line 31, in <module> |
198+ | 7 | from . import tfutil |
199+ | 8 | File "/paperspace/dnnlib/tflib/tfutil.py", line 34, in <module> |
200+ | 9 | def shape_to_list(shape: Iterable[tf.Dimension]) -> List[Union[int, None]]: |
201+ | 10 | AttributeError: module 'tensorflow' has no attribute 'Dimension' |
202+ | 11 | PSEOF |
203+ +------+---------------------------------------------------------------------------------+
204+ """
205+
206+ EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Invalid API token\n "
207+
208+ @mock .patch ("paperspace.cli.cli.client.requests.get" )
209+ def test_command_should_not_send_request_without_required_parameters (self , get_patched ):
210+ cli_runner = CliRunner ()
211+ result = cli_runner .invoke (cli .cli , self .BASIC_COMMAND_WITHOUT_PARAMETERS )
212+ print (result )
213+
214+ get_patched .assert_not_called ()
215+ assert result .exit_code == 2
216+ assert result .output == self .EXPECTED_STDOUT_WITHOUT_PARAMETERS
217+
218+ @mock .patch ("paperspace.cli.cli.client.requests.get" )
219+ def test_should_send_valid_get_request_and_print_available_logs (self , get_patched ):
220+ get_patched .return_value = MockResponse (json_data = self .EXPECTED_RESPONSE_JSON , status_code = 200 )
221+
222+ cli_runner = CliRunner ()
223+ result = cli_runner .invoke (cli .cli , self .BASIC_COMMAND )
224+
225+ get_patched .assert_called_with (self .URL ,
226+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
227+ json = None ,
228+ params = None )
229+
230+ assert result .output == self .EXPECTED_STDOUT
231+ assert result .exit_code == 0
232+
233+ @mock .patch ("paperspace.cli.cli.client.requests.get" )
234+ def test_should_send_valid_get_request_when_log_list_was_used_with_wrong_api_key (self , get_patched ):
235+ get_patched .return_value = MockResponse (json_data = self .RESPONSE_JSON_WITH_WRONG_API_TOKEN , status_code = 400 )
236+
237+ cli_runner = CliRunner ()
238+ result = cli_runner .invoke (cli .cli , self .BASIC_COMMAND )
239+
240+ get_patched .assert_called_with (self .URL ,
241+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
242+ json = None ,
243+ params = None )
244+ assert result .output == self .EXPECTED_STDOUT_WITH_WRONG_API_TOKEN
245+ assert result .exit_code == 0
246+
247+ @mock .patch ("paperspace.cli.cli.client.requests.get" )
248+ def test_should_print_error_message_when_error_status_code_received_but_no_content_was_provided (self , get_patched ):
249+ get_patched .return_value = MockResponse (status_code = 400 )
250+
251+ cli_runner = CliRunner ()
252+ result = cli_runner .invoke (cli .cli , self .BASIC_COMMAND )
253+
254+ get_patched .assert_called_with (self .URL ,
255+ headers = self .EXPECTED_HEADERS_WITH_CHANGED_API_KEY ,
256+ json = None ,
257+ params = None )
258+ assert result .output == "Error while parsing response data: No JSON\n "
259+ assert result .exit_code == 0
0 commit comments