Exemplo n.º 1
0
    def _create_mock_executor(self, ap_id, ap_console):
        """Create a mocked out :class:`APExecutorBase` instance.

        The :meth:`activate_cli_console` method will have a mock installed"""
        executor = APExecutorBase(ap_id, ap_console)
        executor.activate_cli_console = MagicMock(name='activate_cli_console')
        return executor
Exemplo n.º 2
0
    def test_active_executor_cli_console(self):
        """Verify CLI console is executed in :class:`APActiveExecutor`."""
        console = self._create_mock_console()
        executor = APActiveExecutor('ap1', console)
        executor.activate()

        cli_console = self._create_mock_console()
        cli_cmd = ["fake cli command", "another cli cmd"]

        # Case 1: Before set cli_console, run_cli_cmd does nothing
        cmd = APExecutorBase.DumpInitialStateCmd(executor, cli_cmd)
        executor.run_async_cmd(cmd)

        time.sleep(1)
        self.assert_(not cli_console.connect.called)
        self.assert_(not cli_console.disconnect.called)
        self.assert_(not cli_console.run_cmd.called)

        # Case 2: Set cli_console, verify cli_cmd is executed
        executor.set_cli_console(cli_console)
        cmd = APExecutorBase.DumpInitialStateCmd(executor, cli_cmd)
        executor.run_async_cmd(cmd)

        time.sleep(1)
        calls = cli_console.run_cmd.call_args_list
        self.assertEquals(len(cli_cmd), len(calls))
        for i in xrange(len(cli_cmd)):
            self.assertEquals(cli_cmd[i], calls[i][0][0])

        executor.shutdown()
        console.shutdown.assert_called_once_with()
        cli_console.shutdown.assert_called_once_with()
Exemplo n.º 3
0
 def _create_mock_executor(self, ap_id):
     """Create an executor with the necessary methods mocked."""
     executor = APExecutorBase(ap_id, None)
     executor.activate = MagicMock(name='activate')
     executor.run_async_cmd = MagicMock(name='run_async_cmd')
     executor.shutdown = MagicMock(name='shutdown')
     return executor
Exemplo n.º 4
0
    def test_active_executor_diaglog_handler(self):
        """Verify diagnostic logging handler are executed in :class:`APActiveExecutor`."""
        console = self._create_mock_console()
        diaglog_handler = self._create_mock_diaglog_handler('model', 'ap1')

        # Case 1: Executor activates with condition monitor mode enabled,
        #         should not start diaglog handler
        executor = APActiveExecutor('ap1', console)
        executor.add_diaglog_handler(diaglog_handler)
        executor.activate()
        self.assert_(not diaglog_handler.start.called)
        executor.shutdown()
        diaglog_handler.shutdown.assert_called_once_with(None)
        diaglog_handler.shutdown.reset_mock()
        console.shutdown.assert_called_once_with()
        console.shutdown.reset_mock()

        # Case 2: Executor activates with condition monitor mode for async
        #         events, should start diaglog handler
        executor = APActiveExecutor(
            'ap1', console, mode=ConditionMonitorBase.Mode.EVENT_DRIVEN)
        executor.add_diaglog_handler(diaglog_handler)
        executor.activate()
        self.assert_(diaglog_handler.start.called)
        diaglog_handler.start.reset_mock()

        # Case 3: When condition monitor mode is switched to polled, shutdown
        #         diaglog handler
        cmd = APExecutorBase.SetConditionMonitorMode(
            executor, ConditionMonitorBase.Mode.POLLED)
        executor.run_async_cmd(cmd)
        time.sleep(1)
        diaglog_handler.shutdown.assert_called_once_with(None)
        diaglog_handler.shutdown.reset_mock()

        # Case 4: When condition monitor mode is switched to event driven,
        #         start diaglog handler
        cmd = APExecutorBase.SetConditionMonitorMode(
            executor, ConditionMonitorBase.Mode.EVENT_DRIVEN)
        executor.run_async_cmd(cmd)
        time.sleep(1)
        self.assert_(diaglog_handler.start.called)
        diaglog_handler.start.reset_mock()

        executor.shutdown()
        diaglog_handler.shutdown.assert_called_once_with(None)
        diaglog_handler.shutdown.reset_mock()
        console.shutdown.assert_called_once_with()
        console.shutdown.reset_mock()
