test_boto_ec2.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.boto_ec2 as boto_ec2
  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 BotoEc2TestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.boto_ec2
  16. """
  17. def setup_loader_modules(self):
  18. return {boto_ec2: {}}
  19. # 'key_present' function tests: 1
  20. def test_key_present(self):
  21. """
  22. Test to ensure key pair is present.
  23. """
  24. name = "mykeypair"
  25. upublic = "salt://mybase/public_key.pub"
  26. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  27. mock = MagicMock(side_effect=[True, False, False])
  28. mock_bool = MagicMock(side_effect=[IOError, True])
  29. with patch.dict(
  30. boto_ec2.__salt__, {"boto_ec2.get_key": mock, "cp.get_file_str": mock_bool}
  31. ):
  32. comt = "The key name {0} already exists".format(name)
  33. ret.update({"comment": comt})
  34. self.assertDictEqual(boto_ec2.key_present(name), ret)
  35. comt = "File {0} not found.".format(upublic)
  36. ret.update({"comment": comt, "result": False})
  37. self.assertDictEqual(boto_ec2.key_present(name, upload_public=upublic), ret)
  38. with patch.dict(boto_ec2.__opts__, {"test": True}):
  39. comt = "The key {0} is set to be created.".format(name)
  40. ret.update({"comment": comt, "result": None})
  41. self.assertDictEqual(
  42. boto_ec2.key_present(name, upload_public=upublic), ret
  43. )
  44. # 'key_absent' function tests: 1
  45. def test_key_absent(self):
  46. """
  47. Test to deletes a key pair
  48. """
  49. name = "new_table"
  50. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  51. mock = MagicMock(side_effect=[False, True])
  52. with patch.dict(boto_ec2.__salt__, {"boto_ec2.get_key": mock}):
  53. comt = "The key name {0} does not exist".format(name)
  54. ret.update({"comment": comt})
  55. self.assertDictEqual(boto_ec2.key_absent(name), ret)
  56. with patch.dict(boto_ec2.__opts__, {"test": True}):
  57. comt = "The key {0} is set to be deleted.".format(name)
  58. ret.update({"comment": comt, "result": None})
  59. self.assertDictEqual(boto_ec2.key_absent(name), ret)