Esempio n. 1
0
    def test_execute_noresults(self, mock_create_logger, mock_get, mock_now):
        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        mock_get.return_value.json.side_effect = ValueError

        now = datetime.utcnow().replace(tzinfo=utc)
        mock_now.return_value = now
        default_params = {'time__gt': now.isoformat()}

        pub = NotificationPublisher()
        pub.execute()

        self.assertEquals(mock_get.call_args_list, [
            call(pub.api, 'alerts', default_params),
            call(pub.api, 'observations', default_params),
        ])

        self.assertEquals(mock_logger.info.call_args_list, [])
        self.assertEquals(mock_logger.error.call_args_list, [])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)
        self.assertEquals(state, {
            'alerts': default_params,
            'observations': default_params,
        })
Esempio n. 2
0
    def test_execute_noresults(self, mock_create_logger, mock_get, mock_now):
        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        mock_get.return_value.json.side_effect = ValueError

        now = datetime.utcnow().replace(tzinfo=utc)
        mock_now.return_value = now
        default_params = {'time__gt': now.isoformat()}

        pub = NotificationPublisher()
        pub.execute()

        self.assertEquals(mock_get.call_args_list, [
            call(pub.api, 'alerts', default_params),
            call(pub.api, 'observations', default_params),
        ])

        self.assertEquals(mock_logger.info.call_args_list, [])
        self.assertEquals(mock_logger.error.call_args_list, [])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)
        self.assertEquals(state, {
            'alerts': default_params,
            'observations': default_params,
        })
Esempio n. 3
0
    def test_execute_none(self, mock_get):
        env_override = {
            'OBSRVBL_SNMP_ENABLED': 'false',
            'OBSRVBL_SYSLOG_ENABLED': 'false',
        }
        with patch.dict('ona_service.ipfix_pusher.environ', env_override):
            pub = NotificationPublisher()
            pub.execute()

        self.assertEqual(mock_get.call_count, 0)
Esempio n. 4
0
    def test_execute_default(self, mock_create_logger, mock_get, mock_now):
        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        messages = [
            {
                'id': 1,
                'time': '2015-05-01T01:02:03+00:00'
            },
            {
                'id': 2,
                'time': '2015-05-01T01:02:04+00:00'
            },
            {
                'id': 3,
                'time': '2015-05-01T01:02:05+00:00'
            },
        ]
        mock_get.return_value.json.return_value = {'objects': messages}

        now = datetime.utcnow().replace(tzinfo=utc)
        mock_now.return_value = now
        default_params = {'time__gt': now.isoformat()}

        pub = NotificationPublisher()
        pub.execute()

        self.assertEquals(mock_get.call_args_list, [
            call(pub.api, 'alerts', default_params),
            call(pub.api, 'observations', default_params),
        ])

        self.assertEquals(mock_logger.info.call_args_list,
                          [call(m) for m in messages])
        self.assertEquals(mock_logger.error.call_args_list,
                          [call(m) for m in messages])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)

        self.assertEquals(
            state, {
                'alerts': {
                    'time__gt': '2015-05-01T01:02:05+00:00'
                },
                'observations': {
                    'time__gt': '2015-05-01T01:02:05+00:00'
                }
            })
Esempio n. 5
0
    def test_execute_resume(self, mock_create_logger, mock_get):
        last_alert_time = '2015-05-01T01:02:03+00:00'
        last_obs_time = '2015-05-01T01:02:02+00:00'

        state = {
            'alerts': {
                'time__gt': last_alert_time
            },
            'observations': {
                'time__gt': last_obs_time
            }
        }
        with open(STATE_FILE, 'w') as f:
            json.dump(state, f)

        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        messages = [{'id': 5, 'time': '2015-05-01T01:02:05+00:00'}]
        mock_get.return_value.json.return_value = {'objects': messages}

        pub = NotificationPublisher()
        pub.execute()

        self.assertEquals(mock_get.call_args_list, [
            call(pub.api, 'alerts', {'time__gt': last_alert_time}),
            call(pub.api, 'observations', {'time__gt': last_obs_time}),
        ])

        self.assertEquals(mock_logger.info.call_args_list,
                          [call(m) for m in messages])
        self.assertEquals(mock_logger.error.call_args_list,
                          [call(m) for m in messages])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)

        self.assertEquals(
            state, {
                'alerts': {
                    'time__gt': '2015-05-01T01:02:05+00:00'
                },
                'observations': {
                    'time__gt': '2015-05-01T01:02:05+00:00'
                }
            })
