Beispiel #1
0
class TestHandlerContainer(unittest.TestCase):
    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()

    def container(self):
        return Container(MockDeployer.ROOT, MockDeployer.PATH)

    def test_loading(self):
        # Setup
        container = self.container()
        # Test
        container.load()
        handler = container.find("rpm")
        # Verify
        self.assertTrue(handler is not None)

    def test_find(self):
        # Setup
        container = self.container()
        # Test
        container.load()
        handler = container.find("xxx")
        # Verify
        self.assertTrue(handler is None)
class TestHandlerContainer(unittest.TestCase):

    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()
        
    def container(self):
        return Container(MockDeployer.ROOT, MockDeployer.PATH)

    def test_loading(self):
        # Setup
        container = self.container()
        # Test
        container.load()
        # Verify
        handler = container.find('rpm')
        self.assertTrue(handler is not None)
        handler = container.find('puppet')
        self.assertTrue(handler is None)
        errors = container.errors()
        self.assertEquals(len(errors), 2)
        self.assertTrue(isinstance(errors[0], PropertyNotFound))
        self.assertTrue(isinstance(errors[1], SectionNotFound))

    def test_find(self):
        # Setup
        container = self.container()
        # Test
        container.load()
        handler = container.find('xxx')
        # Verify
        self.assertTrue(handler is None)
class TestHandlerContainer(unittest.TestCase):
    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()

    def container(self):
        return Container(MockDeployer.CONF_D, [MockDeployer.PATH])

    def test_loading(self):
        # Setup
        container = self.container()
        # Test
        container.load()
        # Verify
        handler = container.find('rpm')
        self.assertTrue(handler is not None)
        handler = container.find('srpm')
        self.assertTrue(handler is not None)
        handler = container.find('puppet')
        self.assertTrue(handler is None)
        handler = container.find('yum', BIND)
        self.assertTrue(handler is not None)
        handler = container.find('Linux', SYSTEM)
        self.assertTrue(handler is not None)
        errors = container.errors()
        self.assertEquals(len(errors), 3)
        self.assertTrue(isinstance(errors[0], ImportError))
        self.assertTrue(isinstance(errors[1], PropertyNotFound))
        self.assertTrue(isinstance(errors[2], SectionNotFound))

    def test_find(self):
        # Setup
        container = self.container()
        # Test
        container.load()
        handler = container.find('xxx')
        # Verify
        self.assertTrue(handler is None)
 def setUp(self):
     self.deployer = MockDeployer()
     self.deployer.deploy()
