def test_start_shellinabox_console(self, mock_stop, mock_dir_exists, mock_pid_exists, mock_pid, mock_popen): mock_popen.return_value.poll.return_value = 0 mock_pid.return_value = 12345 mock_pid_exists.return_value = True # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) console_utils.start_shellinabox_console(self.info['uuid'], self.info['port'], 'ls&') mock_stop.assert_called_once_with(self.info['uuid']) mock_dir_exists.assert_called_once_with() mock_pid.assert_called_once_with(self.info['uuid']) mock_pid_exists.assert_called_once_with(12345) mock_popen.assert_called_once_with(mock.ANY, stdout=subprocess.PIPE, stderr=subprocess.PIPE) mock_popen.return_value.poll.assert_called_once_with()
def test_start_shellinabox_console_fail_no_pid(self, mock_stop, mock_dir_exists, mock_pid_exists, mock_pid, mock_popen): mock_popen.return_value.poll.return_value = 0 mock_pid.return_value = 12345 mock_pid_exists.return_value = False mock_popen.return_value.communicate.return_value = ('output', 'error') # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) self.assertRaises(exception.ConsoleSubprocessFailed, console_utils.start_shellinabox_console, self.info['uuid'], self.info['port'], 'ls&') mock_stop.assert_called_once_with(self.info['uuid']) mock_dir_exists.assert_called_once_with() mock_pid.assert_called_once_with(self.info['uuid']) mock_pid_exists.assert_called_once_with(12345) mock_popen.assert_called_once_with(mock.ANY, stdout=subprocess.PIPE, stderr=subprocess.PIPE) mock_popen.return_value.poll.assert_called_once_with()
def test_start_shellinabox_console_nopid(self, mock_stop, mock_dir_exists, mock_pid_exists, mock_pid, mock_popen): # no existing PID file before starting mock_stop.side_effect = iter([exception.NoConsolePid('/tmp/blah')]) mock_popen.return_value.poll.return_value = 0 mock_pid.return_value = 12345 mock_pid_exists.return_value = True # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) console_utils.start_shellinabox_console(self.info['uuid'], self.info['port'], 'ls&') mock_stop.assert_called_once_with(self.info['uuid']) mock_dir_exists.assert_called_once_with() mock_pid.assert_called_once_with(self.info['uuid']) mock_pid_exists.assert_called_once_with(12345) mock_popen.assert_called_once_with(mock.ANY, stdout=subprocess.PIPE, stderr=subprocess.PIPE) mock_popen.return_value.poll.assert_called_once_with()
def test__get_console_pid_file(self): tempdir = tempfile.gettempdir() self.config(terminal_pid_dir=tempdir, group='console') path = console_utils._get_console_pid_file(self.info['uuid']) self.assertEqual(path, '%(tempdir)s/%(uuid)s.pid' % {'tempdir': tempdir, 'uuid': self.info.get('uuid')})
def test__get_console_pid_file(self, mock_dir): mock_dir.return_value = tempfile.gettempdir() expected_path = '%(tempdir)s/%(uuid)s.pid' % { 'tempdir': mock_dir.return_value, 'uuid': self.info.get('uuid')} path = console_utils._get_console_pid_file(self.info['uuid']) self.assertEqual(expected_path, path) mock_dir.assert_called_once_with()
def test__get_console_pid_file(self, mock_dir): mock_dir.return_value = tempfile.gettempdir() expected_path = '%(tempdir)s/%(uuid)s.pid' % { 'tempdir': mock_dir.return_value, 'uuid': self.info['uuid']} path = console_utils._get_console_pid_file(self.info['uuid']) self.assertEqual(expected_path, path) mock_dir.assert_called_once_with()
def test_stop_shellinabox_console(self): pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.assertTrue(os.path.exists(pid_file)) console_utils.stop_shellinabox_console(self.info['uuid']) self.assertFalse(os.path.exists(pid_file))
def test__get_console_pid_file(self): tempdir = tempfile.gettempdir() self.config(terminal_pid_dir=tempdir, group='console') path = console_utils._get_console_pid_file(self.info['uuid']) self.assertEqual( path, '%(tempdir)s/%(uuid)s.pid' % { 'tempdir': tempdir, 'uuid': self.info.get('uuid') })
def test__stop_console_nopid(self, mock_pid, mock_kill, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.side_effect = exception.NoConsolePid(pid_path="/tmp/blah") self.assertRaises(exception.NoConsolePid, console_utils._stop_console, self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) self.assertFalse(mock_kill.called) mock_unlink.assert_called_once_with(pid_file)
def test__stop_console(self, mock_pid, mock_execute, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = '12345' console_utils._stop_console(self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) mock_execute.assert_called_once_with('kill', mock_pid.return_value, check_exit_code=[0, 99]) mock_unlink.assert_called_once_with(pid_file)
def test__stop_console(self, mock_pid, mock_kill, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = 12345 console_utils._stop_console(self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) mock_kill.assert_called_once_with(mock_pid.return_value, signal.SIGTERM) mock_unlink.assert_called_once_with(pid_file)
def test_stop_ics_console(self, mock_send_signal): self.config(terminal='ironic-console-server', group='console') # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) console_utils.stop_ics_console(self.info['uuid']) mock_send_signal.assert_called_once_with(mock.ANY, 'USR2')
def test__stop_console_shellinabox_not_running(self, mock_pid, mock_kill, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = 12345 mock_kill.side_effect = OSError(errno.ESRCH, 'message') console_utils._stop_console(self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) mock_kill.assert_called_once_with(mock_pid.return_value, signal.SIGTERM) mock_unlink.assert_called_once_with(pid_file)
def test__stop_console_exception(self, mock_pid, mock_kill, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = 12345 mock_kill.side_effect = OSError(2, 'message') self.assertRaises(exception.ConsoleError, console_utils._stop_console, self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) mock_kill.assert_called_once_with(mock_pid.return_value, signal.SIGTERM) mock_unlink.assert_called_once_with(pid_file)
def test__stop_console_forced_kill(self, mock_pid, mock_psutil, mock_kill, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = 12345 console_utils._stop_console(self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) # Make sure console process receives hard SIGKILL mock_kill.assert_any_call(mock_pid.return_value, signal.SIGKILL) mock_unlink.assert_called_once_with(pid_file)
def test_start_ics_console_fail_nopid(self, mock_send_signal): self.config(terminal='ironic-console-server', group='console') mock_send_signal.side_effect = exception.NoConsolePid(pid_path='/foo') # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) console_utils.start_ics_console(self.info['uuid']) mock_send_signal.assert_called_once_with(mock.ANY, 'USR1')
def test__stop_console_nokill(self, mock_pid, mock_execute, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = '12345' mock_execute.side_effect = processutils.ProcessExecutionError() self.assertRaises(processutils.ProcessExecutionError, console_utils._stop_console, self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) mock_execute.assert_called_once_with('kill', mock_pid.return_value, check_exit_code=[0, 99]) mock_unlink.assert_called_once_with(pid_file)
def test__stop_console(self, mock_pid, mock_kill, mock_unlink): pid_file = console_utils._get_console_pid_file(self.info['uuid']) mock_pid.return_value = 12345 console_utils._stop_console(self.info['uuid']) mock_pid.assert_called_once_with(self.info['uuid']) # a check if process still exist (signal 0) in a loop mock_kill.assert_any_call(mock_pid.return_value, signal.SIG_DFL) # and that it receives the SIGTERM mock_kill.assert_any_call(mock_pid.return_value, signal.SIGTERM) mock_unlink.assert_called_once_with(pid_file)
def test_get_ics_console_log(self, mock_execute): self.config(terminal='ironic-console-server', group='console') mock_execute.return_value = ('output', '') # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) console_utils.get_ics_console_log(self.info['uuid']) mock_execute.assert_called_once_with('tail', '-n', '100', mock.ANY)
def test_start_shellinabox_console(self, mock_popen): mock_popen.return_value.poll.return_value = 0 # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.assertTrue(os.path.exists(pid_file)) console_utils.start_shellinabox_console(self.info['uuid'], self.info['port'], 'ls&') mock_popen.assert_called_once_with(mock.ANY, stdout=subprocess.PIPE, stderr=subprocess.PIPE) mock_popen.return_value.poll.assert_called_once_with()
def test_start_ics_console_log(self, mock_stop, mock_dir_exists, mock_popen): self.config(terminal='ironic-console-server', group='console') mock_popen.return_value.pid = 12345 # touch the pid file pid_file = console_utils._get_console_pid_file(self.info['uuid']) open(pid_file, 'a').close() self.addCleanup(os.remove, pid_file) self.assertTrue(os.path.exists(pid_file)) console_utils.start_ics_console_log(self.info['uuid'], self.info['port'], 'ls&', True) mock_stop.assert_called_once_with(self.info['uuid']) mock_dir_exists.assert_called_once_with() mock_popen.assert_called_once_with(mock.ANY)