c4ce9d7b8db02581680e0fcd6afa5d1da06f9e5d
[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_lib.api import STLError
21 # will raise ImportError: No module named trex_stl_lib.api
22 # trex.py will also try to import a number of trex_stl_lib classes
23 try:
24     import trex_stl_lib.api
25     assert trex_stl_lib.api
26 except ImportError:
27     from types import ModuleType
28
29     # Make up a trex_stl_lib.api.STLError class
30     class STLDummy(Exception):
31         """Dummy class."""
32
33         pass
34
35     stl_lib_mod = ModuleType('trex_stl_lib')
36     sys.modules['trex_stl_lib'] = stl_lib_mod
37     api_mod = ModuleType('trex_stl_lib.api')
38     stl_lib_mod.api = api_mod
39     sys.modules['trex_stl_lib.api'] = api_mod
40     api_mod.STLError = STLDummy
41     api_mod.STLxyz = STLDummy
42     api_mod.CTRexVmInsFixHwCs = STLDummy
43     api_mod.Dot1Q = STLDummy
44     api_mod.Ether = STLDummy
45     api_mod.IP = STLDummy
46     api_mod.STLClient = STLDummy
47     api_mod.STLFlowLatencyStats = STLDummy
48     api_mod.STLFlowStats = STLDummy
49     api_mod.STLPktBuilder = STLDummy
50     api_mod.STLScVmRaw = STLDummy
51     api_mod.STLStream = STLDummy
52     api_mod.STLTXCont = STLDummy
53     api_mod.STLVmFixChecksumHw = STLDummy
54     api_mod.STLVmFlowVar = STLDummy
55     api_mod.STLVmFlowVarRepetableRandom = STLDummy
56     api_mod.STLVmWrFlowVar = STLDummy
57     api_mod.UDP = STLDummy
58     api_mod.bind_layers = STLDummy
59     api_mod.FlagsField = STLDummy
60     api_mod.Packet = STLDummy
61     api_mod.ThreeBytesField = STLDummy
62     api_mod.XByteField = STLDummy
63
64     services_mod = ModuleType('trex_stl_lib.services')
65     stl_lib_mod.services = services_mod
66     sys.modules['trex_stl_lib.services'] = services_mod
67
68     arp_mod = ModuleType('trex_stl_lib.services.trex_stl_service_arp')
69     services_mod.trex_stl_service_arp = arp_mod
70     sys.modules['trex_stl_lib.services.trex_stl_service_arp'] = arp_mod
71     arp_mod.STLServiceARP = STLDummy
72
73 def no_op():
74     """Empty function."""
75     pass