Beispiel #1
0
    def test_accumulate_with_first_value_on_step_boundary(self):
        """
        step:    0              5
               --|--+--+--+--+--|--
        value:   14
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        self.assertEqual(persist.get("key"), None)
        step_data = accumulate(0, 14, "key")
        self.assertEqual(step_data, None)
        self.assertEqual(persist.get("key"), (0, 0))
Beispiel #2
0
    def test_accumulate(self):
        """
        step:    0              5
               --|--+--+--+--+--|--
        value:   0              4
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        self.assertEqual(persist.get("key"), None)
        step_data = accumulate(5, 4, "key")
        self.assertEqual(step_data, (5, 4))
        self.assertEqual(persist.get("key"), (5, 0))
Beispiel #3
0
    def test_accumulate_skipped_step(self):
        """
        step:    0              5              10             15
               --|--+--+--+--+--|--+--+--+--+--|--+--+--+--+--|--
        value:   0                                   4
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        self.assertEqual(persist.get("key"), None)
        step_data = accumulate(12, 4, "key")
        self.assertEqual(step_data, None)
        self.assertEqual(persist.get("key"), (12, 8))
    def run_script(self,
                   shell,
                   code,
                   user=None,
                   time_limit=None,
                   attachments=None,
                   server_supplied_env=None):
        """
        Run a script based on a shell and the code.

        A file will be written with #!<shell> as the first line, as executable,
        and run as the given user.

        XXX: Handle the 'reboot' and 'killall landscape-client' commands
        gracefully.

        @param shell: The interpreter to use.
        @param code: The code to run.
        @param user: The username to run the process as.
        @param time_limit: The number of seconds to allow the process to run
            before killing it and failing the returned Deferred with a
            L{ProcessTimeLimitReachedError}.
        @param attachments: C{dict} of filename/data attached to the script.

        @return: A deferred that will fire with the data printed by the process
            or fail with a L{ProcessTimeLimitReachedError}.
        """
        if not os.path.exists(shell.split()[0]):
            return fail(UnknownInterpreterError(shell))

        uid, gid, path = get_user_info(user)

        fd, filename = tempfile.mkstemp()
        script_file = os.fdopen(fd, "w")
        self.write_script_file(script_file, filename, shell, code, uid, gid)

        env = {"PATH": UBUNTU_PATH, "USER": user or "", "HOME": path or ""}
        if server_supplied_env:
            env.update(server_supplied_env)
        old_umask = os.umask(0022)

        if attachments:
            persist = Persist(filename=os.path.join(
                self.registry.config.data_path, "broker.bpickle"))
            persist = persist.root_at("registration")
            computer_id = persist.get("secure-id")
            d = self._save_attachments(attachments, uid, gid, computer_id)
        else:
            d = succeed(None)

        def prepare_script(attachment_dir):

            if attachment_dir is not None:
                env["LANDSCAPE_ATTACHMENTS"] = attachment_dir

            return self._run_script(filename, uid, gid, path, env, time_limit)

        d.addCallback(prepare_script)
        return d.addBoth(self._cleanup, filename, env, old_umask)
 def test_that_resynchronize_clears_message_blackhole(self):
     """
     When a resynchronisation event occurs the block on new messages
     being stored is lifted.
     """
     self.reactor.fire("resynchronize-clients", [])
     persist = Persist(filename=self.persist_filename)
     self.assertFalse(persist.get("blackhole-messages"))
class IdentityTest(LandscapeTest):

    helpers = [BrokerConfigurationHelper]

    def setUp(self):
        super(IdentityTest, self).setUp()
        self.persist = Persist(filename=self.makePersistFile())
        self.identity = Identity(self.config, self.persist)

    def check_persist_property(self, attr, persist_name):
        value = "VALUE"
        self.assertEqual(getattr(self.identity, attr), None,
                         "%r attribute should default to None, not %r" %
                         (attr, getattr(self.identity, attr)))
        setattr(self.identity, attr, value)
        self.assertEqual(getattr(self.identity, attr), value,
                         "%r attribute should be %r, not %r" %
                         (attr, value, getattr(self.identity, attr)))
        self.assertEqual(
            self.persist.get(persist_name), value,
            "%r not set to %r in persist" % (persist_name, value))

    def check_config_property(self, attr):
        value = "VALUE"
        setattr(self.config, attr, value)
        self.assertEqual(getattr(self.identity, attr), value,
                         "%r attribute should be %r, not %r" %
                         (attr, value, getattr(self.identity, attr)))

    def test_secure_id(self):
        self.check_persist_property("secure_id",
                                    "registration.secure-id")

    def test_secure_id_as_unicode(self):
        """secure-id is expected to be retrieved as unicode."""
        self.identity.secure_id = b"spam"
        self.assertEqual(self.identity.secure_id, "spam")

    def test_insecure_id(self):
        self.check_persist_property("insecure_id",
                                    "registration.insecure-id")

    def test_computer_title(self):
        self.check_config_property("computer_title")

    def test_account_name(self):
        self.check_config_property("account_name")

    def test_registration_key(self):
        self.check_config_property("registration_key")

    def test_client_tags(self):
        self.check_config_property("tags")

    def test_access_group(self):
        self.check_config_property("access_group")
Beispiel #7
0
    def test_flush_saves_persist(self):
        """
        The L{Monitor.flush} method saves any changes made to the persist
        database.
        """
        self.monitor.persist.set("a", 1)
        self.monitor.flush()

        persist = Persist()
        persist.load(self.monitor.persist_filename)
        self.assertEqual(persist.get("a"), 1)
Beispiel #8
0
    def test_flush_after_exchange(self):
        """
        The L{Monitor.exchange} method flushes the monitor after
        C{exchange} on all plugins has been called.
        """
        plugin = BrokerClientPlugin()
        plugin.exchange = lambda: self.monitor.persist.set("a", 1)
        self.monitor.add(plugin)
        self.monitor.exchange()

        persist = Persist()
        persist.load(self.monitor.persist_filename)
        self.assertEqual(persist.get("a"), 1)
Beispiel #9
0
    def test_accumulate_non_zero_accumulated_value(self):
        """
        step:    5              10             15
               --|--+--+--+--+--|--+--+--+--+--|--
        value:         4                 3
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        # Persist data that would have been stored when
        # accumulate(7, 4, "key") was called.
        persist.set("key", (7, 8))
        step_data = accumulate(13, 3, "key")
        self.assertEqual(step_data, (10, float((2 * 4) + (3 * 3)) / 5))
        self.assertEqual(persist.get("key"), (13, 9))
