6f7a40fff6f6cbf4409339ff28799ecf65611df2
[functest.git] / functest / opnfv_tests / Controllers / 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 import functest.utils.functest_constants as ft_constants
21 import functest.utils.functest_utils as ft_utils
22
23
24 class foundation:
25
26     def __init__(self):
27
28         # currentpath = os.getcwd()
29         currentpath = \
30             ft_constants.FUNCTEST_TEST_DIR + '/Controllers/ONOS/Teston/CI'
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 = str(ft_utils.get_db_url())
59         self.masterusername = str(ft_constants.ONOSBENCH_USERNAME)
60         self.masterpassword = str(ft_constants.ONOSBENCH_PASSWORD)
61         self.agentusername = str(ft_constants.ONOSCLI_USERNAME)
62         self.agentpassword = str(ft_constants.ONOSCLI_PASSWORD)
63         self.runtimeout = ft_constants.ONOS_RUNTIMEOUT
64         self.OCT = str(ft_constants.ONOS_OCT)
65         self.OC1 = str(ft_constants.ONOS_OC1)
66         self.OC2 = str(ft_constants.ONOS_OC2)
67         self.OC3 = str(ft_constants.ONOS_OC3)
68         self.OCN = str(ft_constants.ONOS_OCN)
69         self.OCN2 = str(ft_constants.ONOS_OCN2)
70         self.installer_master = str(ft_constants.ONOS_INSTALLER_MASTER)
71         self.installer_master_username = \
72             str(ft_constants.ONOS_INSTALLER_MASTER_USERNAME)
73         self.installer_master_password = \
74             ft_constants.ONOS_INSTALLER_MASTER_PASSWORD
75         self.hosts = [self.OC1, self.OCN, self.OCN2]
76         self.localhost = self.OCT
77
78     def GetResult(self):
79         cmd = "cat " + self.logfilepath + " | grep Fail"
80         Resultbuffer = os.popen(cmd).read()
81         duration = datetime.datetime.now() - self.starttime
82         time.sleep(2)
83
84         if re.search("[1-9]+", Resultbuffer):
85             self.log("Testcase Fails\n" + Resultbuffer)
86             Result = "POK"
87         else:
88             self.log("Testcases Pass")
89             Result = "OK"
90         payload = {'timestart': str(self.starttime),
91                    'duration': str(duration), 'status': Result}
92
93         return payload