test_dpkg_lowpkg.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.dpkg_lowpkg as dpkg
  16. class DpkgTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.modules.dpkg
  19. '''
  20. def setup_loader_modules(self):
  21. return {dpkg: {}}
  22. # 'unpurge' function tests: 2
  23. def test_unpurge(self):
  24. '''
  25. Test if it change package selection for each package
  26. specified to 'install'
  27. '''
  28. mock = MagicMock(return_value=[])
  29. with patch.dict(dpkg.__salt__, {'pkg.list_pkgs': mock,
  30. 'cmd.run': mock}):
  31. self.assertDictEqual(dpkg.unpurge('curl'), {})
  32. def test_unpurge_empty_package(self):
  33. '''
  34. Test if it change package selection for each package
  35. specified to 'install'
  36. '''
  37. self.assertDictEqual(dpkg.unpurge(), {})
  38. # 'list_pkgs' function tests: 1
  39. def test_list_pkgs(self):
  40. '''
  41. Test if it lists the packages currently installed
  42. '''
  43. mock = MagicMock(return_value={'retcode': 0,
  44. 'stderr': '',
  45. 'stdout': 'Salt'})
  46. with patch.dict(dpkg.__salt__, {'cmd.run_all': mock}):
  47. self.assertDictEqual(dpkg.list_pkgs('httpd'), {})
  48. mock = MagicMock(return_value={'retcode': 1,
  49. 'stderr': 'error',
  50. 'stdout': 'Salt'})
  51. with patch.dict(dpkg.__salt__, {'cmd.run_all': mock}):
  52. self.assertEqual(dpkg.list_pkgs('httpd'), 'Error: error')
  53. # 'file_list' function tests: 1
  54. def test_file_list(self):
  55. '''
  56. Test if it lists the files that belong to a package.
  57. '''
  58. mock = MagicMock(return_value={'retcode': 0,
  59. 'stderr': '',
  60. 'stdout': 'Salt'})
  61. with patch.dict(dpkg.__salt__, {'cmd.run_all': mock}):
  62. self.assertDictEqual(dpkg.file_list('httpd'),
  63. {'errors': [], 'files': []})
  64. mock = MagicMock(return_value={'retcode': 1,
  65. 'stderr': 'error',
  66. 'stdout': 'Salt'})
  67. with patch.dict(dpkg.__salt__, {'cmd.run_all': mock}):
  68. self.assertEqual(dpkg.file_list('httpd'), 'Error: error')
  69. # 'file_dict' function tests: 1
  70. def test_file_dict(self):
  71. '''
  72. Test if it lists the files that belong to a package, grouped by package
  73. '''
  74. mock = MagicMock(return_value={'retcode': 0,
  75. 'stderr': '',
  76. 'stdout': 'Salt'})
  77. with patch.dict(dpkg.__salt__, {'cmd.run_all': mock}):
  78. self.assertDictEqual(dpkg.file_dict('httpd'),
  79. {'errors': [], 'packages': {}})
  80. mock = MagicMock(return_value={'retcode': 1,
  81. 'stderr': 'error',
  82. 'stdout': 'Salt'})
  83. with patch.dict(dpkg.__salt__, {'cmd.run_all': mock}):
  84. self.assertEqual(dpkg.file_dict('httpd'), 'Error: error')