validate_type for easier type checks, hltapi move to trex_stl_lib
authorYaroslav Brustinov <[email protected]>
Thu, 11 Feb 2016 10:53:07 +0000 (12:53 +0200)
committerYaroslav Brustinov <[email protected]>
Thu, 11 Feb 2016 10:53:07 +0000 (12:53 +0200)
scripts/automation/regression/hltapi_example.py
scripts/automation/regression/unit_tests/functional_tests/hltapi_stream_builder_test.py
scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py
scripts/automation/trex_control_plane/client_utils/general_utils.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py [moved from scripts/automation/trex_control_plane/client/trex_hltapi.py with 99% similarity]
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/common.py
scripts/automation/trex_perf.py

index 9e69cb1..5695d32 100755 (executable)
@@ -1,7 +1,7 @@
 #!/router/bin/python
 
 import outer_packages
-from client.trex_hltapi import CTRexHltApi, CStreamsPerPort
+from trex_stl_lib.trex_stl_hltapi import CTRexHltApi, CStreamsPerPort
 import traceback
 import sys, time
 from pprint import pprint
index 3b09d8a..534cc82 100755 (executable)
@@ -1,26 +1,27 @@
 #!/router/bin/python
 
-from client.trex_hltapi import CTRexHltApiBuilder
 import os
 import unittest
-
-gen_stream = CTRexHltApiBuilder.generate_stream
+from nose.plugins.attrib import attr
 
 def compare_yamls(yaml1, yaml2):
-    if type(yaml1) is not str:
-        raise Exception("yaml1 is '%s', expected str" % type(yaml1))
-    if type(yaml2) is not str:
-        raise Exception("yaml2 is '%s', expected str" % type(yaml2))
+    from trex_stl_lib.trex_stl_types import validate_type
+    validate_type('yaml1', yaml1, str)
+    validate_type('yaml2', yaml2, str)
     i = 0
     for line1, line2 in zip(yaml1.strip().split('\n'), yaml2.strip().split('\n')):
         i += 1
         if line1 != line2:
             raise Exception('yamls are not equal starting from line %s:\n%s\n\t<->\n%s' % (i, line1.strip(), line2.strip()))
 
+@attr('run_on_trex')
 class CTRexHltApi_Test(unittest.TestCase):
     ''' Checks correct HLTAPI creation of packet/VM '''
 
     def setUp(self):
+        from trex_stl_lib.trex_stl_hltapi import CTRexHltApiBuilder
+        self.gen_stream = CTRexHltApiBuilder.generate_stream
+
         self.golden_yaml = None
         self.test_yaml = None
 
@@ -29,7 +30,7 @@ class CTRexHltApi_Test(unittest.TestCase):
 
     # Eth/IP/TCP, all values default, no VM instructions
     def test_default(self):
