Change in vdsm[master]: netconfpersistence: overload the subtraction operator

asegurap at redhat.com asegurap at redhat.com
Tue Nov 26 16:02:31 UTC 2013


Antoni Segura Puimedon has uploaded a new change for review.

Change subject: netconfpersistence: overload the subtraction operator
......................................................................

netconfpersistence: overload the subtraction operator

This patch makes it easy to diff two network configurations
in a way that gives you a resulting Config object that can
be used for a setupNetworks operation.

If the current configuration is B and we want to put the
system in state A, we'd do:

    diff = A - B
    setupNetwork(diff.networks, diff.bonds, {})

Change-Id: I70a3be5610492fd3cb27d49703e04a84cfad57d1
Signed-off-by: Antoni S. Puimedon <asegurap at redhat.com>
---
M lib/vdsm/netconfpersistence.py
M tests/netconfpersistenceTests.py
2 files changed, 46 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/28/21728/1

diff --git a/lib/vdsm/netconfpersistence.py b/lib/vdsm/netconfpersistence.py
index 0cc6805..d4aad23 100644
--- a/lib/vdsm/netconfpersistence.py
+++ b/lib/vdsm/netconfpersistence.py
@@ -50,6 +50,26 @@
         return '%s(%s, %s)' % (self.__class__.__name__, self.networks,
                                self.bonds)
 
+    def __sub__(self, other):
+        """Returns a diff that shows the config that should be changed for
+        going from other to self."""
+        diff = Config('')
+        diff.networks = self._confDictDiff(self.networks, other.networks)
+        diff.bonds = self._confDictDiff(self.bonds, other.bonds)
+        return diff
+
+    @staticmethod
+    def _confDictDiff(lhs, rhs):
+        result = {}
+        for name in rhs:
+            if name not in lhs:
+                result[name] = {'remove': True}
+
+        for name, attr in lhs.iteritems():
+            if name not in rhs or attr != rhs[name]:
+                result[name] = lhs[name]
+        return result
+
     def _networkPath(self, network):
         return self.networksPath + network
 
diff --git a/tests/netconfpersistenceTests.py b/tests/netconfpersistenceTests.py
index 7a96f6b..0d5d9ae 100644
--- a/tests/netconfpersistenceTests.py
+++ b/tests/netconfpersistenceTests.py
@@ -78,3 +78,29 @@
         self.assertTrue(os.path.exists(filePath))
         persistence.delete()
         self.assertFalse(os.path.exists(filePath))
+
+    def testDiff(self):
+        configA = Config(self.tempdir)
+        configA.setNetwork(NETWORK, NETWORK_ATTRIBUTES)
+        configA.setBonding(BONDING, BONDING_ATTRIBUTES)
+
+        configB = Config(self.tempdir)
+        configB.setNetwork(NETWORK, NETWORK_ATTRIBUTES)
+        configB.setBonding(BONDING, BONDING_ATTRIBUTES)
+
+        diff = configA - configB
+        self.assertEqual(diff.networks, {})
+        self.assertEqual(diff.bonds, {})
+
+        EVIL_NETWORK = 'jarjar'
+        EVIL_BONDING_ATTRIBUTES = {'options': 'mode=3', 'nics': ['eth3']}
+        configB.setNetwork(EVIL_NETWORK, NETWORK_ATTRIBUTES)
+        configB.setBonding(BONDING, EVIL_BONDING_ATTRIBUTES)
+
+        diff = configA - configB
+        self.assertEqual(diff.networks[EVIL_NETWORK], {'remove': True})
+        self.assertEqual(diff.bonds[BONDING], BONDING_ATTRIBUTES)
+
+        configB.removeNetwork(NETWORK)
+        diff = configA - configB
+        self.assertIn(NETWORK, diff.networks)


-- 
To view, visit http://gerrit.ovirt.org/21728
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I70a3be5610492fd3cb27d49703e04a84cfad57d1
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegurap at redhat.com>


More information about the vdsm-patches mailing list