behave_tests: tweak nfvbench results JSON filename
[nfvbench.git] / behave_tests / features / environment.py
1 #!/usr/bin/env python
2 # Copyright 2021 Orange
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15 #
16
17
18 import json
19 import os
20 import logging
21 import pathlib
22 import time
23
24
25 def before_all(context):
26     context.data = {'config': os.getenv('NFVBENCH_CONFIG_PATH', '/etc/nfvbench/nfvbench.cfg')}
27
28     context.data['PROJECT_NAME'] = os.getenv('PROJECT_NAME', 'nfvbench')
29     context.data['TEST_DB_URL'] = os.getenv('TEST_DB_URL')
30     context.data['BASE_TEST_DB_URL'] = ''
31     if context.data['TEST_DB_URL']:
32         context.data['BASE_TEST_DB_URL'] = context.data['TEST_DB_URL'].replace('results', '')
33     context.data['INSTALLER_TYPE'] = os.getenv('INSTALLER_TYPE')
34     context.data['DEPLOY_SCENARIO'] = os.getenv('DEPLOY_SCENARIO')
35     context.data['NODE_NAME'] = os.getenv('NODE_NAME', 'nfvbench')
36     context.data['BUILD_TAG'] = os.getenv('BUILD_TAG')
37
38     # NFVbench server host and port
39     context.host_ip = os.getenv('NFVBENCH_SERVER_HOST', '127.0.0.1')
40     context.port = int(os.getenv('NFVBENCH_SERVER_PORT', '7555'))
41
42
43 def before_feature(context, feature):
44     context.rates = {}
45     context.results = {}
46     context.start_time = time.time()
47     context.CASE_NAME = feature.name
48
49     # Create results dir if needed
50     results_dir = pathlib.Path('/var/lib/xtesting/results/' + context.CASE_NAME)
51     if not results_dir.exists():
52         results_dir.mkdir()
53
54     # Setup a second logger to be able to understand why a test passed or failed
55     # (The main logger is used by behave itself)
56     context.logger = logging.getLogger('behave_tests')
57     context.logger.setLevel(logging.INFO)
58     fh = logging.FileHandler(filename=results_dir / pathlib.Path('behave_tests.log'),
59                              mode='w')  # Re-create the file at the beginning of the feature
60     fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
61     context.logger.addHandler(fh)
62
63     context.logger.info('before_feature: ' + feature.name)
64
65
66 def before_scenario(context, scenario):
67     context.tag = scenario.tags[0]
68     context.json = {'log_file': '/var/lib/xtesting/results/' + context.CASE_NAME + '/nfvbench.log'}
69     user_label = os.getenv('NFVBENCH_USER_LABEL', None)
70     if user_label:
71         context.json['user_label'] = user_label
72     loopvm_flavor = os.getenv('NFVBENCH_LOOPVM_FLAVOR_NAME', None)
73     if loopvm_flavor:
74         context.json['flavor_type'] = loopvm_flavor
75     context.synthesis = {}
76     context.percentage_rate = None
77
78     context.logger.info('before_scenario: ' + scenario.name)
79
80
81 def after_feature(context, feature):
82     if len(context.results) == 0:
83         # No result to dump
84         return
85
86     results_dir = pathlib.Path('/var/lib/xtesting/results/' + context.CASE_NAME)
87     results_file = results_dir / pathlib.Path('campaign_result.json')
88     results_file.write_text(json.dumps(context.results, indent=4))