예제 #1
0
    def test_notification_pusher_pidfile_created_if_pid_flag_set(self):
        temp = notification_pusher.run_application
        notification_pusher.run_application = False
        pid = 1
        argv = ['description', '--pid', str(pid), '--config', '']
        thread = mock.Mock()
        thread.name = mock.Mock()

        def _break_notification_pusher(*args, **kwargs):
            notification_pusher.run_application = False

        with mock.patch('source.notification_pusher.daemonize', mock.Mock()):
            with mock.patch('source.notification_pusher.create_pidfile',
                            mock.Mock()) as pidfile_created_mock:
                with mock.patch('source.notification_pusher._load_config',
                                mock.Mock(return_value=mock.Mock())):
                    with mock.patch('source.notification_pusher.patch_all',
                                    mock.Mock()):
                        with mock.patch(
                                'source.notification_pusher.dictConfig',
                                mock.Mock()):
                            with mock.patch(
                                    'source.notification_pusher.install_signal_handlers',
                                    mock.Mock()):
                                with mock.patch(
                                        'source.notification_pusher.current_thread',
                                        mock.Mock(return_value=thread)):
                                    notification_pusher.main(argv)
        pidfile_created_mock.assert_called_once_with(str(pid))
        notification_pusher.run_application = temp
    def test_main_main_loop_exception(self):
        """
        проверка выполнения mainloop с exception

        :return:
        """

        with mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)):
            notification_pusher.main(['', '-c', './source/tests/config/pusher_config.py'])
            assert notification_pusher.sleep.called
    def test_main_main_loop_success(self):
        """
        проверка выполнения mainloop без exception

        :return:
        """

        with mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)):
            notification_pusher.main(['','-c', './source/tests/config/pusher_config.py'])
            notification_pusher.main_loop.assert_called_once_with(self.config)
 def test_main_with_daemon_arg(self):
     """
     Проверка вызова метода daemonize при передаче параметра
     :return:
     """
     notification_pusher.run_application = False
     with mock.patch('source.notification_pusher.daemonize', mock.Mock()) as daemonize,\
         mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)):
         notification_pusher.main(['','-d', '-c', './source/tests/config/pusher_config.py'])
         daemonize.assert_called_once()
    def test_main_with_create_pidfile_arg(self):
        """
        Проверка вызова метода create_pidfile при передаче параметра

        :return:
        """
        notification_pusher.run_application = False
        with mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)):
            with mock.patch('source.notification_pusher.create_pidfile', mock.Mock()) as create_pidfile:
                notification_pusher.main(['', '-c', './source/tests/config/pusher_config.py', '-P', 'SomePid'])
                create_pidfile.assert_called_once_with("SomePid")
    def test_main_with_daemonize(self):
        setattr(self.config, "LOGGING", {"version": 1})
        stop_notification_pusher();
        notification_pusher.parse_cmd_args.return_value = Namespace(daemon=True, config='config', pidfile=False)
        with mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)), \
             mock.patch('source.notification_pusher.daemonize', mock.Mock()) as daemonize,\
             mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()),\
             mock.patch('source.notification_pusher.patch_all', mock.Mock()):
                notification_pusher.main([])

        assert daemonize.called
    def test_main(self):
        setattr(self.config, "LOGGING", {"version": 1})
        notification_pusher.parse_cmd_args = mock.Mock()
        notification_pusher.parse_cmd_args.return_value = Namespace(daemon=False, config='config', pidfile=False)
        with mock.patch('source.notification_pusher.main_loop', mock.Mock(side_effect=stop_notification_pusher)) as main_loop, \
             mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)),\
             mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()),\
             mock.patch('source.notification_pusher.patch_all', mock.Mock()):
                notification_pusher.main([])

        main_loop.assert_called_once_with(self.config)
 def test_main_without_args(self):
     """
     в параметрах не передается daemon и pidfile
     :return:
     """
     notification_pusher.run_application = False
     with mock.patch('source.notification_pusher.daemonize', mock.Mock()) as daemonize, mock.patch(
             'source.notification_pusher.create_pidfile', mock.Mock()) as create_pidfile,\
             mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)):
         notification_pusher.main(['','-c', './source/tests/config/pusher_config.py'])
         self.assertFalse(daemonize.called)
         self.assertFalse(create_pidfile.called)
예제 #9
0
    def test_main_with_daemonize(self):
        setattr(self.config, "LOGGING", {"version": 1})
        stop_notification_pusher()
        notification_pusher.parse_cmd_args.return_value = Namespace(
            daemon=True, config='config', pidfile=False)
        with mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)), \
             mock.patch('source.notification_pusher.daemonize', mock.Mock()) as daemonize,\
             mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()),\
             mock.patch('source.notification_pusher.patch_all', mock.Mock()):
            notification_pusher.main([])

        assert daemonize.called
