From 6ae09a138209a5b453f03640c8b5fdfe4ef0febe Mon Sep 17 00:00:00 2001 From: Paymaun Heidari Date: Thu, 22 Nov 2018 09:52:46 -0800 Subject: [PATCH] Fix linter issues. --- azext_iot/_params.py | 3 ++- azext_iot/operations/events3/_events.py | 1 + azext_iot/operations/hub.py | 4 +++- azext_iot/tests/test_iot_ext_int.py | 16 ++++++++-------- azext_iot/tests/test_iot_ext_unit.py | 24 ++++++++++++++++-------- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/azext_iot/_params.py b/azext_iot/_params.py index 0f8198d65..af70edb78 100644 --- a/azext_iot/_params.py +++ b/azext_iot/_params.py @@ -40,6 +40,7 @@ 'sys = system properties, app = application properties, anno = annotations' ) + # pylint: disable=too-many-statements def load_arguments(self, _): """ @@ -165,7 +166,7 @@ def load_arguments(self, _): with self.argument_context('iot device') as context: context.argument('data', options_list=['--data', '--da'], help='Message body.') - context.argument('properties', options_list=['--properties', '--props'], + context.argument('properties', options_list=['--properties', '--props', '-p'], help='Message property bag in key-value pairs with the ' 'following format: a=b;c=d') context.argument('msg_count', options_list=['--msg-count', '--mc'], type=int, diff --git a/azext_iot/operations/events3/_events.py b/azext_iot/operations/events3/_events.py index 08a504a60..1e4488b13 100644 --- a/azext_iot/operations/events3/_events.py +++ b/azext_iot/operations/events3/_events.py @@ -114,6 +114,7 @@ def _get_conn_props(): await asyncio.gather(*coroutines, return_exceptions=True) +# pylint: disable=too-many-statements async def monitor_events(endpoint, connection, path, auth, partition, consumer_group, enqueuedtimeutc, properties, device_id=None, timeout=0, output=None, content_type=None): source = uamqp.address.Source('amqps://{}/{}/ConsumerGroups/{}/Partitions/{}'.format(endpoint, path, diff --git a/azext_iot/operations/hub.py b/azext_iot/operations/hub.py index e90f6e29a..0471306c5 100644 --- a/azext_iot/operations/hub.py +++ b/azext_iot/operations/hub.py @@ -940,7 +940,7 @@ def _iot_c2d_message_receive(target, device_id, lock_timeout=60): return { 'ack': result.headers['iothub-ack'], 'correlationId': result.headers['iothub-correlationid'], - 'data': result.response.content, + 'data': result.response.text, 'deliveryCount': result.headers['iothub-deliverycount'], 'enqueuedTime': result.headers['iothub-enqueuedtime'], 'expiry': result.headers['iothub-expiry'], @@ -1126,6 +1126,8 @@ def iot_hub_monitor_events(cmd, hub_name=None, device_id=None, consumer_group='$ config = cmd.cli_ctx.config output = cmd.cli_ctx.invocation.data.get("output", None) + if not output: + output = 'json' ensure_uamqp(config, yes, repair) events3 = importlib.import_module('azext_iot.operations.events3._events') diff --git a/azext_iot/tests/test_iot_ext_int.py b/azext_iot/tests/test_iot_ext_int.py index e8ed0f814..a03156121 100644 --- a/azext_iot/tests/test_iot_ext_int.py +++ b/azext_iot/tests/test_iot_ext_int.py @@ -551,8 +551,8 @@ def test_hub_modules(self): assert len(result) == 4 self.cmd('''iot hub module-identity update -d {} -n {} -g {} -m {} - --set authentication.symmetricKey.primaryKey="" authentication.symmetricKey.secondaryKey=""'''.format( - edge_device_ids[0], LIVE_HUB, LIVE_RG, module_ids[0]), + --set authentication.symmetricKey.primaryKey="" authentication.symmetricKey.secondaryKey=""''' + .format(edge_device_ids[0], LIVE_HUB, LIVE_RG, module_ids[0]), checks=[self.check('deviceId', edge_device_ids[0]), self.check('moduleId', module_ids[0]), self.exists('authentication.symmetricKey.primaryKey'), @@ -560,8 +560,8 @@ def test_hub_modules(self): # With connection string self.cmd('''iot hub module-identity update -d {} --login {} -m {} - --set authentication.symmetricKey.primaryKey="" authentication.symmetricKey.secondaryKey=""'''.format( - edge_device_ids[0], LIVE_HUB_CS, module_ids[0]), + --set authentication.symmetricKey.primaryKey="" authentication.symmetricKey.secondaryKey=""''' + .format(edge_device_ids[0], LIVE_HUB_CS, module_ids[0]), checks=[self.check('deviceId', edge_device_ids[0]), self.check('moduleId', module_ids[0]), self.exists('authentication.symmetricKey.primaryKey'), @@ -1201,14 +1201,14 @@ def test_hub_monitor_events(self): '$.mid=12345;key0=value0;key1=1', 1, LIVE_RG], max_runs=1) # Monitor events for all devices and include sys, anno, app - self.command_execute_assert('iot hub monitor-events -n {} -g {} --cg {} --et {} -t 10 -y -p sys anno app'.format( - LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[0], enqueued_time), + self.command_execute_assert('iot hub monitor-events -n {} -g {} --cg {} --et {} -t 10 -y -p sys anno app' + .format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[0], enqueued_time), device_ids + ['system', 'annotations', 'application', '"message_id": "12345"', '"key0": "value0"', '"key1": "1"']) # Monitor events for a single device - self.command_execute_assert('iot hub monitor-events -n {} -g {} -d {} --cg {} --et {} -t 10 -y -p sys anno app'.format( - LIVE_HUB, LIVE_RG, device_ids[0], LIVE_CONSUMER_GROUPS[1], enqueued_time), + self.command_execute_assert('iot hub monitor-events -n {} -g {} -d {} --cg {} --et {} -t 10 -y -p sys anno app' + .format(LIVE_HUB, LIVE_RG, device_ids[0], LIVE_CONSUMER_GROUPS[1], enqueued_time), [device_ids[0], 'system', 'annotations', 'application', '"message_id": "12345"', '"key0": "value0"', '"key1": "1"']) diff --git a/azext_iot/tests/test_iot_ext_unit.py b/azext_iot/tests/test_iot_ext_unit.py index 702e4f144..160e33bff 100644 --- a/azext_iot/tests/test_iot_ext_unit.py +++ b/azext_iot/tests/test_iot_ext_unit.py @@ -95,12 +95,17 @@ def mqttclient_generic_error(mocker, fixture_ghcs, fixture_sas): return mqtt_client -def build_mock_response(mocker, status_code=200, payload=None, headers=None): +def build_mock_response(mocker, status_code=200, payload=None, headers=None, raw=False): response = mocker.MagicMock(name='response') response.status_code = status_code del response.context del response._attribute_map - response.text.return_value = json.dumps(payload) + + if raw: + response.text = payload + else: + response.text.return_value = json.dumps(payload) + if headers: response.headers = headers return response @@ -1566,14 +1571,17 @@ def serviceclient(self, mocker, fixture_ghcs, fixture_sas, request): service_client.return_value = build_mock_response(mocker) return service_client - def test_c2d_receive(self, serviceclient): - data = "sample data" - serviceclient.return_value.status_code = 200 - serviceclient.return_value.headers = sample_c2d_receive - serviceclient.return_value.content = data + @pytest.fixture + def fixture_mocker(self, mocker, fixture_ghcs, fixture_sas, request): + return mocker + + def test_c2d_receive(self, fixture_mocker): + data = '{"data": "value"}' + service_client = fixture_mocker.patch(path_service_client) + service_client.return_value = build_mock_response(fixture_mocker, 200, data, sample_c2d_receive, raw=True) timeout = 120 result = subject.iot_c2d_message_receive(fixture_cmd, device_id, mock_target['entity'], timeout) - args = serviceclient.call_args + args = service_client.call_args url = args[0][0].url method = args[0][0].method headers = args[0][1]