test_cimc.py 2.1 KB

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