Signed-off-by: Dong, JunX <junx.dong@intel.com>
---
framework/packet.py | 11 ++++++-----
framework/tester.py | 26 ++++++++++++++------------
framework/utils.py | 7 +++++++
3 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/framework/packet.py b/framework/packet.py
index 633dd36..adfa985 100644
--- a/framework/packet.py
+++ b/framework/packet.py
@@ -32,11 +32,12 @@
Generic packet create, transmit and analyze module
Base on scapy(python program for packet manipulation)
"""
+import shutil
from importlib import import_module
from socket import AF_INET6
from scapy.all import *
-from .utils import convert_int2ip, convert_ip2int
+from .utils import convert_int2ip, convert_ip2int, get_module_path
# load extension layers
exec_file = os.path.realpath(__file__)
@@ -47,12 +48,13 @@ TMP_PATH = DTS_PATH[:-1] + '/output/tmp/pcap/' if exec_file.endswith('.pyc') els
if not os.path.exists(TMP_PATH):
os.system('mkdir -p %s' % TMP_PATH)
-# add /tmp/dep to sys.path
-sys.path.append('/tmp/dep')
+# copy dep/Dot1BR to scapy
+_contrib_module_path = get_module_path('scapy.contrib')
+shutil.copy(DTS_PATH + '/dep/scapy_modules/Dot1BR.py', _contrib_module_path)
scapy_modules_required = {'scapy.contrib.gtp': ['GTP_U_Header', 'GTPPDUSessionContainer'],
'scapy.contrib.lldp': ['LLDPDU', 'LLDPDUManagementAddress'],
- 'Dot1BR': ['Dot1BR'],
+ 'scapy.contrib.Dot1BR': ['Dot1BR'],
'scapy.contrib.pfcp': ['PFCP'],
'scapy.contrib.nsh': ['NSH'],
'scapy.contrib.igmp': ['IGMP'],
@@ -73,7 +75,6 @@ def get_scapy_module_impcmd():
cmd_li.append(f'from {m} import {",".join(scapy_modules_required[m])}')
return ';'.join(cmd_li)
-SCAPY_IMP_CMD = get_scapy_module_impcmd()
# packet generator type should be configured later
PACKETGEN = "scapy"
diff --git a/framework/tester.py b/framework/tester.py
index 9dba4e4..9b764e8 100644
--- a/framework/tester.py
+++ b/framework/tester.py
@@ -46,9 +46,9 @@ from .config import PktgenConf
from .crb import Crb
from .exception import ParameterInvalidException
from .packet import (
- SCAPY_IMP_CMD,
- Packet,
compare_pktload,
+ get_scapy_module_impcmd,
+ Packet,
start_tcpdump,
stop_and_load_tcpdump_packets,
strip_pktload,
@@ -92,7 +92,6 @@ class Tester(Crb):
self.bgItf = ''
self.re_run_time = 0
self.pktgen = None
- self.tmp_scapy_module_dir = '/tmp/dep'
# prepare for scapy env
self.scapy_sessions_li = list()
self.scapy_session = self.prepare_scapy_env()
@@ -109,17 +108,20 @@ class Tester(Crb):
session.send_expect('scapy', '>>> ')
file_dir = os.path.dirname(__file__).split(os.path.sep)
lib_path = os.path.sep.join(file_dir[:-1]) + '/dep/scapy_modules/'
- exists_flag = self.alt_session.session.send_expect(f'ls {self.tmp_scapy_module_dir}', '# ', verify=True)
- if exists_flag == 2:
- self.alt_session.session.send_expect(f'mkdir -p {self.tmp_scapy_module_dir}', '# ', verify=True)
- scapy_modules_path = [lib_path+i for i in os.listdir(lib_path) if i.endswith('.py')]
- path = ' '.join(scapy_modules_path)
- session.copy_file_to(src=path, dst=self.tmp_scapy_module_dir)
- session.session.send_expect(f"sys.path.append('{self.tmp_scapy_module_dir}')", ">>> ")
-
- out = session.session.send_expect(SCAPY_IMP_CMD, '>>> ')
+
+ # get contrib module path on tester
+ scapy_module_path = session.session.send_expect("os.path.dirname(sys.modules['scapy'].__file__)", '>>>')
+ contrib_module_path = scapy_module_path.replace("'", "") + '/contrib'
+
+ # copy scapy module to tester
+ scapy_dep_module_path = lib_path + 'Dot1BR.py'
+ session.copy_file_to(src=scapy_dep_module_path, dst=contrib_module_path)
+
+ # import scapy moudle to scapy APP
+ out = session.session.send_expect(get_scapy_module_impcmd(), '>>> ')
if 'ImportError' in out:
session.logger.warning(f'entering import error: {out}')
+
return session
def check_scapy_version(self):
diff --git a/framework/utils.py b/framework/utils.py
index 7075919..269bfbb 100644
--- a/framework/utils.py
+++ b/framework/utils.py
@@ -308,3 +308,10 @@ def check_dts_python_version():
"it is deprecated for use in DTS, "
"and will not work in future releases.")), file=sys.stderr)
print(RED("Please use Python >= 3.6.9 instead"), file=sys.stderr)
+
+def get_module_path(module_name):
+ from importlib import import_module
+ _module = import_module(module_name)
+ _module_file_path = _module.__file__
+ del _module
+ return os.path.dirname(_module_file_path)
--
1.8.3.1