Exemplo n.º 5
0
 def test_base(self):
     """Verify the proper functioning of :class:`APExecutorBase`."""
     console = APConsoleBase('dev2')
     executor = APExecutorBase('ap1', console)
     self.assertRaises(NotImplementedError, executor.add_condition_monitor,
                       None)
     self.assertRaises(NotImplementedError, executor.add_diaglog_handler,
                       None)
     self.assertRaises(NotImplementedError, executor.run_async_cmd, None)
     self.assertRaises(NotImplementedError, executor.set_cli_console, None)
Exemplo n.º 6
0
    def test_executor_db(self):
        """Test basic functionality of :class:`APExecutorDB`."""
        db = APExecutorDB()

        ap_id1 = 'dev1'
        exec1 = APExecutorBase(ap_id1, self._create_mock_console())
        db.add_executor(exec1)
        self.assertEquals(1, len(db))
        self.assertEquals(exec1, db.get_executor(ap_id1))
        self.assertEquals(None, db.get_executor('devX'))

        ap_id2 = 'dev2'
        exec2 = APExecutorBase(ap_id2, self._create_mock_console())
        db.add_executor(exec2)
        self.assertEquals(2, len(db))
        self.assertEquals(exec1, db.get_executor(ap_id1))
        self.assertEquals(exec2, db.get_executor(ap_id2))
        self.assertEquals(None, db.get_executor('devX'))

        # Verify duplicate registrations are rejected.
        self.assertRaises(ValueError, db.add_executor, exec1)
Exemplo n.º 7
0
    def test_active_executor_condition_monitor_mode(self):
        """Verify condition monitors during mode switches.

        Condition monitors operating in polled mode should not be told
        to sample conditions if the executor is operating in event
        driven mode. However, ones that are active in only event driven
        mode or in both modes should be sampled.
        When mode switches occur, condition monitors should be reset and
        then be asked for sampling times again.
        """
        console = self._create_mock_console()
        executor = APActiveExecutor('ap1', console)

        monitor1 = TestAPExecutor.RecordingMonitor(0.500)
        monitor2 = TestAPExecutor.RecordingMonitor(0.200)
        monitor3 = TestAPExecutor.RecordingMonitor(
            0.600, ConditionMonitorBase.Mode.EVENT_DRIVEN)
        monitor4 = TestAPExecutor.RecordingMonitor(
            0.400, ConditionMonitorBase.Mode.BOTH)

        executor.add_condition_monitor(monitor1)
        executor.add_condition_monitor(monitor2)
        executor.add_condition_monitor(monitor3)
        executor.add_condition_monitor(monitor4)

        executor.activate()

        # Case 1: Let some time elapse and then switch into event driven mode.
        time.sleep(2.1)

        # Before switching to event driven mode, verify call times.
        mon1_call_times = monitor1.get_call_times()
        mon2_call_times = monitor2.get_call_times()
        mon3_call_times = monitor3.get_call_times()
        mon4_call_times = monitor4.get_call_times()
        self._verify_call_deltas(mon1_call_times, 0.500, 4)
        self._verify_call_deltas(mon2_call_times, 0.200, 10)
        self._verify_call_deltas(mon4_call_times, 0.400, 5)
        # No sample should be done for monitor 3 due to mode not match
        self.assert_(0 == len(mon3_call_times))

        # Switch to event driven mode
        cmd = APExecutorBase.SetConditionMonitorMode(
            executor, ConditionMonitorBase.Mode.EVENT_DRIVEN)
        executor.run_async_cmd(cmd)

        time.sleep(1)

        # The event driven monitors will be reset
        self.assert_(monitor3.reset_called)
        self.assert_(not monitor4.reset_called)

        monitor3.clear_state()

        # Case 2: Switch back to polling mode. The monitors that operate
        #         in polled mode and both modes will be sampled. The
        #         polled ones will also get reset. The event driven
        #         ones will not be sampled (as the assumption is that
        #         the polling logic will be sufficient to grab any
        #         information needed).
        cmd = APExecutorBase.SetConditionMonitorMode(
            executor, ConditionMonitorBase.Mode.POLLED)
        executor.run_async_cmd(cmd)

        time.sleep(0.9)

        executor.shutdown()
        console.shutdown.assert_called_once_with()

        self.assert_(monitor1.reset_called)
        self.assert_(monitor2.reset_called)
        self.assert_(not monitor3.reset_called)
        self.assert_(not monitor4.reset_called)

        self._verify_call_deltas(monitor1.get_call_times(), 0.500, 2)
        self._verify_call_deltas(monitor2.get_call_times(), 0.200, 5)
        self.assertEquals(0, len(monitor3.get_call_times()))
        self._verify_call_deltas(monitor4.get_call_times(), 0.400, 7)
