From 4ece4e9dce3cf62ce33fb245f507d070255be3aa Mon Sep 17 00:00:00 2001 From: meejah Date: Thu, 7 May 2020 15:24:07 -0600 Subject: [PATCH] specific exception for failing subprocess --- integration/test_grid_manager.py | 7 ++----- integration/test_servers_of_happiness.py | 4 ++-- integration/util.py | 22 +++++++++++++++------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/integration/test_grid_manager.py b/integration/test_grid_manager.py index 0a825f14a..d46bb6b10 100644 --- a/integration/test_grid_manager.py +++ b/integration/test_grid_manager.py @@ -190,8 +190,5 @@ def test_reject_storage_server(reactor, request, storage_nodes, temp_dir, introd stdin="some content" * 200, ) assert False, "Should get a failure" - except Exception as e: - # depending on the full output being in the error-message - # here; see util.py - assert 'UploadUnhappinessError' in str(e) - print("found expected UploadUnhappinessError") + except util.ProcessFailed as e: + assert 'UploadUnhappinessError' in e.output.getvalue() diff --git a/integration/test_servers_of_happiness.py b/integration/test_servers_of_happiness.py index 1984dd507..c9b654d9c 100644 --- a/integration/test_servers_of_happiness.py +++ b/integration/test_servers_of_happiness.py @@ -42,8 +42,8 @@ def test_upload_immutable(reactor, temp_dir, introducer_furl, flog_gatherer, sto try: yield proto.done assert False, "should raise exception" - except Exception as e: - assert "UploadUnhappinessError" in str(e) + except util.ProcessFailed as e: + assert "UploadUnhappinessError" in e.output.getvalue() output = proto.output.getvalue() assert "shares could be placed on only" in output diff --git a/integration/util.py b/integration/util.py index 703402aa8..ffdf5a317 100644 --- a/integration/util.py +++ b/integration/util.py @@ -32,6 +32,20 @@ class _ProcessExitedProtocol(ProcessProtocol): self.done.callback(None) +class ProcessFailed(Exception): + """ + A subprocess has failed. + + :ivar ProcessTerminated reason: the original reason from .processExited + + :ivar StringIO output: all stdout and stderr collected to this point. + """ + + def __init__(self, reason, output): + self.reason = reason + self.output = output + + class _CollectOutputProtocol(ProcessProtocol): """ Internal helper. Collects all output (stdout + stderr) into @@ -54,13 +68,7 @@ class _CollectOutputProtocol(ProcessProtocol): def processExited(self, reason): if not isinstance(reason.value, ProcessDone): - #self.done.errback(reason) - self.done.errback(RuntimeError( - "Process failed: {}\nOutput:\n{}".format( - reason, - self.output.getvalue(), - ) - )) + self.done.errback(ProcessFailed(reason, self.output)) def outReceived(self, data): self.output.write(data)