Merge "Allow unit testing w/o internet connectivity"
[functest.git] / functest / opnfv_tests / features / domino.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # 0.1: This script boots the VM1 and allocates IP address from Nova
11 # Later, the VM2 boots then execute cloud-init to ping VM1.
12 # After successful ping, both the VMs are deleted.
13 # 0.2: measure test duration and publish results under json format
14 # 0.3: add report flag to push results when needed
15 # 0.4: refactoring to match Test abstraction class
16
17 import argparse
18 import os
19 import sys
20 import time
21
22 from functest.core import TestCasesBase
23 import functest.utils.functest_constants as ft_constants
24 import functest.utils.functest_logger as ft_logger
25 import functest.utils.functest_utils as ft_utils
26
27
28 class DominoCases(TestCasesBase.TestCasesBase):
29     DOMINO_REPO = \
30         ft_constants.DOMINO_REPO_DIR
31     RESULTS_DIR = \
32         ft_constants.FUNCTEST_RESULTS_DIR
33     logger = ft_logger.Logger("domino").getLogger()
34
35     def __init__(self):
36         super(DominoCases, self).__init__()
37         self.project_name = "domino"
38         self.case_name = "domino-multinode"
39
40     def main(self, **kwargs):
41         cmd = 'cd %s && ./tests/run_multinode.sh' % self.DOMINO_REPO
42         log_file = os.path.join(self.RESULTS_DIR, "domino.log")
43         start_time = time.time()
44
45         ret = ft_utils.execute_command(cmd,
46                                        output_file=log_file)
47
48         stop_time = time.time()
49         if ret == 0:
50             self.logger.info("domino OK")
51             status = 'PASS'
52         else:
53             self.logger.info("domino FAILED")
54             status = "FAIL"
55
56         # report status only if tests run (FAIL OR PASS)
57         self.criteria = status
58         self.start_time = start_time
59         self.stop_time = stop_time
60         self.details = {}
61
62     def run(self):
63         kwargs = {}
64         return self.main(**kwargs)
65
66 if __name__ == '__main__':
67     parser = argparse.ArgumentParser()
68     parser.add_argument("-r", "--report",
69                         help="Create json result file",
70                         action="store_true")
71     args = vars(parser.parse_args())
72     domino = DominoCases()
73     try:
74         result = domino.main(**args)
75         if result != TestCasesBase.TestCasesBase.EX_OK:
76             sys.exit(result)
77         if args['report']:
78             sys.exit(domino.push_to_db())
79     except Exception:
80         sys.exit(TestCasesBase.TestCasesBase.EX_RUN_ERROR)