test_win_pkg.py 3.6 KB

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