cherrypy_testclasses.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import os
  4. import salt.config
  5. from tests.support.mock import patch
  6. from tests.support.runtests import RUNTIME_VARS
  7. try:
  8. import cherrypy
  9. HAS_CHERRYPY = True
  10. except ImportError:
  11. HAS_CHERRYPY = False
  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(
  49. self.__get_opts__()
  50. or {
  51. "external_auth": {
  52. "auto": {"saltdev": ["@wheel", "@runner", ".*"]},
  53. "pam": {"saltdev": ["@wheel", "@runner", ".*"]},
  54. },
  55. "rest_cherrypy": {"port": 8000, "debug": True},
  56. }
  57. )
  58. root, apiopts, conf = app.get_app(base_opts)
  59. cherrypy.tree.mount(root, "/", conf)
  60. cherrypy.server.unsubscribe()
  61. cherrypy.engine.start()
  62. # Make sure cherrypy does not memleak on it's bus since it keeps
  63. # adding handlers without cleaning the old ones each time we setup
  64. # a new application
  65. for value in cherrypy.engine.listeners.values():
  66. value.clear()
  67. cherrypy.engine._priorities.clear()
  68. self.addCleanup(cherrypy.engine.exit)
  69. class Root(object):
  70. """
  71. The simplest CherryPy app needed to test individual tools
  72. """
  73. exposed = True
  74. _cp_config = {}
  75. def GET(self):
  76. return {"return": ["Hello world."]}
  77. def POST(self, *args, **kwargs):
  78. return {"return": [{"args": args}, {"kwargs": kwargs}]}
  79. if HAS_CHERRYPY:
  80. class BaseToolsTest(BaseCherryPyTestCase): # pylint: disable=E0102
  81. """
  82. A base class so tests can selectively turn individual tools on for testing
  83. """
  84. def __get_conf__(self):
  85. return {
  86. "/": {"request.dispatch": cherrypy.dispatch.MethodDispatcher()},
  87. }
  88. def __get_cp_config__(self):
  89. return {}
  90. def setUp(self):
  91. root = Root()
  92. patcher = patch.object(root, "_cp_config", self.__get_cp_config__())
  93. patcher.start()
  94. self.addCleanup(patcher.stop)
  95. # Make sure cherrypy does not memleak on it's bus since it keeps
  96. # adding handlers without cleaning the old ones each time we setup
  97. # a new application
  98. for value in cherrypy.engine.listeners.values():
  99. value.clear()
  100. cherrypy.engine._priorities.clear()
  101. app = cherrypy.tree.mount(root, "/", self.__get_conf__())
  102. cherrypy.server.unsubscribe()
  103. cherrypy.engine.start()
  104. self.addCleanup(cherrypy.engine.exit)