test_cimc.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import salt.exceptions
  4. import salt.proxy.cimc as cimc
  5. # Import Salt Testing Libs
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import MagicMock, patch
  8. from tests.support.unit import TestCase
  9. log = logging.getLogger(__name__)
  10. class CIMCProxyTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {
  13. cimc: {
  14. "__virtual__": MagicMock(return_value="cimc"),
  15. "DETAILS": {},
  16. "__pillar__": {},
  17. }
  18. }
  19. def setUp(self):
  20. self.opts = {"proxy": {"username": "xxxx", "password": "xxx", "host": "cimc"}}
  21. def test_init(self):
  22. # No host, returns False
  23. opts = {"proxy": {"username": "xxxx", "password": "xxx"}}
  24. ret = cimc.init(opts)
  25. self.assertFalse(ret)
  26. # No username , returns False
  27. opts = {"proxy": {"password": "xxx", "host": "cimc"}}
  28. ret = cimc.init(opts)
  29. self.assertFalse(ret)
  30. # No password, returns False
  31. opts = {"proxy": {"username": "xxxx", "host": "cimc"}}
  32. ret = cimc.init(opts)
  33. self.assertFalse(ret)
  34. with patch.object(cimc, "logon", return_value="9zVG5U8DFZNsTR") as mock_logon:
  35. with patch.object(
  36. cimc, "get_config_resolver_class", return_value="True"
  37. ) as mock_logon:
  38. ret = cimc.init(self.opts)
  39. self.assertEqual(cimc.DETAILS["url"], "https://cimc/nuova")
  40. self.assertEqual(cimc.DETAILS["username"], "xxxx")
  41. self.assertEqual(cimc.DETAILS["password"], "xxx")
  42. self.assertTrue(cimc.DETAILS["initialized"])
  43. def test__validate_response_code(self):
  44. with self.assertRaisesRegex(
  45. salt.exceptions.CommandExecutionError,
  46. "Did not receive a valid response from host.",
  47. ):
  48. cimc._validate_response_code("404")
  49. with patch.object(cimc, "logout", return_value=True) as mock_logout:
  50. with self.assertRaisesRegex(
  51. salt.exceptions.CommandExecutionError,
  52. "Did not receive a valid response from host.",
  53. ):
  54. cimc._validate_response_code("404", "9zVG5U8DFZNsTR")
  55. mock_logout.assert_called_once_with("9zVG5U8DFZNsTR")