From 8e37a76edab4d771631fafe0be31fba97279b15c Mon Sep 17 00:00:00 2001 From: Ben Batha Date: Wed, 13 Apr 2022 11:36:37 -0400 Subject: [PATCH 1/3] fix(clusters): machineTypes list returns no data PLA-867 * Include public cluster data by default. You cannot use --clusterId to see public cluster data because of the way the endpoint works. * When there are inactive vmTypes in the cluster the response does not include enough info to render the table properly so inactive instances are now filtered from the response. --- gradient/api_sdk/repositories/common.py | 48 +++++++++++++------ .../api_sdk/repositories/machine_types.py | 5 +- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/gradient/api_sdk/repositories/common.py b/gradient/api_sdk/repositories/common.py index 064bd011..1e8e14eb 100644 --- a/gradient/api_sdk/repositories/common.py +++ b/gradient/api_sdk/repositories/common.py @@ -58,7 +58,8 @@ def _get(self, **kwargs): url = self.get_request_url(**kwargs) client = self._get_client(**kwargs) response = self._send_request(client, url, json=json_, params=params) - gradient_response = http_client.GradientResponse.interpret_response(response) + gradient_response = http_client.GradientResponse.interpret_response( + response) return gradient_response @@ -118,6 +119,9 @@ def list(self, **kwargs): return instances, meta_data return instances + def _get_request_params(self, kwargs): + return {'includePublicClusters': 'true'} + def _get_instances(self, response, **kwargs): if not response.data: return [] @@ -164,7 +168,8 @@ class CreateResource(BaseRepository): def create(self, instance, data=None, path=None): instance_dict = self._get_instance_dict(instance) - response = self._send_create_request(instance_dict, data=data, path=path) + response = self._send_create_request( + instance_dict, data=data, path=path) self._validate_response(response) handle = self._process_response(response) return handle @@ -189,8 +194,10 @@ def _send_create_request(self, instance_dict, data=None, path=None): json_ = self._get_request_json(instance_dict) params = self._get_request_params(instance_dict) files = self._get_request_files(path) - response = client.post(url, params=params, json=json_, data=data, files=files) - gradient_response = http_client.GradientResponse.interpret_response(response) + response = client.post( + url, params=params, json=json_, data=data, files=files) + gradient_response = http_client.GradientResponse.interpret_response( + response) return gradient_response def _process_response(self, response): @@ -241,7 +248,8 @@ def _send(self, url, **kwargs): client = self._get_client(**kwargs) json_data = self._get_request_json(kwargs) response = self._send_request(client, url, json_data=json_data) - gradient_response = http_client.GradientResponse.interpret_response(response) + gradient_response = http_client.GradientResponse.interpret_response( + response) return gradient_response def _send_request(self, client, url, json_data=None): @@ -300,7 +308,8 @@ def _get_metrics_api_url(self, instance, protocol="https"): if not instance.metrics_url: raise GradientSdkError("Metrics API url not found") - metrics_api_url = concatenate_urls(protocol + "://", instance.metrics_url) + metrics_api_url = concatenate_urls( + protocol + "://", instance.metrics_url) return metrics_api_url def _get(self, **kwargs): @@ -346,7 +355,8 @@ def _get_built_in_metrics_list(self, kwargs): return metrics def _get_start_date(self, instance, kwargs): - datetime_string = kwargs.get("start") or instance.dt_started or instance.dt_created + datetime_string = kwargs.get( + "start") or instance.dt_started or instance.dt_created if not datetime_string: return None @@ -377,6 +387,7 @@ def _format_datetime(self, some_datetime): datetime_str = some_datetime.strftime("%Y-%m-%dT%H:%M:%SZ") return datetime_str + class ListMetrics(GetResource): OBJECT_TYPE = None @@ -390,7 +401,8 @@ def _get_metrics_api_url(self, instance, protocol="https"): if not instance.metrics_url: raise GradientSdkError("Metrics API url not found") - metrics_api_url = concatenate_urls(protocol + "://", instance.metrics_url) + metrics_api_url = concatenate_urls( + protocol + "://", instance.metrics_url) return metrics_api_url def _get(self, **kwargs): @@ -425,7 +437,8 @@ def _get_api_url(self, **kwargs): return api_url def _get_start_date(self, instance, kwargs): - datetime_string = kwargs.get("start") or instance.dt_started or instance.dt_created + datetime_string = kwargs.get( + "start") or instance.dt_started or instance.dt_created if not datetime_string: return None @@ -456,6 +469,7 @@ def _format_datetime(self, some_datetime): datetime_str = some_datetime.strftime("%Y-%m-%dT%H:%M:%SZ") return datetime_str + @six.add_metaclass(abc.ABCMeta) class StreamMetrics(BaseRepository): OBJECT_TYPE = None @@ -467,7 +481,8 @@ def _get_metrics_api_url(self, instance, protocol="https"): if not instance.metrics_url: raise GradientSdkError("Metrics API url not found") - metrics_api_url = concatenate_urls(protocol + "://", instance.metrics_url) + metrics_api_url = concatenate_urls( + protocol + "://", instance.metrics_url) return metrics_api_url def _get_api_url(self, **kwargs): @@ -484,20 +499,23 @@ def stream(self, **kwargs): self.logger.debug("Metrics data: {}".format(data)) yield data except websocket.WebSocketConnectionClosedException as e: - self.logger.debug("WebSocketConnectionClosedException: {}".format(e)) + self.logger.debug( + "WebSocketConnectionClosedException: {}".format(e)) except sdk_exceptions.EndWebsocketStream: return def _get_connection(self, kwargs): url = self._get_full_url(kwargs) - self.logger.debug("(Re)opening websocket connection to: {}".format(url)) + self.logger.debug( + "(Re)opening websocket connection to: {}".format(url)) ws = websocket.create_connection(url) self.logger.debug("Connected") return ws def _get_full_url(self, kwargs): instance_id = kwargs["id"] - metrics_api_url = self._get_metrics_api_url(instance_id, protocol="wss") + metrics_api_url = self._get_metrics_api_url( + instance_id, protocol="wss") url = concatenate_urls(metrics_api_url, self.get_request_url()) return url @@ -529,11 +547,13 @@ def _send_chart_descriptor(self, connection, kwargs): descriptor = self._get_chart_descriptor(kwargs) self.logger.debug("Sending chart descriptor: {}".format(descriptor)) response = connection.send(descriptor) - self.logger.debug("Chart descriptor sent. Response: {}".format(response)) + self.logger.debug( + "Chart descriptor sent. Response: {}".format(response)) def _get_stream_generator(self, connection): return connection + class ListLogs(ListResources): @abc.abstractmethod def _get_request_params(self, kwargs): diff --git a/gradient/api_sdk/repositories/machine_types.py b/gradient/api_sdk/repositories/machine_types.py index a60844fd..a17ad72b 100644 --- a/gradient/api_sdk/repositories/machine_types.py +++ b/gradient/api_sdk/repositories/machine_types.py @@ -19,11 +19,14 @@ def _get_instance_dicts(self, data, cluster_id=None, **kwargs): current_cluster_id = cluster_list_of_vms[0]["clusters"][0]["id"] for vm in cluster_list_of_vms: + if not vm["isAvailable"]: + continue vm_type = vm["vmType"] vm_type_label = vm_type["label"] vm_types_dicts.setdefault(vm_type_label, vm_type) - clusters = vm_types_dicts[vm_type_label].setdefault("clusters", []) + clusters = vm_types_dicts[vm_type_label].setdefault( + "clusters", []) if current_cluster_id not in clusters: clusters.append(current_cluster_id) From ae46e98360ca0707c4383e00156fd482553500b3 Mon Sep 17 00:00:00 2001 From: Ben Batha Date: Wed, 13 Apr 2022 11:55:32 -0400 Subject: [PATCH 2/3] fixup! fix(clusters): machineTypes list returns no data PLA-867 --- tests/functional/test_clusters.py | 11 +- tests/functional/test_machines.py | 236 ++++++++++++++++++++---------- 2 files changed, 161 insertions(+), 86 deletions(-) diff --git a/tests/functional/test_clusters.py b/tests/functional/test_clusters.py index 6bd8ac98..71b52975 100644 --- a/tests/functional/test_clusters.py +++ b/tests/functional/test_clusters.py @@ -127,6 +127,7 @@ class TestListVmTypes(object): RESPONSE_JSON_WHEN_WRONG_API_KEY_WAS_USED = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WHEN_WRONG_API_KEY_WAS_USED = "Failed to fetch data: Invalid API token\n" + DEFAULT_PARAMS = {'includePublicClusters': 'true'} @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_get_request_and_print_list_of_machine_types(self, get_patched): @@ -139,7 +140,7 @@ def test_should_send_get_request_and_print_list_of_machine_types(self, get_patch get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert EXPECTED_HEADERS["X-API-Key"] != "some_key" @@ -154,7 +155,7 @@ def test_should_send_get_request_and_print_list_of_vm_machine_types_filtered_by_ get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_get_request_and_print_list_of_machine_types_but_none_found(self, get_patched): @@ -166,7 +167,7 @@ def test_should_send_get_request_and_print_list_of_machine_types_but_none_found( get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == "No data found\n" @@ -181,7 +182,7 @@ def test_should_print_proper_message_when_wrong_api_key_was_used(self, get_patch get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == self.EXPECTED_STDOUT_WHEN_WRONG_API_KEY_WAS_USED assert EXPECTED_HEADERS["X-API-Key"] != "some_key" @@ -198,4 +199,4 @@ def test_should_read_options_defined_in_a_config_file(self, get_patched, vm_mach get_patched.assert_called_once_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, - params=None) + params=self.DEFAULT_PARAMS) diff --git a/tests/functional/test_machines.py b/tests/functional/test_machines.py index 7d910ce0..4228d98f 100644 --- a/tests/functional/test_machines.py +++ b/tests/functional/test_machines.py @@ -28,11 +28,13 @@ class TestMachineAvailability(object): "--machineType", "P4000", "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "availability", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "availability", "--optionsFile", ] # path added in test EXPECTED_STDOUT = "Machine available: True\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" @mock.patch("gradient.api_sdk.clients.http_client.requests.get") @@ -66,7 +68,8 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, get_patc @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_availability_config_path): get_patched.return_value = MockResponse(self.RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_availability_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_availability_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -81,7 +84,8 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_availabi @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_print_valid_error_message_when_availability_command_was_used_with_invalid_api_token(self, get_patched): - get_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) + get_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -208,12 +212,14 @@ class TestCreateMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "create", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "create", "--optionsFile", ] # path added in test EXPECTED_STDOUT = "New machine created with id: psclbvqpc\n" \ "https://console.paperspace.com/machines/psclbvqpc\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to create resource: Invalid API token\n" UPDATE_TAGS_RESPONSE_JSON_200 = example_responses.UPDATE_TAGS_RESPONSE @@ -239,7 +245,8 @@ class TestCreateMachine(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machine_create_was_used_with_requested_options(self, get_patched): - get_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) + get_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -255,7 +262,8 @@ def test_should_send_valid_post_request_when_machine_create_was_used_with_reques @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machine_create_was_used_with_all_options(self, post_patched): - post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) + post_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.ALL_COMMANDS) @@ -271,7 +279,8 @@ def test_should_send_valid_post_request_when_machine_create_was_used_with_all_op @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_changed_headers_when_api_key_option_was_used(self, post_patched): - post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) + post_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -287,8 +296,10 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_create_config_path): - post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_create_config_path] + post_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_create_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -304,7 +315,8 @@ def test_should_read_options_from_yaml_file(self, post_patched, machines_create_ @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_api_key_was_used(self, post_patched): - post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) + post_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -320,7 +332,8 @@ def test_should_print_error_message_when_wrong_api_key_was_used(self, post_patch @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_template_id_was_used(self, post_patched): - post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_TEMPLATE_ID, 400) + post_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_TEMPLATE_ID, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -353,7 +366,8 @@ def test_should_print_error_message_when_no_content_was_received_in_response(sel @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_mutually_exclusive_options_were_used(self, get_patched): cli_runner = CliRunner() - result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS_USED) + result = cli_runner.invoke( + cli.cli, self.BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS_USED) get_patched.assert_not_called() assert "Error: --userId is mutually exclusive with --email, --password, --firstName and --lastName\n" \ @@ -364,9 +378,11 @@ def test_should_print_error_message_when_mutually_exclusive_options_were_used(se @mock.patch("gradient.api_sdk.clients.http_client.requests.get") @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_proper_data_and_tag_machine(self, post_patched, get_patched, put_patched): - post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) + post_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE, 200) get_patched.return_value = MockResponse({}, 200) - put_patched.return_value = MockResponse(self.UPDATE_TAGS_RESPONSE_JSON_200, 200) + put_patched.return_value = MockResponse( + self.UPDATE_TAGS_RESPONSE_JSON_200, 200) runner = CliRunner() result = runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_TAGS) @@ -410,11 +426,13 @@ class TestDestroyMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "destroy", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "destroy", "--optionsFile", ] # path added in test EXPECTED_STDOUT = "Machine successfully destroyed\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to delete resource: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -462,7 +480,8 @@ def test_should_send_valid_post_request_when_machines_destroy_was_used_with_all_ @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_destroy_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_destroy_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_destroy_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -494,7 +513,8 @@ def test_should_send_valid_post_request_when_machines_destroy_was_used_with_api_ @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machines_destroy_was_used_with_wrong_api_key(self, post_patched): - post_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse( + json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -621,11 +641,13 @@ class TestListMachines(object): }, } - COMMAND_WITH_OPTIONS_FILE = ["machines", "list", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "list", + "--optionsFile", ] # path added in test BASIC_COMMAND_WITH_API_KEY = ["machines", "list", "--apiKey", "some_key"] - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" EXPECTED_STDOUT_WHEN_NO_MACHINES_WERE_FOUND = "No data found\n" @@ -636,9 +658,12 @@ class TestListMachines(object): "--name", "some_name", ] + DEFAULT_PARAMS = {'includePublicClusters': 'true'} + @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_list_was_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -647,12 +672,13 @@ def test_should_send_valid_post_request_and_print_table_when_machines_list_was_u get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_all_options_were_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_ALL_OPTIONS) @@ -661,12 +687,13 @@ def test_should_send_valid_post_request_when_all_options_were_used(self, get_pat get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.ALL_OPTIONS_REQUEST_JSON, - params=None) + params=self.DEFAULT_PARAMS) assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_all_options_were_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_ALL_OPTIONS) @@ -675,12 +702,13 @@ def test_should_send_valid_post_request_when_all_options_were_used(self, get_pat get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.ALL_OPTIONS_REQUEST_JSON, - params=None) + params=self.DEFAULT_PARAMS) assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_params_option_was_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_PARAMS_OPTION) @@ -688,13 +716,14 @@ def test_should_send_valid_post_request_when_params_option_was_used(self, get_pa get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=self.REQUEST_JSON_WITH_PARAMS_OPTION, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_list_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -702,14 +731,16 @@ def test_should_send_valid_post_request_when_machines_list_was_used_with_api_key get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_list_config_path): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_list_config_path] + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_list_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -717,13 +748,14 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_list_con get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.ALL_OPTIONS_REQUEST_JSON, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_list_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse( + json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -731,7 +763,7 @@ def test_should_send_valid_post_request_when_machines_list_was_used_with_wrong_a get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == self.EXPECTED_STDOUT_WITH_WRONG_API_TOKEN assert result.exit_code == 0 @@ -745,7 +777,7 @@ def test_should_print_error_message_when_no_machine_was_not_found(self, get_patc get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == self.EXPECTED_STDOUT_WHEN_NO_MACHINES_WERE_FOUND assert result.exit_code == 0 @@ -759,14 +791,15 @@ def test_should_print_error_message_when_error_status_code_received_but_no_conte get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=None) + params=self.DEFAULT_PARAMS) assert result.output == "Failed to fetch data\n" assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_print_error_message_when_params_option_was_used_with_mutually_exclusive_option(self, get_patched): cli_runner = CliRunner() - result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS) + result = cli_runner.invoke( + cli.cli, self.COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS) get_patched.assert_not_called() assert "You can use either --params dictionary or single filter arguments" in result.output @@ -787,9 +820,11 @@ class TestRestartMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "restart", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "restart", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Unable to restart instance: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -837,7 +872,8 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_config_file(self, post_patched, machines_restart_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_restart_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_restart_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -853,7 +889,8 @@ def test_should_read_options_from_config_file(self, post_patched, machines_resta @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_valid_error_message_when_start_command_was_used_with_invalid_api_token(self, post_patched): - post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -944,9 +981,11 @@ class TestShowMachine(object): "--id", "some_id", "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "details", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "details", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" RESPONSE_JSON_WHEN_MACHINE_WAS_NOT_FOUND = { @@ -960,7 +999,8 @@ class TestShowMachine(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_list_was_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -974,7 +1014,8 @@ def test_should_send_valid_post_request_and_print_table_when_machines_list_was_u @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_show_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -988,8 +1029,10 @@ def test_should_send_valid_post_request_when_machines_show_was_used_with_api_key @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_show_config_path): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_show_config_path] + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_show_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1003,7 +1046,8 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_show_con @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_list_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse( + json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1053,7 +1097,8 @@ class TestStartMachine(object): ] EXPECTED_STDOUT = "Machine started\n" - COMMAND_WITH_OPTIONS_FILE = ["machines", "start", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "start", "--optionsFile", ] # path added in test COMMAND_WITH_API_KEY = [ "machines", "start", @@ -1061,7 +1106,8 @@ class TestStartMachine(object): "--apiKey", "some_key", ] - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Unable to start instance: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -1109,7 +1155,8 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_start_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_start_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_start_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1125,7 +1172,8 @@ def test_should_read_options_from_yaml_file(self, post_patched, machines_start_c @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_valid_error_message_when_start_command_was_used_with_invalid_api_token(self, post_patched): - post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -1181,7 +1229,8 @@ class TestStopMachine(object): ] EXPECTED_STDOUT = "Machine stopped\n" - COMMAND_WITH_OPTIONS_FILE = ["machines", "stop", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "stop", + "--optionsFile", ] # path added in test COMMAND_WITH_API_KEY = [ "machines", "stop", @@ -1189,7 +1238,8 @@ class TestStopMachine(object): "--apiKey", "some_key", ] - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Unable to stop instance: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -1237,7 +1287,8 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_stop_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_stop_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_stop_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1253,7 +1304,8 @@ def test_should_read_options_from_yaml_file(self, post_patched, machines_stop_co @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_valid_error_message_when_stop_command_was_used_with_invalid_api_token(self, post_patched): - post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -1330,7 +1382,8 @@ class TestUpdateMachine(object): "autoSnapshotSaveCount": 1, "dynamicPublicIp": True, } - COMMAND_WITH_OPTIONS_FILE = ["machines", "update", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "update", "--optionsFile", ] # path added in test BASIC_COMMAND_WITH_API_KEY = [ "machines", "update", @@ -1340,10 +1393,12 @@ class TestUpdateMachine(object): ] EXPECTED_STDOUT = "Machine updated\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to update resource: Invalid API token\n" - RESPONSE_JSON_WITH_WRONG_MACHINE_ID = {"error": {"name": "Error", "status": 404, "message": "Not found"}} + RESPONSE_JSON_WITH_WRONG_MACHINE_ID = { + "error": {"name": "Error", "status": 404, "message": "Not found"}} @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machine_create_was_used_with_requested_options(self, get_patched): @@ -1379,7 +1434,8 @@ def test_should_send_valid_post_request_when_machine_create_was_used_with_all_op @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_changed_headers_when_api_key_option_was_used(self, get_patched): - get_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) + get_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1395,8 +1451,10 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, get_patc @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, get_patched, machines_update_config_path): - get_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_update_config_path] + get_patched.return_value = MockResponse( + example_responses.CREATE_MACHINE_RESPONSE, 200) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_update_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1412,7 +1470,8 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_update_c @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_api_key_was_used(self, get_patched): - get_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) + get_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1428,7 +1487,8 @@ def test_should_print_error_message_when_wrong_api_key_was_used(self, get_patche @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_machine_id_was_used(self, get_patched): - get_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_MACHINE_ID, 400) + get_patched.return_value = MockResponse( + self.RESPONSE_JSON_WITH_WRONG_MACHINE_ID, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1485,9 +1545,11 @@ class TestShowMachineUtilization(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "utilization", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "utilization", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" RESPONSE_JSON_WHEN_MACHINE_WAS_NOT_FOUND = { @@ -1501,7 +1563,8 @@ class TestShowMachineUtilization(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_utilizaation_was_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1515,7 +1578,8 @@ def test_should_send_valid_post_request_and_print_table_when_machines_utilizaati @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_utilization_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1529,8 +1593,10 @@ def test_should_send_valid_post_request_when_machines_utilization_was_used_with_ @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_utilization_config_path): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_utilization_config_path] + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_utilization_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1544,7 +1610,8 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_utilizat @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_utilization_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse( + json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1604,9 +1671,11 @@ class TestWaitForMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = ["machines", "waitfor", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = [ + "machines", "waitfor", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = { + "status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" RESPONSE_JSON_WHEN_MACHINE_WAS_NOT_FOUND = { @@ -1620,7 +1689,8 @@ class TestWaitForMachine(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_waitfor_was_used(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1634,7 +1704,8 @@ def test_should_send_valid_post_request_and_print_table_when_machines_waitfor_wa @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse( + json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1648,7 +1719,8 @@ def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_api_ @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse( + json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1662,8 +1734,10 @@ def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_wron @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_waitfor_config_path): - get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_waitfor_config_path] + get_patched.return_value = MockResponse( + json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ + [machines_waitfor_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) From cec0ecd6b67fb3240ac001d92711c914aa3a19e9 Mon Sep 17 00:00:00 2001 From: Ben Batha Date: Wed, 13 Apr 2022 12:14:44 -0400 Subject: [PATCH 3/3] fixup! fixup! fix(clusters): machineTypes list returns no data PLA-867 --- gradient/api_sdk/repositories/common.py | 48 ++-- .../api_sdk/repositories/machine_types.py | 3 + tests/functional/test_machines.py | 236 ++++++------------ 3 files changed, 98 insertions(+), 189 deletions(-) diff --git a/gradient/api_sdk/repositories/common.py b/gradient/api_sdk/repositories/common.py index 1e8e14eb..064bd011 100644 --- a/gradient/api_sdk/repositories/common.py +++ b/gradient/api_sdk/repositories/common.py @@ -58,8 +58,7 @@ def _get(self, **kwargs): url = self.get_request_url(**kwargs) client = self._get_client(**kwargs) response = self._send_request(client, url, json=json_, params=params) - gradient_response = http_client.GradientResponse.interpret_response( - response) + gradient_response = http_client.GradientResponse.interpret_response(response) return gradient_response @@ -119,9 +118,6 @@ def list(self, **kwargs): return instances, meta_data return instances - def _get_request_params(self, kwargs): - return {'includePublicClusters': 'true'} - def _get_instances(self, response, **kwargs): if not response.data: return [] @@ -168,8 +164,7 @@ class CreateResource(BaseRepository): def create(self, instance, data=None, path=None): instance_dict = self._get_instance_dict(instance) - response = self._send_create_request( - instance_dict, data=data, path=path) + response = self._send_create_request(instance_dict, data=data, path=path) self._validate_response(response) handle = self._process_response(response) return handle @@ -194,10 +189,8 @@ def _send_create_request(self, instance_dict, data=None, path=None): json_ = self._get_request_json(instance_dict) params = self._get_request_params(instance_dict) files = self._get_request_files(path) - response = client.post( - url, params=params, json=json_, data=data, files=files) - gradient_response = http_client.GradientResponse.interpret_response( - response) + response = client.post(url, params=params, json=json_, data=data, files=files) + gradient_response = http_client.GradientResponse.interpret_response(response) return gradient_response def _process_response(self, response): @@ -248,8 +241,7 @@ def _send(self, url, **kwargs): client = self._get_client(**kwargs) json_data = self._get_request_json(kwargs) response = self._send_request(client, url, json_data=json_data) - gradient_response = http_client.GradientResponse.interpret_response( - response) + gradient_response = http_client.GradientResponse.interpret_response(response) return gradient_response def _send_request(self, client, url, json_data=None): @@ -308,8 +300,7 @@ def _get_metrics_api_url(self, instance, protocol="https"): if not instance.metrics_url: raise GradientSdkError("Metrics API url not found") - metrics_api_url = concatenate_urls( - protocol + "://", instance.metrics_url) + metrics_api_url = concatenate_urls(protocol + "://", instance.metrics_url) return metrics_api_url def _get(self, **kwargs): @@ -355,8 +346,7 @@ def _get_built_in_metrics_list(self, kwargs): return metrics def _get_start_date(self, instance, kwargs): - datetime_string = kwargs.get( - "start") or instance.dt_started or instance.dt_created + datetime_string = kwargs.get("start") or instance.dt_started or instance.dt_created if not datetime_string: return None @@ -387,7 +377,6 @@ def _format_datetime(self, some_datetime): datetime_str = some_datetime.strftime("%Y-%m-%dT%H:%M:%SZ") return datetime_str - class ListMetrics(GetResource): OBJECT_TYPE = None @@ -401,8 +390,7 @@ def _get_metrics_api_url(self, instance, protocol="https"): if not instance.metrics_url: raise GradientSdkError("Metrics API url not found") - metrics_api_url = concatenate_urls( - protocol + "://", instance.metrics_url) + metrics_api_url = concatenate_urls(protocol + "://", instance.metrics_url) return metrics_api_url def _get(self, **kwargs): @@ -437,8 +425,7 @@ def _get_api_url(self, **kwargs): return api_url def _get_start_date(self, instance, kwargs): - datetime_string = kwargs.get( - "start") or instance.dt_started or instance.dt_created + datetime_string = kwargs.get("start") or instance.dt_started or instance.dt_created if not datetime_string: return None @@ -469,7 +456,6 @@ def _format_datetime(self, some_datetime): datetime_str = some_datetime.strftime("%Y-%m-%dT%H:%M:%SZ") return datetime_str - @six.add_metaclass(abc.ABCMeta) class StreamMetrics(BaseRepository): OBJECT_TYPE = None @@ -481,8 +467,7 @@ def _get_metrics_api_url(self, instance, protocol="https"): if not instance.metrics_url: raise GradientSdkError("Metrics API url not found") - metrics_api_url = concatenate_urls( - protocol + "://", instance.metrics_url) + metrics_api_url = concatenate_urls(protocol + "://", instance.metrics_url) return metrics_api_url def _get_api_url(self, **kwargs): @@ -499,23 +484,20 @@ def stream(self, **kwargs): self.logger.debug("Metrics data: {}".format(data)) yield data except websocket.WebSocketConnectionClosedException as e: - self.logger.debug( - "WebSocketConnectionClosedException: {}".format(e)) + self.logger.debug("WebSocketConnectionClosedException: {}".format(e)) except sdk_exceptions.EndWebsocketStream: return def _get_connection(self, kwargs): url = self._get_full_url(kwargs) - self.logger.debug( - "(Re)opening websocket connection to: {}".format(url)) + self.logger.debug("(Re)opening websocket connection to: {}".format(url)) ws = websocket.create_connection(url) self.logger.debug("Connected") return ws def _get_full_url(self, kwargs): instance_id = kwargs["id"] - metrics_api_url = self._get_metrics_api_url( - instance_id, protocol="wss") + metrics_api_url = self._get_metrics_api_url(instance_id, protocol="wss") url = concatenate_urls(metrics_api_url, self.get_request_url()) return url @@ -547,13 +529,11 @@ def _send_chart_descriptor(self, connection, kwargs): descriptor = self._get_chart_descriptor(kwargs) self.logger.debug("Sending chart descriptor: {}".format(descriptor)) response = connection.send(descriptor) - self.logger.debug( - "Chart descriptor sent. Response: {}".format(response)) + self.logger.debug("Chart descriptor sent. Response: {}".format(response)) def _get_stream_generator(self, connection): return connection - class ListLogs(ListResources): @abc.abstractmethod def _get_request_params(self, kwargs): diff --git a/gradient/api_sdk/repositories/machine_types.py b/gradient/api_sdk/repositories/machine_types.py index a17ad72b..d396d125 100644 --- a/gradient/api_sdk/repositories/machine_types.py +++ b/gradient/api_sdk/repositories/machine_types.py @@ -13,6 +13,9 @@ def get_request_url(self, **kwargs): def _get_api_url(self, **kwargs): return config.config.CONFIG_HOST + def _get_request_params(self, kwargs): + return {'includePublicClusters': 'true'} + def _get_instance_dicts(self, data, cluster_id=None, **kwargs): vm_types_dicts = {} # vmType["label"]: vmType dict for cluster_list_of_vms in data.values(): diff --git a/tests/functional/test_machines.py b/tests/functional/test_machines.py index 4228d98f..7d910ce0 100644 --- a/tests/functional/test_machines.py +++ b/tests/functional/test_machines.py @@ -28,13 +28,11 @@ class TestMachineAvailability(object): "--machineType", "P4000", "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "availability", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "availability", "--optionsFile", ] # path added in test EXPECTED_STDOUT = "Machine available: True\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" @mock.patch("gradient.api_sdk.clients.http_client.requests.get") @@ -68,8 +66,7 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, get_patc @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_availability_config_path): get_patched.return_value = MockResponse(self.RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_availability_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_availability_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -84,8 +81,7 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_availabi @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_print_valid_error_message_when_availability_command_was_used_with_invalid_api_token(self, get_patched): - get_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) + get_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -212,14 +208,12 @@ class TestCreateMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "create", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "create", "--optionsFile", ] # path added in test EXPECTED_STDOUT = "New machine created with id: psclbvqpc\n" \ "https://console.paperspace.com/machines/psclbvqpc\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to create resource: Invalid API token\n" UPDATE_TAGS_RESPONSE_JSON_200 = example_responses.UPDATE_TAGS_RESPONSE @@ -245,8 +239,7 @@ class TestCreateMachine(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machine_create_was_used_with_requested_options(self, get_patched): - get_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE, 200) + get_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -262,8 +255,7 @@ def test_should_send_valid_post_request_when_machine_create_was_used_with_reques @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machine_create_was_used_with_all_options(self, post_patched): - post_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE, 200) + post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.ALL_COMMANDS) @@ -279,8 +271,7 @@ def test_should_send_valid_post_request_when_machine_create_was_used_with_all_op @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_changed_headers_when_api_key_option_was_used(self, post_patched): - post_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE, 200) + post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -296,10 +287,8 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_create_config_path): - post_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_create_config_path] + post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_create_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -315,8 +304,7 @@ def test_should_read_options_from_yaml_file(self, post_patched, machines_create_ @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_api_key_was_used(self, post_patched): - post_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) + post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -332,8 +320,7 @@ def test_should_print_error_message_when_wrong_api_key_was_used(self, post_patch @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_template_id_was_used(self, post_patched): - post_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_TEMPLATE_ID, 400) + post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_TEMPLATE_ID, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -366,8 +353,7 @@ def test_should_print_error_message_when_no_content_was_received_in_response(sel @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_mutually_exclusive_options_were_used(self, get_patched): cli_runner = CliRunner() - result = cli_runner.invoke( - cli.cli, self.BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS_USED) + result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS_USED) get_patched.assert_not_called() assert "Error: --userId is mutually exclusive with --email, --password, --firstName and --lastName\n" \ @@ -378,11 +364,9 @@ def test_should_print_error_message_when_mutually_exclusive_options_were_used(se @mock.patch("gradient.api_sdk.clients.http_client.requests.get") @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_proper_data_and_tag_machine(self, post_patched, get_patched, put_patched): - post_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE, 200) + post_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) get_patched.return_value = MockResponse({}, 200) - put_patched.return_value = MockResponse( - self.UPDATE_TAGS_RESPONSE_JSON_200, 200) + put_patched.return_value = MockResponse(self.UPDATE_TAGS_RESPONSE_JSON_200, 200) runner = CliRunner() result = runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_TAGS) @@ -426,13 +410,11 @@ class TestDestroyMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "destroy", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "destroy", "--optionsFile", ] # path added in test EXPECTED_STDOUT = "Machine successfully destroyed\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to delete resource: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -480,8 +462,7 @@ def test_should_send_valid_post_request_when_machines_destroy_was_used_with_all_ @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_destroy_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_destroy_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_destroy_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -513,8 +494,7 @@ def test_should_send_valid_post_request_when_machines_destroy_was_used_with_api_ @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machines_destroy_was_used_with_wrong_api_key(self, post_patched): - post_patched.return_value = MockResponse( - json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -641,13 +621,11 @@ class TestListMachines(object): }, } - COMMAND_WITH_OPTIONS_FILE = ["machines", "list", - "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "list", "--optionsFile", ] # path added in test BASIC_COMMAND_WITH_API_KEY = ["machines", "list", "--apiKey", "some_key"] - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" EXPECTED_STDOUT_WHEN_NO_MACHINES_WERE_FOUND = "No data found\n" @@ -658,12 +636,9 @@ class TestListMachines(object): "--name", "some_name", ] - DEFAULT_PARAMS = {'includePublicClusters': 'true'} - @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_list_was_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -672,13 +647,12 @@ def test_should_send_valid_post_request_and_print_table_when_machines_list_was_u get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=self.DEFAULT_PARAMS) + params=None) assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_all_options_were_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_ALL_OPTIONS) @@ -687,13 +661,12 @@ def test_should_send_valid_post_request_when_all_options_were_used(self, get_pat get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.ALL_OPTIONS_REQUEST_JSON, - params=self.DEFAULT_PARAMS) + params=None) assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_all_options_were_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_ALL_OPTIONS) @@ -702,13 +675,12 @@ def test_should_send_valid_post_request_when_all_options_were_used(self, get_pat get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.ALL_OPTIONS_REQUEST_JSON, - params=self.DEFAULT_PARAMS) + params=None) assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_params_option_was_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_PARAMS_OPTION) @@ -716,14 +688,13 @@ def test_should_send_valid_post_request_when_params_option_was_used(self, get_pa get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=self.REQUEST_JSON_WITH_PARAMS_OPTION, - params=self.DEFAULT_PARAMS) + params=None) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_list_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -731,16 +702,14 @@ def test_should_send_valid_post_request_when_machines_list_was_used_with_api_key get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, - params=self.DEFAULT_PARAMS) + params=None) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_list_config_path): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_list_config_path] + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_list_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -748,14 +717,13 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_list_con get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=self.ALL_OPTIONS_REQUEST_JSON, - params=self.DEFAULT_PARAMS) + params=None) assert result.output == self.EXPECTED_STDOUT assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_list_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -763,7 +731,7 @@ def test_should_send_valid_post_request_when_machines_list_was_used_with_wrong_a get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS_WITH_CHANGED_API_KEY, json=None, - params=self.DEFAULT_PARAMS) + params=None) assert result.output == self.EXPECTED_STDOUT_WITH_WRONG_API_TOKEN assert result.exit_code == 0 @@ -777,7 +745,7 @@ def test_should_print_error_message_when_no_machine_was_not_found(self, get_patc get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=self.DEFAULT_PARAMS) + params=None) assert result.output == self.EXPECTED_STDOUT_WHEN_NO_MACHINES_WERE_FOUND assert result.exit_code == 0 @@ -791,15 +759,14 @@ def test_should_print_error_message_when_error_status_code_received_but_no_conte get_patched.assert_called_with(self.URL, headers=EXPECTED_HEADERS, json=None, - params=self.DEFAULT_PARAMS) + params=None) assert result.output == "Failed to fetch data\n" assert result.exit_code == 0 @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_print_error_message_when_params_option_was_used_with_mutually_exclusive_option(self, get_patched): cli_runner = CliRunner() - result = cli_runner.invoke( - cli.cli, self.COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS) + result = cli_runner.invoke(cli.cli, self.COMMAND_WITH_MUTUALLY_EXCLUSIVE_OPTIONS) get_patched.assert_not_called() assert "You can use either --params dictionary or single filter arguments" in result.output @@ -820,11 +787,9 @@ class TestRestartMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "restart", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "restart", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Unable to restart instance: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -872,8 +837,7 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_config_file(self, post_patched, machines_restart_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_restart_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_restart_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -889,8 +853,7 @@ def test_should_read_options_from_config_file(self, post_patched, machines_resta @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_valid_error_message_when_start_command_was_used_with_invalid_api_token(self, post_patched): - post_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -981,11 +944,9 @@ class TestShowMachine(object): "--id", "some_id", "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "details", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "details", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" RESPONSE_JSON_WHEN_MACHINE_WAS_NOT_FOUND = { @@ -999,8 +960,7 @@ class TestShowMachine(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_list_was_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1014,8 +974,7 @@ def test_should_send_valid_post_request_and_print_table_when_machines_list_was_u @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_show_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1029,10 +988,8 @@ def test_should_send_valid_post_request_when_machines_show_was_used_with_api_key @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_show_config_path): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_show_config_path] + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_show_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1046,8 +1003,7 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_show_con @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_list_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1097,8 +1053,7 @@ class TestStartMachine(object): ] EXPECTED_STDOUT = "Machine started\n" - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "start", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "start", "--optionsFile", ] # path added in test COMMAND_WITH_API_KEY = [ "machines", "start", @@ -1106,8 +1061,7 @@ class TestStartMachine(object): "--apiKey", "some_key", ] - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Unable to start instance: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -1155,8 +1109,7 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_start_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_start_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_start_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1172,8 +1125,7 @@ def test_should_read_options_from_yaml_file(self, post_patched, machines_start_c @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_valid_error_message_when_start_command_was_used_with_invalid_api_token(self, post_patched): - post_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -1229,8 +1181,7 @@ class TestStopMachine(object): ] EXPECTED_STDOUT = "Machine stopped\n" - COMMAND_WITH_OPTIONS_FILE = ["machines", "stop", - "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "stop", "--optionsFile", ] # path added in test COMMAND_WITH_API_KEY = [ "machines", "stop", @@ -1238,8 +1189,7 @@ class TestStopMachine(object): "--apiKey", "some_key", ] - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Unable to stop instance: Invalid API token\n" RESPONSE_JSON_WITH_404_MACHINE_NOT_FOUND = { @@ -1287,8 +1237,7 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, post_pat @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, post_patched, machines_stop_config_path): post_patched.return_value = MockResponse() - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_stop_config_path] + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_stop_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1304,8 +1253,7 @@ def test_should_read_options_from_yaml_file(self, post_patched, machines_stop_co @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_valid_error_message_when_stop_command_was_used_with_invalid_api_token(self, post_patched): - post_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + post_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.COMMAND) @@ -1382,8 +1330,7 @@ class TestUpdateMachine(object): "autoSnapshotSaveCount": 1, "dynamicPublicIp": True, } - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "update", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "update", "--optionsFile", ] # path added in test BASIC_COMMAND_WITH_API_KEY = [ "machines", "update", @@ -1393,12 +1340,10 @@ class TestUpdateMachine(object): ] EXPECTED_STDOUT = "Machine updated\n" - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to update resource: Invalid API token\n" - RESPONSE_JSON_WITH_WRONG_MACHINE_ID = { - "error": {"name": "Error", "status": 404, "message": "Not found"}} + RESPONSE_JSON_WITH_WRONG_MACHINE_ID = {"error": {"name": "Error", "status": 404, "message": "Not found"}} @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_valid_post_request_when_machine_create_was_used_with_requested_options(self, get_patched): @@ -1434,8 +1379,7 @@ def test_should_send_valid_post_request_when_machine_create_was_used_with_all_op @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_send_changed_headers_when_api_key_option_was_used(self, get_patched): - get_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE, 200) + get_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1451,10 +1395,8 @@ def test_should_send_changed_headers_when_api_key_option_was_used(self, get_patc @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_read_options_from_yaml_file(self, get_patched, machines_update_config_path): - get_patched.return_value = MockResponse( - example_responses.CREATE_MACHINE_RESPONSE, 200) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_update_config_path] + get_patched.return_value = MockResponse(example_responses.CREATE_MACHINE_RESPONSE, 200) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_update_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1470,8 +1412,7 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_update_c @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_api_key_was_used(self, get_patched): - get_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) + get_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1487,8 +1428,7 @@ def test_should_print_error_message_when_wrong_api_key_was_used(self, get_patche @mock.patch("gradient.api_sdk.clients.http_client.requests.post") def test_should_print_error_message_when_wrong_machine_id_was_used(self, get_patched): - get_patched.return_value = MockResponse( - self.RESPONSE_JSON_WITH_WRONG_MACHINE_ID, 400) + get_patched.return_value = MockResponse(self.RESPONSE_JSON_WITH_WRONG_MACHINE_ID, 400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1545,11 +1485,9 @@ class TestShowMachineUtilization(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "utilization", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "utilization", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" RESPONSE_JSON_WHEN_MACHINE_WAS_NOT_FOUND = { @@ -1563,8 +1501,7 @@ class TestShowMachineUtilization(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_utilizaation_was_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1578,8 +1515,7 @@ def test_should_send_valid_post_request_and_print_table_when_machines_utilizaati @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_utilization_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1593,10 +1529,8 @@ def test_should_send_valid_post_request_when_machines_utilization_was_used_with_ @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_utilization_config_path): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_utilization_config_path] + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_utilization_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command) @@ -1610,8 +1544,7 @@ def test_should_read_options_from_yaml_file(self, get_patched, machines_utilizat @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_utilization_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1671,11 +1604,9 @@ class TestWaitForMachine(object): "--apiKey", "some_key", ] - COMMAND_WITH_OPTIONS_FILE = [ - "machines", "waitfor", "--optionsFile", ] # path added in test + COMMAND_WITH_OPTIONS_FILE = ["machines", "waitfor", "--optionsFile", ] # path added in test - RESPONSE_JSON_WITH_WRONG_API_TOKEN = { - "status": 400, "message": "Invalid API token"} + RESPONSE_JSON_WITH_WRONG_API_TOKEN = {"status": 400, "message": "Invalid API token"} EXPECTED_STDOUT_WITH_WRONG_API_TOKEN = "Failed to fetch data: Invalid API token\n" RESPONSE_JSON_WHEN_MACHINE_WAS_NOT_FOUND = { @@ -1689,8 +1620,7 @@ class TestWaitForMachine(object): @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_and_print_table_when_machines_waitfor_was_used(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND) @@ -1704,8 +1634,7 @@ def test_should_send_valid_post_request_and_print_table_when_machines_waitfor_wa @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_api_key_option(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) + get_patched.return_value = MockResponse(json_data=self.EXPECTED_RESPONSE_JSON, status_code=200) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1719,8 +1648,7 @@ def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_api_ @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_wrong_api_key(self, get_patched): - get_patched.return_value = MockResponse( - json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, self.BASIC_COMMAND_WITH_API_KEY) @@ -1734,10 +1662,8 @@ def test_should_send_valid_post_request_when_machines_waitfor_was_used_with_wron @mock.patch("gradient.api_sdk.clients.http_client.requests.get") def test_should_read_options_from_yaml_file(self, get_patched, machines_waitfor_config_path): - get_patched.return_value = MockResponse( - json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) - command = self.COMMAND_WITH_OPTIONS_FILE[:] + \ - [machines_waitfor_config_path] + get_patched.return_value = MockResponse(json_data=self.RESPONSE_JSON_WITH_WRONG_API_TOKEN, status_code=400) + command = self.COMMAND_WITH_OPTIONS_FILE[:] + [machines_waitfor_config_path] cli_runner = CliRunner() result = cli_runner.invoke(cli.cli, command)