Merge "Enable tempest offline by use_custom_images=True"
[functest.git] / functest / opnfv_tests / sdn / onos / teston / adapters / foundation.py
1 """
2 Description:
3     This file include basis functions
4     lanqinglong@huawei.com
5
6 #
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Apache License, Version 2.0
9 # which accompanies this distribution, and is available at
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 """
13
14 import datetime
15 import logging
16 import os
17 import re
18 import time
19
20 from functest.utils.constants import CONST
21
22
23 class Foundation(object):
24
25     def __init__(self):
26
27         # currentpath = os.getcwd()
28         currentpath = ('{0}/sdn/onos/teston/ci'
29                        .format(CONST.__getattribute__('dir_functest_data')))
30         self.cipath = currentpath
31         self.logdir = os.path.join(currentpath, 'log')
32         self.workhome = currentpath[0: currentpath.rfind('opnfv_tests') - 1]
33         self.Result_DB = ''
34         filename = time.strftime('%Y-%m-%d-%H-%M-%S') + '.log'
35         self.logfilepath = os.path.join(self.logdir, filename)
36         self.starttime = datetime.datetime.now()
37
38     def log(self, loginfo):
39         """
40         Record log in log directory for deploying test environment
41         parameters:
42         loginfo(input): record info
43         """
44         logging.basicConfig(level=logging.INFO,
45                             format='%(asctime)s %(filename)s:%(message)s',
46                             datefmt='%d %b %Y %H:%M:%S',
47                             filename=self.logfilepath,
48                             filemode='w')
49         filelog = logging.FileHandler(self.logfilepath)
50         logging.getLogger('Functest').addHandler(filelog)
51         logging.info(loginfo)
52
53     def getdefaultpara(self):
54         """
55         Get Default Parameters value
56         """
57         self.Result_DB = CONST.__getattribute__("results_test_db_url")
58         self.masterusername = CONST.__getattribute__('ONOS_onosbench_username')
59         self.masterpassword = CONST.__getattribute__('ONOS_onosbench_password')
60         self.agentusername = CONST.__getattribute__('ONOS_onoscli_username')
61         self.agentpassword = CONST.__getattribute__('ONOS_onoscli_password')
62         self.runtimeout = CONST.__getattribute__('ONOS_runtimeout')
63         self.OCT = CONST.__getattribute__('ONOS_environment_OCT')
64         self.OC1 = CONST.__getattribute__('ONOS_environment_OC1')
65         self.OC2 = CONST.__getattribute__('ONOS_environment_OC2')
66         self.OC3 = CONST.__getattribute__('ONOS_environment_OC3')
67         self.OCN = CONST.__getattribute__('ONOS_environment_OCN')
68         self.OCN2 = CONST.__getattribute__('ONOS_environment_OCN2')
69         self.installer_master = CONST.__getattribute__(
70             'ONOS_environment_installer_master')
71         self.installer_master_username = (
72             CONST.__getattribute__(
73                 'ONOS_environment_installer_master_username'))
74         self.installer_master_password = (
75             CONST.__getattribute__(
76                 'ONOS_environment_installer_master_password'))
77         self.hosts = [self.OC1, self.OCN, self.OCN2]
78         self.localhost = self.OCT
79
80     def GetResult(self):
81         cmd = "cat " + self.logfilepath + " | grep Fail"
82         Resultbuffer = os.popen(cmd).read()
83         duration = datetime.datetime.now() - self.starttime
84         time.sleep(2)
85
86         if re.search("[1-9]+", Resultbuffer):
87             self.log("Testcase Fails\n" + Resultbuffer)
88             Result = "POK"
89         else:
90             self.log("Testcases Pass")
91             Result = "OK"
92         payload = {'timestart': str(self.starttime),
93                    'duration': str(duration), 'status': Result}
94
95         return payload