~ahasenack/landscape-client/landscape-client-1.5.5.1-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/tests/test_configuration.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-vytyqgbtkiirv5sb
Tags: 1.5.2.1-0ubuntu0.10.04.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from getpass import getpass
3
3
from ConfigParser import ConfigParser
4
4
 
5
 
from dbus import DBusException
6
 
 
7
 
from twisted.internet.defer import Deferred, succeed, fail
8
 
from twisted.internet import reactor
9
 
 
 
5
from twisted.internet.defer import succeed, fail
 
6
 
 
7
from landscape.reactor import TwistedReactor
10
8
from landscape.lib.fetch import HTTPCodeError, PyCurlError
11
9
from landscape.configuration import (
12
10
    print_text, LandscapeSetupScript, LandscapeSetupConfiguration,
15
13
    fetch_import_url, ImportOptionError)
16
14
from landscape.broker.registration import InvalidCredentialsError
17
15
from landscape.sysvconfig import SysVConfig, ProcessError
18
 
from landscape.tests.helpers import (LandscapeTest, LandscapeIsolatedTest,
19
 
                                     RemoteBrokerHelper, EnvironSaverHelper)
20
 
from landscape.tests.mocker import ARGS, KWARGS, ANY, MATCH, CONTAINS, expect
 
16
from landscape.tests.helpers import (
 
17
    LandscapeTest, BrokerServiceHelper, EnvironSaverHelper)
 
18
from landscape.tests.mocker import ARGS, ANY, MATCH, CONTAINS, expect
 
19
from landscape.broker.amp import RemoteBroker, BrokerClientProtocol
21
20
 
22
21
 
23
22
def get_config(self, args):
26
25
[client]
27
26
url = https://landscape.canonical.com/message-system
28
27
""")
29
 
        args.extend(["--config", filename])
 
28
        args.extend(["--config", filename, "--data-path", self.makeDir()])
30
29
    config = LandscapeSetupConfiguration(fetch_import_url)
31
30
    config.load(args)
32
31
    return config
88
87
    def setUp(self):
89
88
        super(LandscapeSetupScriptTest, self).setUp()
90
89
        self.config_filename = self.makeFile()
 
90
 
91
91
        class MyLandscapeSetupConfiguration(LandscapeSetupConfiguration):
92
92
            default_config_filenames = [self.config_filename]
93
93
        self.config = MyLandscapeSetupConfiguration(None)
129
129
        script_mock = self.mocker.patch(self.script)
130
130
        raw_input_mock("Message: ")
131
131
        self.mocker.result("")
132
 
        script_mock.show_help("This option is required to configure Landscape.")
 
132
        script_mock.show_help("This option is required to "
 
133
                              "configure Landscape.")
133
134
        raw_input_mock("Message: ")
134
135
        self.mocker.result("Desktop")
135
136
        self.mocker.replay()
141
142
    def test_prompt_with_required_and_default(self):
142
143
        self.mocker.order()
143
144
        raw_input_mock = self.mocker.replace(raw_input, passthrough=False)
144
 
        script_mock = self.mocker.patch(self.script)
145
145
        raw_input_mock("Message [Desktop]: ")
146
146
        self.mocker.result("")
147
147
        self.mocker.replay()
197
197
        self.mocker.result("")
198
198
 
199
199
        script_mock = self.mocker.patch(self.script)
200
 
        script_mock.show_help("This option is required to configure Landscape.")
 
200
        script_mock.show_help("This option is required to "
 
201
                              "configure Landscape.")
201
202
 
202
203
        mock("Password: ")
203
204
        self.mocker.result("password")
246
247
        self.assertFalse(self.script.prompt_yes_no("Foo"))
247
248
 
248
249
    def get_matcher(self, help_snippet):
 
250
 
249
251
        def match_help(help):
250
252
            return help.strip().startswith(help_snippet)
251
253
        return MATCH(match_help)
297
299
        self.script.query_registration_password()
298
300
 
299
301
    def test_query_registration_password_defined_on_command_line(self):
300
 
        getpass_mock = self.mocker.replace("getpass.getpass", passthrough=False)
 
302
        getpass_mock = self.mocker.replace("getpass.getpass",
 
303
                                           passthrough=False)
301
304
        self.expect(getpass_mock(ANY)).count(0)
302
305
        self.mocker.replay()
303
306
 
458
461
        """
