test_chocolatey.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. Tests for the Chocolatey State
  3. """
  4. import logging
  5. import salt.utils.platform
  6. from tests.support.case import ModuleCase
  7. from tests.support.helpers import destructiveTest
  8. from tests.support.mixins import SaltReturnAssertsMixin
  9. from tests.support.unit import skipIf
  10. log = logging.getLogger(__name__)
  11. __testcontext__ = {}
  12. @destructiveTest
  13. @skipIf(not salt.utils.platform.is_windows(), "Windows Specific Test")
  14. class ChocolateyTest(ModuleCase, SaltReturnAssertsMixin):
  15. """
  16. Chocolatey State Tests
  17. These tests are destructive as the install and remove software
  18. """
  19. def setUp(self):
  20. """
  21. Ensure that Chocolatey is installed
  22. """
  23. super().setUp()
  24. if "chocolatey" not in __testcontext__:
  25. self.run_function("chocolatey.bootstrap")
  26. __testcontext__["chocolatey"] = True
  27. def test_chocolatey(self):
  28. """
  29. Test the following:
  30. - `chocolatey.installed`
  31. - `chocolatey.upgraded`
  32. - `chocolatey.uninstalled`
  33. - `chocolatey.source_present`
  34. """
  35. # If this assert fails, we need to find new targets, this test needs to
  36. # be able to test successful installation of packages, so this package
  37. # needs to NOT be installed before we run the states below
  38. target = "firefox"
  39. pre_version = "52.0.2"
  40. upg_version = "57.0.2"
  41. src_name = "test_repo"
  42. src_location = "https://repo.test.com/chocolatey"
  43. log.debug("Making sure %s is not installed", target)
  44. self.assertFalse(self.run_function("chocolatey.version", [target]))
  45. try:
  46. ####################################################
  47. # Test `chocolatey.installed`
  48. ####################################################
  49. # Install the package
  50. log.debug("Testing chocolatey.installed")
  51. ret = self.run_state(
  52. "chocolatey.installed", name=target, version=pre_version
  53. )
  54. self.assertSaltTrueReturn(ret)
  55. # Verify the package is installed
  56. log.debug("Verifying install success")
  57. ret = self.run_function("chocolatey.version", [target])
  58. self.assertEqual(ret, {"Firefox": [pre_version]})
  59. ####################################################
  60. # Test `chocolatey.upgraded`
  61. ####################################################
  62. # Upgrade the package
  63. log.debug("Testing chocolatey.upgraded")
  64. ret = self.run_state(
  65. "chocolatey.upgraded", name=target, version=upg_version
  66. )
  67. self.assertSaltTrueReturn(ret)
  68. # Verify the package is upgraded
  69. log.debug("Verifying upgrade success")
  70. ret = self.run_function("chocolatey.version", [target])
  71. self.assertEqual(ret, {"Firefox": [upg_version]})
  72. ####################################################
  73. # Test `chocolatey.uninstalled`
  74. ####################################################
  75. # uninstall the package
  76. log.debug("Testing chocolatey.uninstalled")
  77. ret = self.run_state("chocolatey.uninstalled", name=target)
  78. self.assertSaltTrueReturn(ret)
  79. # Verify the package is uninstalled
  80. log.debug("Verifying uninstall success")
  81. ret = self.run_function("chocolatey.version", [target])
  82. self.assertEqual(ret, {})
  83. ####################################################
  84. # Test `chocolatey.source_present`
  85. ####################################################
  86. # add the source
  87. log.debug("Testing chocolatey.source_present")
  88. ret = self.run_state(
  89. "chocolatey.source_present", name=src_name, source_location=src_location
  90. )
  91. self.assertSaltTrueReturn(ret)
  92. # Verify the source is present
  93. log.debug("Verifying source_add success")
  94. ret = self.run_function("chocolatey.list_sources")
  95. self.assertTrue(src_name in ret.keys())
  96. finally:
  97. # Always uninstall
  98. log.debug("Uninstalling %s", target)
  99. self.run_function("chocolatey.uninstall", [target])