test_slack.py 3.8 KB

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