Implement a nose cordtest plugin to restore test case configuration after failure/errors.
This is especially useful for tests like vsg where after a failure, one wants to restore back the original config.
The user needs to implement a:
configRestore @classmethod in their test case class to restore the configuration on test failures/errors.

Change-Id: I810e1586173bcd1066b42b6ce5cf815834cd38b8
diff --git a/src/test/utils/CordTestConfig.py b/src/test/utils/CordTestConfig.py
index fc4aafe..74c4889 100644
--- a/src/test/utils/CordTestConfig.py
+++ b/src/test/utils/CordTestConfig.py
@@ -3,6 +3,50 @@
 import json
 import os
 from nose.tools import assert_not_equal
+from nose.plugins import Plugin
+from CordTestUtils import log_test as log
+
+log.setLevel('INFO')
+
+class CordTestConfigRestore(Plugin):
+    name = 'cordTestConfigRestore'
+    context = None
+    restore_methods = ('configRestore', 'config_restore',)
+
+    def options(self, parser, env=os.environ):
+        super(CordTestConfigRestore, self).options(parser, env = env)
+
+    def configure(self, options, conf):
+        self.enabled = True
+
+    #just save the test case context on start
+    def startContext(self, context):
+        if inspect.isclass(context) and issubclass(context, unittest.TestCase):
+            if context.__name__.endswith('exchange'):
+                self.context = context
+
+    #reset the context on exit
+    def afterContext(self, context):
+        if inspect.isclass(context) and issubclass(context, unittest.TestCase):
+            if context.__name__.endswith('exchange'):
+                self.context = None
+
+    def doFailure(self, test, exception):
+        if self.context:
+            log.info('Inside test case failure for test: %s' %self.context.__name__)
+            for restore_method in self.restore_methods:
+                if hasattr(self.context, restore_method):
+                    method = getattr(self.context, restore_method)
+                    #check only for class/static methods
+                    if method.__self__ is self.context:
+                        method()
+                        break
+
+    def addError(self, test, exception):
+        self.doFailure(test, exception)
+
+    def addFailure(self, test, exception):
+        self.doFailure(test, exception)
 
 def setup_module(module):
     class_test = None
diff --git a/src/test/vsg/__init__.py b/src/test/vsg/__init__.py
index b2581e2..082009e 100644
--- a/src/test/vsg/__init__.py
+++ b/src/test/vsg/__init__.py
@@ -15,6 +15,7 @@
 #
 import os,sys
 import logging
+from nose import main as nosetest_main
 logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
 ##add the python path to lookup the utils
 working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
@@ -26,3 +27,5 @@
 __path__.append(subscriber_dir)
 cli_dir = os.path.join(working_dir, '../cli')
 __path__.append(cli_dir)
+from CordTestConfig import CordTestConfigRestore
+nosetest_main(addplugins = [ CordTestConfigRestore() ])
diff --git a/src/test/vsg/vsgTest.py b/src/test/vsg/vsgTest.py
index bb37e20..fc35925 100644
--- a/src/test/vsg/vsgTest.py
+++ b/src/test/vsg/vsgTest.py
@@ -361,12 +361,12 @@
         Algo:
         1. Get dhcp IP to vcpe interface in cord-tester
         2. Verifying vcpe interface gets dhcp IP
-        3. Ping to 8.8.8.8 and Verifying ping should success
+        3. Ping to 8.8.8.8 and Verifying ping succeeds
 	4. Now down the WAN interface of vcpe
-	5. Ping to 8.8.8.8 and Verifying ping should not success
+	5. Ping to 8.8.8.8 and Verifying ping fails
 	6. Now Up the WAN interface of vcpe
-	7. Ping to 8.8.8.8 and Verifying ping should success
-	8. Restoring management interface configuration in  cord-tester
+	7. Ping to 8.8.8.8 and Verifying ping succeeds
+	8. Restoring management interface configuration in cord-tester
         """
         host = '8.8.8.8'
         mgmt = 'eth0'
@@ -378,7 +378,7 @@
         assert_not_equal(vcpe_ip, None)
         log.info('Got DHCP IP %s for %s' %(vcpe_ip, self.vcpe_dhcp))
         log.info('Sending ICMP pings to host %s' %(host))
-        """st, _ = getstatusoutput('ping -c 1 {}'.format(host))
+        st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         if st != 0:
             VSGAccess.restore_interface_config(mgmt, vcpe = self.vcpe_dhcp)
         assert_equal(st, 0)
@@ -397,7 +397,7 @@
         assert_equal(st, True)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         VSGAccess.restore_interface_config(mgmt, vcpe = self.vcpe_dhcp)
-        assert_equal(st, 0)"""
+        assert_equal(st, 0)
 
     def test_vsg_for_external_connectivity_with_lan_interface_toggle_in_vcpe(self):
         """
@@ -453,7 +453,7 @@
 	"""
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -479,7 +479,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -508,7 +508,7 @@
         host1 = '8.8.8.8'
         host2 = '204.79.197.203'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host1))
         assert_equal(st, False)
@@ -537,7 +537,7 @@
         host1 = '8.8.8.8'
         host2 = '204.79.197.203'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host1))
         assert_equal(st, False)
@@ -572,7 +572,7 @@
         host1 = '8.8.8.8'
         host2 = '204.79.197.203'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host1))
         assert_equal(st, False)
@@ -604,7 +604,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -634,7 +634,7 @@
         host1 = '8.8.8.8'
         host2 = '204.79.197.203'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -664,7 +664,7 @@
         host1 = '204.79.197.46'
         host2 = '204.79.197.51'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host1))
         assert_equal(st, False)
@@ -694,7 +694,7 @@
         host2 = '204.79.197.51'
         host2 = '204.79.197.63'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host1))
         assert_equal(st, False)
@@ -726,7 +726,7 @@
         host = '8.8.8.8'
         source_ip = self.vcpe_dhcp
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -752,7 +752,7 @@
         host = '8.8.8.8'
         source_ip = self.vcpe_dhcp
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -780,7 +780,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -809,7 +809,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format('8.8.8.8'))
         assert_equal(st, False)
@@ -838,7 +838,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -856,7 +856,7 @@
     def test_vsg_firewall_changing_deny_rule_to_accept_rule_with_icmp_protocol_echo_reply_type(self, vcpe=None):
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, out1 = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -885,7 +885,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -920,7 +920,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -957,7 +957,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -989,7 +989,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -1017,7 +1017,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -1045,7 +1045,7 @@
         """
         host = '8.8.8.8'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)
@@ -1073,7 +1073,7 @@
         """
         host = 'www.google.com'
         if not vcpe:
-                vcpe = self.vcpe_container
+            vcpe = self.vcpe_container
         vsg = VSGAccess.get_vcpe_vsg(vcpe)
         st, _ = getstatusoutput('ping -c 1 {}'.format(host))
         assert_equal(st, False)