NFVBENCH-153 Add support for python3
[nfvbench.git] / test / mock_trex.py
1 # Copyright 2018 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14 """This module creates the missing Trex library classes when they are not installed."""
15
16 import sys
17
18 # Because trex_stl_lib may not be installed when running unit test
19 # nfvbench.traffic_client will try to import STLError:
20 # from trex.stl.api import STLError
21 # will raise ImportError: No module named trex.stl.api
22 # trex_gen.py will also try to import a number of trex.stl.api classes
23 try:
24     import trex.stl.api
25     assert trex.stl.api
26 except ImportError:
27     from types import ModuleType
28
29     # Make up a trex.stl.api.STLError class
30     class STLDummy(Exception):
31         """Dummy class."""
32
33     trex_lib_mod = ModuleType('trex')
34     sys.modules['trex'] = trex_lib_mod
35     stl_lib_mod = ModuleType('trex.stl')
36     trex_lib_mod.stl = stl_lib_mod
37     sys.modules['trex.stl'] = stl_lib_mod
38     api_mod = ModuleType('trex.stl.api')
39     stl_lib_mod.api = api_mod
40     sys.modules['trex.stl.api'] = api_mod
41     api_mod.STLError = STLDummy
42     api_mod.STLxyz = STLDummy
43     api_mod.CTRexVmInsFixHwCs = STLDummy
44     api_mod.Dot1Q = STLDummy
45     api_mod.Ether = STLDummy
46     api_mod.IP = STLDummy
47     api_mod.STLClient = STLDummy
48     api_mod.STLFlowLatencyStats = STLDummy
49     api_mod.STLFlowStats = STLDummy
50     api_mod.STLPktBuilder = STLDummy
51     api_mod.STLScVmRaw = STLDummy
52     api_mod.STLStream = STLDummy
53     api_mod.STLTXCont = STLDummy
54     api_mod.STLVmFixChecksumHw = STLDummy
55     api_mod.STLVmFlowVar = STLDummy
56     api_mod.STLVmFlowVarRepeatableRandom = STLDummy
57     api_mod.STLVmWrFlowVar = STLDummy
58     api_mod.UDP = STLDummy
59     api_mod.bind_layers = STLDummy
60     api_mod.FlagsField = STLDummy
61     api_mod.Packet = STLDummy
62     api_mod.ThreeBytesField = STLDummy
63     api_mod.XByteField = STLDummy
64
65     common_mod = ModuleType('trex.common')
66     trex_lib_mod.common = common_mod
67     sys.modules['trex.common'] = common_mod
68     services_mod = ModuleType('trex.common.services')
69     common_mod.services = services_mod
70     sys.modules['trex.common.services'] = services_mod
71     arp_mod = ModuleType('trex.common.services.trex_service_arp')
72     services_mod.trex_stl_service_arp = arp_mod
73     sys.modules['trex.common.services.trex_service_arp'] = arp_mod
74     arp_mod.ServiceARP = STLDummy
75
76 def no_op():
77     """Empty function."""