commit 9d6a74ae16600349e5a414e9b545560581525571
Author: Jiri Pirko <jpirko(a)redhat.com>
Date: Fri Jul 1 14:11:37 2011 +0200
Tests: add TestPktCounter
Signed-off-by: Jiri Pirko <jpirko(a)redhat.com>
Tests/TestPktCounter.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 69 insertions(+), 0 deletions(-)
---
diff --git a/Tests/TestPktCounter.py b/Tests/TestPktCounter.py
new file mode 100644
index 0000000..92a155c
--- /dev/null
+++ b/Tests/TestPktCounter.py
@@ -0,0 +1,69 @@
+"""
+This module defines packet counter implemented by iptables
+
+Copyright 2011 Red Hat, Inc.
+Licensed under the GNU General Public License, version 2 as
+published by the Free Software Foundation; see COPYING for details.
+"""
+
+__author__ = """
+jpirko(a)redhat.com (Jiri Pirko)
+"""
+
+import logging
+import re
+import os
+from Common.TestsCommon import TestGeneric
+from Common.ExecCmd import exec_cmd, ExecCmdFail
+
+def get_pkt_count(indev_name, dport, proto):
+ if indev_name:
+ p_indev_name = indev_name
+ else:
+ p_indev_name = "\*"
+ if proto:
+ p_proto = proto
+ p_protodport = "%s dpt:%s" % (proto, dport)
+ else:
+ p_proto = "all"
+ p_protodport = ""
+ pttr =
(r'\s*(\d+)\s+\d+\s+%s\s+\-\-\s+%s\s+\*\s+0\.0\.0\.0\/0\s+0\.0\.0\.0\/0\s*%s'
+ % (p_proto, p_indev_name, p_protodport))
+ data_stdout = exec_cmd("iptables -L -v -x -n")[0]
+ match = re.search(pttr, data_stdout)
+ if not match:
+ return None
+ return match.groups()[0]
+
+class TestPktCounter(TestGeneric):
+ def run(self):
+ indev_name = self.get_opt("input_netdev_name")
+ dport = self.get_opt("dport")
+ proto = self.get_opt("proto")
+ params = ""
+ if indev_name:
+ params += " -i %s" % indev_name
+
+ if proto:
+ params += " -p %s" % proto
+
+ if dport:
+ params += " --dport %s" % dport
+
+ '''
+ Remove all same already existing rules
+ '''
+ while True:
+ if get_pkt_count(indev_name, dport, proto) == None:
+ break
+ exec_cmd("iptables -D INPUT%s" % params)
+
+ exec_cmd("iptables -I INPUT%s" % params)
+
+ self.wait_on_interrupt()
+
+ count = get_pkt_count(indev_name, dport, proto)
+
+ exec_cmd("iptables -D INPUT%s" % params)
+
+ return self.set_pass(res_data={"pkt_count": count})
Show replies by date