459
462
        Confirm with the user for users specified for the ScriptPlugin.
460
463
        """
461
 
        pwnam_mock = self.mocker.replace("pwd.getpwnam")
462
 
        pwnam_mock("root")
463
 
        self.mocker.result(True)
464
 
        pwnam_mock("nobody")
465
 
        self.mocker.result(True)
466
 
        pwnam_mock("landscape")
467
 
        self.mocker.result(True)
 
464
        self.config.include_manager_plugins = "FooPlugin"
468
465
        self.mocker.order()
469
466
        script_mock = self.mocker.patch(self.script)
470
467
        script_mock.show_help(ANY)
481
478
        self.assertEquals(self.config.script_users,
482
479
                          "root, nobody, landscape")
483
480
 
484
 
    def test_query_script_users_defined_on_command_line_with_unknown_user(self):
 
481
    def test_query_script_users_on_command_line_with_unknown_user(self):
485
482
        """
486
483
        If several users are provided on the command line, we verify the users
487
484
        and raise a ConfigurationError if any are unknown on this system.
522
519
        self.assertEquals(self.config.script_users,
523
520
                          "ALL")
524
521
 
525
 
    def test_query_script_users_defined_on_command_line_with_ALL_and_extra_user(self):
 
522
    def test_query_script_users_command_line_with_ALL_and_extra_user(self):
526
523
        """
527
524
        If ALL and additional users are provided as the users on the command
528
525
        line, this should raise an appropriate ConfigurationError.
658
655
                                 "https_proxy = https://old.proxy\n"
659
656
                                 "url = http://url\n"
660
657
                                 "include_manager_plugins = ScriptExecution\n"
661
 
                                 "tags = london, server"
662
 
                                 )
 
658
                                 "tags = london, server")
663
659
 
664
660
        raw_input = self.mocker.replace("__builtin__.raw_input",
665
661
                                        name="raw_input")
716
712
[client]
717
713
url = https://landscape.canonical.com/message-system
718
714
computer_title = rex
 
715
data_path = %s
719
716
account_name = account
720
 
""")
 
