Skip to content

Commit

Permalink
cli: paginate reana-client logs and ls commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidalgarcia committed Jul 22, 2020
1 parent 79d23f8 commit 1e69bc3
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 90 deletions.
37 changes: 21 additions & 16 deletions reana_client/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ def get_workflows(
size=size,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
return response.get("items")
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)

except HTTPError as e:
logging.debug(
Expand Down Expand Up @@ -311,11 +310,15 @@ def upload_file(workflow_id, file_, file_name, access_token):
raise e


def get_workflow_logs(workflow_id, access_token, steps=None):
def get_workflow_logs(workflow_id, access_token, steps=None, page=None, size=None):
"""Get logs from a workflow engine."""
try:
(response, http_response) = current_rs_api_client.api.get_workflow_logs(
workflow_id_or_name=workflow_id, steps=steps, access_token=access_token
workflow_id_or_name=workflow_id,
steps=steps,
access_token=access_token,
page=page,
size=size,
).result()

if http_response.status_code == 200:
Expand Down Expand Up @@ -421,7 +424,7 @@ def delete_file(workflow_id, file_name, access_token):
raise e


def list_files(workflow_id, access_token):
def list_files(workflow_id, access_token, page=None, size=None):
"""Return the list of file for a given workflow workspace.
:param workflow_id: UUID which identifies the workflow.
Expand All @@ -430,16 +433,18 @@ def list_files(workflow_id, access_token):
"""
try:
(response, http_response) = current_rs_api_client.api.get_files(
workflow_id_or_name=workflow_id, access_token=access_token
workflow_id_or_name=workflow_id,
access_token=access_token,
page=page,
size=size,
).result()

if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
return response.get("items")
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)

except HTTPError as e:
logging.debug(
Expand Down
8 changes: 6 additions & 2 deletions reana_client/cli/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from reana_client.api.utils import get_path_from_operation_id
from reana_client.cli.utils import (
add_access_token_options,
add_pagination_options,
add_workflow_option,
check_connection,
filter_data,
Expand Down Expand Up @@ -66,8 +67,11 @@ def files_group(ctx):
help="Get URLs of output files.",
)
@add_access_token_options
@add_pagination_options
@click.pass_context
def get_files(ctx, workflow, _filter, output_format, access_token): # noqa: D301
def get_files(
ctx, workflow, _filter, output_format, access_token, page, size
): # noqa: D301
"""List workspace files.
The `ls` command lists workspace files of a workflow specified by the
Expand All @@ -89,7 +93,7 @@ def get_files(ctx, workflow, _filter, output_format, access_token): # noqa: D30
if workflow:
logging.info('Workflow "{}" selected'.format(workflow))
try:
response = list_files(workflow, access_token)
response = list_files(workflow, access_token, page, size)
headers = ["name", "size", "last-modified"]
data = []
file_path = get_path_from_operation_id(
Expand Down
18 changes: 16 additions & 2 deletions reana_client/cli/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,19 @@ def add_verbose_data_from_response(response, verbose_headers, headers, data):
multiple=True,
help="Filter job logs to include only those steps that match certain filtering criteria. Use --filter name=value pairs. Available filters are compute_backend, docker_img, status and step.",
)
@add_pagination_options
@check_connection
@click.pass_context
def workflow_logs(ctx, workflow, access_token, json_format, filters=None): # noqa: D301
def workflow_logs(
ctx,
workflow,
access_token,
json_format,
steps=None,
filters=None,
page=None,
size=None,
): # noqa: D301
"""Get workflow logs.
The `logs` command allows to retrieve logs of running workflow. Note that
Expand Down Expand Up @@ -802,7 +812,11 @@ def workflow_logs(ctx, workflow, access_token, json_format, filters=None): # no
sys.exit(1)
try:
response = get_workflow_logs(
workflow, access_token, None if not steps else list(set(steps))
workflow,
access_token,
steps=None if not steps else list(set(steps)),
page=page,
size=size,
)
workflow_logs = json.loads(response["logs"])
if filters:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cli_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_list_files_server_no_token():
def test_list_files_ok():
"""Test list workflow workspace files successfull."""
status_code = 200
response = [{"last-modified": "string", "name": "string", "size": 0}]
response = {"items": [{"last-modified": "string", "name": "string", "size": 0}]}
env = {"REANA_SERVER_URL": "localhost"}
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand All @@ -61,13 +61,13 @@ def test_list_files_ok():
assert result.exit_code == 0
assert isinstance(json_response, list)
assert len(json_response) == 1
assert json_response[0]["name"] in response[0]["name"]
assert json_response[0]["name"] in response["items"][0]["name"]


def test_list_files_url():
"""Test list workflow workspace files' urls."""
status_code = 200
response = [{"last-modified": "string", "name": "string", "size": 0}]
response = {"items": [{"last-modified": "string", "name": "string", "size": 0}]}
env = {"REANA_SERVER_URL": "localhost"}
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand All @@ -85,7 +85,7 @@ def test_list_files_url():
)
assert result.exit_code == 0
assert workflow_name in result.output
assert response[0]["name"] in result.output
assert response["items"][0]["name"] in result.output


