1
0

test_win_pkg.py 3.7 KB

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