Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: fetch_cloudos_id function #130

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9e507a1
feat: add `--request-interval` parameter (#93)
danielboloc Oct 3, 2022
70d3dd0
Adds ssl verification control (#103)
dapineyro Oct 31, 2022
2a1d4f0
Docs patch for SSL functionality (#106)
cgpu Oct 31, 2022
8eb820c
Update README.md
cgpu Nov 2, 2022
6477245
Update README.md
cgpu Nov 2, 2022
e14fc92
Change default cost limit 30 $ (#102)
egenc Nov 4, 2022
c816047
Resolve conflicts
dapineyro Nov 4, 2022
ddedb58
Feat: make --wdl-importsFile optional (#98)
dapineyro Nov 7, 2022
4f47770
Adds api key support for cromwell (#104)
dapineyro Nov 7, 2022
4b9bcf6
Merge branch 'main' into dev
dapineyro Nov 7, 2022
209d8ee
Remove cromwell_token
dapineyro Nov 7, 2022
b782677
Pytests added for _create_cromwell_header function (#114)
egenc Nov 25, 2022
8af1801
test: add tests for 'get_job_list' (#116)
danielboloc Nov 25, 2022
dd6e1b4
Fix: resolve JSON error in error message handling (#124)
dapineyro Dec 2, 2022
94c6245
test: add fetch_cloudos_id function test
danielboloc Dec 12, 2022
3703b07
Merge branch 'main' into test_fetch_cloudos_id
danielboloc Dec 12, 2022
3a30cad
Merge branch 'dev' into test_fetch_cloudos_id
danielboloc Dec 12, 2022
80d55a6
Pytest for BadRequestException Class (#126)
egenc Dec 13, 2022
806f2a9
Pytest added for function project_id (#120)
egenc Dec 16, 2022
0ce9deb
Pytest added for function detect_workflow (#119)
egenc Dec 16, 2022
60700a4
pytest added for output workflow_id (#121)
egenc Dec 16, 2022
cfdb56b
Pytest workflow (#132)
egenc Dec 16, 2022
7eed847
Pytest added for function process_workflow_list (#118)
egenc Dec 16, 2022
0202f26
test: adds tests for 'get_workflow_list' (#117)
danielboloc Dec 16, 2022
738369a
Merge branch 'dev' into test_fetch_cloudos_id
egenc Dec 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ old_scripts/

# pychache
__pycache__

# .DS_Store
.DS_Store
._.DS_Store
**/.DS_Store
**/._.DS_Store
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## lifebit-ai/cloudos-cli: changelog

### 1.3.1 - 2022-12-01
- Patch: fixes `BarRequestException` and `TimeOutException` messages when the response from
the API server is empty.

### 1.3.0 - 2022-11-07
- All Cromwell functionality works now with personal API key. The
`--cromwell-token` argument is maintained for backwards compatibility, but can
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# cloudos

__Date:__ 2022-11-07\
__Version:__ 1.3.0
__Date:__ 2022-12-01\
__Version:__ 1.3.1


Python package for interacting with CloudOS
Expand All @@ -25,7 +25,7 @@ and the `environment.yml` files provided.
To run the existing docker image at `quay.io`:

```bash
docker run --rm -it quay.io/lifebitaiorg/cloudos-cli:v1.3.0
docker run --rm -it quay.io/lifebitaiorg/cloudos-cli:v1.3.1
```

### From Github
Expand Down Expand Up @@ -78,7 +78,7 @@ cloudos job run --help
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand Down Expand Up @@ -220,7 +220,7 @@ If everything went well, you should see something like:
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand Down Expand Up @@ -263,7 +263,7 @@ previous command should have an output similar to:
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand Down Expand Up @@ -292,7 +292,7 @@ The expected output should be something similar to:
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand Down Expand Up @@ -327,7 +327,7 @@ The expected output is something similar to:
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand All @@ -350,7 +350,7 @@ cloudos job list \
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand Down Expand Up @@ -383,7 +383,7 @@ The expected output is something similar to:
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS workflow functionality: list workflows in CloudOS.

Expand All @@ -405,7 +405,7 @@ cloudos workflow list \
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS workflow functionality: list workflows in CloudOS.

Expand All @@ -432,7 +432,7 @@ cloudos cromwell status \
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

Cromwell server functionality: check status, start and stop.

Expand All @@ -451,7 +451,7 @@ cloudos cromwell start \
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

Cromwell server functionality: check status, start and stop.

Expand All @@ -472,7 +472,7 @@ cloudos cromwell stop \
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

Cromwell server functionality: check status, start and stop.

Expand Down Expand Up @@ -538,7 +538,7 @@ cloudos job run \
```console
CloudOS python package: a package for interacting with CloudOS.

Version: 1.3.0
Version: 1.3.1

CloudOS job functionality: run and check jobs in CloudOS.

Expand Down
2 changes: 1 addition & 1 deletion cloudos/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.3.0'
__version__ = '1.3.1'
12 changes: 4 additions & 8 deletions cloudos/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
Specific classes to handle errors.
"""

import json


class BadRequestException(Exception):
"""Handle bad request exceptions and shows improved messages.

Parameters
----------
rv : request variable
rv : requests.Response
The request variable returned that caused the error.
"""
def __init__(self, rv):
msg = "Server returned status {}. Response:\n{}".format(
rv.status_code, json.dumps(rv.json())
)
msg = "Server returned status {}. Reason: {}".format(rv.status_code, rv.reason)
super(BadRequestException, self).__init__(msg)
self.rv = rv

Expand All @@ -26,11 +22,11 @@ class TimeOutException(Exception):

Parameters
----------
rv : request variable
rv : requests.Response
The request variable returned that caused the error.
"""
def __init__(self, rv):
msg = ("Server exceeded the max time to process request. " +
"Response:\n{}".format(json.dumps(rv)))
"Status: {}; Reason: {}".format(rv.status_code, rv.reason))
super(TimeOutException, self).__init__(msg)
self.rv = rv
Empty file added tests/test_clos/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions tests/test_clos/test_create_cromwell_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from cloudos.clos import Cloudos

TOKEN = "lifebit_test_token"
URL = "lifebit.ai"

EXPECTED_OUTPUT_APIKEY = {
"Accept": "application/json",
"apikey": TOKEN
}

EXPECTED_OUTPUT_CROMWELL_TOKEN = {
"Accept": "application/json",
"Authorization": f'Bearer {TOKEN}'
}


def test_create_cromwell_header_apikey():
"""Testing only apikey is provided."""
clos = Cloudos(apikey=TOKEN, cromwell_token=None, cloudos_url=URL)
output = clos._create_cromwell_header()
assert output == EXPECTED_OUTPUT_APIKEY


def test_create_cromwell_header_cromwell_token():
"""Test only cromwell_token is provided."""
clos = Cloudos(apikey=None, cromwell_token=TOKEN, cloudos_url=URL)
output = clos._create_cromwell_header()
assert output == EXPECTED_OUTPUT_CROMWELL_TOKEN


def test_create_cromwell_header_both():
"""Test both cromwell_token and apikey are provided."""
clos = Cloudos(apikey=TOKEN, cromwell_token=TOKEN, cloudos_url=URL)
output = clos._create_cromwell_header()
assert output == EXPECTED_OUTPUT_CROMWELL_TOKEN
40 changes: 40 additions & 0 deletions tests/test_clos/test_detect_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Pytest for method Cloudos.detect_workflow"""
import mock
import responses
from responses import matchers
from cloudos.clos import Cloudos
from tests.functions_for_pytest import load_json_file

INPUT = "tests/test_data/process_workflow_list_initial_request.json"
APIKEY = 'vnoiweur89u2ongs'
CLOUDOS_URL = 'http://cloudos.lifebit.ai'
WORKSPACE_ID = 'lv89ufc838sdig'


@mock.patch('cloudos.clos', mock.MagicMock())
@responses.activate
def test_detect_workflow():
"""
Test 'get_workflow_list' to work as intended
API request is mocked and replicated with json files
"""
json_data = load_json_file(INPUT)
params = {"teamId": WORKSPACE_ID, "apikey": APIKEY}
header = {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=UTF-8"
}
search_str = f"teamId={WORKSPACE_ID}&apikey={APIKEY}"
# mock GET method with the .json
responses.add(
responses.GET,
url=f"{CLOUDOS_URL}/api/v1/workflows?{search_str}",
body=json_data,
headers=header,
match=[matchers.query_param_matcher(params)],
status=200)
# start cloudOS service
clos = Cloudos(apikey=APIKEY, cromwell_token=None, cloudos_url=CLOUDOS_URL)
# get mock response
response = clos.detect_workflow(workspace_id=WORKSPACE_ID, workflow_name="picard")
assert response == 'docker'
77 changes: 77 additions & 0 deletions tests/test_clos/test_get_job_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import mock
import json
import pytest
import requests
import responses
from responses import matchers
from cloudos.clos import Cloudos
from cloudos.utils.errors import BadRequestException
from tests.functions_for_pytest import load_json_file

INPUT = "tests/test_data/process_job_list_initial_json.json"
APIKEY = 'vnoiweur89u2ongs'
CLOUDOS_URL = 'http://cloudos.lifebit.ai'
WORKSPACE_ID = 'lv89ufc838sdig'


@mock.patch('cloudos.clos', mock.MagicMock())
@responses.activate
def test_get_job_list_correct_response():
"""
Test 'get_job_list' to work as intended
API request is mocked and replicated with json files
"""
create_json = load_json_file(INPUT)
params = {"teamId": WORKSPACE_ID, "apikey": APIKEY}
header = {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=UTF-8"
}
search_str = f"teamId={WORKSPACE_ID}&apikey={APIKEY}"
# mock GET method with the .json
responses.add(
responses.GET,
url=f"{CLOUDOS_URL}/api/v1/jobs?{search_str}",
body=create_json,
headers=header,
match=[matchers.query_param_matcher(params)],
status=200)
# start cloudOS service
clos = Cloudos(apikey=APIKEY, cromwell_token=None, cloudos_url=CLOUDOS_URL)
# get mock response
response = clos.get_job_list(WORKSPACE_ID)
# check the response
assert response.status_code == 200
assert isinstance(response, requests.models.Response)


@mock.patch('cloudos.clos', mock.MagicMock())
@responses.activate
def test_get_job_list_incorrect_response():
"""
Test 'get_job_list' to fail with '400' response
"""
# prepare error message
error_message = {"statusCode": 400, "code": "BadRequest",
"message": "Bad Request.", "time": "2022-11-23_17:31:07"}
error_json = json.dumps(error_message)
params = {"teamId": WORKSPACE_ID, "apikey": APIKEY}
header = {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=UTF-8"
}
search_str = f"teamId={WORKSPACE_ID}&apikey={APIKEY}"
# mock GET method with the .json
responses.add(
responses.GET,
url=f"{CLOUDOS_URL}/api/v1/jobs?{search_str}",
body=error_json,
headers=header,
match=[matchers.query_param_matcher(params)],
status=400)
# raise 400 error
with pytest.raises(BadRequestException) as error:
# check if it failed
clos = Cloudos(apikey=APIKEY, cromwell_token=None, cloudos_url=CLOUDOS_URL)
clos.get_job_list(WORKSPACE_ID)
assert "Bad Request" in (str(error))
Loading