2015-03-06 18:42:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import python libs
|
2018-01-19 20:59:53 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-03-06 18:42:27 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
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
|
2015-03-06 18:42:27 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
from salt.exceptions import CommandExecutionError
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.uptime as uptime
|
2015-03-06 18:42:27 +00:00
|
|
|
|
2015-03-10 16:30:49 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
class RequestMock(Mock):
|
2018-01-19 20:59:53 +00:00
|
|
|
'''
|
|
|
|
Request Mock
|
|
|
|
'''
|
2015-03-10 12:01:37 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
return RequestResponseMock()
|
2015-03-10 11:02:37 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
return RequestPutResponseMock()
|
2015-03-10 11:02:37 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
return RequestResponseMock()
|
|
|
|
|
2015-03-10 16:30:49 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
class RequestResponseMock(Mock):
|
2015-03-10 11:02:37 +00:00
|
|
|
|
2015-03-06 18:42:27 +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-06 18:42:27 +00:00
|
|
|
|
2015-03-10 16:30:49 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
class RequestPutResponseMock(Mock):
|
2015-03-10 11:02:37 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
ok = True
|
2015-03-10 11:02:37 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
def json(self):
|
2015-03-10 16:30:49 +00:00
|
|
|
return {'_id': 4321}
|
2015-03-06 18:42:27 +00:00
|
|
|
|
|
|
|
REQUEST_MOCK = RequestMock()
|
|
|
|
|
2015-03-10 16:30:49 +00:00
|
|
|
|
2015-03-06 18:42:27 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class UptimeTestCase(TestCase, LoaderModuleMockMixin):
|
2018-01-19 20:59:53 +00:00
|
|
|
'''
|
|
|
|
UptimeTestCase
|
|
|
|
'''
|
2015-03-06 18:42:27 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
2017-02-19 15:35:30 +00:00
|
|
|
return {
|
2017-03-22 12:12:36 +00:00
|
|
|
uptime: {
|
|
|
|
'__salt__': {
|
|
|
|
'pillar.get': Mock(return_value='http://localhost:5000'),
|
|
|
|
'requests.put': Mock(),
|
|
|
|
},
|
|
|
|
'requests': REQUEST_MOCK
|
|
|
|
}
|
2017-02-19 15:35:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 18:42:27 +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'])
|
2015-03-06 18:42:27 +00:00
|
|
|
|
|
|
|
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)
|
2015-03-06 18:42:27 +00:00
|
|
|
|
|
|
|
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)
|