SHA256
1
0
forked from pool/salt
salt/0007-Add-zypp-notify-plugin.patch
Klaus Kämpf 44f6a131de Accepting request 438684 from systemsmanagement:saltstack:testing
- Update to 2016.3.4
  see https://docs.saltstack.com/en/latest/topics/releases/2016.3.4.html
- Removed Patches, applied upstream
  * 0008-checksum-validation-when-zypper-pkg.download.patch
  * 0009-unit-tests-for-rpm.checksum-and-zypper.download.patch
  * 0010-snapper-execution-module.patch
  * 0011-fix-salt-summary-to-count-not-responding-minions-cor.patch
  * 0012-Run-salt-api-as-user-salt-bsc-990029.patch
  * 0013-Deprecate-status.uptime-one-version-later.patch
  * 0014-Add-ignore_repo_failure-option-to-suppress-zypper-s-.patch
  * 0015-Remove-zypper-s-raise-exception-if-mod_repo-has-no-a.patch
  * 0016-Improve-Mock-to-be-flexible-and-able-to-mock-methods.patch
  * 0017-Check-for-single-quote-before-splitting-on-single-qu.patch
  * 0018-Unit-tests-fixes-for-2016.3.2.patch
  * 0019-Fix-snapper_test-for-python26.patch
  * 0020-Integration-tests-fixes-for-2016.3.2.patch
  * 0021-Fix-pkg.upgrade-for-zypper.patch
  * 0022-Setting-up-OS-grains-for-SLES-Expanded-Support-SUSE-.patch
  * 0023-acl.delfacl-fix-position-of-X-option-to-setfacl.patch
  * 0024-Change-travis-configuration-file-to-use-salt-toaster.patch
  * 0025-Adding-dist-upgrade-support-to-zypper-module.patch
  * 0026-Fix-pkg.latest_version-when-latest-already-installed.patch
  * 0027-Including-resolver-params-for-Zypper-debug-solver.patch
- Added patches
  * 0008-snapper-execution-module.patch
  * 0009-fix-salt-summary-to-count-not-responding-minions-cor.patch
  * 0010-Run-salt-api-as-user-salt-bsc-990029.patch
  * 0011-Fix-snapper_test-for-python26.patch
  * 0012-Fix-pkg.upgrade-for-zypper.patch
  * 0013-Setting-up-OS-grains-for-SLES-Expanded-Support-SUSE-.patch

OBS-URL: https://build.opensuse.org/request/show/438684
OBS-URL: https://build.opensuse.org/package/show/systemsmanagement:saltstack/salt?expand=0&rev=81
2016-11-06 11:48:16 +00:00

222 lines
5.7 KiB
Diff

