test_win_pkg.py 3.4 KB

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