def test_main_no_pidfile(self): argv = ['redirect_checker.py', '-c', '/test/', '-P', '/test/', '-d'] args = mock.Mock() args.daemon = True args.pidfile = None args.config = 'test' config = mock.Mock() config.LOGGING = 'LOG' config.EXIT_CODE = 'EXIT' parse_cmd_args_mock = mock.Mock(return_value=args) daemonize_mock = mock.Mock() create_pidfile_mock = mock.Mock() load_config_from_pyfile_mock = mock.Mock(return_value=config) dictConfig_mock = mock.Mock() main_loop_mock = mock.Mock() with mock.patch('redirect_checker.parse_cmd_args', parse_cmd_args_mock): with mock.patch('redirect_checker.daemonize', daemonize_mock): with mock.patch('redirect_checker.create_pidfile', create_pidfile_mock): with mock.patch('redirect_checker.load_config_from_pyfile', load_config_from_pyfile_mock): with mock.patch('redirect_checker.dictConfig', dictConfig_mock): with mock.patch('redirect_checker.main_loop', main_loop_mock): self.assertEqual(config.EXIT_CODE, main(argv)) self.assertFalse(create_pidfile_mock.called)
def test_main_false(self): args = Mock() args.daemon = False args.pidfile = False config = Mock() config.LOGGING = Mock() config.EXIT_CODE = 0 with patch('redirect_checker.parse_cmd_args', Mock(return_value=args)): with patch('redirect_checker.daemonize', Mock()) as daemonize: with patch('redirect_checker.create_pidfile', Mock()) as create_pidfile: with patch('redirect_checker.dictConfig', Mock()) as dictConfig: with patch('redirect_checker.main_loop', Mock()) as main_loop: with patch( 'redirect_checker.load_config_from_pyfile', Mock(return_value=config)): with patch('os.path.realpath', Mock()): with patch('os.path.expanduser', Mock()): res = redirect_checker.main('argv') self.assertFalse(daemonize.called) self.assertFalse(create_pidfile.called) self.assertTrue(dictConfig.called) self.assertTrue(main_loop.called) self.assertEqual(config.EXIT_CODE, res)
def test_main_false_args(self): argv = mock.MagicMock() args = mock.Mock() args.daemon = False args.pidfile = False config = mock.Mock() config.LOGGING = mock.Mock() config.EXIT_CODE = 13 with mock.patch("redirect_checker.parse_cmd_args", mock.Mock(return_value=args)): with mock.patch("redirect_checker.daemonize", mock.Mock()) as mock_daemonize: with mock.patch("redirect_checker.create_pidfile", mock.Mock()) as mock_create_pidfile: with mock.patch("redirect_checker.load_config_from_pyfile", mock.Mock(return_value=config)): with mock.patch("os.path.realpath", mock.Mock()): with mock.patch("os.path.expanduser", mock.Mock()): with mock.patch("redirect_checker.dictConfig", mock.Mock()): with mock.patch( "redirect_checker.main_loop", mock.Mock()): exit_code = redirect_checker.main(argv) assert mock_daemonize.call_count == 0 assert mock_create_pidfile.call_count == 0 assert exit_code == config.EXIT_CODE
def test_main_2(self): args = '1 -c /conf -P /pidfile' args = args.split(' ') conf = Mock() conf.LOGGING = {} conf.EXIT_CODE = 1222 with patch("redirect_checker.create_pidfile") as mock_create_pidfile: with patch("redirect_checker.load_config_from_pyfile", Mock(return_value=conf)): with patch("redirect_checker.main_loop") as mock_main_loop: with patch("os.path.realpath"), patch( "os.path.expanduser"): with patch("redirect_checker.dictConfig"): self.assertEqual(main(args), 1222) mock_main_loop.assert_called_with(conf) mock_create_pidfile.assert_called_with("/pidfile")
def test_main_false(self): args = Mock() args.daemon = False args.pidfile = False config = Mock() config.LOGGING = Mock() config.EXIT_CODE = 0 with patch('redirect_checker.parse_cmd_args', Mock(return_value=args)): with patch('redirect_checker.daemonize', Mock()) as daemonize: with patch('redirect_checker.create_pidfile', Mock()) as create_pidfile: with patch('redirect_checker.dictConfig', Mock()) as dictConfig: with patch('redirect_checker.main_loop', Mock()) as main_loop: with patch('redirect_checker.load_config_from_pyfile', Mock(return_value=config)): with patch('os.path.realpath', Mock()): with patch('os.path.expanduser', Mock()): res = redirect_checker.main('argv') self.assertFalse(daemonize.called) self.assertFalse(create_pidfile.called) self.assertTrue(dictConfig.called) self.assertTrue(main_loop.called) self.assertEqual(config.EXIT_CODE, res)
def test_main_not_if(self, main_loop, dictConfig, load_config_from_pyfile, parse_cmd_args): argv = [1, 2, 3] args = mock.MagicMock() args.daemon = False args.pidfile = False logging = mock.Mock() config = mock.MagicMock() config.LOGGING = logging config.EXIT_CODE = 42 parse_cmd_args.return_value = args load_config_from_pyfile.return_value = config assert 42 == redirect_checker.main(argv) parse_cmd_args.assert_called_once_with(argv[1:]) dictConfig.assert_called_once_with(logging) main_loop.assert_called_once_with(config)
def test_main(self): argv = mock.MagicMock() args = mock.MagicMock() config = mock.Mock() config.LOGGING = mock.Mock() config.EXIT_CODE = 13 with mock.patch("redirect_checker.parse_cmd_args", mock.Mock(return_value=args)): with mock.patch("redirect_checker.daemonize", mock.Mock()) as mock_daemonize: with mock.patch("redirect_checker.create_pidfile", mock.Mock()) as mock_create_pidfile: with mock.patch("redirect_checker.load_config_from_pyfile", mock.Mock(return_value=config)): with mock.patch("os.path.realpath", mock.Mock()): with mock.patch("os.path.expanduser", mock.Mock()): with mock.patch("redirect_checker.dictConfig", mock.Mock()): with mock.patch("redirect_checker.main_loop", mock.Mock()): exit_code = redirect_checker.main(argv) mock_daemonize.assert_called() mock_create_pidfile.assert_called() assert exit_code == config.EXIT_CODE
def test_main_no_daemon_no_pidfile(self): args = Mock() args.daemon = False args.pidfile = None args.config = 'config' mocked_config = Mock() mocked_config.EXIT_CODE = 44 with patch('redirect_checker.parse_cmd_args', Mock(return_value=args)): with patch('redirect_checker.daemonize') as mocked_daemonize: with patch('redirect_checker.create_pidfile') as mocked_create_pidfile: with patch('redirect_checker.load_config_from_pyfile', Mock(return_value=mocked_config)) as mocked_load_config_from_pyfile: with patch('redirect_checker.main_loop') as mocked_main_loop: assert main('args') == mocked_config.EXIT_CODE assert not mocked_daemonize.called assert not mocked_create_pidfile.called assert mocked_load_config_from_pyfile.called mocked_main_loop.assert_called_once_with(mocked_config)