2c18b232a6c954773174e7b1364d13a3cf2ea3d0
[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 sys
26 import concurrent.futures
27 from concurrent.futures import ALL_COMPLETED
28 from rapid_cli import RapidCli
29 from rapid_log import RapidLog
30 from rapid_parser import RapidConfigParser
31 from rapid_defaults import RapidDefaults
32 from rapid_machine import RapidMachine
33 from rapid_generator_machine import RapidGeneratorMachine
34 from rapid_flowsizetest import FlowSizeTest
35 from rapid_corestatstest import CoreStatsTest
36 from rapid_portstatstest import PortStatsTest
37 from rapid_impairtest import ImpairTest
38 from rapid_irqtest import IrqTest
39 from rapid_warmuptest import WarmupTest
40
41 class RapidTestManager(object):
42     """
43     RapidTestManager Class
44     """
45     @staticmethod
46     def get_defaults():
47         return (RapidDefaults.test_params)
48
49     @staticmethod
50     def run_tests(test_params):
51         test_params = RapidConfigParser.parse_config(test_params)
52         RapidLog.debug(test_params)
53         monitor_gen = monitor_sut = False
54         background_machines = []
55         sut_machine = gen_machine = None
56         machines = []
57         for machine_params in test_params['machines']:
58             if 'gencores' in machine_params.keys():
59                 machine = RapidGeneratorMachine(test_params['key'],
60                         test_params['user'], test_params['vim_type'],
61                         test_params['rundir'], machine_params,
62                         test_params['ipv6'])
63                 if machine_params['monitor']:
64                     if monitor_gen:
65                         RapidLog.exception("Can only monitor 1 generator")
66                         raise Exception("Can only monitor 1 generator")
67                     else:
68                         monitor_gen = True
69                         gen_machine = machine
70                 else:
71                     background_machines.append(machine)
72             else:
73                 machine = RapidMachine(test_params['key'], test_params['user'],
74                         test_params['vim_type'], test_params['rundir'],
75                         machine_params)
76                 if machine_params['monitor']:
77                     if monitor_sut:
78                         RapidLog.exception("Can only monitor 1 sut")
79                         raise Exception("Can only monitor 1 sut")
80                     else:
81                         monitor_sut = True
82                         sut_machine = machine
83             machines.append(machine)
84         if test_params['configonly']:
85             sys.exit()
86         prox_executor = concurrent.futures.ThreadPoolExecutor(max_workers=len(machines))
87         future_to_prox = {prox_executor.submit(machine.start_prox): machine for machine in machines}
88         with concurrent.futures.ThreadPoolExecutor(max_workers=len(machines)) as executor:
89             future_to_connect_prox = {executor.submit(machine.connect_prox): machine for machine in machines}
90             concurrent.futures.wait(future_to_connect_prox,return_when=ALL_COMPLETED)
91         result = True
92         for test_param in test_params['tests']:
93             RapidLog.info(test_param['test'])
94             if test_param['test'] in ['flowsizetest', 'TST009test',
95                     'fixed_rate', 'increment_till_fail']:
96                 test = FlowSizeTest(test_param, test_params['lat_percentile'],
97                         test_params['runtime'], 
98                         test_params['TestName'], 
99                         test_params['environment_file'], gen_machine,
100                         sut_machine, background_machines)
101             elif test_param['test'] in ['corestats']:
102                 test = CoreStatsTest(test_param, test_params['runtime'],
103                         test_params['TestName'], 
104                         test_params['environment_file'], machines)
105             elif test_param['test'] in ['portstats']:
106                 test = PortStatsTest(test_param, test_params['runtime'],
107                         test_params['TestName'], 
108                         test_params['environment_file'], machines)
109             elif test_param['test'] in ['impairtest']:
110                 test = ImpairTest(test_param, test_params['lat_percentile'],
111                         test_params['runtime'],
112                         test_params['TestName'], 
113                         test_params['environment_file'], gen_machine,
114                         sut_machine)
115             elif test_param['test'] in ['irqtest']:
116                 test = IrqTest(test_param, test_params['runtime'],
117                         test_params['TestName'], 
118                         test_params['environment_file'], machines)
119             elif test_param['test'] in ['warmuptest']:
120                 test = WarmupTest(test_param, gen_machine)
121             else:
122                 RapidLog.debug('Test name ({}) is not valid:'.format(
123                     test_param['test']))
124             single_test_result = test.run()
125             if not single_test_result:
126                 result = False
127         for machine in machines:
128             machine.close_prox()
129         return (result)
130
131 def main():
132     """Main function.
133     """
134     test_params = RapidTestManager.get_defaults()
135     # When no cli is used, the process_cli can be replaced by code modifying
136     # test_params
137     test_params = RapidCli.process_cli(test_params)
138     log_file = 'RUN{}.{}.log'.format(test_params['environment_file'],
139             test_params['test_file'])
140     RapidLog.log_init(log_file, test_params['loglevel'],
141             test_params['screenloglevel'] , test_params['version']  )
142     test_result = RapidTestManager.run_tests(test_params)
143     RapidLog.info('Test result is : {}'.format(test_result))
144
145 if __name__ == "__main__":
146     main()