summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2016-02-11 12:53:07 +0200
committerYaroslav Brustinov <ybrustin@cisco.com>2016-02-11 12:53:07 +0200
commit9adfbada8a429dcdd7bf95d9b52d28bb3448e1d4 (patch)
treed8473c54c1520103a5ae45200868ffb949d11592 /scripts/automation/trex_control_plane
parent4b3092339d578f2c08de2fa1a55de5dea5ecf053 (diff)
validate_type for easier type checks, hltapi move to trex_stl_lib
Diffstat (limited to 'scripts/automation/trex_control_plane')
-rwxr-xr-xscripts/automation/trex_control_plane/client_utils/general_utils.py8
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py10
-rwxr-xr-xscripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py (renamed from scripts/automation/trex_control_plane/client/trex_hltapi.py)3
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py15
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/common.py9
5 files changed, 33 insertions, 12 deletions
diff --git a/scripts/automation/trex_control_plane/client_utils/general_utils.py b/scripts/automation/trex_control_plane/client_utils/general_utils.py
index 9a510ec5..d2521f02 100755
--- a/scripts/automation/trex_control_plane/client_utils/general_utils.py
+++ b/scripts/automation/trex_control_plane/client_utils/general_utils.py
@@ -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
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py
index 45acc72e..e84b032b 100644
--- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py
@@ -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)
-
diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py
index fc507497..0afc75e7 100755
--- a/scripts/automation/trex_control_plane/client/trex_hltapi.py
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py
@@ -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):
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
index 1164076b..d387ac9c 100644
--- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
@@ -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')
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/common.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/common.py
index 117017c3..9490c1b0 100644
--- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/common.py
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/common.py
@@ -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