def test_does_not_call_second_or_third_op_if_first_op_fails( self, mocker, stage, op, op2, op3, callback ): op.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback) assert stage.next._run_op.call_count == 1 assert stage.next._run_op.call_args == mocker.call(op)
def test_callback_raises_exception( self, stage, op, op2, op3, finally_op, fake_exception, mocker, unhandled_error_handler ): callback = mocker.Mock(side_effect=fake_exception) run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert unhandled_error_handler.call_count == 1 assert unhandled_error_handler.call_args == mocker.call(fake_exception)
def test_runs_finally_op_if_third_op_fails( self, mocker, stage, op, op2, op3, finally_op, callback ): op3.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert stage.next._run_op.call_count == 4 assert stage.next._run_op.call_args_list[3] == mocker.call(finally_op)
def test_calls_callback_with_error_if_third_op_and_finally_op_both_fail( self, stage, op, op2, op3, finally_op, callback ): op3.action = "fail" finally_op.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert_callback_failed(callback=callback, op=finally_op, error=op3.error)
def test_calls_callbacK_with_error_when_first_op_and_finally_op_both_fail( self, stage, op, op2, op3, finally_op, callback ): op.action = "fail" finally_op.action = "fail" run_ops_in_serial(stage, op, op2, op3, finally_op=finally_op, callback=callback) assert_callback_failed(callback=callback, op=finally_op, error=finally_op.error)
def test_runs_second_op(self, mocker, stage, op, op2, op3, finally_op, callback): op.action = "pend" op2.action = "pend" run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert stage.next._run_op.call_count == 1 assert stage.next._run_op.call_args == mocker.call(op) complete_op(stage.next, op) assert stage.next._run_op.call_args_list[1] == mocker.call(op2)
def test_calls_third_op_after_second_op_succeeds(self, mocker, stage, op, op2, op3, callback): op2.action = "pend" run_ops_in_serial(stage, op, op2, op3, callback=callback) assert stage.next._run_op.call_count == 2 assert stage.next._run_op.call_args_list[0] == mocker.call(op) assert stage.next._run_op.call_args_list[1] == mocker.call(op2) complete_op(stage.next, op2) assert stage.next._run_op.call_count == 3 assert stage.next._run_op.call_args_list[2] == mocker.call(op3)
def test_runs_second_op_after_first_op_succceeds(self, mocker, stage, op, op2, op3, callback): op.action = "pend" op2.action = "pend" run_ops_in_serial(stage, op, op2, op3, callback=callback) assert stage.next._run_op.call_count == 1 assert stage.next._run_op.call_args == mocker.call(op) complete_op(stage.next, op) assert stage.next._run_op.call_count == 2 assert stage.next._run_op.call_args_list[1] == mocker.call(op2)
def test_callback_throws_exception( self, stage, mocker, fake_exception, op, unhandled_error_handler ): callback = mocker.Mock(side_effect=fake_exception) run_ops_in_serial(stage, op, callback=callback) assert callback.call_count == 1 assert callback.call_args == mocker.call(op) assert unhandled_error_handler.call_count == 1 assert unhandled_error_handler.call_args == mocker.call(fake_exception)
def test_runs_finally_op_when_second_op_fails( self, mocker, stage, op, op2, op3, finally_op, callback ): op2.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert stage.next._run_op.call_count == 3 assert stage.next._run_op.call_args_list[0] == mocker.call(op) assert stage.next._run_op.call_args_list[1] == mocker.call(op2) assert stage.next._run_op.call_args_list[2] == mocker.call(finally_op)
def _run_op(self, op): def pipeline_ops_done(completed_op): op.error = completed_op.error op.callback(op) if isinstance(op, pipeline_ops_iothub.SetAuthProviderOperation): auth_provider = op.auth_provider operation_flow.run_ops_in_serial( self, pipeline_ops_iothub.SetAuthProviderArgsOperation( device_id=auth_provider.device_id, module_id=getattr(auth_provider, "module_id", None), hostname=auth_provider.hostname, gateway_hostname=getattr(auth_provider, "gateway_hostname", None), ca_cert=getattr(auth_provider, "ca_cert", None), ), pipeline_ops_base.SetSasTokenOperation( sas_token=auth_provider.get_current_sas_token() ), callback=pipeline_ops_done, ) elif isinstance(op, pipeline_ops_iothub.SetX509AuthProviderOperation): auth_provider = op.auth_provider operation_flow.run_ops_in_serial( self, pipeline_ops_iothub.SetAuthProviderArgsOperation( device_id=auth_provider.device_id, module_id=getattr(auth_provider, "module_id", None), hostname=auth_provider.hostname, gateway_hostname=getattr(auth_provider, "gateway_hostname", None), ca_cert=getattr(auth_provider, "ca_cert", None), ), pipeline_ops_base.SetClientAuthenticationCertificateOperation( certificate=auth_provider.get_x509_certificate() ), callback=pipeline_ops_done, ) else: operation_flow.pass_op_to_next_stage(self, op)
def _run_op(self, op): def pipeline_ops_done(completed_op): op.error = completed_op.error op.callback(op) if isinstance( op, pipeline_ops_provisioning. SetSymmetricKeySecurityClientOperation): security_client = op.security_client operation_flow.run_ops_in_serial( self, pipeline_ops_provisioning.SetSecurityClientArgsOperation( provisioning_host=security_client.provisioning_host, registration_id=security_client.registration_id, id_scope=security_client.id_scope, ), pipeline_ops_base.SetSasTokenOperation( sas_token=security_client.get_current_sas_token()), callback=pipeline_ops_done, ) elif isinstance( op, pipeline_ops_provisioning.SetX509SecurityClientOperation): security_client = op.security_client operation_flow.run_ops_in_serial( self, pipeline_ops_provisioning.SetSecurityClientArgsOperation( provisioning_host=security_client.provisioning_host, registration_id=security_client.registration_id, id_scope=security_client.id_scope, ), pipeline_ops_base.SetClientAuthenticationCertificateOperation( certificate=security_client.get_x509_certificate()), callback=pipeline_ops_done, ) else: operation_flow.pass_op_to_next_stage(self, op)
def test_callback_with_success_if_op_and_finally_op_succeed( self, stage, callback, op, finally_op ): run_ops_in_serial(stage, op, finally_op=finally_op, callback=callback) assert_callback_succeeded(callback=callback, op=finally_op)
def test_successful_operation(self, stage, callback, op): run_ops_in_serial(stage, op, callback=callback) assert_callback_succeeded(op=op, callback=callback)
def test_failed_operation(self, stage, callback, op): op.action = "fail" run_ops_in_serial(stage, op, callback=callback) assert_callback_failed(op=op, callback=callback)
def test_throws_on_missing_callback(self, stage, op, op2): with pytest.raises(TypeError): run_ops_in_serial(stage, op, op2)
def test_runs_operation(self, mocker, stage, callback, op): run_ops_in_serial(stage, op, callback=callback) assert stage.next._run_op.call_count == 1 assert stage.next._run_op.call_args == mocker.call(op)
def test_callback_raises_base_exception( self, stage, op, op2, op3, finally_op, fake_base_exception, mocker ): callback = mocker.Mock(side_effect=fake_base_exception) with pytest.raises(UnhandledException): run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op)
def test_throws_for_unknown_keyword_args(self, stage, op, op2, op3, callback): with pytest.raises(TypeError): run_ops_in_serial(stage, op, op2, callback=callback, unknown_arg=op3)
def test_does_not_run_third_op_if_second_op_fails(self, mocker, stage, op, op2, op3, callback): op2.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback) assert stage.next._run_op.call_count == 2 assert stage.next._run_op.call_args_list[0] == mocker.call(op) assert stage.next._run_op.call_args_list[1] == mocker.call(op2)
def test_accepts_default_args(self, stage, op, op2, callback): run_ops_in_serial(stage, op, op2, callback=callback)
def test_calls_callback_with_third_op_succeeds(self, stage, op, op2, op3, callback): run_ops_in_serial(stage, op, op2, op3, callback=callback) assert_callback_succeeded(callback=callback, op=op3)
def test_calls_callback_with_no_error_if_third_op_and_finally_op_both_succeed( self, stage, op, op2, op3, finally_op, callback ): run_ops_in_serial(stage, op, op2, op3, callback=callback, finally_op=finally_op) assert_callback_succeeded(callback=callback, op=finally_op)
def test_calls_callback_with_error_when_second_op_fails_and_finally_op_succeeds( self, stage, op, op2, op3, finally_op, callback ): op2.action = "fail" run_ops_in_serial(stage, op, op2, op3, finally_op=finally_op, callback=callback) assert_callback_failed(callback=callback, op=finally_op, error=op.error)
def test_runs_finally_op_on_op_failure(self, mocker, stage, callback, op, finally_op): op.action = "fail" run_ops_in_serial(stage, op, finally_op=finally_op, callback=callback) assert stage.next._run_op.call_args_list[1] == mocker.call(finally_op)
def test_calls_callback_when_third_op_fails(self, stage, op, op2, op3, callback): op3.action = "fail" run_ops_in_serial(stage, op, op2, op3, callback=callback) assert_callback_failed(callback=callback, op=op3)
def test_calls_callback_with_error_if_op_succeeds_and_finally_op_fails( self, stage, callback, op, finally_op ): finally_op.action = "fail" run_ops_in_serial(stage, op, finally_op=finally_op, callback=callback) assert_callback_failed(callback=callback, op=finally_op, error=finally_op.error)
def test_accepts_finally_op(self, stage, op, op2, finally_op, callback): run_ops_in_serial(stage, op, op2, callback=callback, finally_op=finally_op)
def test_runs_finally_op_on_success(self, mocker, stage, callback, op, finally_op): run_ops_in_serial(stage, op, finally_op=finally_op, callback=callback) assert stage.next._run_op.call_args_list[1] == mocker.call(finally_op)
def test_runs_first_op(self, mocker, stage, op, op2, op3, finally_op, callback): op.action = "pend" run_ops_in_serial(stage, op, op2, op3, finally_op=finally_op, callback=callback) assert stage.next._run_op.call_count == 1 assert stage.next._run_op.call_args == mocker.call(op)