nfvbenchvm: add option to use local nfvbench code
[nfvbench.git] / behave_tests / behavedriver.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 """Define classes required to run any Behave test suites."""
18
19 from __future__ import division
20
21 import json
22 import logging
23 import os
24
25 from xtesting.core.behaveframework import BehaveFramework
26
27 __author__ = "François-Régis Menguy <francoisregis.menguy@orange.com>"
28
29
30 class BehaveDriver(BehaveFramework):
31     """NFVbench custom BehaveDriver for Xtesting."""
32     # pylint: disable=too-many-instance-attributes
33
34     __logger = logging.getLogger('xtesting.core.behavedriver')
35
36     def __init__(self, **kwargs):
37         super().__init__(**kwargs)
38         self.campaign_json_file = os.path.join(self.res_dir, 'campaign_result.json')
39
40     def extract_nfvbench_results(self):
41         with open(self.campaign_json_file) as stream_:
42             self.details['results'] = json.load(stream_)
43
44     def run(self, **kwargs):
45
46         """Override existing Xtesting BehaveFramework core script run method
47          to extract NFVbench result and push them to DB
48
49         Here are the steps:
50            * run Xtesting behave method:
51             * create the output directories if required,
52             * run behave features with parameters
53             * get the behave results in output.json,
54             * get the nfvbench results in campaign_result.json
55
56         Args:
57             kwargs: Arbitrary keyword arguments.
58
59         Returns:
60             EX_OK if all suites ran well.
61             EX_RUN_ERROR otherwise.
62         """
63         try:
64             super().run(**kwargs)
65             self.extract_nfvbench_results()
66             self.__logger.info("NFVbench results were successfully parsed")
67         except Exception:  # pylint: disable=broad-except
68             self.__logger.exception("Cannot parse NFVbench results")
69             return self.EX_RUN_ERROR
70         return self.EX_OK