717
""" % config.data_path)
721
718
 
722
719
    def test_silent_setup_without_computer_title(self):
723
720
        """A computer title is required."""
960
957
        self.mocker.throw(ProcessError)
961
958
 
962
959
        print_text_mock("Couldn't restart the Landscape client.", error=True)
963
 
        print_text_mock(CONTAINS("This machine will be registered"), error=True)
 
960
        print_text_mock(CONTAINS("This machine will be registered"),
 
961
                        error=True)
964
962
 
965
963
        self.mocker.replay()
966
964
 
981
979
        self.mocker.throw(ProcessError)
982
980
 
983
981
        print_text_mock("Couldn't restart the Landscape client.", error=True)
984
 
        print_text_mock(CONTAINS("This machine will be registered"), error=True)
 
982
        print_text_mock(CONTAINS("This machine will be registered"),
 
983
                        error=True)
985
984
 
986
985
        self.mocker.replay()
987
986
 
1094
1093
            "https_proxy = https://new.proxy\n"
1095
1094
            "url = http://new.url\n")
1096
1095
 
1097
 
        import_filename = self.makeFile(configuration, basename="import_config")
 
1096
        import_filename = self.makeFile(configuration,
 
1097
                                        basename="import_config")
1098
1098
        config_filename = self.makeFile("", basename="final_config")
1099
1099
 
1100
1100
        config = self.get_config(["--config", config_filename, "--silent",
1123
1123
            self.get_config(["--config", config_filename, "--silent",
1124
1124
                             "--import", import_filename])
1125
1125
        except ImportOptionError, error:
1126
 
            self.assertEquals(str(error), 
 
1126
            self.assertEquals(str(error),
1127
1127
                              "Nothing to import at %s." % import_filename)
1128
1128
        else:
1129
1129
            self.fail("ImportOptionError not raised")
1139
1139
            self.get_config(["--config", config_filename, "--silent",
1140
1140
                             "--import", import_filename])
1141
1141
        except ImportOptionError, error:
1142
 
            self.assertEquals(str(error), 
 
1142
            self.assertEquals(str(error),
1143
1143
                              "File %s doesn't exist." % import_filename)
1144
1144
        else:
1145
1145
            self.fail("ImportOptionError not raised")
1158
1158
            self.get_config(["--config", config_filename, "--silent",
1159
1159
                             "--import", import_filename])
1160
1160
        except ImportOptionError, error:
1161
 
            self.assertEquals(str(error), 
 
1161
            self.assertEquals(str(error),
1162
1162
                              "Nothing to import at %s." % import_filename)
1163
1163
        else:
1164
1164
            self.fail("ImportOptionError not raised")
1364
1364
        try:
1365
1365
            self.get_config(["--silent", "--import", "https://config.url"])
1366
1366
        except ImportOptionError, error:
1367
 
            self.assertEquals(str(error), 
 
1367
            self.assertEquals(str(error),
1368
1368
                              "Nothing to import at https://config.url.")
1369
1369
        else:
1370
1370
            self.fail("ImportOptionError not raised")
1460
1460
        self.ensure_import_from_url_honors_proxy_options("https_proxy")
1461
1461
 
1462
1462
    def ensure_import_from_url_honors_proxy_options(self, proxy_option):
 
1463
 
1463
1464
        def check_proxy(url):
1464
1465
            self.assertEquals(os.environ.get(proxy_option), "http://proxy")
1465
1466
 
1488
1489
                 # we care about are done.
1489
1490
 
1490
1491
 
1491
 
class RegisterFunctionTest(LandscapeIsolatedTest):
1492
 
 
1493
 
    # Due to the way these tests run, the run() method on the reactor is called
1494
 
    # *before* any of the remote methods (reload, register) are called, because
1495
 
    # these tests "hold" the reactor until after the tests runs, then the
1496
 
    # reactor is given back control of the process, *then* all the remote calls
1497
 
    # in the dbus queue are fired.
1498
 
 
1499
 
    helpers = [RemoteBrokerHelper]
 
1492
class RegisterFunctionTest(LandscapeTest):
 
1493
 
 
1494
    helpers = [BrokerServiceHelper]
1500
1495
 
1501
1496
    def test_register_success(self):
1502
1497
        service = self.broker_service
1504
1499
        registration_mock = self.mocker.replace(service.registration)
1505
1500
        config_mock = self.mocker.replace(service.config)
1506
1501
        print_text_mock = self.mocker.replace(print_text)
1507
 
        reactor_mock = self.mocker.proxy("twisted.internet.reactor")
1508
 
        install_mock = self.mocker.replace("landscape.reactor.install")
 
1502
        reactor_mock = self.mocker.patch(TwistedReactor)
1509
1503
 
1510
1504
        # This must necessarily happen in the following order.
1511
1505
        self.mocker.order()
1512
1506
 
1513
 
        install_mock()
1514
 
 
1515
1507
        # This very informative message is printed out.
1516
1508
        print_text_mock("Please wait... ", "")
1517
1509
 
1527
1519
        # The register() method is called.  We fire the "registration-done"
1528
1520
        # event after it's done, so that it cascades into a deferred callback.
1529
1521
 
1530
 
        def register_done(deferred_result):
 
1522
        def register_done():
1531
1523
            service.reactor.fire("registration-done")
1532
1524
        registration_mock.register()
1533
 
        self.mocker.passthrough(register_done)
 
1525
        self.mocker.call(register_done)
1534
1526
 
1535
1527
        # The deferred callback finally prints out this message.
1536
1528
        print_text_mock("System successfully registered.")
1537
1529
 
1538
 
        result = Deferred()
1539
1530
        reactor_mock.stop()
1540
 
        self.mocker.call(lambda: result.callback(None))
1541
1531
 
1542
1532
        # Nothing else is printed!
1543
1533
        print_text_mock(ANY)
1546
1536
        self.mocker.replay()
1547
1537
 
1548
1538
        # DO IT!
1549
 
        register(service.config, reactor_mock)
1550
 
 
1551
 
        return result
 
1539
        return register(service.config)
1552
1540
 
1553
1541
    def test_register_failure(self):
1554
1542
        """
