class TestMain(unittest.TestCase): """ Main function tests """ @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('signal.pause') def test_main_function(self, m_pause): """ Test cli.serial_redirection main function Run and simulate 'stop' with a Ctrl+C """ m_pause.side_effect = KeyboardInterrupt() args = ['serial_redirection.py'] with mock.patch('sys.argv', args): serial_redirection.main() self.assertTrue(m_pause.called) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('a8', linux_open_node_type='a8_m3')) @mock.patch('signal.pause') def test_open_linux_node(self, m_pause): """ Test open Linux node cli.serial_redirection Run and simulate 'stop' with a Ctrl+C """ m_pause.side_effect = KeyboardInterrupt() args = ['serial_redirection.py'] with mock.patch('sys.argv', args): serial_redirection.main() self.assertTrue(m_pause.called) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) def test_signal_handling(self): # pylint: disable=no-self-use """ Test signal handling """ pid = os.getpid() def trigger_signal(): """ trigger sigterm signal """ time.sleep(2) os.kill(pid, signal.SIGTERM) thread = threading.Thread(target=trigger_signal) thread.daemon = True thread.start() args = ['serial_redirection.py'] with mock.patch('sys.argv', args): serial_redirection.main()
class TestControlNodeSerial(unittest.TestCase): def setUp(self): self.popen_patcher = mock.patch( 'gateway_code.utils.subprocess_timeout.Popen') popen_class = self.popen_patcher.start() self.popen = popen_class.return_value self.popen.terminate.side_effect = self._terminate self.popen.poll.return_value = None self.readline_ret_vals = Queue.Queue(0) self.popen.stderr.readline.side_effect = self.readline_ret_vals.get self.readline_ret_vals.put('cn_serial_ready\n') self.cn = cn_interface.ControlNodeSerial('tty') self.log_error = LogCapture('gateway_code', level=logging.WARNING) def tearDown(self): self.cn.stop() mock.patch.stopall() self.log_error.uninstall() def _terminate(self): self.readline_ret_vals.put('') def test_normal_start_stop(self): ret_start = self.cn.start() self.assertEquals(0, ret_start) self.assertTrue(self.popen.stderr.readline.called) self.cn.stop() self.assertTrue(self.popen.terminate.called) self.assertTrue(self.readline_ret_vals.empty()) def test_start_error_in_cn_serial(self): # poll should return an error self.popen.poll.return_value = 2 ret_start = self.cn.start() self.assertNotEquals(0, ret_start) self.log_error.check( ('gateway_code', 'ERROR', 'Control node serial reader thread ended prematurely')) self.cn.stop() def test_stop_before_start(self): self.cn.stop() def test_stop_with_cn_interface_allready_stopped(self): # Simulate cn_interface stopped self.readline_ret_vals.put('') self.popen.stdin.write.side_effect = IOError() self.popen.terminate.side_effect = OSError() self.cn.start() # try sending command ret = self.cn.send_command(['test', 'cmd']) self.assertEquals(None, ret) self.log_error.check(('gateway_code', 'ERROR', 'control_node_serial process is terminated')) self.log_error.clear() self.cn.stop() self.log_error.check(('gateway_code', 'ERROR', 'Control node process already terminated')) def test_stop_terminate_failed(self): """Stop cn_interface but terminate does not stop it.""" # terminate does not stop process self.popen.terminate.side_effect = None timeout_expired = cn_interface.subprocess_timeout.TimeoutExpired self.popen.wait.side_effect = timeout_expired('cn_serial_interface', 3) # kill does it self.popen.kill.side_effect = self._terminate self.cn.start() self.cn.stop() self.assertTrue(self.popen.kill.called) self.log_error.check(('gateway_code', 'WARNING', 'Control node serial not terminated, kill it')) # Test command sending def test_send_command(self): self.popen.stdin.write.side_effect = \ (lambda *x: self.readline_ret_vals.put('start ACK\n')) self.cn.start() ret = self.cn.send_command(['start', 'DC']) self.assertEquals(['start', 'ACK'], ret) self.cn.stop() def test_send_command_no_answer(self): self.cn.start() ret = self.cn.send_command(['start', 'DC']) self.assertIsNone(ret) self.cn.stop() def test_send_command_cn_interface_stoped(self): ret = self.cn.send_command(['lala']) self.assertIsNone(ret) def test_answer_and_answer_with_queue_full(self): # get two answers without sending command self.readline_ret_vals.put('set ACK\n') self.readline_ret_vals.put('start ACK\n') self.cn.start() self.cn.stop() self.log_error.check( ('gateway_code', 'ERROR', 'Control node answer queue full: %r' % ['start', 'ACK'])) # _cn_interface_args def test__cn_interface_args(self): args = self.cn._cn_interface_args() self.assertIn(self.cn.tty, args) self.assertNotIn('-c', args) self.assertNotIn('-d', args) # OML config args = self.cn._cn_interface_args('<omlc></omlc>') self.assertIn('-c', args) self.assertNotIn('-d', args) self.cn._oml_cfg_file.close() # Debug mode self.cn.measures_debug = (lambda x: None) args = self.cn._cn_interface_args() self.assertNotIn('-c', args) self.assertIn('-d', args) # _config_oml coverage tests def test_empty_config_oml(self): # No experiment description ret = self.cn._oml_config_file(None) self.assertIsNone(ret) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) def test_config_oml(self): oml_xml_cfg = '''<omlc id='{node_id}' exp_id='{exp_id}'>\n</omlc>''' self.cn.start(oml_xml_cfg) self.assertIsNotNone(self.cn._oml_cfg_file) self.cn.stop() def test_oml_xml_config(self): exp_files = { 'consumption': '/tmp/consumption', 'radio': '/tmp/radio', 'event': '/tmp/event', 'sniffer': '/tmp/sniffer', 'log': '/tmp/log', } oml_xml_cfg = self.cn.oml_xml_config('m3-1', '1234', exp_files) self.assertIsNotNone(oml_xml_cfg) self.assertTrue(oml_xml_cfg.startswith('<omlc')) # No output if none or empty oml_xml_cfg = self.cn.oml_xml_config('m3-1', '1234', None) self.assertIsNone(oml_xml_cfg) oml_xml_cfg = self.cn.oml_xml_config('m3-1', '1234', {}) self.assertIsNone(oml_xml_cfg)
class TestsProgrammer(unittest.TestCase): """ Test programmer """ @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.subprocess_timeout.call') def test_openocd_method(self, call_mock): """ Test openocd method """ call_mock.return_value = 0 args = ['programmer.py', '/dev/null'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 0) args += ['-q'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 0) args2 = ['programmer.py'] with mock.patch('sys.argv', args2): ret = reset() self.assertEqual(ret, 0) args2 += ['-q'] with mock.patch('sys.argv', args2): ret = reset() self.assertEqual(ret, 0) args3 = ['programmer.py', '/path/to/firmware'] with mock.patch('sys.argv', args3): ret = flash() self.assertEqual(ret, 1) call_mock.return_value = 42 with mock.patch('sys.argv', args2): ret = reset() self.assertEqual(ret, 42) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.subprocess_timeout.call') @mock.patch.dict(os.environ, {'FW': 'xxx'}) def test_env_flash_failed(self, call_mock): """ Test env flash failed""" args = ['programmer.py'] call_mock.return_value = 0 with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 1) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.subprocess_timeout.call') def test_flash_failed(self, call_mock): """ Test flash failed""" args = ['programmer.py'] call_mock.return_value = 1 with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, -2) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('a8')) def test_no_flash(self): """ Test flash failed""" args = ['programmer.py', '/dev/null'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, -1) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.subprocess_timeout.call') @mock.patch.dict(os.environ, {'FW': 'autotest'}) def test_autotest_flash(self, call_mock): """ Test autotest flash """ args = ['programmer.py'] call_mock.return_value = 0 with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.subprocess_timeout.call') @mock.patch.dict(os.environ, {'FW': 'idle'}) def test_idle_flash(self, call_mock): """ Test idle flash """ call_mock.return_value = 0 args = ['programmer.py'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 0) args2 = ['programmer.py', '/dev/null'] call_mock.return_value = 0 with mock.patch('sys.argv', args2): ret = flash() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.openocd.OpenOCD._openocd_args') @mock.patch("signal.pause") def test_openocd_debug(self, pause_mock, ocd_mock): """ Test openocd debug """ args = ['programmer.py'] ocd_mock.return_value = {'args': ['sleep', '2']} pause_mock.side_effect = KeyboardInterrupt with mock.patch('sys.argv', args): ret = debug() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3')) @mock.patch('gateway_code.utils.openocd.OpenOCD.debug_start') def test_openocd_debug_failed(self, debug_mock): """ Test openocd debug failed """ debug_mock.return_value = 1 args = ['programmer.py'] with mock.patch('sys.argv', args): ret = debug() self.assertEqual(ret, 1) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3', control_node_type='iotlab')) @mock.patch('gateway_code.utils.subprocess_timeout.call') def test_control_node_method(self, call_mock): """ Test control node method """ call_mock.return_value = 0 args = ['programmer.py', '-cn'] with mock.patch('sys.argv', args): ret = reset() self.assertEqual(ret, 0) args2 = ['programmer.py', '-cn', '/dev/null'] with mock.patch('sys.argv', args2): ret = flash() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3', control_node_type='iotlab')) @mock.patch.dict(os.environ, {'FW': 'autotest'}) def test_control_node_autotest(self): """ Test control node autotest flash """ args = ['programmer.py', '-cn'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, -2) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('m3', control_node_type='no')) def test_control_node_detection(self): """ Test control node detection """ args = ['programmer.py', '-cn'] with mock.patch('sys.argv', args): with self.assertRaises(SystemExit): flash() @mock.patch(utils.READ_CONFIG, utils.read_config_mock('leonardo')) @mock.patch('gateway_code.utils.avrdude.AvrDude.trigger_bootloader') @mock.patch('gateway_code.common.wait_tty') @mock.patch('gateway_code.utils.subprocess_timeout.call') def test_avrdude_flash(self, call_mock, wait_mock, bootloader_mock): """ Test avrdude flash """ bootloader_mock.return_value = 0 call_mock.return_value = 0 wait_mock.return_value = 0 args = ['programmer.py', '/dev/null'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('leonardo')) @mock.patch('gateway_code.utils.avrdude.AvrDude.trigger_bootloader') @mock.patch('gateway_code.common.wait_tty') def test_avrdude_reset(self, wait_mock, bootloader_mock): """ Test avrdude reset """ bootloader_mock.return_value = 0 wait_mock.return_value = 0 args = ['programmer.py'] with mock.patch('sys.argv', args): ret = reset() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('leonardo')) def test_avrdude_no_method(self): """ Test avrdude no method """ args = ['programmer.py'] with mock.patch('sys.argv', args): ret = debug() self.assertEqual(ret, -1) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('a8')) def test_linux_node_no_method(self): """ Test Linux node no flash method """ args = ['programmer.py', '/path/to/firmware'] with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, -1) args2 = ['programmer.py'] with mock.patch('sys.argv', args2): ret = reset() self.assertEqual(ret, -1) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('firefly')) @mock.patch('gateway_code.utils.cc2538.CC2538.flash') @mock.patch('gateway_code.utils.subprocess_timeout.call') def test_cc2538_reset_method(self, call_mock, flash_mock): """ Test cc2538 reset method """ call_mock.return_value = 0 flash_mock.return_value = 0 args = ['programmer.py'] with mock.patch('sys.argv', args): ret = reset() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('a8', linux_open_node_type='a8_m3')) @mock.patch('gateway_code.utils.subprocess_timeout.call') def test_linux_open_node_flash(self, call_mock): """ Test Linux open node flash """ args = ['programmer.py', '/dev/null'] call_mock.return_value = 0 with mock.patch('sys.argv', args): ret = flash() self.assertEqual(ret, 0) @mock.patch(utils.READ_CONFIG, utils.read_config_mock('a8', linux_open_node_type='a8_m3')) def test_linux_open_node_no_cn(self): """ Test Linux open node without control node""" args = ['programmer.py', '-cn'] with mock.patch('sys.argv', args): with self.assertRaises(SystemExit): reset()
def setUp(self): mock.patch(utils.READ_CONFIG, utils.read_config_mock('a8')).start() gateway_manager = mock.Mock() self.g_v = autotest.AutoTestManager(gateway_manager)
def setUp(self): config = utils.read_config_mock('a8', linux_open_node_type='a8_m3') mock.patch(utils.READ_CONFIG, config).start() gateway_manager = mock.Mock() self.g_v = autotest.AutoTestManager(gateway_manager)