salt/tests/unit/modules/test_uptime.py

90 lines
2.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt Testing libs
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
2017-02-19 15:35:30 +00:00
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, Mock
# Import salt libs
from salt.exceptions import CommandExecutionError
import salt.modules.uptime as uptime
2015-03-10 16:30:49 +00:00
class RequestMock(Mock):
'''
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}
2018-12-21 00:19:01 +00:00
REQUEST_MOCK = RequestMock()
2015-03-10 16:30:49 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class UptimeTestCase(TestCase, LoaderModuleMockMixin):
'''
UptimeTestCase
'''
def setup_loader_modules(self):
2017-02-19 15:35:30 +00:00
return {
uptime: {
'__salt__': {
'pillar.get': Mock(return_value='http://localhost:5000'),
'requests.put': Mock(),
},
'requests': REQUEST_MOCK
}
2017-02-19 15:35:30 +00:00
}
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)