class TestDispatcher(unittest.TestCase):
    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()

    def container(self):
        return Container(MockDeployer.CONF_D, [MockDeployer.PATH])

    def test_install(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 2)
        self.assertFalse(report.reboot['scheduled'])

    def test_install_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        report = ContentReport()
        report.set_failed({'a': 1})
        handler.install = Mock(return_value=report)
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a': 1})

    def test_install_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        handler.install = Mock(side_effect=ValueError())
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_install_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        options = dict(reboot=True)
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, [unit], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 2)
        self.assertTrue(report.reboot['scheduled'])

    def test_install_failed_no_handler(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='xxx', unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 1)
        # RPM passed
        rpm = report.details['rpm']
        self.assertTrue(rpm['succeeded'])
        # XXX failed
        xxx = report.details['xxx']
        self.assertFalse(xxx['succeeded'])

    def test_update(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 2)

    def test_update_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        report = ContentReport()
        report.set_failed({'a': 1})
        handler.update = Mock(return_value=report)
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a': 1})

    def test_update_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        handler.update = Mock(side_effect=ValueError())
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_uninstall(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, [unit], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 1)

    def test_uninstall_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        report = ContentReport()
        report.set_failed({'a': 1})
        handler.uninstall = Mock(return_value=report)
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a': 1})

    def test_uninstall_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id='rpm', unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(type_id='rpm', unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        handler.uninstall = Mock(side_effect=ValueError())
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_profile(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 0)

    def test_profile_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find('rpm')
        report = ProfileReport()
        report.set_failed({'a': 1})
        handler.profile = Mock(return_value=report)
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a': 1})

    def test_profile_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find('rpm')
        handler.profile = Mock(side_effect=ValueError())
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.reboot(conduit, {})
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 0)

    def test_bind(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id, details={})
        options = {}
        report = dispatcher.bind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEqual(report.num_changes, 1)
        self.assertTrue(report.details[type_id][repo_id]['succeeded'])
        self.assertEqual(report.details[type_id][repo_id]['details'], {})

    def test_bind_failed(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = BindReport(repo_id)
        report.set_failed({'a': 1})
        handler.bind = Mock(return_value=report)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id, details={})
        options = {}
        report = dispatcher.bind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        self.assertFalse(report.details[type_id][repo_id]['succeeded'])
        self.assertEqual(report.details[type_id][repo_id]['details'], {'a': 1})

    def test_bind_raised(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.bind = Mock(side_effect=ValueError())
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id, details={})
        options = {}
        report = dispatcher.bind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        self.assertTrue(
            report.details[type_id][repo_id]['details'].has_key('message'))
        self.assertTrue(
            report.details[type_id][repo_id]['details'].has_key('trace'))

    def test_unbind(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEqual(report.num_changes, 1)
        self.assertEqual(report.details[type_id][repo_id]['details'], {})

    def test_unbind_failed(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = BindReport(repo_id)
        report.set_failed({'a': 1})
        handler.unbind = Mock(return_value=report)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        self.assertFalse(report.details[type_id][repo_id]['succeeded'])
        self.assertEqual(report.details[type_id][repo_id]['details'], {'a': 1})

    def test_unbind_raised(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.unbind = Mock(side_effect=ValueError)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        self.assertFalse(report.details[type_id][repo_id]['succeeded'])
        self.assertTrue(
            report.details[type_id][repo_id]['details'].has_key('message'))
        self.assertTrue(
            report.details[type_id][repo_id]['details'].has_key('trace'))

    def test_unbind_all(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        binding = dict(type_id=None, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEqual(report.num_changes, 1)
        self.assertEqual(report.details[type_id][repo_id]['details'], {})

    def test_unbind_all_failed(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = BindReport(repo_id)
        report.set_failed({'a': 1})
        handler.unbind = Mock(return_value=report)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        self.assertFalse(report.details[type_id][repo_id]['succeeded'])
        self.assertEqual(report.details[type_id][repo_id]['details'], {'a': 1})

    def test_unbind_all_raised(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.unbind = Mock(side_effect=ValueError)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [
            binding,
        ], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        self.assertFalse(report.details[type_id][repo_id]['succeeded'])
        self.assertTrue(
            report.details[type_id][repo_id]['details'].has_key('message'))
        self.assertTrue(
            report.details[type_id][repo_id]['details'].has_key('trace'))

    def test_clean(self):
        type_id = 'yum'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 1)
        self.assertEqual(report.details[type_id]['details'], {})

    def test_clean_failed(self):
        type_id = 'yum'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = CleanReport()
        report.set_failed({'a': 1})
        handler.clean = Mock(return_value=report)
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertEqual(report.details[type_id]['details'], {'a': 1})

    def test_clean_raised(self):
        type_id = 'yum'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.clean = Mock(side_effect=ValueError)
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertTrue(report.details[type_id]['details'].has_key('message'))
        self.assertTrue(report.details[type_id]['details'].has_key('trace'))
Beispiel #6
0
 def setUp(self):
     self.deployer = MockDeployer()
     self.deployer.deploy()
Beispiel #7
0
class TestDispatcher(unittest.TestCase):
    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()

    def container(self):
        return Container(MockDeployer.ROOT, MockDeployer.PATH)

    def test_install(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id="rpm", unit_key=dict(name="zsh"))
        units.append(unit)
        unit = dict(type_id="rpm", unit_key=dict(name="ksh"))
        units.append(unit)
        options = {}
        # Test
        report = dispatcher.install(units, options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 2)
        self.assertFalse(report.reboot["scheduled"])

    def test_install_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(type_id="rpm", unit_key=dict(name="zsh"))
        options = dict(reboot=True)
        # Test
        report = dispatcher.install([unit], options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)
        self.assertTrue(report.reboot["scheduled"])

    def test_install_failed_no_handler(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id="rpm", unit_key=dict(name="zsh"))
        units.append(unit)
        unit = dict(type_id="xxx", unit_key=dict(name="ksh"))
        units.append(unit)
        options = {}
        # Test
        report = dispatcher.install(units, options)
        pprint(report.dict())
        self.assertFalse(report.status)
        self.assertEquals(report.chgcnt, 1)
        # RPM passed
        rpm = report.details["rpm"]
        self.assertTrue(rpm["status"])
        # XXX failed
        xxx = report.details["xxx"]
        self.assertFalse(xxx["status"])

    def test_update(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(type_id="rpm", unit_key=dict(name="zsh"))
        units.append(unit)
        unit = dict(type_id="rpm", unit_key=dict(name="ksh"))
        units.append(unit)
        options = {}
        # Test
        report = dispatcher.update(units, options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 2)

    def test_uninstall(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(type_id="rpm", unit_key=dict(name="zsh"))
        options = {}
        # Test
        report = dispatcher.uninstall([unit], options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_profile(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        report = dispatcher.profile()
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 0)

    def test_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        report = dispatcher.reboot()
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 0)

    def test_bind(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        definition = dict(type_id="yum", repo={})
        report = dispatcher.bind([definition])
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_rebind(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        definition = dict(type_id="yum", repo={})
        report = dispatcher.rebind([definition])
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_unbind(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        report = dispatcher.unbind("repo-1")
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_clean(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        report = dispatcher.clean()
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)
class TestDispatcher(unittest.TestCase):

    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()

    def container(self):
        return Container(MockDeployer.CONF_D, [MockDeployer.PATH])

    def test_install(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 2)
        self.assertFalse(report.reboot['scheduled'])

    def test_install_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        report = ContentReport()
        report.set_failed({'a':1})
        handler.install = Mock(return_value=report)
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a':1})

    def test_install_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        handler.install = Mock(side_effect=ValueError())
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_install_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        options = dict(reboot=True)
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, [unit], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 2)
        self.assertTrue(report.reboot['scheduled'])

    def test_install_failed_no_handler(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='xxx',
            unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 1)
        # RPM passed
        rpm = report.details['rpm']
        self.assertTrue(rpm['succeeded'])
        # XXX failed
        xxx = report.details['xxx']
        self.assertFalse(xxx['succeeded'])

    def test_update(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 2)

    def test_update_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        report = ContentReport()
        report.set_failed({'a':1})
        handler.update = Mock(return_value=report)
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a':1})

    def test_update_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        handler.update = Mock(side_effect=ValueError())
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_uninstall(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, [unit], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 1)

    def test_uninstall_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        report = ContentReport()
        report.set_failed({'a':1})
        handler.uninstall = Mock(return_value=report)
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a':1})

    def test_uninstall_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        handler = dispatcher.container.find('rpm')
        handler.uninstall = Mock(side_effect=ValueError())
        # Test
        options = {}
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.reboot['scheduled'])
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_profile(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 0)

    def test_profile_failed(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find('rpm')
        report = ProfileReport()
        report.set_failed({'a':1})
        handler.profile = Mock(return_value=report)
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertEqual(report.details['rpm']['details'], {'a':1})

    def test_profile_raised(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find('rpm')
        handler.profile = Mock(side_effect=ValueError())
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        self.assertFalse(report.details['rpm']['succeeded'])
        self.assertTrue(report.details['rpm']['details'].has_key('message'))
        self.assertTrue(report.details['rpm']['details'].has_key('trace'))

    def test_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.reboot(conduit, {})
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 0)

    def test_bind(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id, details={})
        options = {}
        report = dispatcher.bind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEqual(report.num_changes, 1)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertTrue(details['succeeded'])
        self.assertEqual(details['details'], {})

    def test_bind_failed(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = BindReport(repo_id)
        report.set_failed({'a':1})
        handler.bind = Mock(return_value=report)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id, details={})
        options = {}
        report = dispatcher.bind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertFalse(details['succeeded'])
        self.assertEqual(details['details'], {'a':1})

    def test_bind_raised(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.bind = Mock(side_effect=ValueError())
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id, details={})
        options = {}
        report = dispatcher.bind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertTrue(details['details'].has_key('message'))
        self.assertTrue(details['details'].has_key('trace'))

    def test_unbind(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEqual(report.num_changes, 1)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertEqual(details['details'], {})

    def test_unbind_failed(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = BindReport(repo_id)
        report.set_failed({'a':1})
        handler.unbind = Mock(return_value=report)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertFalse(details['succeeded'])
        self.assertEqual(details['details'], {'a':1})

    def test_unbind_raised(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.unbind = Mock(side_effect=ValueError)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertFalse(details['succeeded'])
        self.assertTrue(details['details'].has_key('message'))
        self.assertTrue(details['details'].has_key('trace'))

    def test_unbind_all(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        binding = dict(type_id=None, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEqual(report.num_changes, 1)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertEqual(details['details'], {})

    def test_unbind_all_failed(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = BindReport(repo_id)
        report.set_failed({'a':1})
        handler.unbind = Mock(return_value=report)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertFalse(details['succeeded'])
        self.assertEqual(details['details'], {'a':1})

    def test_unbind_all_raised(self):
        type_id = 'yum'
        repo_id = 'repo-1'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.unbind = Mock(side_effect=ValueError)
        # Test
        conduit = Conduit()
        binding = dict(type_id=type_id, repo_id=repo_id)
        options = {}
        report = dispatcher.unbind(conduit, [binding,], options)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEqual(report.num_changes, 0)
        details = report.details[type_id][0]
        self.assertEqual(details['repo_id'], repo_id)
        self.assertFalse(details['succeeded'])
        self.assertTrue(details['details'].has_key('message'))
        self.assertTrue(details['details'].has_key('trace'))

    def test_clean(self):
        type_id = 'yum'
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertTrue(report.succeeded)
        self.assertEquals(report.num_changes, 1)
        details = report.details[type_id]
        self.assertEqual(details['details'], {})

    def test_clean_failed(self):
        type_id = 'yum'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        report = CleanReport()
        report.set_failed({'a':1})
        handler.clean = Mock(return_value=report)
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        details = report.details[type_id]
        self.assertEqual(details['details'], {'a':1})

    def test_clean_raised(self):
        type_id = 'yum'
        # Setup
        dispatcher = Dispatcher(self.container())
        handler = dispatcher.container.find(type_id, BIND)
        handler.clean = Mock(side_effect=ValueError)
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertFalse(report.succeeded)
        self.assertEquals(report.num_changes, 0)
        details = report.details[type_id]
        self.assertTrue(details['details'].has_key('message'))
        self.assertTrue(details['details'].has_key('trace'))
class TestDispatcher(unittest.TestCase):

    def setUp(self):
        self.deployer = MockDeployer()
        self.deployer.deploy()

    def tearDown(self):
        self.deployer.clean()

    def container(self):
        return Container(MockDeployer.ROOT, MockDeployer.PATH)

    def test_install(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 2)
        self.assertFalse(report.reboot['scheduled'])

    def test_install_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        options = dict(reboot=True)
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, [unit], options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 2)
        self.assertTrue(report.reboot['scheduled'])

    def test_install_failed_no_handler(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='xxx',
            unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.install(conduit, units, options)
        pprint(report.dict())
        self.assertFalse(report.status)
        self.assertEquals(report.chgcnt, 1)
        # RPM passed
        rpm = report.details['rpm']
        self.assertTrue(rpm['status'])
        # XXX failed
        xxx = report.details['xxx']
        self.assertFalse(xxx['status'])

    def test_update(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        units = []
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        units.append(unit)
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='ksh'))
        units.append(unit)
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.update(conduit, units, options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 2)

    def test_uninstall(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        unit = dict(
            type_id='rpm',
            unit_key=dict(name='zsh'))
        options = {}
        # Test
        conduit = Conduit()
        report = dispatcher.uninstall(conduit, [unit], options)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_profile(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.profile(conduit)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 0)

    def test_reboot(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.reboot(conduit, {})
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 0)

    def test_bind(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        definition = dict(type_id='yum', repo={})
        report = dispatcher.bind(conduit, [definition,])
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_rebind(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        definition = dict(type_id='yum', repo={})
        report = dispatcher.rebind(conduit, [definition,])
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_unbind(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.unbind(conduit, 'repo-1')
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)

    def test_clean(self):
        # Setup
        dispatcher = Dispatcher(self.container())
        # Test
        conduit = Conduit()
        report = dispatcher.clean(conduit)
        pprint(report.dict())
        self.assertTrue(report.status)
        self.assertEquals(report.chgcnt, 1)