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