Пример #1
0
    def test_controller_with_terminal_renderer(self):
        model = CephSaltModel(None)
        renderer = TerminalRenderer(model)
        controller = CephSaltController(model, renderer)

        controller.begin()
        time.sleep(0.2)
        controller.handle_begin_stage(
            begin_stage('node1.ceph.com', 'Stage 1', 49))
        time.sleep(0.2)
        controller.handle_begin_stage(
            begin_stage('node2.ceph.com', 'Stage 1', 50))
        time.sleep(0.2)
        controller.handle_begin_step(begin_step('node1.ceph.com', 'Step 1',
                                                51))
        time.sleep(0.2)
        controller.handle_end_step(end_step('node1.ceph.com', 'Step 1', 52))
        time.sleep(0.2)
        controller.handle_begin_step(begin_step('node2.ceph.com', 'Step 2',
                                                53))
        time.sleep(0.2)
        controller.handle_end_step(end_step('node2.ceph.com', 'Step 2', 54))
        time.sleep(0.2)
        controller.handle_end_stage(end_stage('node2.ceph.com', 'Stage 1', 55))
        time.sleep(0.2)
        controller.handle_end_stage(end_stage('node1.ceph.com', 'Stage 1', 56))

        tstamp1 = datetime.datetime.strptime('2020-01-17T15:19:58.819390',
                                             "%Y-%m-%dT%H:%M:%S.%f")
        tstamp2 = datetime.datetime.strptime('2020-01-17T15:19:59.819390',
                                             "%Y-%m-%dT%H:%M:%S.%f")
        controller.minion_finished('node1.ceph.com', tstamp2, False)
        controller.minion_finished('node2.ceph.com', tstamp1, False)

        time.sleep(0.2)
        controller.minion_failure(
            'node2.ceph.com',
            Event('begin_step', 'Step 2', Event('begin_stage', 'Stage 1')),
            failure())
        time.sleep(0.2)
        controller.minion_failure(
            'node1.ceph.com',
            Event('end_step', 'Step 1', Event('begin_stage', 'Stage 1')),
            failure())

        time.sleep(0.2)
        controller.end()

        self.assertEqual(len(model.minions_list()), 2)
        self.assertIsNotNone(model.begin_time)
        self.assertTrue(model.finished())
        self.assertEqual(model.minions_finished(), 2)
        self.assertEqual(model.minions_total(), 2)
        self.assertEqual(model.minions_failed(), 2)
        self.assertEqual(model.minions_succeeded(), 0)

        node1 = model.get_minion('node1.ceph.com')
        self.assertTrue(node1.finished())
        self.assertEqual(len(node1.stages), 1)
        self.assertFalse(node1.success)
        stage = node1.last_stage
        self.assertEqual(stage.desc, 'Stage 1')
        self.assertEqual(len(stage.steps), 2)
        self.assertFalse(stage.success)
        steps = list(stage.steps.values())
        self.assertEqual(steps[0].desc, 'Step 1')
        self.assertTrue(steps[0].finished())
        self.assertTrue(steps[0].success)
        self.assertIsInstance(steps[1], dict)
        self.assertEqual(
            steps[1]['state'],
            'file_|-/etc/chrony.conf_|-/etc/chrony.conf_|-managed')

        node2 = model.get_minion('node2.ceph.com')
        self.assertTrue(node2.finished())
        self.assertEqual(len(node2.stages), 1)
        self.assertFalse(node2.success)
        stage = node2.last_stage
        self.assertEqual(stage.desc, 'Stage 1')
        self.assertEqual(len(stage.steps), 1)
        self.assertFalse(stage.success)
        step = stage.last_step
        self.assertEqual(step.desc, 'Step 2')
        self.assertTrue(step.finished())
        self.assertFalse(step.success)
        self.assertIsNotNone(step.failure)
        self.assertEqual(
            step.failure['state'],
            'file_|-/etc/chrony.conf_|-/etc/chrony.conf_|-managed')
