test_debconfmod.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@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.debconfmod as debconfmod
  16. import os
  17. class DebconfmodTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.DebconfmodTestCase
  20. '''
  21. def setup_loader_modules(self):
  22. return {debconfmod: {}}
  23. def test_get_selections(self):
  24. '''
  25. Test for Answers to debconf questions for all packages
  26. '''
  27. mock = MagicMock(return_value=[])
  28. with patch.dict(debconfmod.__salt__, {'cmd.run_stdout': mock}):
  29. with patch.object(debconfmod, '_unpack_lines', mock):
  30. self.assertEqual(debconfmod.get_selections(False), {})
  31. def test_show(self):
  32. '''
  33. Test for Answers to debconf questions for a package
  34. '''
  35. mock = MagicMock(return_value={})
  36. with patch.object(debconfmod, 'get_selections', mock):
  37. self.assertEqual(debconfmod.show('name'), None)
  38. def test_set_(self):
  39. '''
  40. Test for Set answers to debconf questions for a package.
  41. '''
  42. mock = MagicMock(return_value=None)
  43. with patch.object(os, 'write', mock):
  44. with patch.object(os, 'close', mock):
  45. with patch.object(debconfmod, '_set_file', mock):
  46. with patch.object(os, 'unlink', mock):
  47. self.assertTrue(debconfmod.set_('package',
  48. 'question',
  49. 'type', 'value'))
  50. def test_set_template(self):
  51. '''
  52. Test for Set answers to debconf questions from a template.
  53. '''
  54. mock = MagicMock(return_value='A')
  55. with patch.dict(debconfmod.__salt__, {'cp.get_template': mock}):
  56. with patch.object(debconfmod, 'set_file', mock):
  57. self.assertEqual(debconfmod.set_template('path',
  58. 'template',
  59. 'context',
  60. 'defaults',
  61. 'saltenv'), 'A')
  62. def test_set_file(self):
  63. '''
  64. Test for Set answers to debconf questions from a file.
  65. '''
  66. mock = MagicMock(return_value='A')
  67. with patch.dict(debconfmod.__salt__, {'cp.cache_file': mock}):
  68. mock = MagicMock(return_value=None)
  69. with patch.object(debconfmod, '_set_file', mock):
  70. self.assertTrue(debconfmod.set_file('path'))
  71. mock = MagicMock(return_value=False)
  72. with patch.dict(debconfmod.__salt__, {'cp.cache_file': mock}):
  73. mock = MagicMock(return_value=None)
  74. with patch.object(debconfmod, '_set_file', mock):
  75. self.assertFalse(debconfmod.set_file('path'))