test_win_pkg.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import textwrap
  6. import pytest
  7. # Import Salt Testing libs
  8. from tests.support.case import ModuleCase
  9. from tests.support.unit import skipIf
  10. from tests.support.runtests import RUNTIME_VARS
  11. # Import Salt libs
  12. import salt.utils.files
  13. import salt.utils.platform
  14. @skipIf(not salt.utils.platform.is_windows(), 'windows test only')
  15. @pytest.mark.windows_whitelisted
  16. class WinPKGTest(ModuleCase):
  17. '''
  18. Tests for salt.modules.win_pkg. There are already
  19. some pkg execution module tests in the the test
  20. integration.modules.test_pkg but this will be for
  21. specific windows software respository tests while
  22. using the win_pkg module.
  23. '''
  24. @classmethod
  25. def setUpClass(cls):
  26. cls.repo_dir = os.path.join(RUNTIME_VARS.FILES, 'file', 'base', 'win', 'repo-ng')
  27. cls.curl_sls_path = os.path.join(cls.repo_dir, 'curl.sls')
  28. def tearDown(self):
  29. if os.path.isfile(self.curl_sls_path):
  30. os.remove(self.curl_sls_path)
  31. @pytest.mark.destructive_test
  32. def test_adding_removing_pkg_sls(self):
  33. '''
  34. Test add and removing a new pkg sls
  35. in the windows software repository
  36. '''
  37. def _check_pkg(pkgs, check_refresh, exists=True):
  38. refresh = self.run_function('pkg.refresh_db')
  39. self.assertEqual(check_refresh, refresh['total'],
  40. msg='total returned {0}. Expected return {1}'.format(refresh['total'], check_refresh))
  41. repo_data = self.run_function('pkg.get_repo_data')
  42. repo_cache = os.path.join(RUNTIME_VARS.TMP, 'rootdir', 'cache', 'files', 'base', 'win', 'repo-ng')
  43. for pkg in pkgs:
  44. if exists:
  45. assert pkg in str(repo_data), str(repo_data)
  46. else:
  47. assert pkg not in str(repo_data), str(repo_data)
  48. for root, dirs, files in os.walk(repo_cache):
  49. if exists:
  50. assert pkg + '.sls' in files
  51. else:
  52. assert pkg + '.sls' not in files
  53. pkgs = ['putty', '7zip']
  54. # check putty and 7zip are in cache and repo query
  55. _check_pkg(pkgs, 2)
  56. # now add new sls
  57. with salt.utils.files.fopen(self.curl_sls_path, 'w') as fp_:
  58. fp_.write(textwrap.dedent('''
  59. curl:
  60. '7.46.0':
  61. full_name: 'cURL'
  62. {% if grains['cpuarch'] == 'AMD64' %}
  63. installer: 'salt://win/repo-ng/curl/curl-7.46.0-win64.msi'
  64. uninstaller: 'salt://win/repo-ng/curl/curl-7.46.0-win64.msi'
  65. {% else %}
  66. installer: 'salt://win/repo-ng/curl/curl-7.46.0-win32.msi'
  67. uninstaller: 'salt://win/repo-ng/curl/curl-7.46.0-win32.msi'
  68. {% endif %}
  69. install_flags: '/qn /norestart'
  70. uninstall_flags: '/qn /norestart'
  71. msiexec: True
  72. locale: en_US
  73. reboot: False
  74. '''))
  75. # now check if curl is also in cache and repo query
  76. pkgs.append('curl')
  77. for pkg in pkgs:
  78. self.assertIn(pkg + '.sls', os.listdir(self.repo_dir))
  79. _check_pkg(pkgs, 3)
  80. # remove curl sls and check its not in cache and repo query
  81. os.remove(self.curl_sls_path)
  82. _check_pkg(['curl'], 2, exists=False)