Skip to content

Commit 19fdb0b

Browse files
committed
Add more tests for clusters list
1 parent 595f743 commit 19fdb0b

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
apiKey: some_key
2+
limit: 2

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,10 @@ def entity_tags_remove_config_path():
382382
p = Path(__file__)
383383
fixture_dir = p.parent / "config_files" / "entity_tags_remove.yaml"
384384
return str(fixture_dir.resolve())
385+
386+
387+
@pytest.fixture
388+
def clusters_list_config_path():
389+
p = Path(__file__)
390+
fixture_dir = p.parent / "config_files" / "clusters_list.yaml"
391+
return str(fixture_dir.resolve())

tests/example_responses.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,6 +6141,7 @@
61416141
"tagFilter": []
61426142
}
61436143

6144+
61446145
EXAMPLE_CLUSTERS_LIST_RESPONSE = [
61456146
{
61466147
"id": "cluster_id_1",
@@ -6188,3 +6189,37 @@
61886189
"modelId": 1170
61896190
}
61906191
]
6192+
6193+
6194+
LIMITED_EXAMPLE_CLUSTERS_LIST_RESPONSE = [
6195+
{
6196+
"id": "cluster_id_1",
6197+
"name": "cluster name 1",
6198+
"type": "Job Cluster",
6199+
"region": "Private",
6200+
"cloud": "private",
6201+
"teamId": "team_id",
6202+
"isDefault": True,
6203+
"dtCreated": "2019-07-05T23:28:17.416Z",
6204+
"dtModified": "2019-07-05T23:28:17.416Z",
6205+
"clusterId": 91,
6206+
"isPrivate": True,
6207+
"modelName": "team",
6208+
"modelId": 1170
6209+
},
6210+
{
6211+
"id": "cluster_id_2",
6212+
"name": "cluster name 2",
6213+
"type": "Kubernetes Processing Site",
6214+
"region": "Private",
6215+
"cloud": "aws",
6216+
"teamId": "team_id",
6217+
"isDefault": False,
6218+
"dtCreated": "2019-07-22T14:50:10.170Z",
6219+
"dtModified": "2019-11-21T18:12:27.723Z",
6220+
"clusterId": 92,
6221+
"isPrivate": True,
6222+
"modelName": "team",
6223+
"modelId": 1170
6224+
}
6225+
]

tests/functional/test_clusters.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ class TestClustersList(object):
1010
URL = "https://api.paperspace.io/clusters/getClusters"
1111

1212
COMMAND = ["clusters", "list"]
13+
COMMAND_WITH_LIMIT = COMMAND[:] + ["-l", "2"]
1314
COMMAND_WITH_OPTIONS_FILE = ["clusters", "list", "--optionsFile", ] # path added in test
1415

1516
LIST_CLUSTERS = example_responses.EXAMPLE_CLUSTERS_LIST_RESPONSE
17+
LIMITED_LIST_CLUSTERS = example_responses.LIMITED_EXAMPLE_CLUSTERS_LIST_RESPONSE
1618

1719
DEFAULT_PARAMS = {
1820
"filter": '{"limit": 20, "offset": 0, "where": {"isPrivate": true}}'
1921
}
22+
LIMITED_PARAMS = {
23+
"filter": '{"limit": 2, "offset": 0, "where": {"isPrivate": true}}'
24+
}
2025

2126
COMMAND_WITH_API_KEY = ["clusters", "list", "--apiKey", "some_key"]
2227
EXPECTED_HEADERS = http_client.default_headers.copy()
@@ -30,6 +35,16 @@ class TestClustersList(object):
3035
| cluster_id_2 | cluster name 2 | Kubernetes Processing Site |
3136
| cluster_id_3 | cluster name 3 | Job Cluster |
3237
+--------------+----------------+----------------------------+
38+
"""
39+
PAGINATED_LIST_STDOUT = """+--------------+----------------+----------------------------+
40+
| ID | Name | Type |
41+
+--------------+----------------+----------------------------+
42+
| cluster_id_1 | cluster name 1 | Job Cluster |
43+
| cluster_id_2 | cluster name 2 | Kubernetes Processing Site |
44+
+--------------+----------------+----------------------------+
45+
46+
Do you want to continue? [y/N]:
47+
Aborted!
3348
"""
3449

3550
@mock.patch("gradient.api_sdk.clients.http_client.requests.get")
@@ -44,3 +59,32 @@ def test_should_send_get_request_and_print_list_of_clusters(self, get_patched):
4459
headers=self.EXPECTED_HEADERS,
4560
json=None,
4661
params=self.DEFAULT_PARAMS)
62+
63+
@mock.patch("gradient.api_sdk.clients.http_client.requests.get")
64+
def test_should_send_get_request_and_paginate_list_of_clusters(self, get_patched):
65+
get_patched.return_value = MockResponse(self.LIMITED_LIST_CLUSTERS, 200, "fake content")
66+
67+
runner = CliRunner()
68+
result = runner.invoke(cli.cli, self.COMMAND_WITH_LIMIT)
69+
70+
assert self.PAGINATED_LIST_STDOUT in str(result.output)
71+
get_patched.assert_called_once_with(self.URL,
72+
headers=self.EXPECTED_HEADERS,
73+
json=None,
74+
params=self.LIMITED_PARAMS)
75+
76+
@mock.patch("gradient.api_sdk.clients.http_client.requests.get")
77+
def test_should_read_options_defined_in_a_config_file(self, get_patched, clusters_list_config_path):
78+
get_patched.return_value = MockResponse(json_data=self.LIMITED_LIST_CLUSTERS,
79+
status_code=200)
80+
command = self.COMMAND_WITH_OPTIONS_FILE[:] + [clusters_list_config_path]
81+
82+
runner = CliRunner()
83+
result = runner.invoke(cli.cli, command)
84+
85+
assert self.PAGINATED_LIST_STDOUT in str(result.output)
86+
get_patched.assert_called_once_with(self.URL,
87+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
88+
json=None,
89+
params=self.LIMITED_PARAMS,
90+
)

0 commit comments

Comments
 (0)