Пример #1
0
class ProcessDispatcherStoreTests(unittest.TestCase, StoreTestMixin):

    ZK_HOSTS = "localhost:2181"

    def setUp(self):
        self.store = ProcessDispatcherStore()

    def test_queued_processes(self):

        source = [("u1", "proc1", 0), ("u1", "proc2", 1), ("u2", "proc1", 0),
            ("u2", "proc2", 0), ("u3", "proc3", 3)]

        for key in source:
            self.store.enqueue_process(*key)

        queued = self.store.get_queued_processes()
        self.assertEqual(source, queued)

        toremove = source.pop()
        self.store.remove_queued_process(*toremove)

        queued = self.store.get_queued_processes()
        self.assertEqual(source, queued)

    def assertProcessDefinitionsEqual(self, d1, d2):
        attrs = ('definition_id', 'definition_type', 'executable',
                             'name', 'description', 'version')
        for attr in attrs:
            self.assertEqual(getattr(d1, attr), getattr(d2, attr))

    def test_add_update_remove_definition(self):

        d1 = ProcessDefinitionRecord.new("d1", "t1", "notepad.exe", "proc1")
        d2 = ProcessDefinitionRecord.new("d2", "t2", "cat", "proc2")

        self.store.add_definition(d1)
        self.store.add_definition(d2)

        # adding again should get a WriteConflict
        self.assertRaises(WriteConflictError, self.store.add_definition, d1)

        all_ids = self.store.list_definition_ids()
        self.assertEqual(set(all_ids), set(["d1", "d2"]))

        got_d1 = self.store.get_definition("d1")
        self.assertProcessDefinitionsEqual(d1, got_d1)

        got_d2 = self.store.get_definition("d2")
        self.assertProcessDefinitionsEqual(d2, got_d2)

        self.assertIsNone(self.store.get_definition("d3"))

        d1.executable = "ps"
        self.store.update_definition(d1)
        got_d1 = self.store.get_definition("d1")
        self.assertProcessDefinitionsEqual(d1, got_d1)

        self.store.remove_definition("d1")
        self.store.remove_definition("d2")

        self.assertRaises(NotFoundError, self.store.update_definition, d2)

        self.assertFalse(self.store.list_definition_ids())

        self.assertIsNone(self.store.get_definition("d1"))
        self.assertIsNone(self.store.get_definition("neverexisted"))

    def test_not_unicode(self):
        d1 = ProcessDefinitionRecord.new("d1", "t1", "notepad.exe", "proc1")
        self.store.add_definition(d1)
        got_d1 = self.store.get_definition("d1")

        # ensure strings don't come back as unicode
        self.assertIsInstance(got_d1.definition_id, str)
        self.assertIsInstance(got_d1.name, str)