test_serverdensity_device.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.serverdensity_device as serverdensity_device
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class ServerdensityDeviceTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.serverdensity_device
  16. """
  17. def setup_loader_modules(self):
  18. return {serverdensity_device: {}}
  19. # 'monitored' function tests: 1
  20. def test_monitored(self):
  21. """
  22. Test to device is monitored with Server Density.
  23. """
  24. name = "my_special_server"
  25. ret = {"name": name, "changes": {}, "result": True, "comment": ""}
  26. mock_dict = MagicMock(return_value={"id": name})
  27. mock_t = MagicMock(side_effect=[True, {"agentKey": True}, [{"agentKey": True}]])
  28. mock_sd = MagicMock(side_effect=[["sd-agent"], [], True])
  29. with patch.multiple(
  30. serverdensity_device,
  31. __salt__={
  32. "status.all_status": mock_dict,
  33. "grains.items": mock_dict,
  34. "serverdensity_device.ls": mock_t,
  35. "pkg.list_pkgs": mock_sd,
  36. "serverdensity_device.install_agent": mock_sd,
  37. },
  38. __opts__={"test": False},
  39. ):
  40. comt = (
  41. "Such server name already exists in this"
  42. " Server Density account. And sd-agent is installed"
  43. )
  44. ret.update({"comment": comt})
  45. self.assertDictEqual(serverdensity_device.monitored(name), ret)
  46. comt = (
  47. "Successfully installed agent and created"
  48. " device in Server Density db."
  49. )
  50. ret.update(
  51. {
  52. "comment": comt,
  53. "changes": {
  54. "created_device": {"agentKey": True},
  55. "installed_agent": True,
  56. },
  57. }
  58. )
  59. self.assertDictEqual(serverdensity_device.monitored(name), ret)