fix machine name in DEBUG logging
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / runrapid.py
1 #!/usr/bin/python3
2
3 ##
4 ## Copyright (c) 2010-2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18 from __future__ import print_function
19 from __future__ import print_function
20 from __future__ import division
21
22 from future import standard_library
23 standard_library.install_aliases()
24 from builtins import object
25 import os
26 import sys
27 import concurrent.futures
28 from concurrent.futures import ALL_COMPLETED
29 from rapid_cli import RapidCli
30 from rapid_log import RapidLog
31 from rapid_parser import RapidConfigParser
32 from rapid_defaults import RapidDefaults
33 from rapid_machine import RapidMachine
34 from rapid_generator_machine import RapidGeneratorMachine
35 from rapid_flowsizetest import FlowSizeTest
36 from rapid_corestatstest import CoreStatsTest
37 from rapid_portstatstest import PortStatsTest
38 from rapid_impairtest import ImpairTest
39 from rapid_irqtest import IrqTest
40 from rapid_warmuptest import WarmupTest
41
42 class RapidTestManager(object):
43     """
44     RapidTestManager Class
45     """
46     def __init__(self):
47         """
48         Init Function
49         """
50         self.machines = []
51
52     def __del__(self):
53         for machine in self.machines:
54             machine.close_prox()
55
56     @staticmethod
57     def get_defaults():
58         return (RapidDefaults.test_params)
59
60     def run_tests(self, test_params):
61         test_params = RapidConfigParser.parse_config(test_params)
62         monitor_gen = monitor_sut = False
63         background_machines = []
64         sut_machine = gen_machine = None
65         configonly = test_params['configonly']
66         machine_names = []
67         machine_counter = {}
68         for machine_params in test_params['machines']:
69             if machine_params['name'] not in machine_names:
70                 machine_names.append(machine_params['name'])
71                 machine_counter[machine_params['name']] = 1
72             else:
73                 machine_counter[machine_params['name']] += 1
74                 machine_params['name'] = '{}_{}'.format(machine_params['name'],
75                         machine_counter[machine_params['name']])
76             if 'gencores' in machine_params.keys():
77                 machine = RapidGeneratorMachine(test_params['key'],
78                         test_params['user'], test_params['password'],
79                         test_params['vim_type'], test_params['rundir'],
80                         test_params['resultsdir'], machine_params, configonly,
81                         test_params['ipv6'])
82                 if machine_params['monitor']:
83                     if monitor_gen:
84                         RapidLog.exception("Can only monitor 1 generator")
85                         raise Exception("Can only monitor 1 generator")
86                     else:
87                         monitor_gen = True
88                         gen_machine = machine
89                 else:
90                     background_machines.append(machine)
91             else:
92                 machine = RapidMachine(test_params['key'], test_params['user'],
93                         test_params['password'], test_params['vim_type'],
94                         test_params['rundir'], test_params['resultsdir'],
95                         machine_params, configonly)
96                 if machine_params['monitor']:
97                     if monitor_sut:
98                         RapidLog.exception("Can only monitor 1 sut")
99                         raise Exception("Can only monitor 1 sut")
100                     else:
101                         monitor_sut = True
102                         if machine_params['prox_socket']:
103                             sut_machine = machine
104             self.machines.append(machine)
105         RapidLog.debug(test_params)
106         try:
107             prox_executor = concurrent.futures.ThreadPoolExecutor(max_workers=len(self.machines))
108             self.future_to_prox = {prox_executor.submit(machine.start_prox): machine for machine in self.machines}
109             if configonly:
110                 concurrent.futures.wait(self.future_to_prox,return_when=ALL_COMPLETED)
111                 sys.exit()
112             socket_executor = concurrent.futures.ThreadPoolExecutor(max_workers=len(self.machines))
113             future_to_connect_prox = {socket_executor.submit(machine.connect_prox): machine for machine in self.machines}
114             concurrent.futures.wait(future_to_connect_prox,return_when=ALL_COMPLETED)
115             result = 0
116             for test_param in test_params['tests']:
117                 RapidLog.info(test_param['test'])
118                 if test_param['test'] in ['flowsizetest', 'TST009test',
119                         'fixed_rate', 'increment_till_fail']:
120                     test = FlowSizeTest(test_param,
121                             test_params['lat_percentile'],
122                             test_params['runtime'],
123                             test_params['TestName'],
124                             test_params['environment_file'],
125                             gen_machine,
126                             sut_machine, background_machines,
127                             test_params['sleep_time'])
128                 elif test_param['test'] in ['corestatstest']:
129                     test = CoreStatsTest(test_param,
130                             test_params['runtime'],
131                             test_params['TestName'],
132                             test_params['environment_file'],
133                             self.machines)
134                 elif test_param['test'] in ['portstatstest']:
135                     test = PortStatsTest(test_param,
136                             test_params['runtime'],
137                             test_params['TestName'],
138                             test_params['environment_file'],
139                             self.machines)
140                 elif test_param['test'] in ['impairtest']:
141                     test = ImpairTest(test_param,
142                             test_params['lat_percentile'],
143                             test_params['runtime'],
144                             test_params['TestName'],
145                             test_params['environment_file'],
146                             gen_machine,
147                             sut_machine, background_machines)
148                 elif test_param['test'] in ['irqtest']:
149                     test = IrqTest(test_param,
150                             test_params['runtime'],
151                             test_params['TestName'],
152                             test_params['environment_file'],
153                             self.machines)
154                 elif test_param['test'] in ['warmuptest']:
155                     test = WarmupTest(test_param,
156                             gen_machine)
157                 else:
158                     RapidLog.debug('Test name ({}) is not valid:'.format(
159                         test_param['test']))
160                 single_test_result, result_details = test.run()
161                 result = result + single_test_result
162             for machine in self.machines:
163                 machine.close_prox()
164             concurrent.futures.wait(self.future_to_prox,
165                     return_when=ALL_COMPLETED)
166         except (ConnectionError, KeyboardInterrupt) as e:
167             result = result_details = None
168             socket_executor.shutdown(wait=False)
169             socket_executor._threads.clear()
170             prox_executor.shutdown(wait=False)
171             prox_executor._threads.clear()
172             concurrent.futures.thread._threads_queues.clear()
173             RapidLog.error("Test interrupted: {} {}".format(
174                 type(e).__name__,e))
175         return (result, result_details)
176
177 def main():
178     """Main function.
179     """
180     test_params = RapidTestManager.get_defaults()
181     # When no cli is used, the process_cli can be replaced by code modifying
182     # test_params
183     test_params = RapidCli.process_cli(test_params)
184     _, test_file_name = os.path.split(test_params['test_file'])
185     _, environment_file_name = os.path.split(test_params['environment_file'])
186     if 'resultsdir' in test_params:
187         res_dir = test_params['resultsdir']
188         log_file = '{}/RUN{}.{}.log'.format(res_dir,environment_file_name,
189                 test_file_name)
190     else:
191         log_file = 'RUN{}.{}.log'.format(environment_file_name, test_file_name)
192     RapidLog.log_init(log_file, test_params['loglevel'],
193             test_params['screenloglevel'] , test_params['version']  )
194     test_manager = RapidTestManager()
195     test_result, _ = test_manager.run_tests(test_params)
196     RapidLog.log_close()
197
198 if __name__ == "__main__":
199     main()