예제 #10
0
    def test_main(self):
        setattr(self.config, "LOGGING", {"version": 1})
        notification_pusher.parse_cmd_args = mock.Mock()
        notification_pusher.parse_cmd_args.return_value = Namespace(
            daemon=False, config='config', pidfile=False)
        with mock.patch('source.notification_pusher.main_loop', mock.Mock(side_effect=stop_notification_pusher)) as main_loop, \
             mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock(return_value=self.config)),\
             mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()),\
             mock.patch('source.notification_pusher.patch_all', mock.Mock()):
            notification_pusher.main([])

        main_loop.assert_called_once_with(self.config)
    def test_main_daemon_and_pidfile_given_and_main_loop_is_fine(self):
        mock_argv = [1, 1, 1]
        mock_parse_cmd_args = mock.Mock()
        config = Config()
        config.LOGGING = mock.Mock()
        mock_dictConfig = mock.Mock()
        initial_run_application = notification_pusher.run_application

        def break_run(*args, **kwargs):
            notification_pusher.run_application = False

        mock_main_loop = mock.Mock(side_effect=break_run)
        mock_daemonize = mock.Mock()
        mock_create_pidfile = mock.Mock()
        notification_pusher.run_application = True
        with mock.patch('source.notification_pusher.daemonize', mock_daemonize):
            with mock.patch('source.notification_pusher.parse_cmd_args', mock_parse_cmd_args):
                with mock.patch('source.notification_pusher.dictConfig', mock_dictConfig):
                    with mock.patch('source.notification_pusher.main_loop', mock_main_loop):
                        with mock.patch('source.notification_pusher.load_config_from_pyfile',
                                        mock.Mock(return_value=config)):
                            with mock.patch('source.notification_pusher.os.path.realpath',
                                        mock.Mock()):
                                with mock.patch('source.notification_pusher.os.path.expanduser',
                                        mock.Mock()):
                                    with mock.patch('source.notification_pusher.create_pidfile',
                                                    mock_create_pidfile):
                                        with mock.patch('source.notification_pusher.patch_all', mock.Mock()):
                                            exit_code = notification_pusher.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 notification_pusher.exit_code == exit_code
        notification_pusher.run_application = initial_run_application
 def test_main_all_success(self):
     config = MockConfig()
     with mock.patch('source.notification_pusher.exit_code', 0):
         with mock.patch('source.notification_pusher.configuration', mock.Mock(return_value=config)):
             with mock.patch('source.notification_pusher.main_loop',
                             mock.Mock(side_effect=stop_loop)) as main_loop_m:
                 ret = notification_pusher.main([1])
     main_loop_m.assert_called_once_with(config)
     self.assertEqual(ret, 0)
예제 #13
0
    def test_notification_pusher_daemonize_called_if_daemon_flag_set(self):
        temp = notification_pusher.run_application
        notification_pusher.run_application = False
        argv = ['description', '--daemon', '--config', '']
        thread = mock.Mock()
        thread.name = mock.Mock()

        def _break_notification_pusher(*args, **kwargs):
            notification_pusher.run_application = False

        with mock.patch('source.notification_pusher.daemonize', mock.Mock()) as daemonize_mock:
            with mock.patch('source.notification_pusher.create_pidfile', mock.Mock()):
                with mock.patch('source.notification_pusher._load_config', mock.Mock(return_value=mock.Mock())):
                    with mock.patch('source.notification_pusher.patch_all', mock.Mock()):
                        with mock.patch('source.notification_pusher.dictConfig', mock.Mock()):
                            with mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()):
                                with mock.patch('source.notification_pusher.current_thread', mock.Mock(return_value=thread)):
                                    notification_pusher.main(argv)
        daemonize_mock.assert_called_once_with()
        notification_pusher.run_application = temp
