def test_pipeline_configuration(self, auth_provider, pipeline_configuration): pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) curr_stage = pipeline._pipeline expected_stage_order = [ pipeline_stages_base.PipelineRootStage, pipeline_stages_iothub.UseAuthProviderStage, pipeline_stages_iothub.TwinRequestResponseStage, pipeline_stages_base.CoordinateRequestAndResponseStage, pipeline_stages_iothub_mqtt.IoTHubMQTTTranslationStage, pipeline_stages_base.AutoConnectStage, pipeline_stages_base.ReconnectStage, pipeline_stages_base.ConnectionLockStage, pipeline_stages_base.RetryStage, pipeline_stages_base.OpTimeoutStage, pipeline_stages_mqtt.MQTTTransportStage, ] # Assert that all PipelineStages are there, and they are in the right order for i in range(len(expected_stage_order)): expected_stage = expected_stage_order[i] assert isinstance(curr_stage, expected_stage) curr_stage = curr_stage.next # Assert there are no more additional stages assert curr_stage is None
def test_cert_auth(self, mocker, x509): mocker.patch.object(pipeline_stages_base, "PipelineRootStage") auth_provider = X509AuthenticationProvider("somehostname", "somedevice", x509) pipeline = IoTHubPipeline(auth_provider) op = pipeline._pipeline.run_op.call_args[0][0] assert pipeline._pipeline.run_op.call_count == 1 assert isinstance(op, pipeline_ops_iothub.SetX509AuthProviderOperation) assert op.auth_provider is auth_provider
def test_sas_auth(self, mocker, device_connection_string, pipeline_configuration): mocker.spy(pipeline_stages_base.PipelineRootStage, "run_op") auth_provider = SymmetricKeyAuthenticationProvider.parse(device_connection_string) pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) op = pipeline._pipeline.run_op.call_args[0][1] assert pipeline._pipeline.run_op.call_count == 1 assert isinstance(op, pipeline_ops_iothub.SetAuthProviderOperation) assert op.auth_provider is auth_provider
def test_handlers_set_to_none(self, auth_provider, pipeline_configuration): pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) assert pipeline.on_connected is None assert pipeline.on_disconnected is None assert pipeline.on_c2d_message_received is None assert pipeline.on_input_message_received is None assert pipeline.on_method_request_received is None assert pipeline.on_twin_patch_received is None
def test_cert_auth_op_fail(self, mocker, x509): mocker.patch.object(pipeline_stages_base, "PipelineRootStage") auth_provider = X509AuthenticationProvider("somehostname", "somedevice", x509) pipeline = IoTHubPipeline(auth_provider) op = pipeline._pipeline.run_op.call_args[0][0] op.error = Exception() with pytest.raises(Exception): op.callback(op)
def test_sas_auth_op_fail(self, mocker, device_connection_string): mocker.patch.object(pipeline_stages_base, "PipelineRootStage") auth_provider = SymmetricKeyAuthenticationProvider.parse(device_connection_string) pipeline = IoTHubPipeline(auth_provider) op = pipeline._pipeline.run_op.call_args[0][0] op.error = Exception() with pytest.raises(Exception): op.callback(op)
def test_cert_auth(self, mocker, x509, pipeline_configuration): mocker.spy(pipeline_stages_base.PipelineRootStage, "run_op") auth_provider = X509AuthenticationProvider(hostname="somehostname", device_id="somedevice", x509=x509) pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) op = pipeline._pipeline.run_op.call_args[0][1] assert pipeline._pipeline.run_op.call_count == 1 assert isinstance(op, pipeline_ops_iothub.SetX509AuthProviderOperation) assert op.auth_provider is auth_provider
def test_sas_auth_op_fail(self, mocker, device_connection_string, fake_exception): old_execute_op = pipeline_stages_base.PipelineRootStage._execute_op def fail_set_auth_provider(self, op): if isinstance(op, pipeline_stages_base.SetAuthProviderOperation): op.error = fake_exception operation_flow.complete_op(stage=self, op=op) else: old_execute_op(self, op) mocker.patch.object( pipeline_stages_base.PipelineRootStage, "_execute_op", side_effect=fail_set_auth_provider, ) auth_provider = SymmetricKeyAuthenticationProvider.parse(device_connection_string) with pytest.raises(fake_exception.__class__): IoTHubPipeline(auth_provider)
def test_cert_auth_op_fail(self, mocker, x509, arbitrary_exception, pipeline_configuration): old_run_op = pipeline_stages_base.PipelineRootStage._run_op def fail_set_auth_provider(self, op): if isinstance(op, pipeline_ops_iothub.SetX509AuthProviderOperation): op.complete(error=arbitrary_exception) else: old_run_op(self, op) mocker.patch.object( pipeline_stages_base.PipelineRootStage, "_run_op", side_effect=fail_set_auth_provider, autospec=True, ) auth_provider = X509AuthenticationProvider( hostname="somehostname", device_id="somedevice", x509=x509 ) with pytest.raises(arbitrary_exception.__class__): IoTHubPipeline(auth_provider, pipeline_configuration)
def test_sas_auth_op_fail(self, mocker, device_connection_string, arbitrary_exception, pipeline_configuration): old_execute_op = pipeline_stages_base.PipelineRootStage._execute_op def fail_set_auth_provider(self, op): if isinstance(op, pipeline_ops_iothub.SetAuthProviderOperation): self._complete_op(op, error=arbitrary_exception) else: old_execute_op(self, op) mocker.patch.object( pipeline_stages_base.PipelineRootStage, "_execute_op", side_effect=fail_set_auth_provider, autospec=True, ) auth_provider = SymmetricKeyAuthenticationProvider.parse( device_connection_string) with pytest.raises(arbitrary_exception.__class__): IoTHubPipeline(auth_provider, pipeline_configuration)
def test_cert_auth_op_fail(self, mocker, x509, fake_exception): old_execute_op = pipeline_stages_base.PipelineRootStage._execute_op def fail_set_auth_provider(self, op): if isinstance(op, pipeline_stages_base.SetX509AuthProviderOperation): op.error = fake_exception operation_flow.complete_op(stage=self, op=op) else: old_execute_op(self, op) mocker.patch.object( pipeline_stages_base.PipelineRootStage, "_execute_op", side_effect=fail_set_auth_provider, ) auth_provider = X509AuthenticationProvider( hostname="somehostname", device_id="somedevice", x509=x509 ) with pytest.raises(fake_exception.__class__): IoTHubPipeline(auth_provider)
def test_pipeline_configuration(self, auth_provider): pipeline = IoTHubPipeline(auth_provider) curr_stage = pipeline._pipeline expected_stage_order = [ pipeline_stages_base.PipelineRootStage, pipeline_stages_iothub.UseAuthProviderStage, pipeline_stages_iothub.HandleTwinOperationsStage, pipeline_stages_base.CoordinateRequestAndResponseStage, pipeline_stages_iothub_mqtt.IoTHubMQTTConverterStage, pipeline_stages_base.EnsureConnectionStage, pipeline_stages_base.SerializeConnectOpsStage, pipeline_stages_mqtt.MQTTTransportStage, ] # Assert that all PipelineStages are there, and they are in the right order for i in range(len(expected_stage_order)): expected_stage = expected_stage_order[i] assert isinstance(curr_stage, expected_stage) curr_stage = curr_stage.next # Assert there are no more additional stages assert curr_stage is None
def test_features_disabled(self, auth_provider, pipeline_configuration): pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) for key in pipeline.feature_enabled: assert not pipeline.feature_enabled[key]
def test_features(self, auth_provider, pipeline_configuration, feature): pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) pipeline.feature_enabled[feature]
def pipeline(mocker, auth_provider, pipeline_configuration): pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) mocker.patch.object(pipeline._pipeline, "run_op") return pipeline
def pipeline(mocker, auth_provider): pipeline = IoTHubPipeline(auth_provider) mocker.patch.object(pipeline._pipeline, "run_op") return pipeline
def test_handlers_configured(self, auth_provider, pipeline_configuration): pipeline = IoTHubPipeline(auth_provider, pipeline_configuration) assert pipeline._pipeline.on_pipeline_event_handler is not None assert pipeline._pipeline.on_connected_handler is not None assert pipeline._pipeline.on_disconnected_handler is not None
def test_features(self, auth_provider, feature): pipeline = IoTHubPipeline(auth_provider) pipeline.feature_enabled[feature]