def test_download_file():
Expand Down
142 changes: 76 additions & 66 deletions tests/test_cli_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ def test_workflows_no_token():

def test_workflows_server_ok():
"""Test workflows command when server is reachable."""
response = [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
}
]
response = {
"items": [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
}
]
}
status_code = 200
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand All @@ -73,26 +75,28 @@ def test_workflows_server_ok():

def test_workflows_sorting():
"""Test workflows sorting."""
response = [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
"progress": {
"run_started_at": "2018-06-13T09:47:40.28223",
"run_finished_at": "2018-06-13T10:30:03.70303",
response = {
"items": [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
"progress": {
"run_started_at": "2018-06-13T09:47:40.28223",
"run_finished_at": "2018-06-13T10:30:03.70303",
},
},
},
{
"status": "running",
"created": "2018-06-13T09:55:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.2",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a2",
},
]
{
"status": "running",
"created": "2018-06-13T09:55:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.2",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a2",
},
]
}
status_code = 200
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand Down Expand Up @@ -122,18 +126,20 @@ def test_workflows_sorting():

def test_workflows_sessions():
"""Test list command for getting interactive sessions."""
response = [
{
"created": "2019-03-19T14:37:58",
"id": "29136cd0-b259-4d48-8c1e-afe3572df408",
"name": "workflow.1",
"session_type": "jupyter",
"session_uri": "/29136cd0-b259-4d48-8c1e-afe3572df408",
"size": "0",
"status": "created",
"user": "00000000-0000-0000-0000-000000000000",
}
]
response = {
"items": [
{
"created": "2019-03-19T14:37:58",
"id": "29136cd0-b259-4d48-8c1e-afe3572df408",
"name": "workflow.1",
"session_type": "jupyter",
"session_uri": "/29136cd0-b259-4d48-8c1e-afe3572df408",
"size": "0",
"status": "created",
"user": "00000000-0000-0000-0000-000000000000",
}
]
}
status_code = 200
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand All @@ -154,16 +160,18 @@ def test_workflows_sessions():

def test_workflows_valid_json():
"""Test workflows command with --json and -v flags."""
response = [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
"size": "0K",
}
]
response = {
"items": [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
"size": "0K",
}
]
}
status_code = 200
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand Down Expand Up @@ -191,22 +199,24 @@ def test_workflows_valid_json():

def test_workflows_filter():
"""Test workflows command with --filter."""
response = [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
},
{
"status": "failed",
"created": "2018-06-14T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.2",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a2",
},
]
response = {
"items": [
{
"status": "running",
"created": "2018-06-13T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.1",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a1",
},
{
"status": "failed",
"created": "2018-06-14T09:47:35.66097",
"user": "00000000-0000-0000-0000-000000000000",
"name": "mytest.2",
"id": "256b25f4-4cfb-4684-b7a8-73872ef455a2",
},
]
}
status_code = 200
mock_http_response, mock_response = Mock(), Mock()
mock_http_response.status_code = status_code
Expand Down

0 comments on commit 1e69bc3

Please sign in to comment.