예제 #14
0
    def test_main_loop(self):
        temp = notification_pusher.run_application
        notification_pusher.run_application = True
        argv = ['description', '--config', '']
        thread = mock.Mock()
        thread.name = mock.Mock()

        def _break_notification_pusher(*args, **kwargs):
            notification_pusher.run_application = False

        with mock.patch('source.notification_pusher.daemonize', mock.Mock()):
            with mock.patch('source.notification_pusher.create_pidfile',
                            mock.Mock()):
                with mock.patch('source.notification_pusher._load_config',
                                mock.Mock(return_value=mock.Mock())):
                    with mock.patch('source.notification_pusher.patch_all',
                                    mock.Mock()):
                        with mock.patch(
                                'source.notification_pusher.dictConfig',
                                mock.Mock()):
                            with mock.patch(
                                    'source.notification_pusher.install_signal_handlers',
                                    mock.Mock()):
                                with mock.patch(
                                        'source.notification_pusher.current_thread',
                                        mock.Mock(return_value=thread)):
                                    with mock.patch(
                                            'source.notification_pusher.main_loop',
                                            mock.Mock(
                                                side_effect=
                                                _break_notification_pusher)
                                    ) as main_loop_mock:
                                        with mock.patch(
                                                'source.notification_pusher.sleep',
                                                mock.Mock(
                                                    side_effect=
                                                    _break_notification_pusher)
                                        ):
                                            notification_pusher.main(argv)
        main_loop_mock.assert_called_once_with(mock.ANY)
        notification_pusher.run_application = temp
예제 #15
0
    def test_main_loop_raised_exception(self):
        temp = notification_pusher.run_application
        notification_pusher.run_application = True
        argv = ['description', '--config', '']
        thread = mock.Mock()
        thread.name = mock.Mock()

        def _break_notification_pusher(*args, **kwargs):
            notification_pusher.run_application = False

        with mock.patch('source.notification_pusher.daemonize', mock.Mock()):
            with mock.patch('source.notification_pusher.create_pidfile', mock.Mock()):
                with mock.patch('source.notification_pusher._load_config', mock.Mock(return_value=mock.Mock())):
                    with mock.patch('source.notification_pusher.patch_all', mock.Mock()):
                        with mock.patch('source.notification_pusher.dictConfig', mock.Mock()):
                            with mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()):
                                with mock.patch('source.notification_pusher.current_thread', mock.Mock(return_value=thread)):
                                    with mock.patch('source.notification_pusher.main_loop', mock.Mock(side_effect=Exception)):
                                        with mock.patch('source.notification_pusher.sleep', mock.Mock(side_effect=_break_notification_pusher)) as sleep_mock:
                                            notification_pusher.main(argv)
        sleep_mock.assert_called_once_with(mock.ANY)
        notification_pusher.run_application = temp
    def test_main_fail(self):
        config = mock.Mock()
        config.SLEEP_ON_FAIL = 23

        expected_ret_code = 42
        with mock.patch('source.notification_pusher.run_application', True):
            with mock.patch('source.notification_pusher.exit_code', expected_ret_code):
                with mock.patch('source.lib.utils.prepare', mock.Mock(return_value=config)):
                    with mock.patch('source.notification_pusher.main_loop',
                                    mock.Mock(side_effect=Exception)) as main_loop_m:
                        with mock.patch('source.notification_pusher.sleep', mock.Mock(side_effect=break_run)) as sleep_m:
                            ret_code = notification_pusher.main([1, 2, 3, 4])

        main_loop_m.assert_called_once_with(config)
        self.assertEqual(ret_code, expected_ret_code, 'notification_pusher.main returned unexpected value')
        sleep_m.assert_called_once_with(config.SLEEP_ON_FAIL)
    def _test_run(self, daemon, pidfile, config):
        args = type('', (), {
            'daemon': daemon,
            'pidfile': pidfile,
            'config': config
        })

        with mock.patch('source.notification_pusher.parse_cmd_args', mock.Mock(return_value=args)), \
                mock.patch('source.notification_pusher.load_config_from_pyfile', mock.Mock()), \
                mock.patch('os.path.realpath', mock.Mock()), \
                mock.patch('os.path.expanduser', mock.Mock()), \
                mock.patch('source.notification_pusher.patch_all', mock.Mock()), \
                mock.patch('source.notification_pusher.dictConfig', mock.Mock()), \
                mock.patch('source.notification_pusher.current_thread', mock.Mock()), \
                mock.patch('source.notification_pusher.install_signal_handlers', mock.Mock()):
            return notification_pusher.main("args")
    def test_main_all_success(self, patch_all_m, install_handlers_m):
        config = mock.Mock()

        expected_ret_code = 42
        with mock.patch('source.notification_pusher.run_application', True):
            with mock.patch('source.notification_pusher.exit_code', expected_ret_code):
                with mock.patch('source.lib.utils.prepare', mock.Mock(return_value=config)):
                    with mock.patch('source.notification_pusher.main_loop',
                                    mock.Mock(side_effect=break_run)) as main_loop_m:
                        with mock.patch('source.notification_pusher.sleep'):
                            ret_code = notification_pusher.main([1, 2, 3, 4])

        self.assertTrue(install_handlers_m.called)
        self.assertTrue(patch_all_m.called)
        main_loop_m.assert_called_once_with(config)
        self.assertEqual(ret_code, expected_ret_code, 'notification_pusher.main returned unexpected value')