-        test_stream = gen_stream(name = 'stream-0')
+        test_stream = self.gen_stream(name = 'stream-0')
         self.test_yaml = test_stream.dump_to_yaml(self.yaml_save_location())
         self.golden_yaml = '''
 - name: stream-0
@@ -52,7 +53,7 @@ class CTRexHltApi_Test(unittest.TestCase):
 
     # Eth/IP/TCP, ip src and dest is changed by VM
     def test_ip_ranges(self):
-        test_stream = gen_stream(ip_src_addr = '192.168.1.1',
+        test_stream = self.gen_stream(ip_src_addr = '192.168.1.1',
                                  ip_src_mode = 'increment',
                                  ip_src_count = 5,
                                  ip_dst_addr = '5.5.5.5',
@@ -108,7 +109,7 @@ class CTRexHltApi_Test(unittest.TestCase):
 
     # Eth / IP / TCP, tcp ports are changed by VM
     def test_tcp_ranges(self):
-        test_stream = gen_stream(tcp_src_port_mode = 'decrement',
+        test_stream = self.gen_stream(tcp_src_port_mode = 'decrement',
                                  tcp_src_port_count = 10,
                                  tcp_dst_port_mode = 'random',
                                  tcp_dst_port_count = 10,
@@ -163,7 +164,7 @@ class CTRexHltApi_Test(unittest.TestCase):
     # Eth / IP / UDP, udp ports are changed by VM
     def test_udp_ranges(self):
         # UDP is not set, expecting ignore of wrong UDP arguments
-        gen_stream(udp_src_port_mode = 'qwerqwer',
+        self.gen_stream(udp_src_port_mode = 'qwerqwer',
                    udp_src_port_count = 'weqwer',
                    udp_src_port = 'qwerqwer',
                    udp_dst_port_mode = 'qwerqwe',
@@ -171,7 +172,7 @@ class CTRexHltApi_Test(unittest.TestCase):
                    udp_dst_port = 'sdfgsdfg')
         # UDP is set, expecting fail due to wrong UDP arguments
         with self.assertRaises(Exception):
-            gen_stream(l4_protocol = 'udp',
+            self.gen_stream(l4_protocol = 'udp',
                        udp_src_port_mode = 'qwerqwer',
                        udp_src_port_count = 'weqwer',
                        udp_src_port = 'qwerqwer',
@@ -179,7 +180,7 @@ class CTRexHltApi_Test(unittest.TestCase):
                        udp_dst_port_count = 'sfgsdfg',
                        udp_dst_port = 'sdfgsdfg')
         # generate it already with correct arguments
-        test_stream = gen_stream(l4_protocol = 'udp',
+        test_stream = self.gen_stream(l4_protocol = 'udp',
                                  udp_src_port_mode = 'decrement',
                                  udp_src_port_count = 10,
                                  udp_src_port = 1234,
@@ -237,15 +238,15 @@ class CTRexHltApi_Test(unittest.TestCase):
     def test_pkt_len_by_framesize(self):
         # frame_size_step should be 1 (as default)
         with self.assertRaises(Exception):
-            test_stream = gen_stream(length_mode = 'decrement',
+            test_stream = self.self.gen_stream(length_mode = 'decrement',
                                      frame_size_min = 100,
                                      frame_size_max = 3000,
                                      frame_size_step = 20)
         # just check errors, no compare to golden
-        gen_stream(length_mode = 'increment',
+        self.gen_stream(length_mode = 'increment',
                    frame_size_min = 100,
                    frame_size_max = 3000)
-        test_stream = gen_stream(length_mode = 'decrement',
+        test_stream = self.gen_stream(length_mode = 'decrement',
                                  frame_size_min = 100,
                                  frame_size_max = 3000,
                                  name = 'stream-0')
@@ -289,12 +290,12 @@ class CTRexHltApi_Test(unittest.TestCase):
     def test_pkt_len_by_l3length(self):
         # l3_length_step should be 1
         with self.assertRaises(Exception):
-            gen_stream(l4_protocol = 'udp',
+            self.gen_stream(l4_protocol = 'udp',
                        length_mode = 'random',
                        l3_length_min = 100,
                        l3_length_max = 400,
                        l3_length_step = 20)
-        test_stream = gen_stream(l4_protocol = 'udp',
+        test_stream = self.gen_stream(l4_protocol = 'udp',
                                  length_mode = 'random',
                                  l3_length_min = 100,
                                  l3_length_max = 400,
index ad5013b..eac6838 100644 (file)
@@ -105,7 +105,8 @@ class CStlBasic_Test(functional_general_test.CGeneralFunctional_Test):
 
         output_cap = os.path.join("/tmp/", "{0}_test.cap".format(testname))
         golden_cap = os.path.join(self.test_path, "stl/golden/{0}_golden.cap".format(testname))
-
+        if os.path.exists(output_cap):
+            os.unlink(output_cap)
         try:
             rc = self.run_sim(self.profiles[profile], output_cap, options, silent)
             assert_equal(rc, True)
@@ -120,6 +121,8 @@ class CStlBasic_Test(functional_general_test.CGeneralFunctional_Test):
         output_cap = "a.pcap"
         input_file =  os.path.join('stl/', profile)
         golden_file = os.path.join('exp',os.path.basename(profile).split('.')[0]+'.pcap');
+        if os.path.exists(output_cap):
+            os.unlink(output_cap)
         try:
             rc = self.run_sim(input_file, output_cap, options, silent)
             assert_equal(rc, True)
@@ -143,7 +146,7 @@ class CStlBasic_Test(functional_general_test.CGeneralFunctional_Test):
     def test_tuple_gen (self):
         self.golden_run("basic_tuple_gen", "imix_tuple_gen", "-m 50kpps --limit 500 --cores 8", silent = False)
 
-    def test_all_profiles (self):
+    def test_stl_profiles (self):
         p = [ 
             ["udp_1pkt_1mac_override.py","-m 1 -l 50",True],
             ["syn_attack.py","-m 1 -l 50",False],               # can't compare random now 
@@ -178,6 +181,14 @@ class CStlBasic_Test(functional_general_test.CGeneralFunctional_Test):
             self.run_py_profile_path (obj[0],obj[1],compare =obj[2], do_no_remove=False)
 
 
+    def test_hlt_profiles (self):
+        p = (
+            ['hlt/hlt_udp_inc_len_9k.py', '-m 1 -l 50', False],
+            )
+        
+
+        for obj in p:
+            self.run_py_profile_path (obj[0], obj[1], compare =obj[2], do_no_remove=False)
 
     # valgrind tests
     def test_valgrind_various_profiles (self):
index 9a510ec..d2521f0 100755 (executable)
@@ -90,14 +90,6 @@ def id_count_gen():
         yield return_id
         return_id += 1
 
-# try to get number from input, return None in case of fail
-def get_number(input):
-    try:
-        if type(input) in (int, long):
-            return input
-        return int(input)
-    except:
-        return None
 
 if __name__ == "__main__":
     pass
index 45acc72..e84b032 100644 (file)
@@ -10,6 +10,8 @@ class STLError(Exception):
 
     def __str__ (self):
         exc_type, exc_obj, exc_tb = sys.exc_info()
+        if not exc_tb:
+            return self.msg
         fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
 
 
@@ -35,7 +37,7 @@ class STLPortStateError(STLError):
         self.msg = "Operation '{0}' on port(s) '{1}' is not valid while port(s) '{2}'".format(op, port, state)
 
 
-# raised when argument is not valid for operation
+# raised when argument value is not valid for operation
 class STLArgumentError(STLError):
     def __init__ (self, name, got, valid_values = None, extended = None):
         self.msg = "Argument: '{0}' invalid value: '{1}'".format(name, got)
@@ -45,10 +47,14 @@ class STLArgumentError(STLError):
         if extended:
             self.msg += "\n{0}".format(extended)
 
+# raised when argument type is not valid for operation
+class STLTypeError(STLError):
+    def __init__ (self, arg_name, arg_type, valid_types):
+        self.msg = "Argument: '%s' invalid type: %s, expecting type(s): %s." % (arg_name, arg_type, valid_types)
+
 # raised when timeout occurs
 class STLTimeoutError(STLError):
     def __init__ (self, timeout):
         self.msg = "Timeout: operation took more than '{0}' seconds".format(timeout)
 
 
-
@@ -132,10 +132,9 @@ sys.path.append(os.path.join(CURRENT_PATH, '../stl/'))
 
 from trex_stl_lib.api import *
 
-from client_utils.general_utils import get_number
+from utils.common import get_number
 import socket
 import copy
-from misc_methods import print_r
 
 
 class HLT_ERR(dict):
index 1164076..d387ac9 100644 (file)
@@ -1,6 +1,7 @@
 
 from collections import namedtuple
 from utils.text_opts import *
+from trex_stl_exceptions import *
 
 RpcCmdData = namedtuple('RpcCmdData', ['method', 'params'])
 
@@ -93,3 +94,17 @@ def RC_ERR (err):
 
 def RC_WARN (warn):
     return RC(True, warn, is_warn = True)
+
+
+# validate type of arg
+# example1: validate_type('somearg', somearg, [int, long])
+# example2: validate_type('another_arg', another_arg, str)
+def validate_type(arg_name, arg, valid_types):
+    if type(valid_types) is list:
+        valid_types = tuple(valid_types)
+    if type(valid_types) is type or type(valid_types) is tuple:
+        if isinstance(arg, valid_types):
+            return
+        raise STLTypeError(arg_name, type(arg), valid_types)
+    else:
+        raise STLError('validate_type: valid_types should be type or list or tuple of types')
index 117017c..9490c1b 100644 (file)
@@ -45,3 +45,12 @@ def random_id_gen(length=8):
         for i in range(length):
             return_id += random.choice(id_chars)
         yield return_id
+
+# try to get number from input, return None in case of fail
+def get_number(input):
+    try:
+        if type(input) in (int, long):
+            return input
+        return int(input)
+    except:
+        return None
index beec506..6187476 100755 (executable)
@@ -643,7 +643,7 @@ def _trex_run (job_summary, m, duration):
 
     try:
         results = trex_thread.run(m, duration)
-    except Exception,e:
+    except Exception as e:
         p.stop()
         raise
 
@@ -1042,7 +1042,7 @@ def prepare_for_run (job_summary):
     # create dir for reports
     try:
         job_summary['job_dir'] = os.path.abspath( os.path.join(os.getcwd(), 'logs', job_summary['job_dir']) )
-        print job_summary['job_dir']
+        print(job_summary['job_dir'])
         os.makedirs( job_summary['job_dir'] )
         
     except OSError as err: