Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop reactor in landscape-config on broker error. #13

Merged
merged 1 commit into from Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions landscape/configuration.py
Expand Up @@ -650,10 +650,12 @@ def got_connection(add_result, connector, reactor, remote):
return results


def got_error(failure, print=print):
"""...from broker."""
def got_error(failure, reactor, add_result, print=print):
"""Handle errors contacting broker."""
print(failure.getTraceback(), file=sys.stderr)
raise SystemExit
# Can't just raise SystemExit; it would be ignored by the reactor.
add_result(SystemExit())
reactor.stop()


def register(config, reactor=None, connector_factory=RemoteBrokerConnector,
Expand Down Expand Up @@ -682,6 +684,7 @@ def register(config, reactor=None, connector_factory=RemoteBrokerConnector,
"""
if reactor is None:
reactor = LandscapeReactor()

if results is None:
results = []
add_result = results.append
Expand All @@ -690,13 +693,17 @@ def register(config, reactor=None, connector_factory=RemoteBrokerConnector,
connection = connector.connect(max_retries=max_retries, quiet=True)
connection.addCallback(
partial(got_connection, add_result, connector, reactor))
connection.addErrback(got_error)
connection.addErrback(
partial(got_error, reactor=reactor, add_result=add_result))
reactor.run()

assert len(results) == 1, "We expect exactly one result."
# Results will be things like "success" or "ssl-error".
result = results[0]

if isinstance(result, SystemExit):
raise result

# If there was an error and the caller requested that errors be reported
# to the on_error callable, then do so.
if result != "success" and on_error is not None:
Expand Down
11 changes: 8 additions & 3 deletions landscape/tests/test_configuration.py
Expand Up @@ -192,14 +192,19 @@ class FauxFailure(object):
def getTraceback(self):
return "traceback"

results = []
printed = []

def faux_print(text, file):
printed.append((text, file))

with self.assertRaises(SystemExit):
got_error(FauxFailure(), print=faux_print)
mock_reactor = mock.Mock()

got_error(FauxFailure(), reactor=mock_reactor,
add_result=results.append, print=faux_print)
mock_reactor.stop.assert_called_once_with()

self.assertIsInstance(results[0], SystemExit)
self.assertEqual([('traceback', sys.stderr)], printed)


Expand Down Expand Up @@ -1931,7 +1936,7 @@ def connector_factory(reactor, config):
self.assertTrue(1, len(connector.connection.errbacks))
self.assertEqual(
'got_error',
connector.connection.errbacks[0].__name__)
connector.connection.errbacks[0].func.__name__)
# We ask for retries because networks aren't reliable.
self.assertEqual(99, connector.max_retries)

Expand Down