Repo structure modification
[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_utils as ft_utils
21
22
23 class foundation:
24
25     def __init__(self):
26
27         # currentpath = os.getcwd()
28         REPO_PATH = ft_utils.FUNCTEST_REPO + '/'
29         currentpath = REPO_PATH + 'opnfv_tests/Controllers/ONOS/Teston/CI'
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 = str(
58             ft_utils.get_functest_config('results.test_db_url'))
59         self.masterusername = str(
60             ft_utils.get_functest_config('ONOS.general.onosbench_username'))
61         self.masterpassword = str(
62             ft_utils.get_functest_config('ONOS.general.onosbench_password'))
63         self.agentusername = str(
64             ft_utils.get_functest_config('ONOS.general.onoscli_username'))
65         self.agentpassword = str(
66             ft_utils.get_functest_config('ONOS.general.onoscli_password'))
67         self.runtimeout = \
68             ft_utils.get_functest_config('ONOS.general.runtimeout')
69         self.OCT = str(ft_utils.get_functest_config('ONOS.environment.OCT'))
70         self.OC1 = str(ft_utils.get_functest_config('ONOS.environment.OC1'))
71         self.OC2 = str(ft_utils.get_functest_config('ONOS.environment.OC2'))
72         self.OC3 = str(ft_utils.get_functest_config('ONOS.environment.OC3'))
73         self.OCN = str(ft_utils.get_functest_config('ONOS.environment.OCN'))
74         self.OCN2 = str(ft_utils.get_functest_config('ONOS.environment.OCN2'))
75         self.installer_master = str(
76             ft_utils.get_functest_config('ONOS.environment.installer_master'))
77         self.installer_master_username = str(ft_utils.get_functest_config(
78             'ONOS.environment.installer_master_username'))
79         self.installer_master_password = str(ft_utils.get_functest_config(
80             'ONOS.environment.installer_master_password'))
81         self.hosts = [self.OC1, self.OCN, self.OCN2]
82         self.localhost = self.OCT
83
84     def GetResult(self):
85         cmd = "cat " + self.logfilepath + " | grep Fail"
86         Resultbuffer = os.popen(cmd).read()
87         duration = datetime.datetime.now() - self.starttime
88         time.sleep(2)
89
90         if re.search("[1-9]+", Resultbuffer):
91             self.log("Testcase Fails\n" + Resultbuffer)
92             Result = "POK"
93         else:
94             self.log("Testcases Pass")
95             Result = "OK"
96         payload = {'timestart': str(self.starttime),
97                    'duration': str(duration), 'status': Result}
98
99         return payload