test_winrepo.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.config
  17. import salt.utils.path
  18. from salt.syspaths import BASE_FILE_ROOTS_DIR
  19. import salt.states.winrepo as winrepo
  20. class MockRunnerClient(object):
  21. '''
  22. Mock RunnerClient class
  23. '''
  24. def __init__(self):
  25. pass
  26. class RunnerClient(object):
  27. '''
  28. Mock RunnerClient class
  29. '''
  30. def __init__(self, master_config):
  31. '''
  32. init method
  33. '''
  34. @staticmethod
  35. def cmd(arg1, arg2):
  36. '''
  37. Mock cmd method
  38. '''
  39. # TODO: Figure out how to have this return an empty dict or a dict
  40. # TODO: with expected data
  41. return []
  42. class WinrepoTestCase(TestCase, LoaderModuleMockMixin):
  43. '''
  44. Validate the winrepo state
  45. '''
  46. def setup_loader_modules(self):
  47. patcher = patch('salt.states.winrepo.salt.runner', MockRunnerClient)
  48. patcher.start()
  49. self.addCleanup(patcher.stop)
  50. return {winrepo: {}}
  51. def test_genrepo(self):
  52. '''
  53. Test to refresh the winrepo.p file of the repository
  54. '''
  55. expected = {'name': 'salt',
  56. 'changes': {},
  57. 'result': False,
  58. 'comment': ''}
  59. mock_config = MagicMock(return_value={'winrepo_dir': 'salt',
  60. 'winrepo_cachefile': 'abc'})
  61. mock_stat = MagicMock(return_value=[0, 1, 2, 3, 4, 5, 6, 7, 8])
  62. mock_empty_list = MagicMock(return_value=[])
  63. with patch.object(salt.config, 'master_config', mock_config), \
  64. patch.object(os, 'stat', mock_stat), \
  65. patch.object(salt.utils.path, 'os_walk', mock_empty_list), \
  66. patch.dict(winrepo.__opts__, {'test': True}):
  67. # With test=True
  68. expected.update({'comment': '', 'result': None})
  69. self.assertDictEqual(winrepo.genrepo('salt'), expected)
  70. with patch.dict(winrepo.__opts__, {'test': False}):
  71. # With test=False
  72. expected.update({'result': True})
  73. self.assertDictEqual(winrepo.genrepo('salt'), expected)
  74. # Now with no changes, existing winrepo.p
  75. expected.update({'changes': {'winrepo': []}})
  76. self.assertDictEqual(winrepo.genrepo('salt', True), expected)
  77. def test_genrepo_no_dir(self):
  78. '''
  79. Test genrepo when the dir does not exist
  80. '''
  81. expected = {'name': 'salt',
  82. 'changes': {},
  83. 'result': False,
  84. 'comment': '{0} is missing'.format(
  85. os.sep.join([BASE_FILE_ROOTS_DIR, 'win', 'repo']))}
  86. with patch.dict(winrepo.__opts__, {'test': False}), \
  87. patch('os.path.exists', MagicMock(return_value=False)):
  88. ret = winrepo.genrepo('salt')
  89. self.assertDictEqual(ret, expected)
  90. def test_genrepo_no_dir_force(self):
  91. '''
  92. Test genrepo when the dir does not exist and force=True
  93. '''
  94. expected = {'name': 'salt',
  95. 'changes': {'winrepo': []},
  96. 'result': True,
  97. 'comment': ''}
  98. with patch.dict(winrepo.__opts__, {'test': False}), \
  99. patch('os.path.exists', MagicMock(return_value=False)):
  100. ret = winrepo.genrepo('salt', force=True)
  101. self.assertDictEqual(ret, expected)