test_influxdb08_database.py 4.0 KB

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