2013-07-18 20:42:21 +00:00
|
|
|
# coding: utf-8
|
2014-06-19 11:59:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import cherrypy
|
|
|
|
HAS_CHERRYPY = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_CHERRYPY = False
|
|
|
|
|
2013-07-18 20:42:21 +00:00
|
|
|
import mock
|
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
if HAS_CHERRYPY:
|
|
|
|
from . cptestcase import BaseCherryPyTestCase
|
|
|
|
from salt.netapi.rest_cherrypy import app
|
|
|
|
else:
|
|
|
|
from salttesting.unit import TestCase, skipIf
|
|
|
|
|
|
|
|
@skipIf(HAS_CHERRYPY is False, 'The CherryPy python package needs to be installed')
|
|
|
|
class BaseCherryPyTestCase(TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BaseToolsTest(BaseCherryPyTestCase):
|
|
|
|
pass
|
2013-07-18 20:42:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseRestCherryPyTest(BaseCherryPyTestCase):
|
|
|
|
'''
|
|
|
|
A base TestCase subclass for the rest_cherrypy module
|
|
|
|
|
|
|
|
This mocks all interactions with Salt-core and sets up a dummy
|
|
|
|
(unsubscribed) CherryPy web server.
|
|
|
|
'''
|
2014-04-08 19:40:39 +00:00
|
|
|
__opts__ = None
|
|
|
|
|
2014-06-19 01:40:51 +00:00
|
|
|
@mock.patch('salt.netapi.NetapiClient', autospec=True)
|
2013-07-18 20:42:21 +00:00
|
|
|
@mock.patch('salt.auth.Resolver', autospec=True)
|
2014-04-04 04:01:12 +00:00
|
|
|
@mock.patch('salt.auth.LoadAuth', autospec=True)
|
2014-06-18 22:51:51 +00:00
|
|
|
@mock.patch('salt.utils.event.get_event', autospec=True)
|
2014-06-19 01:40:51 +00:00
|
|
|
def setUp(self, get_event, LoadAuth, Resolver, NetapiClient):
|
|
|
|
app.salt.netapi.NetapiClient = NetapiClient
|
2013-07-18 20:42:21 +00:00
|
|
|
app.salt.auth.Resolver = Resolver
|
2014-04-04 04:01:12 +00:00
|
|
|
app.salt.auth.LoadAuth = LoadAuth
|
2014-06-18 22:51:51 +00:00
|
|
|
app.salt.utils.event.get_event = get_event
|
2013-07-18 20:42:21 +00:00
|
|
|
|
|
|
|
# Make local references to mocked objects so individual tests can
|
|
|
|
# access and modify the mocked interfaces.
|
|
|
|
self.Resolver = Resolver
|
2014-06-19 01:40:51 +00:00
|
|
|
self.NetapiClient = NetapiClient
|
2014-06-18 22:51:51 +00:00
|
|
|
self.get_event = get_event
|
2013-07-18 20:42:21 +00:00
|
|
|
|
2014-04-08 19:40:39 +00:00
|
|
|
__opts__ = self.__opts__ or {
|
2013-07-18 20:42:21 +00:00
|
|
|
'external_auth': {
|
|
|
|
'auto': {
|
|
|
|
'saltdev': [
|
|
|
|
'@wheel',
|
|
|
|
'@runner',
|
|
|
|
'.*',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'rest_cherrypy': {
|
|
|
|
'port': 8000,
|
|
|
|
'debug': True,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
root, apiopts, conf = app.get_app(__opts__)
|
|
|
|
|
|
|
|
cherrypy.tree.mount(root, '/', conf)
|
|
|
|
cherrypy.server.unsubscribe()
|
|
|
|
cherrypy.engine.start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
cherrypy.engine.exit()
|
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
|
2014-02-26 21:43:44 +00:00
|
|
|
class Root(object):
|
|
|
|
'''
|
|
|
|
The simplest CherryPy app needed to test individual tools
|
|
|
|
'''
|
|
|
|
exposed = True
|
|
|
|
|
|
|
|
_cp_config = {}
|
|
|
|
|
|
|
|
def GET(self):
|
|
|
|
return {'return': ['Hello world.']}
|
|
|
|
|
|
|
|
def POST(self, *args, **kwargs):
|
|
|
|
return {'return': [{'args': args}, {'kwargs': kwargs}]}
|
|
|
|
|
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
if HAS_CHERRYPY:
|
|
|
|
class BaseToolsTest(BaseCherryPyTestCase):
|
|
|
|
'''
|
|
|
|
A base class so tests can selectively turn individual tools on for testing
|
|
|
|
'''
|
|
|
|
conf = {
|
|
|
|
'/': {
|
|
|
|
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
|
|
|
|
},
|
|
|
|
}
|
2014-02-26 21:43:44 +00:00
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
def setUp(self):
|
|
|
|
Root._cp_config = self._cp_config
|
|
|
|
root = Root()
|
2014-02-26 21:43:44 +00:00
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
cherrypy.tree.mount(root, '/', self.conf)
|
|
|
|
cherrypy.server.unsubscribe()
|
|
|
|
cherrypy.engine.start()
|
2014-02-26 21:43:44 +00:00
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
def tearDown(self):
|
|
|
|
cherrypy.engine.exit()
|