1561
1549
        registration_mock = self.mocker.replace(service.registration)
1562
1550
        config_mock = self.mocker.replace(service.config)
1563
1551
        print_text_mock = self.mocker.replace(print_text)
1564
 
        reactor_mock = self.mocker.proxy("twisted.internet.reactor")
1565
 
        install_mock = self.mocker.replace("landscape.reactor.install")
 
1552
        reactor_mock = self.mocker.patch(TwistedReactor)
1566
1553
 
1567
1554
        # This must necessarily happen in the following order.
1568
1555
        self.mocker.order()
1569
1556
 
1570
 
        install_mock()
1571
 
 
1572
1557
        # This very informative message is printed out.
1573
1558
        print_text_mock("Please wait... ", "")
1574
1559
 
1583
1568
 
1584
1569
        # The register() method is called.  We fire the "registration-failed"
1585
1570
        # event after it's done, so that it cascades into a deferred errback.
1586
 
        def register_done(deferred_result):
 
1571
 
 
1572
        def register_done():
1587
1573
            service.reactor.fire("registration-failed")
1588
1574
        registration_mock.register()
1589
 
        self.mocker.passthrough(register_done)
 
1575
        self.mocker.call(register_done)
1590
1576
 
1591
1577
        # The deferred errback finally prints out this message.
1592
1578
        print_text_mock("Invalid account name or registration password.",
1593
1579
                        error=True)
1594
1580
 
1595
 
        result = Deferred()
1596
1581
        reactor_mock.stop()
1597
 
        self.mocker.call(lambda: result.callback(None))
1598
1582
 
1599
1583
        # Nothing else is printed!
1600
1584
        print_text_mock(ANY)
1603
1587
        self.mocker.replay()
1604
1588
 
1605
1589
        # DO IT!
1606
 
        register(service.config, reactor_mock)
1607
 
 
1608
 
        return result
 
1590
        return register(service.config)
1609
1591
 
1610
1592
    def test_register_exchange_failure(self):