From 484b0fa0250b4adc0da34d033ebc5e019e3e1240 Mon Sep 17 00:00:00 2001
From: Bo Maryniuk <bo@suse.de>
Date: Mon, 9 May 2016 10:33:44 +0200
Subject: [PATCH 07/17] Add zypp-notify plugin
* Add unit test to the libzypp drift detector plugin
---
scripts/zypper/plugins/commit/README.md | 3 ++
scripts/zypper/plugins/commit/zyppnotify | 59 +++++++++++++++++++++++++++++
tests/unit/zypp_plugins_test.py | 51 +++++++++++++++++++++++++
tests/zypp_plugin.py | 64 ++++++++++++++++++++++++++++++++
4 files changed, 177 insertions(+)
create mode 100644 scripts/zypper/plugins/commit/README.md
create mode 100755 scripts/zypper/plugins/commit/zyppnotify
create mode 100644 tests/unit/zypp_plugins_test.py
create mode 100644 tests/zypp_plugin.py
diff --git a/scripts/zypper/plugins/commit/README.md b/scripts/zypper/plugins/commit/README.md
new file mode 100644
index 0000000..01c8917
--- /dev/null
+++ b/scripts/zypper/plugins/commit/README.md
@@ -0,0 +1,3 @@
+# Zypper plugins
+
+Plugins here are required to interact with SUSE Manager in conjunction of SaltStack and Zypper.
diff --git a/scripts/zypper/plugins/commit/zyppnotify b/scripts/zypper/plugins/commit/zyppnotify
new file mode 100755
index 0000000..268298b
--- /dev/null
+++ b/scripts/zypper/plugins/commit/zyppnotify
@@ -0,0 +1,59 @@
+#!/usr/bin/python
+#
+# Copyright (c) 2016 SUSE Linux LLC
+# All Rights Reserved.
+#
+# Author: Bo Maryniuk <bo@suse.de>
+
+import sys
+import os
+import hashlib
+
+from zypp_plugin import Plugin
+
+
+class DriftDetector(Plugin):
+ """
+ Return diff of the installed packages outside the Salt.
+ """
+ def __init__(self):
+ Plugin.__init__(self)
+ self.ck_path = "/var/cache/salt/minion/rpmdb.cookie"
+ self.rpm_path = "/var/lib/rpm/Packages"
+
+ def _get_mtime(self):
+ '''
+ Get the modified time of the RPM Database.
+ Returns:
+ Unix ticks
+ '''
+ return os.path.exists(self.rpm_path) and int(os.path.getmtime(self.rpm_path)) or 0
+
+ def _get_checksum(self):
+ '''
+ Get the checksum of the RPM Database.
+ Returns:
+ hexdigest
+ '''
+ digest = hashlib.md5()
+ with open(self.rpm_path, "rb") as rpm_db_fh:
+ while True:
+ buff = rpm_db_fh.read(0x1000)
+ if not buff:
+ break
+ digest.update(buff)
+
+ return digest.hexdigest()
+
+ def PLUGINEND(self, headers, body):
+ """
+ Hook when plugin closes Zypper's transaction.
+ """
+ if 'SALT_RUNNING' not in os.environ:
+ with open(self.ck_path, 'w') as ck_fh:
+ ck_fh.write('{chksum} {mtime}\n'.format(chksum=self._get_checksum(), mtime=self._get_mtime()))
+
+ self.ack()
+
+
+DriftDetector().main()
diff --git a/tests/unit/zypp_plugins_test.py b/tests/unit/zypp_plugins_test.py
new file mode 100644
index 0000000..550403c
--- /dev/null
+++ b/tests/unit/zypp_plugins_test.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+'''
+ :codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
+'''
+
+# Import Python Libs
+from __future__ import absolute_import
+
+# Import Salt Testing Libs
+from salttesting.helpers import ensure_in_syspath
+from salttesting import TestCase, skipIf
+from salttesting.mock import (
+ MagicMock,
+ patch,
+ NO_MOCK,
+ NO_MOCK_REASON
+)
+
+ensure_in_syspath('../')
+
+import os
+import imp
+from zypp_plugin import BogusIO
+
+zyppnotify = imp.load_source('zyppnotify', os.path.sep.join(os.path.dirname(__file__).split(
+ os.path.sep)[:-2] + ['scripts', 'zypper', 'plugins', 'commit', 'zyppnotify']))
+
+@skipIf(NO_MOCK, NO_MOCK_REASON)
+class ZyppPluginsTestCase(TestCase):
+ '''
+ Test shipped libzypp plugins.
+ '''
+ def test_drift_detector(self):
+ '''
+ Test drift detector for a correct cookie file.
+ Returns:
+
+ '''
+ drift = zyppnotify.DriftDetector()
+ drift._get_mtime = MagicMock(return_value=123)
+ drift._get_checksum = MagicMock(return_value='deadbeef')
+ bogus_io = BogusIO()
+ with patch('zyppnotify.open', bogus_io):
+ drift.PLUGINEND(None, None)
+ self.assertEqual(str(bogus_io), 'deadbeef 123\n')
+ self.assertEqual(bogus_io.mode, 'w')
+ self.assertEqual(bogus_io.path, '/var/cache/salt/minion/rpmdb.cookie')
+
+if __name__ == '__main__':
+ from integration import run_tests
+ run_tests(ZyppPluginsTestCase, needs_daemon=False)
diff --git a/tests/zypp_plugin.py b/tests/zypp_plugin.py
new file mode 100644
index 0000000..218f703
--- /dev/null
+++ b/tests/zypp_plugin.py
@@ -0,0 +1,64 @@
+'''
+Related to zypp_plugins_test.py module.
+'''
+
+
+class Plugin(object):
+ '''
+ Bogus module for Zypp Plugins tests.
+ '''
+ def ack(self):
+ '''
+ Acknowledge that the plugin had finished the transaction
+ Returns:
+
+ '''
+
+ def main(self):
+ '''
+ Register plugin
+ Returns:
+
+ '''
+
+
+class BogusIO(object):
+ '''
+ Read/write logger.
+ '''
+
+ def __init__(self):
+ self.content = list()
+ self.closed = False
+
+ def __str__(self):
+ return '\n'.join(self.content)
+
+ def __call__(self, *args, **kwargs):
+ self.path, self.mode = args
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.close()
+
+ def __enter__(self):
+ return self
+
+ def write(self, data):
+ '''
+ Simulate writing data
+ Args:
+ data:
+
+ Returns:
+
+ '''
+ self.content.append(data)
+
+ def close(self):
+ '''
+ Simulate closing the IO object.
+ Returns:
+
+ '''
+ self.closed = True
--
2.10.1