Exemplo n.º 8
0
    def test_controller(self):
        """Test :class:`Controller`."""
        # Note that the values below need to be kept in sync with this sample
        # config file.
        test_dir = os.path.dirname(__file__)
        device_db = config.create_device_db_from_yaml(
            os.path.join(test_dir, '..', 'model', 'sample_config.yaml'))
        self.assert_(device_db)

        model = self._create_mock_model(device_db)

        controller = Controller(model)
        self.assertIsNotNone(controller.ap_executor_db)

        # Add some mocked out executors to confirm that they are called
        # in the activate/shutdown paths as well as when changing whether
        # band steering is enabled or not.
        executor1 = self._create_mock_executor('ap1')
        executor2 = self._create_mock_executor('ap2')

        controller.ap_executor_db.add_executor(executor1)
        controller.ap_executor_db.add_executor(executor2)

        controller.activate()

        self.assert_(executor1.activate.called)
        self.assert_(not executor1.shutdown.called)
        self.assert_(executor2.activate.called)
        self.assert_(not executor2.shutdown.called)

        executor1.activate.reset_mock()
        executor2.activate.reset_mock()

        # Verify that when band steering is enabled, two commands are
        # enqueued to each executor. The first will actually enable band
        # steering and the second disables the condition monitors.
        #
        # Also confirm that the command notifier resets the model once
        # the condition monitor mode changes are done.
        controller.enable_bsteering(True)
        self.assert_(executor1.run_async_cmd.called)
        self.assert_(executor2.run_async_cmd.called)

        self.assert_(not model.reset_associated_stations.called)
        self.assert_(not model.reset_utilizations.called)
        self.assert_(not model.reset_steering_blackout_status.called)
        self.assert_(not model.reset_overload_status.called)

        calls = executor1.run_async_cmd.call_args_list
        self.assertEquals(((APExecutorBase.SetConditionMonitorMode(
            executor1, ConditionMonitorBase.Mode.EVENT_DRIVEN), ), ), calls[0])
        self.assertEquals(((EnableBSteeringCmd(True, executor1), ), ),
                          calls[1])

        cmd = calls[0][0][0]
        cmd._callback.notify_cmd_complete(cmd)
        self.assert_(not model.reset_associated_stations.called)
        self.assert_(not model.reset_utilizations.called)
        self.assert_(not model.reset_steering_blackout_status.called)
        self.assert_(not model.reset_overload_status.called)

        calls = executor2.run_async_cmd.call_args_list
        self.assertEquals(((APExecutorBase.SetConditionMonitorMode(
            executor2, ConditionMonitorBase.Mode.EVENT_DRIVEN), ), ), calls[0])
        self.assertEquals(((EnableBSteeringCmd(True, executor2), ), ),
                          calls[1])

        # Completes both commands, so the model resets can occur.
        cmd = calls[0][0][0]
        cmd._callback.notify_cmd_complete(cmd)
        self.assert_(model.reset_associated_stations.called)
        self.assert_(model.reset_utilizations.called)
        self.assert_(model.reset_steering_blackout_status.called)
        self.assert_(model.reset_overload_status.called)

        executor1.run_async_cmd.reset_mock()
        executor2.run_async_cmd.reset_mock()
        model.reset_associated_stations.reset_mock()
        model.reset_utilizations.reset_mock()
        model.reset_steering_blackout_status.reset_mock()
        model.reset_overload_status.reset_mock()

        # The opposite is true when band steering is disabled. The first
        # command disables band steering and the second enables the condition
        # monitors.
        controller.enable_bsteering(False)
        self.assert_(executor1.run_async_cmd.called)
        self.assert_(executor2.run_async_cmd.called)

        self.assert_(not model.reset_associated_stations.called)
        self.assert_(not model.reset_utilizations.called)
        self.assert_(not model.reset_steering_blackout_status.called)
        self.assert_(not model.reset_overload_status.called)

        calls = executor1.run_async_cmd.call_args_list
        self.assertEquals(((EnableBSteeringCmd(False, executor1), ), ),
                          calls[0])
        self.assertEquals(((APExecutorBase.SetConditionMonitorMode(
            executor1, ConditionMonitorBase.Mode.POLLED), ), ), calls[1])

        cmd = calls[1][0][0]
        cmd._callback.notify_cmd_complete(cmd)
        self.assert_(not model.reset_associated_stations.called)
        self.assert_(not model.reset_utilizations.called)
        self.assert_(not model.reset_steering_blackout_status.called)
        self.assert_(not model.reset_overload_status.called)

        calls = executor2.run_async_cmd.call_args_list
        self.assertEquals(((EnableBSteeringCmd(False, executor2), ), ),
                          calls[0])
        self.assertEquals(((APExecutorBase.SetConditionMonitorMode(
            executor2, ConditionMonitorBase.Mode.POLLED), ), ), calls[1])

        executor1.run_async_cmd.reset_mock()
        executor2.run_async_cmd.reset_mock()

        # If enable band steering when it is already enabled, execute
        # SetConditionMonitorMode command and DumpInitialStateCmd command.
        # This is what happens on startup if LBD is already running.
        controller.enable_bsteering(True, already_enabled=True)
        self.assert_(executor1.run_async_cmd.called)
        self.assert_(executor2.run_async_cmd.called)

        self.assert_(not model.reset_associated_stations.called)
        self.assert_(not model.reset_utilizations.called)
        self.assert_(not model.reset_steering_blackout_status.called)
        self.assert_(not model.reset_overload_status.called)

        calls = executor1.run_async_cmd.call_args_list
        self.assertEquals(1, len(calls))
        self.assertEquals(((APExecutorBase.SetConditionMonitorMode(
            executor1, ConditionMonitorBase.Mode.EVENT_DRIVEN), ), ), calls[0])
        executor1.run_async_cmd.reset_mock()

        cmd = calls[0][0][0]
        cmd._callback.notify_cmd_complete(cmd)
        self.assert_(not model.reset_associated_stations.called)
        self.assert_(not model.reset_utilizations.called)
        self.assert_(not model.reset_steering_blackout_status.called)
        self.assert_(not model.reset_overload_status.called)

        calls = executor2.run_async_cmd.call_args_list
        self.assertEquals(1, len(calls))
        self.assertEquals(((APExecutorBase.SetConditionMonitorMode(
            executor2, ConditionMonitorBase.Mode.EVENT_DRIVEN), ), ), calls[0])
        executor2.run_async_cmd.reset_mock()

        # Completes both commands, so the model resets can occur, and
        # will dump all associated STAs
        cmd = calls[0][0][0]
        cmd._callback.notify_cmd_complete(cmd)
        self.assert_(model.reset_associated_stations.called)
        self.assert_(model.reset_utilizations.called)
        self.assert_(model.reset_overload_status.called)
        self.assert_(model.reset_steering_blackout_status.called)

        executor1.run_async_cmd.assert_called_once_with(
            APExecutorBase.DumpInitialStateCmd(executor1,
                                               DUMP_INIT_STATE_CMDS))
        executor2.run_async_cmd.assert_called_once_with(
            APExecutorBase.DumpInitialStateCmd(executor2,
                                               DUMP_INIT_STATE_CMDS))

        controller.shutdown()

        self.assert_(not executor1.activate.called)
        self.assert_(executor1.shutdown.called)
        self.assert_(not executor2.activate.called)
        self.assert_(executor2.shutdown.called)