Fix some pylint errors in onos package
[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 import functest.utils.functest_utils as ft_utils
22
23
24 class Foundation(object):
25
26     def __init__(self):
27
28         # currentpath = os.getcwd()
29         currentpath = ('{0}/sdn/onos/teston/ci'
30                        .format(CONST.__getattribute__('dir_functest_data')))
31         self.cipath = currentpath
32         self.logdir = os.path.join(currentpath, 'log')
33         self.workhome = currentpath[0: currentpath.rfind('opnfv_tests') - 1]
34         self.Result_DB = ''
35         filename = time.strftime('%Y-%m-%d-%H-%M-%S') + '.log'
36         self.logfilepath = os.path.join(self.logdir, filename)
37         self.starttime = datetime.datetime.now()
38
39     def log(self, loginfo):
40         """
41         Record log in log directory for deploying test environment
42         parameters:
43         loginfo(input): record info
44         """
45         logging.basicConfig(level=logging.INFO,
46                             format='%(asctime)s %(filename)s:%(message)s',
47                             datefmt='%d %b %Y %H:%M:%S',
48                             filename=self.logfilepath,
49                             filemode='w')
50         filelog = logging.FileHandler(self.logfilepath)
51         logging.getLogger('Functest').addHandler(filelog)
52         logging.info(loginfo)
53
54     def getdefaultpara(self):
55         """
56         Get Default Parameters value
57         """
58         self.Result_DB = ft_utils.get_db_url()
59         self.masterusername = CONST.__getattribute__('ONOS_onosbench_username')
60         self.masterpassword = CONST.__getattribute__('ONOS_onosbench_password')
61         self.agentusername = CONST.__getattribute__('ONOS_onoscli_username')
62         self.agentpassword = CONST.__getattribute__('ONOS_onoscli_password')
63         self.runtimeout = CONST.__getattribute__('ONOS_runtimeout')
64         self.OCT = CONST.__getattribute__('ONOS_environment_OCT')
65         self.OC1 = CONST.__getattribute__('ONOS_environment_OC1')
66         self.OC2 = CONST.__getattribute__('ONOS_environment_OC2')
67         self.OC3 = CONST.__getattribute__('ONOS_environment_OC3')
68         self.OCN = CONST.__getattribute__('ONOS_environment_OCN')
69         self.OCN2 = CONST.__getattribute__('ONOS_environment_OCN2')
70         self.installer_master = CONST.__getattribute__(
71             'ONOS_environment_installer_master')
72         self.installer_master_username = (
73             CONST.__getattribute__(
74                 'ONOS_environment_installer_master_username'))
75         self.installer_master_password = (
76             CONST.__getattribute__(
77                 'ONOS_environment_installer_master_password'))
78         self.hosts = [self.OC1, self.OCN, self.OCN2]
79         self.localhost = self.OCT
80
81     def GetResult(self):
82         cmd = "cat " + self.logfilepath + " | grep Fail"
83         Resultbuffer = os.popen(cmd).read()
84         duration = datetime.datetime.now() - self.starttime
85         time.sleep(2)
86
87         if re.search("[1-9]+", Resultbuffer):
88             self.log("Testcase Fails\n" + Resultbuffer)
89             Result = "POK"
90         else:
91             self.log("Testcases Pass")
92             Result = "OK"
93         payload = {'timestart': str(self.starttime),
94                    'duration': str(duration), 'status': Result}
95
96         return payload