1
0

test_influxdb08_database.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.influxdb08_database as influxdb08_database
  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 InfluxdbDatabaseTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.influxdb08_database
  16. """
  17. def setup_loader_modules(self):
  18. return {influxdb08_database: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure that the named database is present.
  23. """
  24. name = "salt"
  25. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  26. mock = MagicMock(side_effect=[False, False, False, True])
  27. mock_t = MagicMock(side_effect=[True, False])
  28. with patch.dict(
  29. influxdb08_database.__salt__,
  30. {"influxdb08.db_exists": mock, "influxdb08.db_create": mock_t},
  31. ):
  32. with patch.dict(influxdb08_database.__opts__, {"test": True}):
  33. comt = "Database {0} is absent and needs to be created".format(name)
  34. ret.update({"comment": comt})
  35. self.assertDictEqual(influxdb08_database.present(name), ret)
  36. with patch.dict(influxdb08_database.__opts__, {"test": False}):
  37. comt = "Database {0} has been created".format(name)
  38. ret.update(
  39. {"comment": comt, "result": True, "changes": {"salt": "Present"}}
  40. )
  41. self.assertDictEqual(influxdb08_database.present(name), ret)
  42. comt = "Failed to create database {0}".format(name)
  43. ret.update({"comment": comt, "result": False, "changes": {}})
  44. self.assertDictEqual(influxdb08_database.present(name), ret)
  45. comt = "Database {0} is already present, so cannot be created".format(name)
  46. ret.update({"comment": comt, "result": True})
  47. self.assertDictEqual(influxdb08_database.present(name), ret)
  48. # 'absent' function tests: 1
  49. def test_absent(self):
  50. """
  51. Test to ensure that the named database is absent.
  52. """
  53. name = "salt"
  54. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  55. mock = MagicMock(side_effect=[True, True, True, False])
  56. mock_t = MagicMock(side_effect=[True, False])
  57. with patch.dict(
  58. influxdb08_database.__salt__,
  59. {"influxdb08.db_exists": mock, "influxdb08.db_remove": mock_t},
  60. ):
  61. with patch.dict(influxdb08_database.__opts__, {"test": True}):
  62. comt = "Database {0} is present and needs to be removed".format(name)
  63. ret.update({"comment": comt})
  64. self.assertDictEqual(influxdb08_database.absent(name), ret)
  65. with patch.dict(influxdb08_database.__opts__, {"test": False}):
  66. comt = "Database {0} has been removed".format(name)
  67. ret.update(
  68. {"comment": comt, "result": True, "changes": {"salt": "Absent"}}
  69. )
  70. self.assertDictEqual(influxdb08_database.absent(name), ret)
  71. comt = "Failed to remove database {0}".format(name)
  72. ret.update({"comment": comt, "result": False, "changes": {}})
  73. self.assertDictEqual(influxdb08_database.absent(name), ret)
  74. comt = "Database {0} is not present, so it cannot be removed".format(name)
  75. ret.update({"comment": comt, "result": True})
  76. self.assertDictEqual(influxdb08_database.absent(name), ret)