test_zypp_plugins.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Bo Maryniuk <bo@suse.de>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. import imp
  8. import os
  9. import sys
  10. from tests.support.mock import MagicMock, patch
  11. # Import Salt Testing Libs
  12. from tests.support.unit import TestCase, skipIf
  13. try:
  14. from zypp_plugin import BogusIO
  15. HAS_ZYPP_PLUGIN = True
  16. except ImportError:
  17. HAS_ZYPP_PLUGIN = False
  18. if sys.version_info >= (3,):
  19. BUILTINS_OPEN = "builtins.open"
  20. else:
  21. BUILTINS_OPEN = "__builtin__.open"
  22. ZYPPNOTIFY_FILE = os.path.sep.join(
  23. os.path.dirname(__file__).split(os.path.sep)[:-2]
  24. + ["scripts", "suse", "zypper", "plugins", "commit", "zyppnotify"]
  25. )
  26. @skipIf(not HAS_ZYPP_PLUGIN, "zypp_plugin is missing.")
  27. class ZyppPluginsTestCase(TestCase):
  28. """
  29. Test shipped libzypp plugins.
  30. """
  31. @skipIf(
  32. not os.path.exists(ZYPPNOTIFY_FILE),
  33. "Required file '{}' does not exist.".format(ZYPPNOTIFY_FILE),
  34. )
  35. def test_drift_detector(self):
  36. """
  37. Test drift detector for a correct cookie file.
  38. Returns:
  39. """
  40. zyppnotify = imp.load_source("zyppnotify", ZYPPNOTIFY_FILE)
  41. drift = zyppnotify.DriftDetector()
  42. drift._get_mtime = MagicMock(return_value=123)
  43. drift._get_checksum = MagicMock(return_value="deadbeef")
  44. bogus_io = BogusIO()
  45. with patch(BUILTINS_OPEN, bogus_io):
  46. drift.PLUGINEND(None, None)
  47. self.assertEqual(str(bogus_io), "deadbeef 123\n")
  48. self.assertEqual(bogus_io.mode, "w")
  49. self.assertEqual(bogus_io.path, "/var/cache/salt/minion/rpmdb.cookie")