1
0

test_slack.py 3.9 KB

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