1611
1593
        """
1617
1599
        registration_mock = self.mocker.replace(service.registration)
1618
1600
        config_mock = self.mocker.replace(service.config)
1619
1601
        print_text_mock = self.mocker.replace(print_text)
1620
 
        reactor_mock = self.mocker.proxy("twisted.internet.reactor")
1621
 
        install_mock = self.mocker.replace("landscape.reactor.install")
 
1602
        reactor_mock = self.mocker.patch(TwistedReactor)
1622
1603
 
1623
1604
        # This must necessarily happen in the following order.
1624
1605
        self.mocker.order()
1625
1606
 
1626
 
        install_mock()
1627
 
 
1628
1607
        # This very informative message is printed out.
1629
1608
        print_text_mock("Please wait... ", "")
1630
1609
 
1637
1616
        # After a nice dance the configuration is reloaded.
1638
1617
        config_mock.reload()
1639
1618
 
1640
 
        def register_done(deferred_result):
 
1619
        def register_done():
1641
1620
            service.reactor.fire("exchange-failed")
1642
1621
        registration_mock.register()
1643
 
        self.mocker.passthrough(register_done)
 
1622
        self.mocker.call(register_done)
1644
1623
 
1645
1624
        # The deferred errback finally prints out this message.
1646
1625
        print_text_mock("We were unable to contact the server. "
1647
1626
                        "Your internet connection may be down. "
1648
 
                        "The landscape client will continue to try and contact "
1649
 
                        "the server periodically.",
 
1627
                        "The landscape client will continue to try and "
 
1628
                        "contact the server periodically.",
1650
1629
                        error=True)
1651
1630
 
1652
1631
 
1653
 
        result = Deferred()
1654
1632
        reactor_mock.stop()
1655
 
        self.mocker.call(lambda: result.callback(None))
1656
1633
 
1657
1634
        # Nothing else is printed!
1658
1635
        print_text_mock(ANY)
1661
1638
        self.mocker.replay()
1662
1639
 
1663
1640
        # DO IT!
1664
 
        register(service.config, reactor_mock)
1665
 
 
1666
 
        return result
 
1641
        return register(service.config)
1667
1642
 
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
1674
1645
 
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)
 
1651
 
 
1652
        protocol_mock = self.mocker.patch(BrokerClientProtocol)
 
1653
        protocol_mock.timeout
 
1654
        self.mocker.result(0.1)
 
1655
        self.mocker.count(0, None)
1680
1656
 
1681
1657
        # This must necessarily happen in the following order.
1682
1658
        self.mocker.order()
1683
1659
 
1684
 
        install_mock()
1685
 
 
1686
1660
        # This very informative message is printed out.
1687
1661
        print_text_mock("Please wait... ", "")
1688
1662
 
1692
1666
 
1693
1667
        reactor_mock.run()
1694
1668
 
 
1669
        remote_mock.call_on_event(ANY)
 
1670
        self.mocker.result(succeed(None))
 
1671
 
1695
1672
        # After a nice dance the configuration is reloaded.
1696
1673
        config_mock.reload()
1697
1674
 
 
1675
 
1698
1676
        registration_mock.register()
1699
1677
        self.mocker.passthrough()
1700
1678
 
1704
1682
 
1705
1683
        self.mocker.replay()
1706
1684
 
1707
 
        result = Deferred()
1708
 
 
1709
 
        reactor.addSystemEventTrigger("during",
1710
 
                                      "landscape-registration-error",
1711
 
                                      result.callback, None)
1712
1685
        # DO IT!
1713
 
        register(service.config, reactor_mock)
1714
 
 
1715
 
        return result
 
1686
        return register(service.config)
1716
1687
 
1717
1688
    def test_register_bus_connection_failure(self):
1718
1689
        """
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.
1721
1692
        """
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")
1727
 
 
1728
 
        install_mock()
 
1696
        sys_mock = self.mocker.replace("sys")
 
1697
        reactor_mock = self.mocker.patch(TwistedReactor)
 
1698
 
 
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))
 
1704
 
1729
1705
        print_text_mock(ARGS)
1730
1706
        time_mock.sleep(ANY)
1731
 
 
1732
 
        remote_broker_factory(ARGS, KWARGS)
1733
 
        self.mocker.throw(DBusException)
 
1707
        reactor_mock.run()
1734
1708
 
1735
1709
        print_text_mock(
1736
1710
            CONTAINS("There was an error communicating with the "
1737
1711
                     "Landscape client"),
1738
1712
            error=True)
1739
 
        print_text_mock(CONTAINS("This machine will be registered"), error=True)
 
1713
        print_text_mock(CONTAINS("This machine will be registered"),
 
1714
                        error=True)
 
1715
 
 
1716
        sys_mock.exit(2)
 
1717
        connector.disconnect()
 
1718
        reactor_mock.stop()
1740
1719
 
1741
1720
        self.mocker.replay()
 
1721
 
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)
1745
1724
 
1746
1725
    def test_register_bus_connection_failure_ok_no_register(self):
1747
1726
        """
