Merge "Adapt Domino to FeatureBase"
[functest.git] / functest / core / testcase_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 import os
11
12 import functest.utils.functest_logger as ft_logger
13 import functest.utils.functest_utils as ft_utils
14
15
16 class TestcaseBase(object):
17
18     EX_OK = os.EX_OK
19     EX_RUN_ERROR = os.EX_SOFTWARE
20     EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1
21     EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
22
23     logger = ft_logger.Logger(__name__).getLogger()
24
25     def __init__(self):
26         self.details = {}
27         self.project_name = "functest"
28         self.case_name = ""
29         self.criteria = ""
30         self.start_time = ""
31         self.stop_time = ""
32
33     def check_criteria(self):
34         try:
35             assert self.criteria
36             if self.criteria == 'PASS':
37                 return TestcaseBase.EX_OK
38         except:
39             self.logger.error("Please run test before checking the results")
40         return TestcaseBase.EX_TESTCASE_FAILED
41
42     def run(self, **kwargs):
43         self.logger.error("Run must be implemented")
44         return TestcaseBase.EX_RUN_ERROR
45
46     def push_to_db(self):
47         try:
48             assert self.project_name
49             assert self.case_name
50             assert self.criteria
51             assert self.start_time
52             assert self.stop_time
53             if ft_utils.push_results_to_db(
54                     self.project_name, self.case_name, self.start_time,
55                     self.stop_time, self.criteria, self.details):
56                 self.logger.info("The results were successfully pushed to DB")
57                 return TestcaseBase.EX_OK
58             else:
59                 self.logger.error("The results cannot be pushed to DB")
60                 return TestcaseBase.EX_PUSH_TO_DB_ERROR
61         except Exception:
62             self.logger.exception("The results cannot be pushed to DB")
63             return TestcaseBase.EX_PUSH_TO_DB_ERROR