test_custom_module.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Daniel Mizyrycki (mzdaniel@glidelink.net)
  4. tests.integration.cli.custom_module
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. Test salt-ssh sls with a custom module work.
  7. $ cat srv/custom_module.sls
  8. custom-module:
  9. module.run:
  10. - name: test.recho
  11. - text: hello
  12. $ cat srv/_modules/override_test.py
  13. __virtualname__ = 'test'
  14. def __virtual__():
  15. return __virtualname__
  16. def recho(text):
  17. return text[::-1]
  18. $ salt-ssh localhost state.sls custom_module
  19. localhost:
  20. olleh
  21. This test can be run in a small test suite with:
  22. $ python tests/runtests.py -C --ssh
  23. """
  24. from __future__ import absolute_import, print_function, unicode_literals
  25. import pytest
  26. from tests.support.case import SSHCase
  27. from tests.support.helpers import slowTest
  28. @pytest.mark.windows_whitelisted
  29. class SSHCustomModuleTest(SSHCase):
  30. """
  31. Test sls with custom module functionality using ssh
  32. """
  33. @slowTest
  34. def test_ssh_regular_module(self):
  35. """
  36. Test regular module work using SSHCase environment
  37. """
  38. expected = "hello"
  39. cmd = self.run_function("test.echo", arg=["hello"])
  40. self.assertEqual(expected, cmd)
  41. @slowTest
  42. def test_ssh_custom_module(self):
  43. """
  44. Test custom module work using SSHCase environment
  45. """
  46. expected = "hello"[::-1]
  47. cmd = self.run_function("test.recho", arg=["hello"])
  48. self.assertEqual(expected, cmd)
  49. @slowTest
  50. def test_ssh_sls_with_custom_module(self):
  51. """
  52. Test sls with custom module work using SSHCase environment
  53. """
  54. expected = {
  55. "module_|-regular-module_|-test.echo_|-run": "hello",
  56. "module_|-custom-module_|-test.recho_|-run": "olleh",
  57. }
  58. cmd = self.run_function("state.sls", arg=["custom_module"])
  59. for key in cmd:
  60. if not isinstance(cmd, dict) or not isinstance(cmd[key], dict):
  61. raise AssertionError("{0} is not a proper state return".format(cmd))
  62. elif not cmd[key]["result"]:
  63. raise AssertionError(cmd[key]["comment"])
  64. cmd_ret = cmd[key]["changes"].get("ret", None)
  65. self.assertEqual(cmd_ret, expected[key])