Пример #2
0
    def test_controller_with_curses_renderer(self, initscr, newpad, *args):
        stdscr = mock.MagicMock()
        initscr.return_value = stdscr
        stdscr.getmaxyx = mock.MagicMock(return_value=(10, 80))

        class FakeGetCh:
            def __init__(self):
                self.next_char = None

            def __call__(self):
                time.sleep(0.2)
                cha = -1 if self.next_char is None else self.next_char
                self.next_char = None
                return cha

        fake_getch = FakeGetCh()
        stdscr.getch = mock.MagicMock(side_effect=fake_getch)

        body = mock.MagicMock()
        newpad.return_value = body

        class Addstr:
            def __init__(self):
                self.current_row = 0

            def __call__(self, row, *args):
                self.current_row = row

            def getyx(self):
                logger.info("return body current pos: %s, 80",
                            self.current_row)
                return self.current_row, 80

        addstr = Addstr()
        body.addstr = mock.MagicMock(side_effect=addstr)
        body.getyx = mock.MagicMock(side_effect=addstr.getyx)

        model = CephSaltModel(None)
        renderer = CursesRenderer(model)
        controller = CephSaltController(model, renderer)

        class ExecutionThread(threading.Thread):
            def run(self):
                while not renderer.running:
                    time.sleep(0.2)
                logger.info("starting injecting salt events in controller")

                controller.begin()
                time.sleep(0.3)
                controller.handle_begin_stage(
                    begin_stage('node1.ceph.com', 'Stage 1', 49))
                time.sleep(0.3)
                controller.handle_begin_stage(
                    begin_stage('node2.ceph.com', 'Stage 1', 50))
                time.sleep(0.3)
                controller.handle_begin_step(
                    begin_step('node1.ceph.com', 'Step 1', 51))
                time.sleep(0.3)
                controller.handle_end_step(
                    end_step('node1.ceph.com', 'Step 1', 52))
                time.sleep(0.3)
                controller.handle_begin_step(
                    begin_step('node2.ceph.com', 'Step 2', 53))
                fake_getch.next_char = ord('c')
                time.sleep(0.3)
                controller.handle_end_step(
                    end_step('node2.ceph.com', 'Step 2', 54))
                fake_getch.next_char = ord('j')
                time.sleep(0.3)
                fake_getch.next_char = ord('j')
                controller.handle_end_stage(
                    end_stage('node2.ceph.com', 'Stage 1', 55))
                time.sleep(0.3)
                fake_getch.next_char = ord('j')
                controller.handle_end_stage(
                    end_stage('node1.ceph.com', 'Stage 1', 56))

                tstamp1 = datetime.datetime.strptime(
                    '2020-01-17T15:19:58.819390', "%Y-%m-%dT%H:%M:%S.%f")
                tstamp2 = datetime.datetime.strptime(
                    '2020-01-17T15:19:59.819390', "%Y-%m-%dT%H:%M:%S.%f")
                controller.minion_finished('node1.ceph.com', tstamp2, False)
                controller.minion_finished('node2.ceph.com', tstamp1, False)

                time.sleep(0.3)
                controller.minion_failure(
                    'node2.ceph.com',
                    Event('begin_step', 'Step 2',
                          Event('begin_stage', 'Stage 1')), failure())
                time.sleep(0.3)
                controller.minion_failure(
                    'node1.ceph.com',
                    Event('end_step', 'Step 1', Event('begin_stage',
                                                      'Stage 1')), failure())

                time.sleep(0.3)
                controller.end()
                fake_getch.next_char = ord('q')

        exec_thread = ExecutionThread()
        exec_thread.setDaemon(True)
        exec_thread.start()

        renderer.run()

        self.assertEqual(len(model.minions_list()), 2)
        self.assertIsNotNone(model.begin_time)
        self.assertTrue(model.finished())
        self.assertEqual(model.minions_finished(), 2)
        self.assertEqual(model.minions_total(), 2)
        self.assertEqual(model.minions_failed(), 2)
        self.assertEqual(model.minions_succeeded(), 0)

        node1 = model.get_minion('node1.ceph.com')
        self.assertTrue(node1.finished())
        self.assertEqual(len(node1.stages), 1)
        self.assertFalse(node1.success)
        stage = node1.last_stage
        self.assertEqual(stage.desc, 'Stage 1')
        self.assertEqual(len(stage.steps), 2)
        self.assertFalse(stage.success)
        steps = list(stage.steps.values())
        self.assertEqual(steps[0].desc, 'Step 1')
        self.assertTrue(steps[0].finished())
        self.assertTrue(steps[0].success)
        self.assertIsInstance(steps[1], dict)
        self.assertEqual(
            steps[1]['state'],
            'file_|-/etc/chrony.conf_|-/etc/chrony.conf_|-managed')

        node2 = model.get_minion('node2.ceph.com')
        self.assertTrue(node2.finished())
        self.assertEqual(len(node2.stages), 1)
        self.assertFalse(node2.success)
        stage = node2.last_stage
        self.assertEqual(stage.desc, 'Stage 1')
        self.assertEqual(len(stage.steps), 1)
        self.assertFalse(stage.success)
        step = stage.last_step
        self.assertEqual(step.desc, 'Step 2')
        self.assertTrue(step.finished())
        self.assertFalse(step.success)
        self.assertIsNotNone(step.failure)
        self.assertEqual(
            step.failure['state'],
            'file_|-/etc/chrony.conf_|-/etc/chrony.conf_|-managed')