Esempio n. 1
0
    def setUp(self):
        self.mock_api = MagicMock()
        self.now = utcnow()
        self.later = self.now + timedelta(seconds=1)
        self.test_data = [b'line_1\n', b'line_2\n']

        self.inst = WatchNode('test_type', self.mock_api, timedelta(seconds=1))
        self.inst.last_send = self.now

        # Creates a temporary file in a known location
        self.tmpdir = mkdtemp()

        def fixed_temp_file(*args, **kwargs):
            return NamedTemporaryFile(delete=False, dir=self.tmpdir)

        self.fixed_temp_file = fixed_temp_file
Esempio n. 2
0
    def setUp(self):
        self.mock_api = MagicMock()
        self.now = utcnow()
        self.later = self.now + timedelta(seconds=1)
        self.test_data = [b'line_1\n', b'line_2\n']

        self.inst = WatchNode('test_type', self.mock_api, timedelta(seconds=1))
        self.inst.last_send = self.now

        # Creates a temporary file in a known location
        self.tmpdir = mkdtemp()

        def fixed_temp_file(*args, **kwargs):
            return NamedTemporaryFile(delete=False, dir=self.tmpdir)

        self.fixed_temp_file = fixed_temp_file
Esempio n. 3
0
    def test_execute_no_messages(self):
        # Empty response data - no publish attempts should happen
        response = Response()
        response.status_code = 200
        response._content = dumps({'objects': []})

        inst = self._get_instance()
        inst.api.get_data.return_value = response
        inst.logger = MagicMock()

        now = utcnow().replace(tzinfo=utc)
        inst.execute(now=now)

        self.assertEqual(inst.logger.error.call_count, 0)
        self.assertEqual(inst.logger.info.call_count, 0)

        # The state file should be filled in with the call time, even though
        # there were no messages
        for data_type in ('alerts', 'observations'):
            actual_dt = inst.state[data_type]['time__gt']
            expected_dt = now.isoformat()
            self.assertGreaterEqual(actual_dt, expected_dt)
Esempio n. 4
0
    def test_basic(self):
        api = MagicMock()
        data_path = 'file:///tmp/path.ext'
        api.send_file.return_value = data_path

        now = utcnow()

        m = mock_open()
        mock_path = 'ona_service.utils.NamedTemporaryFile'
        with patch(mock_path, m, create=True):
            send_observations(
                api=api,
                obs_type='some_type_v1',
                obs_data=[{
                    'key': 'value'
                }, {
                    'key': None
                }],
                now=now,
                suffix='some_suffix',
            )

        obs_1 = {'observation_type': 'some_type_v1', 'key': 'value'}
        obs_2 = {'observation_type': 'some_type_v1', 'key': None}
        m.return_value.write.assert_has_calls([
            MockCall(dumps(obs_1, sort_keys=True).encode('utf-8')),
            MockCall(b'\n'),
            MockCall(dumps(obs_2, sort_keys=True).encode('utf-8')),
            MockCall(b'\n'),
        ])

        self.assertEqual(api.send_file.call_count, 1)
        self.assertEqual(api.send_file.call_args[0][0], 'logs')
        self.assertEqual(api.send_file.call_args[0][2], now)
        self.assertEqual(api.send_file.call_args[1]['suffix'], 'some_suffix')

        data = {'path': data_path, 'log_type': 'observations'}
        api.send_signal.assert_called_once_with('logs', data)
Esempio n. 5
0
 def test_empty(self):
     api = MagicMock()
     send_observations(api, 'some_type_v1', [], utcnow())
     self.assertEqual(api.send_file.call_count, 0)
     self.assertEqual(api.send_signal.call_count, 0)