2015-11-25 15:14:35 +00:00
|
|
|
#!/usr/bin/env python
|
2015-11-23 15:09:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class TestNotifier(unittest.TestCase):
|
2015-12-14 09:02:20 +00:00
|
|
|
@mock.patch('pungi.paths.translate_path')
|
2015-11-23 15:09:01 +00:00
|
|
|
@mock.patch('kobo.shortcuts.run')
|
2015-12-14 09:02:20 +00:00
|
|
|
def test_invokes_script(self, run, translate_path):
|
2015-11-23 15:09:01 +00:00
|
|
|
compose = mock.Mock(
|
|
|
|
compose_id='COMPOSE_ID',
|
|
|
|
paths=mock.Mock(
|
|
|
|
compose=mock.Mock(
|
|
|
|
topdir=mock.Mock(return_value='/a/b')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-11-26 07:45:33 +00:00
|
|
|
n = PungiNotifier('run-notify')
|
|
|
|
n.compose = compose
|
2015-11-23 15:09:01 +00:00
|
|
|
data = {'foo': 'bar', 'baz': 'quux'}
|
|
|
|
n.send('cmd', **data)
|
|
|
|
|
|
|
|
data['compose_id'] = 'COMPOSE_ID'
|
2015-12-14 09:02:20 +00:00
|
|
|
data['location'] = '/a/b'
|
2015-11-23 15:09:01 +00:00
|
|
|
run.assert_called_once_with(('run-notify', 'cmd'),
|
|
|
|
stdin_data=json.dumps(data),
|
|
|
|
can_fail=True, return_stdout=False, workdir='/a/b')
|
|
|
|
|
|
|
|
@mock.patch('kobo.shortcuts.run')
|
|
|
|
def test_does_not_run_without_config(self, run):
|
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)
|
|
|
|
|
2015-12-14 09:02:20 +00:00
|
|
|
@mock.patch('pungi.paths.translate_path')
|
2015-11-23 15:09:01 +00:00
|
|
|
@mock.patch('kobo.shortcuts.run')
|
2015-12-14 09:02:20 +00:00
|
|
|
def test_logs_warning_on_failure(self, run, translate_path):
|
2015-11-23 15:09:01 +00:00
|
|
|
compose = mock.Mock(
|
|
|
|
compose_id='COMPOSE_ID',
|
|
|
|
log_warning=mock.Mock(),
|
|
|
|
paths=mock.Mock(
|
|
|
|
compose=mock.Mock(
|
|
|
|
topdir=mock.Mock(return_value='/a/b')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
|
2015-11-26 07:45:33 +00:00
|
|
|
n = PungiNotifier('run-notify')
|
|
|
|
n.compose = compose
|
2015-11-23 15:09:01 +00:00
|
|
|
n.send('cmd')
|
|
|
|
|
|
|
|
run.assert_called_once_with(('run-notify', 'cmd'),
|
2015-12-14 09:02:20 +00:00
|
|
|
stdin_data=json.dumps({'compose_id': 'COMPOSE_ID', 'location': '/a/b'}),
|
2015-11-23 15:09:01 +00:00
|
|
|
can_fail=True, return_stdout=False, workdir='/a/b')
|
|
|
|
self.assertTrue(compose.log_warning.called)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|