Esempio n. 6
0
    def test_execute_resume(self, mock_create_logger, mock_get):
        last_alert_time = '2015-05-01T01:02:03+00:00'
        last_obs_time = '2015-05-01T01:02:02+00:00'

        state = {
            'alerts': {'time__gt': last_alert_time},
            'observations': {'time__gt': last_obs_time}
        }
        with open(STATE_FILE, 'w') as f:
            json.dump(state, f)

        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        messages = [{'id': 5, 'time': '2015-05-01T01:02:05+00:00'}]
        mock_get.return_value.json.return_value = {'objects': messages}

        pub = NotificationPublisher()
        pub.execute()

        self.assertEquals(
            mock_get.call_args_list,
            [
                call(pub.api, 'alerts', {'time__gt': last_alert_time}),
                call(pub.api, 'observations', {'time__gt': last_obs_time}),
            ]
        )

        self.assertEquals(mock_logger.info.call_args_list, [
            call(m) for m in messages
        ])
        self.assertEquals(mock_logger.error.call_args_list, [
            call(m) for m in messages
        ])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)

        self.assertEquals(
            state,
            {
                'alerts': {'time__gt': '2015-05-01T01:02:05+00:00'},
                'observations': {'time__gt': '2015-05-01T01:02:05+00:00'}
            }
        )
Esempio n. 7
0
    def test_execute_detail_only(self, mock_create_logger, mock_get, mock_now):
        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        messages = [
            {
                'id': 1,
                'time': '2015-05-01T01:02:03+00:00'
            },
            {
                'id': 2,
                'time': '2015-05-01T01:02:04+00:00'
            },
            {
                'id': 3,
                'time': '2015-05-01T01:02:05+00:00'
            },
        ]
        mock_get.return_value.json.return_value = {'objects': messages}

        now = datetime.utcnow().replace(tzinfo=utc)
        mock_now.return_value = now
        default_params = {'time__gt': now.isoformat()}

        with patch.dict('os.environ'):
            os.environ['OBSRVBL_NOTIFICATION_TYPES'] = 'alerts-detail bogus'
            pub = NotificationPublisher()
            pub.execute()

        self.assertEquals(mock_get.call_args_list, [
            call(pub.api, 'alert-notifications', default_params),
        ])

        self.assertEquals(mock_logger.error.call_args_list,
                          [call(m) for m in messages])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)

        self.assertEquals(state, {
            'alerts-detail': {
                'time__gt': '2015-05-01T01:02:05+00:00'
            },
        })
Esempio n. 8
0
    def test_execute_default(self, mock_create_logger, mock_get, mock_now):
        mock_logger = Mock()
        mock_create_logger.return_value = mock_logger

        messages = [
            {'id': 1, 'time': '2015-05-01T01:02:03+00:00'},
            {'id': 2, 'time': '2015-05-01T01:02:04+00:00'},
            {'id': 3, 'time': '2015-05-01T01:02:05+00:00'},
        ]
        mock_get.return_value.json.return_value = {'objects': messages}

        now = datetime.utcnow().replace(tzinfo=utc)
        mock_now.return_value = now
        default_params = {'time__gt': now.isoformat()}

        pub = NotificationPublisher()
        pub.execute()

        self.assertEquals(mock_get.call_args_list, [
            call(pub.api, 'alerts', default_params),
            call(pub.api, 'observations', default_params),
        ])

        self.assertEquals(mock_logger.info.call_args_list, [
            call(m) for m in messages
        ])
        self.assertEquals(mock_logger.error.call_args_list, [
            call(m) for m in messages
        ])

        with open(STATE_FILE, 'r') as f:
            state = json.load(f)

        self.assertEquals(
            state,
            {
                'alerts': {'time__gt': '2015-05-01T01:02:05+00:00'},
                'observations': {'time__gt': '2015-05-01T01:02:05+00:00'}
            }
        )