1748
1727
        Exit code 0 will be returned if we can't contact Landscape via DBus and
1749
1728
        --ok-no-register was passed.
1750
1729
        """
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)
1756
1733
 
1757
 
        install_mock()
1758
1734
        print_text_mock(ARGS)
1759
1735
        time_mock.sleep(ANY)
1760
 
 
1761
 
        remote_broker_factory(ARGS, KWARGS)
1762
 
        self.mocker.throw(DBusException)
 
1736
        reactor_mock.run()
 
1737
        reactor_mock.stop()
1763
1738
 
1764
1739
        print_text_mock(
1765
1740
            CONTAINS("There was an error communicating with the "
1766
1741
                     "Landscape client"),
1767
1742
            error=True)
1768
 
        print_text_mock(CONTAINS("This machine will be registered"), error=True)
 
1743
        print_text_mock(CONTAINS("This machine will be registered"),
 
1744
                        error=True)
1769
1745
 
1770
1746
        self.mocker.replay()
 
1747
 
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)
1775
 
 
1776
 
 
1777
 
class RegisterFunctionNoServiceTest(LandscapeIsolatedTest):
1778
 
 
1779
 
    def setUp(self):
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"])
1784
 
 
1785
 
    def test_register_dbus_error(self):
1786
 
        """
1787
 
        When registration fails because of a DBUS error, a message is printed
1788
 
        and the program exits.
1789
 
        """
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")
1793
 
 
1794
 
        install_mock()
1795
 
        print_text_mock("Please wait... ", "")
1796
 
 
1797
 
        print_text_mock("Error occurred contacting Landscape Client. "
1798
 
                        "Is it running?",
1799
 
                        error=True)
1800
 
 
1801
 
        # WHOAH DUDE. This waits for callLater(0, reactor.stop).
1802
 
        result = Deferred()
1803
 
        reactor_mock.callLater(0, ANY)
1804
 
        self.mocker.call(lambda seconds, thingy: thingy())
1805
 
        reactor_mock.stop()
1806
 
        self.mocker.call(lambda: result.callback(None))
1807
 
        reactor_mock.run()
1808
 
 
1809
 
        self.mocker.replay()
1810
 
 
1811
 
        # DO IT!
1812
 
        register(self.configuration, reactor_mock)
1813
 
 
1814
 
        return result
 
1750
        return self.assertSuccess(register(config))
 
1751
 
 
1752
 
 
1753
class RegisterFunctionNoServiceTest(LandscapeTest):
1815
1754
 
1816
1755
    def test_register_unknown_error(self):
1817
1756
        """
1818
1757
        When registration fails because of an unknown error, a message is
1819
1758
        printed and the program exits.
1820
1759
        """
 
1760
        configuration = LandscapeSetupConfiguration(None)
 
1761
 
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()
1824
1766
 
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)
 
1769
 
1828
1770
        # This is unordered. It's just way too much of a pain.
1829
 
 
1830
 
        install_mock()
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)
 
1775
        reactor_mock.run()
1835
1776
 
1836
1777
        # SNORE
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)
1844
1785
 
1845
1786
        # here it is!
1846
1787
        remote_broker.register()
1847
1788
        self.mocker.result(fail(ZeroDivisionError))
1848
1789
 
1849
1790
        print_text_mock(ANY, error=True)
 
1791
 
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)
1854
1796
 
1855
1797
        # WHOAH DUDE. This waits for callLater(0, reactor.stop).
1856
 
        result = Deferred()
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))
1861
 
 
1862
 
        reactor_mock.run()
1863
1800
 
1864
1801
        self.mocker.replay()
1865
1802
 
1866
 
        register(self.configuration, reactor_mock)
1867
 
 
1868
 
        return result
 
1803
        return register(configuration)