behave_tests: create results dir if needed
[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 pathlib
21 import time
22
23
24 def before_all(context):
25     context.data = {'config': os.getenv('NFVBENCH_CONFIG_PATH', '/etc/nfvbench/nfvbench.cfg')}
26
27     context.data['PROJECT_NAME'] = os.getenv('PROJECT_NAME', 'nfvbench')
28     context.data['TEST_DB_EXT_URL'] = os.getenv('TEST_DB_EXT_URL')
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
50 def before_scenario(context, scenario):
51     context.tag = scenario.tags[0]
52     context.json = {'log_file': '/var/lib/xtesting/results/' + context.CASE_NAME + '/nfvbench.log'}
53     user_label = os.getenv('NFVBENCH_USER_LABEL', None)
54     if user_label:
55         context.json['user_label'] = user_label
56     loopvm_flavor = os.getenv('NFVBENCH_LOOPVM_FLAVOR_NAME', None)
57     if loopvm_flavor:
58         context.json['flavor_type'] = loopvm_flavor
59     context.synthesis = {}
60
61
62 def after_feature(context, feature):
63     if len(context.results) == 0:
64         # No result to dump
65         return
66
67     results_dir = pathlib.Path('/var/lib/xtesting/results/' + context.CASE_NAME)
68     if not results_dir.exists():
69         results_dir.mkdir()
70
71     results_file = results_dir / pathlib.Path('campaign_result.json')
72     results_file.write_text(json.dumps(context.results, indent=4))