2015-11-25 15:14:35 +00:00
|
|
|
#!/usr/bin/env python
|
2015-11-23 15:09:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
from datetime import datetime
|
2015-11-23 15:09:01 +00:00
|
|
|
import json
|
|
|
|
import mock
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
|
|
|
|
from pungi.notifier import PungiNotifier
|
|
|
|
|
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
mock_datetime = mock.Mock()
|
|
|
|
mock_datetime.utcnow.return_value = datetime(2017, 6, 28, 9, 34)
|
|
|
|
mock_datetime.side_effect = lambda *args, **kwargs: datetime(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('pungi.util.makedirs')
|
|
|
|
@mock.patch('pungi.notifier.datetime', new=mock_datetime)
|
2015-11-23 15:09:01 +00:00
|
|
|
class TestNotifier(unittest.TestCase):
|
2017-06-28 08:26:29 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestNotifier, self).setUp()
|
|
|
|
self.logfile = '/logs/notifications/notification-2017-06-28_09-34-00.log'
|
|
|
|
self.compose = mock.Mock(
|
2015-11-23 15:09:01 +00:00
|
|
|
compose_id='COMPOSE_ID',
|
2017-06-28 08:26:29 +00:00
|
|
|
log_warning=mock.Mock(),
|
2015-11-23 15:09:01 +00:00
|
|
|
paths=mock.Mock(
|
|
|
|
compose=mock.Mock(
|
|
|
|
topdir=mock.Mock(return_value='/a/b')
|
2017-06-28 08:26:29 +00:00
|
|
|
),
|
|
|
|
log=mock.Mock(
|
|
|
|
topdir=mock.Mock(return_value='/logs')
|
2015-11-23 15:09:01 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-06-28 08:26:29 +00:00
|
|
|
self.data = {'foo': 'bar', 'baz': 'quux'}
|
|
|
|
|
|
|
|
def _call(self, script, cmd, **kwargs):
|
|
|
|
data = self.data.copy()
|
|
|
|
data['compose_id'] = 'COMPOSE_ID'
|
|
|
|
data['location'] = '/a/b'
|
|
|
|
data.update(kwargs)
|
|
|
|
return mock.call((script, cmd),
|
|
|
|
stdin_data=json.dumps(data),
|
|
|
|
can_fail=True, return_stdout=False,
|
|
|
|
workdir=self.compose.paths.compose.topdir.return_value,
|
|
|
|
show_cmd=True, logfile=self.logfile)
|
2015-11-23 15:09:01 +00:00
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
@mock.patch('pungi.util.translate_path')
|
|
|
|
@mock.patch('kobo.shortcuts.run')
|
|
|
|
def test_invokes_script(self, run, translate_path, makedirs):
|
2015-11-23 15:09:01 +00:00
|
|
|
run.return_value = (0, None)
|
2015-12-14 09:02:20 +00:00
|
|
|
translate_path.side_effect = lambda compose, x: x
|
2015-11-23 15:09:01 +00:00
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
n = PungiNotifier(['run-notify'])
|
|
|
|
n.compose = self.compose
|
|
|
|
n.send('cmd', **self.data)
|
2015-11-23 15:09:01 +00:00
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
makedirs.assert_called_once_with('/logs/notifications')
|
|
|
|
self.assertItemsEqual(run.call_args_list, [self._call('run-notify', 'cmd')])
|
2015-11-23 15:09:01 +00:00
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
@mock.patch('pungi.util.translate_path')
|
2016-09-26 13:50:49 +00:00
|
|
|
@mock.patch('kobo.shortcuts.run')
|
2017-06-28 08:26:29 +00:00
|
|
|
def test_invokes_multiple_scripts(self, run, translate_path, makedirs):
|
|
|
|
run.return_value = (0, None)
|
|
|
|
translate_path.side_effect = lambda compose, x: x
|
|
|
|
|
|
|
|
n = PungiNotifier(['run-notify', 'ping-user'])
|
|
|
|
n.compose = self.compose
|
|
|
|
n.send('cmd', **self.data)
|
|
|
|
|
|
|
|
self.assertItemsEqual(
|
|
|
|
run.call_args_list,
|
|
|
|
[self._call('run-notify', 'cmd'),
|
|
|
|
self._call('ping-user', 'cmd')])
|
|
|
|
|
|
|
|
@mock.patch('kobo.shortcuts.run')
|
|
|
|
def test_translates_path(self, run, makedirs):
|
|
|
|
self.compose.paths.compose.topdir.return_value = '/root/a/b'
|
|
|
|
self.compose.conf = {
|
|
|
|
"translate_paths": [("/root/", "http://example.com/compose/")],
|
|
|
|
}
|
2016-09-26 13:50:49 +00:00
|
|
|
|
|
|
|
run.return_value = (0, None)
|
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
n = PungiNotifier(['run-notify'])
|
|
|
|
n.compose = self.compose
|
|
|
|
n.send('cmd', **self.data)
|
2016-09-26 13:50:49 +00:00
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
self.assertItemsEqual(
|
|
|
|
run.call_args_list,
|
|
|
|
[self._call('run-notify', 'cmd', location='http://example.com/compose/a/b')])
|
2016-09-26 13:50:49 +00:00
|
|
|
|
2015-11-23 15:09:01 +00:00
|
|
|
@mock.patch('kobo.shortcuts.run')
|
2017-06-28 08:26:29 +00:00
|
|
|
def test_does_not_run_without_config(self, run, makedirs):
|
2015-11-26 07:45:33 +00:00
|
|
|
n = PungiNotifier(None)
|
2015-11-23 15:09:01 +00:00
|
|
|
n.send('cmd', foo='bar', baz='quux')
|
|
|
|
self.assertFalse(run.called)
|
|
|
|
|
2017-03-24 21:20:55 +00:00
|
|
|
@mock.patch('pungi.util.translate_path')
|
2015-11-23 15:09:01 +00:00
|
|
|
@mock.patch('kobo.shortcuts.run')
|
2017-06-28 08:26:29 +00:00
|
|
|
def test_logs_warning_on_failure(self, run, translate_path, makedirs):
|
2015-12-14 09:02:20 +00:00
|
|
|
translate_path.side_effect = lambda compose, x: x
|
2015-11-23 15:09:01 +00:00
|
|
|
run.return_value = (1, None)
|
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
n = PungiNotifier(['run-notify'])
|
|
|
|
n.compose = self.compose
|
|
|
|
n.send('cmd', **self.data)
|
2015-11-23 15:09:01 +00:00
|
|
|
|
2017-06-28 08:26:29 +00:00
|
|
|
self.assertItemsEqual(run.call_args_list, [self._call('run-notify', 'cmd')])
|
|
|
|
self.assertTrue(self.compose.log_warning.called)
|
2015-11-23 15:09:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|