salt/tests/unit/modules/test_uptime.py

94 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import, print_function
# Import Salt Testing libs
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, Mock, patch
ensure_in_syspath('../../')
ensure_in_syspath('../../../')
# Import salt libs
from salt.exceptions import CommandExecutionError
from salt.modules import uptime
uptime.__grains__ = None # in order to stub it w/patch below
uptime.__salt__ = None # in order to stub it w/patch below
if NO_MOCK is False:
SALT_STUB = {
'pillar.get': Mock(return_value='http://localhost:5000'),
'requests.put': Mock(),
}
else:
SALT_STUB = {}
2015-03-10 16:30:49 +00:00
class RequestMock(Mock):
2015-03-10 11:02:37 +00:00
''' Request Mock'''
2015-03-10 12:01:37 +00:00
def get(self, *args, **kwargs):
return RequestResponseMock()
2015-03-10 11:02:37 +00:00
def put(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
return RequestPutResponseMock()
2015-03-10 11:02:37 +00:00
def delete(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
return RequestResponseMock()
2015-03-10 16:30:49 +00:00
class RequestResponseMock(Mock):
2015-03-10 11:02:37 +00:00
def json(self):
2015-03-10 16:30:49 +00:00
return [{'url': 'http://example.org',
2015-03-10 12:01:37 +00:00
'_id': 1234}, ]
2015-03-10 16:30:49 +00:00
class RequestPutResponseMock(Mock):
2015-03-10 11:02:37 +00:00
ok = True
2015-03-10 11:02:37 +00:00
def json(self):
2015-03-10 16:30:49 +00:00
return {'_id': 4321}
REQUEST_MOCK = RequestMock()
2015-03-10 16:30:49 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(uptime,
2015-03-10 10:32:16 +00:00
requests=REQUEST_MOCK,
__salt__=SALT_STUB)
class UptimeTestCase(TestCase):
2015-03-10 11:02:37 +00:00
''' UptimeTestCase'''
def test_checks_list(self):
ret = uptime.checks_list()
2015-03-10 16:30:49 +00:00
self.assertListEqual(ret, ['http://example.org'])
def test_checks_exists(self):
self.assertTrue(uptime.check_exists('http://example.org') is True)
def test_checks_create(self):
self.assertRaises(CommandExecutionError, uptime.create,
'http://example.org')
self.assertEqual(4321, uptime.create('http://example.com'))
2015-03-10 10:32:16 +00:00
self.assertEqual(('http://localhost:5000/api/checks',),
REQUEST_MOCK.args)
def test_checks_delete(self):
self.assertRaises(CommandExecutionError, uptime.delete,
'http://example.com')
self.assertTrue(uptime.delete('http://example.org') is True)
2015-03-10 10:32:16 +00:00
self.assertEqual(('http://localhost:5000/api/checks/1234',),
REQUEST_MOCK.args)
if __name__ == '__main__':
from integration import run_tests
run_tests(UptimeTestCase, needs_daemon=False)