]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: assert sysctl values
authorRafael Zalamena <rzalamena@gmail.com>
Wed, 14 Jun 2017 13:30:10 +0000 (10:30 -0300)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 28 Nov 2018 01:22:11 +0000 (20:22 -0500)
Implemented two functions to help setting sysctl values:

* set_sysctl: set a sysctl and return an auditable return value
* assert_sysctl: uses the previous function to assert that the sysctl
  was set

tests/topotests/lib/topotest.py

index b5505d559c3f804eb3ddd75acaf17ff47e5790ba..e24c7ca5b5f251702a87a61f2d494def9703114b 100644 (file)
@@ -114,20 +114,40 @@ def addRouter(topo, name):
                          '/var/log']
     return topo.addNode(name, cls=Router, privateDirs=MyPrivateDirs)
 
+def set_sysctl(node, sysctl, value):
+    "Set a sysctl value and return None on success or an error string"
+    valuestr = '{}'.format(value)
+    command = "sysctl {0}={1}".format(sysctl, valuestr)
+    cmdret = node.cmd(command)
+
+    matches = re.search(r'([^ ]+) = ([^\s]+)', cmdret)
+    if matches is None:
+        return cmdret
+    if matches.group(1) != sysctl:
+        return cmdret
+    if matches.group(2) != valuestr:
+        return cmdret
+
+    return None
+
+def assert_sysctl(node, sysctl, value):
+    "Set and assert that the sysctl is set with the specified value."
+    assert set_sysctl(node, sysctl, value) is None
+
 class LinuxRouter(Node):
     "A Node with IPv4/IPv6 forwarding enabled."
 
     def config(self, **params):
         super(LinuxRouter, self).config(**params)
         # Enable forwarding on the router
-        self.cmd('sysctl net.ipv4.ip_forward=1')
-        self.cmd('sysctl net.ipv6.conf.all.forwarding=1')
+        assert_sysctl(self, 'net.ipv4.ip_forward', 1)
+        assert_sysctl(self, 'net.ipv6.conf.all.forwarding', 1)
     def terminate(self):
         """
         Terminate generic LinuxRouter Mininet instance
         """
-        self.cmd('sysctl net.ipv4.ip_forward=0')
-        self.cmd('sysctl net.ipv6.conf.all.forwarding=0')
+        set_sysctl(self, 'net.ipv4.ip_forward', 0)
+        set_sysctl(self, 'net.ipv6.conf.all.forwarding', 0)
         super(LinuxRouter, self).terminate()
 
 class Router(Node):
@@ -144,12 +164,13 @@ class Router(Node):
         else:
             raise Exception('No FRR or Quagga found in ususal location')
         # Enable forwarding on the router
-        self.cmd('sysctl net.ipv4.ip_forward=1')
-        self.cmd('sysctl net.ipv6.conf.all.forwarding=1')
+        assert_sysctl(self, 'net.ipv4.ip_forward', 1)
+        assert_sysctl(self, 'net.ipv6.conf.all.forwarding', 1)
         # Enable coredumps
-        self.cmd('sysctl kernel.core_uses_pid=1')
-        self.cmd('sysctl fs.suid_dumpable=2')
-        self.cmd("sysctl kernel.core_pattern=/tmp/%s_%%e_core-sig_%%s-pid_%%p.dmp" % self.name)
+        assert_sysctl(self, 'kernel.core_uses_pid', 1)
+        assert_sysctl(self, 'fs.suid_dumpable', 2)
+        corefile = '/tmp/{0}_%e_core-sig_%s-pid_%p.dmp'.format(self.name)
+        assert_sysctl(self, 'kernel.core_pattern', corefile)
         self.cmd('ulimit -c unlimited')
         # Set ownership of config files
         self.cmd('chown %s:%svty /etc/%s' % (self.routertype, self.routertype, self.routertype))
@@ -164,8 +185,8 @@ class Router(Node):
         #     self.cmd('kill -7 `cat %s`' % d.rstrip())
         #     self.waitOutput()
         # Disable forwarding
-        self.cmd('sysctl net.ipv4.ip_forward=0')
-        self.cmd('sysctl net.ipv6.conf.all.forwarding=0')
+        set_sysctl(self, 'net.ipv4.ip_forward', 0)
+        set_sysctl(self, 'net.ipv6.conf.all.forwarding', 0)
         super(Router, self).terminate()
     def stopRouter(self):
         # Stop Running Quagga or FRR Daemons