1
0

cherrypy_testclasses.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. try:
  4. import cherrypy
  5. HAS_CHERRYPY = True
  6. except ImportError:
  7. HAS_CHERRYPY = False
  8. import os
  9. import salt.config
  10. from tests.support.mock import patch
  11. from tests.support.runtests import RUNTIME_VARS
  12. if HAS_CHERRYPY:
  13. from tests.support.cptestcase import BaseCherryPyTestCase
  14. from salt.netapi.rest_cherrypy import app
  15. else:
  16. from tests.support.unit import TestCase, skipIf
  17. @skipIf(HAS_CHERRYPY is False, 'The CherryPy python package needs to be installed')
  18. class BaseCherryPyTestCase(TestCase):
  19. pass
  20. class BaseToolsTest(BaseCherryPyTestCase):
  21. pass
  22. class BaseRestCherryPyTest(BaseCherryPyTestCase):
  23. '''
  24. A base TestCase subclass for the rest_cherrypy module
  25. This mocks all interactions with Salt-core and sets up a dummy
  26. (unsubscribed) CherryPy web server.
  27. '''
  28. def __get_opts__(self):
  29. return None
  30. @classmethod
  31. def setUpClass(cls):
  32. master_conf = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master')
  33. cls.config = salt.config.client_config(master_conf)
  34. cls.base_opts = {}
  35. cls.base_opts.update(cls.config)
  36. @classmethod
  37. def tearDownClass(cls):
  38. del cls.config
  39. del cls.base_opts
  40. def setUp(self):
  41. # Make a local reference to the CherryPy app so we can mock attributes.
  42. self.app = app
  43. self.addCleanup(delattr, self, 'app')
  44. master_conf = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master')
  45. client_config = salt.config.client_config(master_conf)
  46. base_opts = {}
  47. base_opts.update(client_config)
  48. base_opts.update(self.__get_opts__() or {
  49. 'external_auth': {
  50. 'auto': {
  51. 'saltdev': [
  52. '@wheel',
  53. '@runner',
  54. '.*',
  55. ],
  56. },
  57. 'pam': {
  58. 'saltdev': [
  59. '@wheel',
  60. '@runner',
  61. '.*',
  62. ],
  63. }
  64. },
  65. 'rest_cherrypy': {
  66. 'port': 8000,
  67. 'debug': True,
  68. },
  69. })
  70. root, apiopts, conf = app.get_app(base_opts)
  71. cherrypy.tree.mount(root, '/', conf)
  72. cherrypy.server.unsubscribe()
  73. cherrypy.engine.start()
  74. # Make sure cherrypy does not memleak on it's bus since it keeps
  75. # adding handlers without cleaning the old ones each time we setup
  76. # a new application
  77. for value in cherrypy.engine.listeners.values():
  78. value.clear()
  79. cherrypy.engine._priorities.clear()
  80. self.addCleanup(cherrypy.engine.exit)
  81. class Root(object):
  82. '''
  83. The simplest CherryPy app needed to test individual tools
  84. '''
  85. exposed = True
  86. _cp_config = {}
  87. def GET(self):
  88. return {'return': ['Hello world.']}
  89. def POST(self, *args, **kwargs):
  90. return {'return': [{'args': args}, {'kwargs': kwargs}]}
  91. if HAS_CHERRYPY:
  92. class BaseToolsTest(BaseCherryPyTestCase): # pylint: disable=E0102
  93. '''
  94. A base class so tests can selectively turn individual tools on for testing
  95. '''
  96. def __get_conf__(self):
  97. return {
  98. '/': {
  99. 'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
  100. },
  101. }
  102. def __get_cp_config__(self):
  103. return {}
  104. def setUp(self):
  105. root = Root()
  106. patcher = patch.object(root, '_cp_config', self.__get_cp_config__())
  107. patcher.start()
  108. self.addCleanup(patcher.stop)
  109. # Make sure cherrypy does not memleak on it's bus since it keeps
  110. # adding handlers without cleaning the old ones each time we setup
  111. # a new application
  112. for value in cherrypy.engine.listeners.values():
  113. value.clear()
  114. cherrypy.engine._priorities.clear()
  115. app = cherrypy.tree.mount(root, '/', self.__get_conf__())
  116. cherrypy.server.unsubscribe()
  117. cherrypy.engine.start()
  118. self.addCleanup(cherrypy.engine.exit)