test_chocolatey.py 3.6 KB

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