def test_daemonize_called_if_daemon_flag_set(self): argv = ['description', '--daemon', '--config', ''] with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemonize_mock: with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()): with mock.patch('source.redirect_checker._load_config', mock.Mock(return_value=mock.Mock())): with mock.patch('source.redirect_checker.dictConfig', mock.Mock()): with mock.patch('source.redirect_checker.main_loop', mock.Mock()): redirect_checker.main(argv) daemonize_mock.assert_called_once_with()
def test_main_no_demon_no_pidfile(self): setattr(self.config, "LOGGING", {"version": 1}) setattr(self.config, "EXIT_CODE", 0) with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemon: with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=self.config)): with mock.patch('os.path', mock.MagicMock(return_value=self.config)): with mock.patch('source.redirect_checker.parse_cmd_args', mock.Mock(return_value=Namespace(daemon=False, config=self.config, pidfile=0))): with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as pidfile: redirect_checker.main([]) assert not daemon.called assert not pidfile.called
def test_main_args_daemon_exist(self): mock_argv = mock.MagicMock() mock_parse_cmd_args = mock.Mock() config = Config() config.LOGGING = mock.Mock() config.EXIT_CODE = 100 mock_dictConfig = mock.Mock() mock_main_loop = mock.Mock() mock_daemonize = mock.Mock() mock_create_pidfile = mock.Mock() with mock.patch('source.redirect_checker.daemonize', mock_daemonize): with mock.patch('source.redirect_checker.parse_cmd_args', mock_parse_cmd_args): with mock.patch('source.redirect_checker.dictConfig', mock_dictConfig): with mock.patch('source.redirect_checker.main_loop', mock_main_loop): with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=config)): with mock.patch('source.redirect_checker.os.path.realpath', mock.Mock()): with mock.patch('source.redirect_checker.os.path.expanduser', mock.Mock()): with mock.patch('source.redirect_checker.create_pidfile', mock_create_pidfile): exit_code = redirect_checker.main(mock_argv) mock_daemonize.assert_called_once_with() mock_create_pidfile.assert_called() mock_dictConfig.assert_called_once_with(config.LOGGING) mock_main_loop.assert_called_once_with(config) assert config.EXIT_CODE == exit_code
def test_main(self, main_loop_m): config = mock.Mock() config.EXIT_CODE = 42 with mock.patch('source.lib.utils.prepare', mock.Mock(return_value=config)): ret_code = redirect_checker.main([1, 2, 3, 4]) main_loop_m.assert_called_once_with(config) self.assertEqual(ret_code, config.EXIT_CODE)
def test_main_no_demon_with_pidfile(self): setattr(self.config, "LOGGING", {"version": 1}) setattr(self.config, "EXIT_CODE", 0) with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemon: with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=self.config)): with mock.patch('os.path', mock.MagicMock(return_value=self.config)): with mock.patch( 'source.redirect_checker.parse_cmd_args', mock.Mock(return_value=Namespace( daemon=False, config=self.config, pidfile=1))): with mock.patch( 'source.redirect_checker.create_pidfile', mock.Mock()) as pidfile: redirect_checker.main([]) assert not daemon.called assert pidfile.called
def test_returned_valid_exit_code(self): config = mock.Mock() config.EXIT_CODE = 2 argv = ['description', '--config', ''] with mock.patch('source.redirect_checker.daemonize', mock.Mock()): with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()): with mock.patch('source.redirect_checker._load_config', mock.Mock(return_value=config)): with mock.patch('source.redirect_checker.dictConfig', mock.Mock()): with mock.patch('source.redirect_checker.main_loop', mock.Mock()): EXIT_CODE = redirect_checker.main(argv) self.assertEqual(EXIT_CODE, config.EXIT_CODE)
def test_main_with_demon_with_pidfile(self): exitcode = 322 setattr(self.config, "LOGGING", {"version": 1}) setattr(self.config, "EXIT_CODE", exitcode) with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemon: with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=self.config)): with mock.patch('os.path', mock.MagicMock(return_value=self.config)): with mock.patch('source.redirect_checker.parse_cmd_args', mock.Mock(return_value=Namespace(daemon=True, config=self.config, pidfile=1))): with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as pidfile: assert redirect_checker.main([]) == exitcode assert daemon.called assert pidfile.called
def test_main_with_daemon_and_pidfile(self): exitcode = 100550 setattr(self.config, "LOGGING", {"version": 1}) setattr(self.config, "EXIT_CODE", exitcode) with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemon: with mock.patch('source.redirect_checker.main_loop', mock.Mock()) as main_loop: with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=self.config)): with mock.patch('os.path', mock.MagicMock(return_value=self.config)): with mock.patch('source.redirect_checker.parse_cmd_args', mock.Mock(return_value=Namespace(daemon=True, pidfile=1, config=self.config))): with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as pidfile: assert redirect_checker.main([]) == exitcode, "Exit code not same" assert daemon.called, "Daemon doesn't called" assert pidfile.called, "create pid file doens't call"
def test_main_with_daemon_arg_without_pidfile(self): """ Проверка вызова метода daemonize при передаче параметра create_pidfile в параметрах не указан :return: """ with mock.patch('source.redirect_checker.daemonize', mock.Mock("daemonize mock")) as daemonize: with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as create_pid: self.assertEqual(redirect_checker.main(['','-d', '-c', './source/tests/config/pusher_config.py']), self.config.EXIT_CODE) assert create_pid.not_called daemonize.assert_called_once_with() redirect_checker.main_loop.assert_called_once_with(self.config)
def test_main_with_pidfile_without_daemon(self): """ Проверка вызова метода create_pidfile при передаче параметров daemonize в параметрах не указан :return: """ with mock.patch('source.redirect_checker.daemonize', mock.Mock("daemonize mock")) as daemonize: with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as create_pid: self.assertEqual( redirect_checker.main(['','-P', 'somePID', '-c', './source/tests/config/pusher_config.py']), self.config.EXIT_CODE) self.assertFalse(daemonize.called) create_pid.assert_called_once_with("somePID") redirect_checker.main_loop.assert_called_once_with(self.config)
def _redirect_checker_main(self, args): config = Config() config.LOGGING = mock.Mock() config.EXIT_CODE = 0 with mock.patch('source.redirect_checker.parse_cmd_args', mock.MagicMock(return_value=args)),\ mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemonize,\ mock.patch('source.redirect_checker.dictConfig', mock.Mock()),\ mock.patch('source.redirect_checker.main_loop', mock.Mock()),\ mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=config)),\ mock.patch('os.path.realpath', mock.Mock()),\ mock.patch('os.path.expanduser', mock.Mock()),\ mock.patch('source.redirect_checker.sys.exit', mock.Mock(side_effect=None)),\ mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as create_pid: exit_code = redirect_checker.main(args) return exit_code == config.EXIT_CODE, daemonize.called, create_pid.called
def test_main_daemon_pidfile(self): """ DFS ;-) """ args = mock.MagicMock() config = Config() config.LOGGING = mock.Mock() config.EXIT_CODE = 0 with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemonize: with mock.patch('source.redirect_checker.parse_cmd_args', mock.Mock()): with mock.patch('source.redirect_checker.dictConfig', mock.Mock()): with mock.patch('source.redirect_checker.main_loop', mock.Mock()): with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=config)): with mock.patch('source.redirect_checker.os.path.realpath', mock.Mock()): with mock.patch('source.redirect_checker.os.path.expanduser', mock.Mock()): with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as create_pid: daemonize.assert_called() create_pid.assert_called() self.assertEqual(config.EXIT_CODE, redirect_checker.main(args))
def test_main_no_daemon_no_pidfile(self): """ main without deamon or pidfile """ args = mock.MagicMock() args.daemon = False args.pidfile = False config = Config() config.LOGGING = mock.Mock() config.EXIT_CODE = 0 with mock.patch('source.redirect_checker.parse_cmd_args', mock.MagicMock(return_value=args)): with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemonize: with mock.patch('source.redirect_checker.dictConfig', mock.Mock()): with mock.patch('source.redirect_checker.main_loop', mock.Mock()): with mock.patch('source.redirect_checker.load_config_from_pyfile', mock.Mock(return_value=config)): with mock.patch('source.redirect_checker.os.path.realpath', mock.Mock()): with mock.patch('source.redirect_checker.os.path.expanduser', mock.Mock()): with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as create_pid: self.assertFalse(daemonize.called) self.assertFalse(create_pid.called) self.assertEqual(config.EXIT_CODE, redirect_checker.main(args))
def test_main_loop(self, m_main_loop_function): with mock.patch('source.redirect_checker.sleep', mock.Mock(side_effect=break_while)): redirect_checker.main([1]) self.assertTrue(m_main_loop_function.called)
def test_run_no_daemon_no_pidfile(self): with mock.patch('source.redirect_checker.daemonize', mock.Mock()) as daemon: with mock.patch('source.redirect_checker.create_pidfile', mock.Mock()) as pid_creator: redirect_checker.main([]) assert not daemon.called assert not pid_creator.called, 'pid file should created'