2014-10-07 14:13:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2014-06-19 11:59:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import cherrypy
|
2014-09-29 11:34:13 +00:00
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
HAS_CHERRYPY = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_CHERRYPY = False
|
|
|
|
|
2014-07-14 23:09:25 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import salt.config
|
|
|
|
from ..integration import TMP_CONF_DIR
|
2013-07-18 20:42:21 +00:00
|
|
|
|
2014-06-19 11:59:23 +00:00
|
|
|
if HAS_CHERRYPY:
|
2014-09-29 11:34:13 +00:00
|
|
|
from .cptestcase import BaseCherryPyTestCase
|
2014-06-19 11:59:23 +00:00
|
|
|
from salt.netapi.rest_cherrypy import app
|
|
|
|
else:
|
2014-10-07 03:24:46 +00:00
|
|
|
from salttesting.unit import (
|
|
|
|
TestCase,
|
|
|
|
skipIf,
|
|
|
|
)
|
2014-06-19 11:59:23 +00:00
|
|
|
|
|
|
|
@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-07-14 23:09:25 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(BaseRestCherryPyTest, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
master_conf = os.path.join(TMP_CONF_DIR, 'master')
|
|
|
|
self.config = salt.config.client_config(master_conf)
|
|
|
|
|
2014-07-15 02:17:23 +00:00
|
|
|
def setUp(self, *args, **kwargs):
|
|
|
|
# Make a local reference to the CherryPy app so we can mock attributes.
|
|
|
|
self.app = app
|
2013-07-18 20:42:21 +00:00
|
|
|
|
2014-07-14 23:09:25 +00:00
|
|
|
__opts__ = self.config.copy()
|
|
|
|
__opts__.update(self.__opts__ or {
|
2013-07-18 20:42:21 +00:00
|
|
|
'external_auth': {
|
|
|
|
'auto': {
|
|
|
|
'saltdev': [
|
|
|
|
'@wheel',
|
|
|
|
'@runner',
|
|
|
|
'.*',
|
|
|
|
],
|
2016-03-14 19:19:06 +00:00
|
|
|
},
|
|
|
|
'pam': {
|
|
|
|
'saltdev': [
|
|
|
|
'@wheel',
|
|
|
|
'@runner',
|
|
|
|
'.*',
|
|
|
|
],
|
|
|
|
|
2013-07-18 20:42:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'rest_cherrypy': {
|
|
|
|
'port': 8000,
|
|
|
|
'debug': True,
|
|
|
|
},
|
2014-07-14 23:09:25 +00:00
|
|
|
})
|
2013-07-18 20:42:21 +00:00
|
|
|
|
|
|
|
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:
|
2014-06-19 23:25:56 +00:00
|
|
|
class BaseToolsTest(BaseCherryPyTestCase): # pylint: disable=E0102
|
2014-06-19 11:59:23 +00:00
|
|
|
'''
|
|
|
|
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):
|
2016-10-25 07:51:30 +00:00
|
|
|
if not hasattr(self, '_cp_config'):
|
2016-10-25 07:53:18 +00:00
|
|
|
self._cp_config = {}
|
2014-06-19 11:59:23 +00:00
|
|
|
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()
|