e4055cc57d2302d39d84153b595c9a39ad1f296e
[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         RapidLog.debug(test_params)
63         monitor_gen = monitor_sut = False
64         background_machines = []
65         sut_machine = gen_machine = None
66         configonly = test_params['configonly']
67         for machine_params in test_params['machines']:
68             if 'gencores' in machine_params.keys():
69                 machine = RapidGeneratorMachine(test_params['key'],
70                         test_params['user'], test_params['password'],
71                         test_params['vim_type'], test_params['rundir'],
72                         test_params['resultsdir'], machine_params, configonly,
73                         test_params['ipv6'])
74                 if machine_params['monitor']:
75                     if monitor_gen:
76                         RapidLog.exception("Can only monitor 1 generator")
77                         raise Exception("Can only monitor 1 generator")
78                     else:
79                         monitor_gen = True
80                         gen_machine = machine
81                 else:
82                     background_machines.append(machine)
83             else:
84                 machine = RapidMachine(test_params['key'], test_params['user'],
85                         test_params['password'], test_params['vim_type'],
86                         test_params['rundir'], test_params['resultsdir'],
87                         machine_params, configonly)
88                 if machine_params['monitor']:
89                     if monitor_sut:
90                         RapidLog.exception("Can only monitor 1 sut")
91                         raise Exception("Can only monitor 1 sut")
92                     else:
93                         monitor_sut = True
94                         if machine_params['prox_socket']:
95                             sut_machine = machine
96             self.machines.append(machine)
97         try:
98             prox_executor = concurrent.futures.ThreadPoolExecutor(max_workers=len(self.machines))
99             self.future_to_prox = {prox_executor.submit(machine.start_prox): machine for machine in self.machines}
100             if configonly:
101                 concurrent.futures.wait(self.future_to_prox,return_when=ALL_COMPLETED)
102                 sys.exit()
103             socket_executor = concurrent.futures.ThreadPoolExecutor(max_workers=len(self.machines))
104             future_to_connect_prox = {socket_executor.submit(machine.connect_prox): machine for machine in self.machines}
105             concurrent.futures.wait(future_to_connect_prox,return_when=ALL_COMPLETED)
106             result = 0
107             for test_param in test_params['tests']:
108                 RapidLog.info(test_param['test'])
109                 if test_param['test'] in ['flowsizetest', 'TST009test',
110                         'fixed_rate', 'increment_till_fail']:
111                     test = FlowSizeTest(test_param,
112                             test_params['lat_percentile'],
113                             test_params['runtime'],
114                             test_params['TestName'],
115                             test_params['environment_file'],
116                             gen_machine,
117                             sut_machine, background_machines)
118                 elif test_param['test'] in ['corestatstest']:
119                     test = CoreStatsTest(test_param,
120                             test_params['runtime'],
121                             test_params['TestName'],
122                             test_params['environment_file'],
123                             self.machines)
124                 elif test_param['test'] in ['portstatstest']:
125                     test = PortStatsTest(test_param,
126                             test_params['runtime'],
127                             test_params['TestName'],
128                             test_params['environment_file'],
129                             self.machines)
130                 elif test_param['test'] in ['impairtest']:
131                     test = ImpairTest(test_param,
132                             test_params['lat_percentile'],
133                             test_params['runtime'],
134                             test_params['TestName'],
135                             test_params['environment_file'],
136                             gen_machine,
137                             sut_machine, background_machines)
138                 elif test_param['test'] in ['irqtest']:
139                     test = IrqTest(test_param,
140                             test_params['runtime'],
141                             test_params['TestName'],
142                             test_params['environment_file'],
143                             self.machines)
144                 elif test_param['test'] in ['warmuptest']:
145                     test = WarmupTest(test_param,
146                             gen_machine)
147                 else:
148                     RapidLog.debug('Test name ({}) is not valid:'.format(
149                         test_param['test']))
150                 single_test_result, result_details = test.run()
151                 result = result + single_test_result
152             for machine in self.machines:
153                 machine.close_prox()
154             concurrent.futures.wait(self.future_to_prox,
155                     return_when=ALL_COMPLETED)
156         except (ConnectionError, KeyboardInterrupt) as e:
157             result = result_details = None
158             socket_executor.shutdown(wait=False)
159             socket_executor._threads.clear()
160             prox_executor.shutdown(wait=False)
161             prox_executor._threads.clear()
162             concurrent.futures.thread._threads_queues.clear()
163             RapidLog.error("Test interrupted: {} {}".format(
164                 type(e).__name__,e))
165         return (result, result_details)
166
167 def main():
168     """Main function.
169     """
170     test_params = RapidTestManager.get_defaults()
171     # When no cli is used, the process_cli can be replaced by code modifying
172     # test_params
173     test_params = RapidCli.process_cli(test_params)
174     _, test_file_name = os.path.split(test_params['test_file'])
175     _, environment_file_name = os.path.split(test_params['environment_file'])
176     if 'resultsdir' in test_params:
177         res_dir = test_params['resultsdir']
178         log_file = '{}/RUN{}.{}.log'.format(res_dir,environment_file_name,
179                 test_file_name)
180     else:
181         log_file = 'RUN{}.{}.log'.format(environment_file_name, test_file_name)
182     RapidLog.log_init(log_file, test_params['loglevel'],
183             test_params['screenloglevel'] , test_params['version']  )
184     test_manager = RapidTestManager()
185     test_result, _ = test_manager.run_tests(test_params)
186     RapidLog.log_close()
187
188 if __name__ == "__main__":
189     main()