Beispiel #10
0
    def test_accumulate_within_step_with_nonzero_start_accumulated_value(self):
        """
        step:    0              5
               --|--+--+--+--+--|--
        value:   0     3     4
        """
        persist = Persist()
        accumulate = Accumulator(persist, 5)

        # Persist data that would have been stored when
        # accumulate(2, 3, "key") was called.
        persist.set("key", (2, 6))
        step_data = accumulate(4, 4, "key")
        self.assertEqual(step_data, None)
        self.assertEqual(persist.get("key"), (4, 14))
Beispiel #11
0
class PatchTest(LandscapeTest):
    def setUp(self):
        LandscapeTest.setUp(self)
        self.persist = Persist()
        self.manager = UpgradeManager()

    def test_wb_nopatches(self):
        """
        Applying no patches should make no change to the database,
        apart from maybe specifying a default version.
        """
        self.assertEqual(self.persist._hardmap, {})
        self.manager.apply(self.persist)
        self.assertEqual(self.persist._hardmap, {"system-version": 0})

    def test_one_patch(self):
        """Test that patches are called and passed a L{Persist} object."""
        calls = []
        self.manager.register_upgrader(1, calls.append)
        self.manager.apply(self.persist)
        self.assertEqual(calls, [self.persist])

    def test_two_patches(self):
        """Test that patches are run in order."""
        calls = []
        self.manager.register_upgrader(2, lambda x: calls.append(2))
        self.manager.register_upgrader(1, lambda x: calls.append(1))

        self.manager.apply(self.persist)
        self.assertEqual(calls, [1, 2])

    def test_record_version(self):
        """When a patch is run it should update the C{system-version}."""
        self.assertEqual(self.persist.get("system-version"), None)
        self.manager.register_upgrader(1, lambda x: None)
        self.manager.apply(self.persist)
        self.assertEqual(self.persist.get("system-version"), 1)

    def test_only_apply_unapplied_versions(self):
        """Upgraders should only be run if they haven't been run before."""
        calls = []
        self.manager.register_upgrader(1, lambda x: calls.append(1))
        self.manager.apply(self.persist)
        self.manager.apply(self.persist)
        self.assertEqual(calls, [1])

    def test_initialize(self):
        """Marking no upgraders as applied should leave the version at 0."""
        self.manager.initialize(self.persist)
        self.assertEqual(self.persist.get("system-version"), 0)

    def test_initialize_with_upgraders(self):
        """
        After registering some upgraders, initialize should set the
        version for the new persist to the highest version number
        available, without running any of the upgraders.
        """
        self.manager.register_upgrader(1, lambda x: 1 / 0)
        self.manager.register_upgrader(5, lambda x: 1 / 0)
        self.manager.register_upgrader(3, lambda x: 1 / 0)
        self.manager.initialize(self.persist)

        self.assertEqual(self.persist.get("system-version"), 5)

    def test_decorated_upgraders_run(self):
        """
        Upgraders that use the L{upgrader} decorator should
        automatically register themselves with a given
        L{UpgradeManager} and be run when the manager applies patches.
        """
        upgrade_manager = UpgradeManager()

        @upgrade_manager.upgrader(1)
        def upgrade(persist):
            self.persist.set("upgrade-called", True)

        upgrade_manager.apply(self.persist)
        self.assertTrue(self.persist.get("upgrade-called"))