1661
1638
self.mocker.replay()
1664
register(service.config, reactor_mock)
1641
return register(service.config)
1668
1643
def test_register_timeout_failure(self):
1669
# XXX This test will take about 30 seconds to run on some versions of
1670
# dbus, as it really is waiting for the dbus call to timeout. We can
1671
# remove it after it's possible for us to specify dbus timeouts on all
1672
# supported platforms (current problematic ones are edgy through gutsy)
1673
1644
service = self.broker_service
1675
1646
registration_mock = self.mocker.replace(service.registration)
1676
1647
config_mock = self.mocker.replace(service.config)
1677
1648
print_text_mock = self.mocker.replace(print_text)
1678
reactor_mock = self.mocker.proxy("twisted.internet.reactor")
1679
install_mock = self.mocker.replace("landscape.reactor.install")
1649
reactor_mock = self.mocker.patch(TwistedReactor)
1650
remote_mock = self.mocker.patch(RemoteBroker)
1652
protocol_mock = self.mocker.patch(BrokerClientProtocol)
1653
protocol_mock.timeout
1654
self.mocker.result(0.1)
1655
self.mocker.count(0, None)
1681
1657
# This must necessarily happen in the following order.
1682
1658
self.mocker.order()
1686
1660
# This very informative message is printed out.
1687
1661
print_text_mock("Please wait... ", "")
1705
1683
self.mocker.replay()
1709
reactor.addSystemEventTrigger("during",
1710
"landscape-registration-error",
1711
result.callback, None)
1713
register(service.config, reactor_mock)
1686
return register(service.config)
1717
1688
def test_register_bus_connection_failure(self):
1719
If the bus can't be connected to, landscape-config will print an
1690
If the socket can't be connected to, landscape-config will print an
1720
1691
explanatory message and exit cleanly.
1722
remote_broker_factory = self.mocker.replace(
1723
"landscape.broker.remote.RemoteBroker", passthrough=False)
1693
# This will make the RemoteBrokerConnector.connect call fail
1724
1694
print_text_mock = self.mocker.replace(print_text)
1725
install_mock = self.mocker.replace("landscape.reactor.install")
1726
1695
time_mock = self.mocker.replace("time")
1696
sys_mock = self.mocker.replace("sys")
1697
reactor_mock = self.mocker.patch(TwistedReactor)
1699
connector_factory = self.mocker.replace(
1700
"landscape.broker.amp.RemoteBrokerConnector", passthrough=False)
1701
connector = connector_factory(ANY, ANY)
1702
connector.connect(max_retries=0, quiet=True)
1703
self.mocker.result(fail(ZeroDivisionError))
1729
1705
print_text_mock(ARGS)
1730
1706
time_mock.sleep(ANY)
1732
remote_broker_factory(ARGS, KWARGS)
1733
self.mocker.throw(DBusException)
1735
1709
print_text_mock(
1736
1710
CONTAINS("There was an error communicating with the "
1737
1711
"Landscape client"),
1739
print_text_mock(CONTAINS("This machine will be registered"), error=True)
1713
print_text_mock(CONTAINS("This machine will be registered"),
1717
connector.disconnect()
1741
1720
self.mocker.replay()
1742
1722
config = get_config(self, ["-a", "accountname", "--silent"])
1743
system_exit = self.assertRaises(SystemExit, register, config)
1744
self.assertEquals(system_exit.code, 2)
1723
return register(config)
1746
1725
def test_register_bus_connection_failure_ok_no_register(self):
1748
1727
Exit code 0 will be returned if we can't contact Landscape via DBus and
1749
1728
--ok-no-register was passed.
1751
remote_broker_factory = self.mocker.replace(
1752
"landscape.broker.remote.RemoteBroker", passthrough=False)
1753
1730
print_text_mock = self.mocker.replace(print_text)
1754
install_mock = self.mocker.replace("landscape.reactor.install")
1755
1731
time_mock = self.mocker.replace("time")
1732
reactor_mock = self.mocker.patch(TwistedReactor)
1758
1734
print_text_mock(ARGS)
1759
1735
time_mock.sleep(ANY)
1761
remote_broker_factory(ARGS, KWARGS)
1762
self.mocker.throw(DBusException)
1764
1739
print_text_mock(
1765
1740
CONTAINS("There was an error communicating with the "
1766
1741
"Landscape client"),
1768
print_text_mock(CONTAINS("This machine will be registered"), error=True)
1743
print_text_mock(CONTAINS("This machine will be registered"),
1770
1746
self.mocker.replay()
1771
1748
config = get_config(self, ["-a", "accountname", "--silent",
1772
1749
"--ok-no-register"])
1773
system_exit = self.assertRaises(SystemExit, register, config)
1774
self.assertEquals(system_exit.code, 0)
1777
class RegisterFunctionNoServiceTest(LandscapeIsolatedTest):
1780
super(RegisterFunctionNoServiceTest, self).setUp()
1781
self.configuration = LandscapeSetupConfiguration(None)
1782
# Let's not mess about with the system bus
1783
self.configuration.load_command_line(["--bus", "session"])
1785
def test_register_dbus_error(self):
1787
When registration fails because of a DBUS error, a message is printed
1788
and the program exits.
1790
print_text_mock = self.mocker.replace(print_text)
1791
reactor_mock = self.mocker.proxy("twisted.internet.reactor")
1792
install_mock = self.mocker.replace("landscape.reactor.install")
1795
print_text_mock("Please wait... ", "")
1797
print_text_mock("Error occurred contacting Landscape Client. "
1801
# WHOAH DUDE. This waits for callLater(0, reactor.stop).
1803
reactor_mock.callLater(0, ANY)
1804
self.mocker.call(lambda seconds, thingy: thingy())
1806
self.mocker.call(lambda: result.callback(None))
1809
self.mocker.replay()
1812
register(self.configuration, reactor_mock)
1750
return self.assertSuccess(register(config))
1753
class RegisterFunctionNoServiceTest(LandscapeTest):
1816
1755
def test_register_unknown_error(self):
1818
1757
When registration fails because of an unknown error, a message is
1819
1758
printed and the program exits.
1760
configuration = LandscapeSetupConfiguration(None)
1821
1762
# We'll just mock the remote here to have it raise an exception.
1822
remote_broker_factory = self.mocker.replace(
1823
"landscape.broker.remote.RemoteBroker", passthrough=False)
1763
connector_factory = self.mocker.replace(
1764
"landscape.broker.amp.RemoteBrokerConnector", passthrough=False)
1765
remote_broker = self.mocker.mock()
1825
1767
print_text_mock = self.mocker.replace(print_text)
1826
reactor_mock = self.mocker.proxy("twisted.internet.reactor")
1827
install_mock = self.mocker.replace("landscape.reactor.install")
1768
reactor_mock = self.mocker.patch(TwistedReactor)
1828
1770
# This is unordered. It's just way too much of a pain.
1831
1771
print_text_mock("Please wait... ", "")
1832
1772
time_mock = self.mocker.replace("time")
1833
1773
time_mock.sleep(ANY)
1834
1774
self.mocker.count(1)
1837
remote_broker = remote_broker_factory(ANY, retry_timeout=0)
1838
self.mocker.result(succeed(None))
1778
connector = connector_factory(ANY, configuration)
1779
connector.connect(max_retries=0, quiet=True)
1780
self.mocker.result(succeed(remote_broker))
1839
1781
remote_broker.reload_configuration()
1840
1782
self.mocker.result(succeed(None))
1841
remote_broker.connect_to_signal(ARGS, KWARGS)
1783
remote_broker.call_on_event(ANY)
1842
1784
self.mocker.result(succeed(None))
1843
self.mocker.count(3)
1846
1787
remote_broker.register()
1847
1788
self.mocker.result(fail(ZeroDivisionError))
1849
1790
print_text_mock(ANY, error=True)
1850
1792
def check_logged_failure(text, error):
1851
1793
self.assertTrue("ZeroDivisionError" in text)
1852
1794
self.mocker.call(check_logged_failure)
1853
1795
print_text_mock("Unknown error occurred.", error=True)
1855
1797
# WHOAH DUDE. This waits for callLater(0, reactor.stop).
1857
reactor_mock.callLater(0, ANY)
1858
self.mocker.call(lambda seconds, thingy: thingy())
1798
connector.disconnect()
1859
1799
reactor_mock.stop()
1860
self.mocker.call(lambda: result.callback(None))
1864
1801
self.mocker.replay()
1866
register(self.configuration, reactor_mock)
1803
return register(configuration)