test_slack.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for the slack engine
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.config
  8. # Import Salt Libs
  9. import salt.engines.slack as slack
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase, skipIf
  14. @skipIf(slack.HAS_SLACKCLIENT is False, "The SlackClient is not installed")
  15. class EngineSlackTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.engine.slack
  18. """
  19. def setup_loader_modules(self):
  20. return {slack: {}}
  21. def setUp(self):
  22. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  23. token = "xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx"
  24. with patch.dict(slack.__opts__, mock_opts):
  25. with patch(
  26. "slackclient.SlackClient.rtm_connect", MagicMock(return_value=True)
  27. ):
  28. self.client = slack.SlackClient(token)
  29. def test_control_message_target(self):
  30. """
  31. Test slack engine: control_message_target
  32. """
  33. trigger_string = "!"
  34. loaded_groups = {
  35. "default": {
  36. "targets": {},
  37. "commands": set(["cmd.run", "test.ping"]),
  38. "default_target": {"tgt_type": "glob", "target": "*"},
  39. "users": set(["gareth"]),
  40. "aliases": {
  41. "whoami": {"cmd": "cmd.run whoami"},
  42. "list_pillar": {"cmd": "pillar.items"},
  43. },
  44. }
  45. }
  46. slack_user_name = "gareth"
  47. # Check for correct cmdline
  48. _expected = (True, {"tgt_type": "glob", "target": "*"}, ["cmd.run", "whoami"])
  49. text = "!cmd.run whoami"
  50. target_commandline = self.client.control_message_target(
  51. slack_user_name, text, loaded_groups, trigger_string
  52. )
  53. self.assertEqual(target_commandline, _expected)
  54. # Check aliases result in correct cmdline
  55. text = "!whoami"
  56. target_commandline = self.client.control_message_target(
  57. slack_user_name, text, loaded_groups, trigger_string
  58. )
  59. self.assertEqual(target_commandline, _expected)
  60. # Check pillar is overrided
  61. _expected = (
  62. True,
  63. {"tgt_type": "glob", "target": "*"},
  64. ["pillar.items", 'pillar={"hello": "world"}'],
  65. )
  66. text = r"""!list_pillar pillar='{"hello": "world"}'"""
  67. target_commandline = self.client.control_message_target(
  68. slack_user_name, text, loaded_groups, trigger_string
  69. )
  70. self.assertEqual(target_commandline, _expected)
  71. # Check target is overrided
  72. _expected = (
  73. True,
  74. {"tgt_type": "glob", "target": "localhost"},
  75. ["cmd.run", "whoami"],
  76. )
  77. text = "!cmd.run whoami target='localhost'"
  78. target_commandline = self.client.control_message_target(
  79. slack_user_name, text, loaded_groups, trigger_string
  80. )
  81. self.assertEqual(target_commandline, _expected)