test_openstack_config.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. )
  14. # Import Salt Libs
  15. import salt.modules.openstack_config as openstack_config
  16. from salt.exceptions import CommandExecutionError
  17. class OpenstackConfigTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.openstack_config
  20. '''
  21. def setup_loader_modules(self):
  22. patcher = patch('salt.utils.path.which', MagicMock(return_value=True))
  23. patcher.start()
  24. self.addCleanup(patcher.stop)
  25. return {openstack_config: {}}
  26. # 'set_' function tests: 1
  27. def test_set(self):
  28. '''
  29. Test if it set a value in an OpenStack configuration file.
  30. '''
  31. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  32. 'stdout': 'salt'})
  33. with patch.dict(openstack_config.__salt__, {'cmd.run_all': mock}):
  34. self.assertEqual(openstack_config.set_('/etc/keystone/keys.conf',
  35. 'sql', 'connection', 'foo'),
  36. 'salt')
  37. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error',
  38. 'stdout': 'salt'})
  39. with patch.dict(openstack_config.__salt__, {'cmd.run_all': mock}):
  40. self.assertRaises(CommandExecutionError, openstack_config.set_,
  41. '/etc/keystone/keystone.conf', 'sql',
  42. 'connection', 'foo')
  43. # 'get' function tests: 1
  44. def test_get(self):
  45. '''
  46. Test if it get a value from an OpenStack configuration file.
  47. '''
  48. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  49. 'stdout': 'salt'})
  50. with patch.dict(openstack_config.__salt__, {'cmd.run_all': mock}):
  51. self.assertEqual(openstack_config.get('/etc/keystone/keys.conf',
  52. 'sql', 'connection'), 'salt')
  53. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error',
  54. 'stdout': 'salt'})
  55. with patch.dict(openstack_config.__salt__, {'cmd.run_all': mock}):
  56. self.assertRaises(CommandExecutionError, openstack_config.get,
  57. '/etc/key/keystone.conf', 'sql', 'connection')
  58. # 'delete' function tests: 1
  59. def test_delete(self):
  60. '''
  61. Test if it delete a value from an OpenStack configuration file.
  62. '''
  63. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  64. 'stdout': 'salt'})
  65. with patch.dict(openstack_config.__salt__, {'cmd.run_all': mock}):
  66. self.assertEqual(openstack_config.delete('/etc/keystone/keys.conf',
  67. 'sql', 'connection'),
  68. 'salt')
  69. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error',
  70. 'stdout': 'salt'})
  71. with patch.dict(openstack_config.__salt__, {'cmd.run_all': mock}):
  72. self.assertRaises(CommandExecutionError, openstack_config.delete,
  73. '/etc/key/keystone.conf', 'sql', 'connection')