stop hardcoded FUNCTEST_REPO path everywhere
[functest.git] / testcases / 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 yaml
21 from functest.utils.functest_utils import FUNCTEST_REPO as FUNCTEST_REPO
22
23
24 class foundation:
25
26     def __init__(self):
27
28         # currentpath = os.getcwd()
29         REPO_PATH = FUNCTEST_REPO + '/'
30         currentpath = REPO_PATH + 'testcases/Controllers/ONOS/Teston/CI'
31         self.cipath = currentpath
32         self.logdir = os.path.join(currentpath, 'log')
33         self.workhome = currentpath[0: currentpath.rfind('testcases') - 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         print loginfo
53         logging.info(loginfo)
54
55     def getdefaultpara(self):
56         """
57         Get Default Parameters value
58         """
59         with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
60             functest_yaml = yaml.safe_load(f)
61
62         self.Result_DB = str(functest_yaml.get("results").get("test_db_url"))
63         self.masterusername = str(functest_yaml.get("ONOS").get("general").
64                                   get('onosbench_username'))
65         self.masterpassword = str(functest_yaml.get("ONOS").get("general").
66                                   get("onosbench_password"))
67         self.agentusername = str(functest_yaml.get("ONOS").get("general").
68                                  get("onoscli_username"))
69         self.agentpassword = str(functest_yaml.get("ONOS").get("general").
70                                  get("onoscli_password"))
71         self.runtimeout = functest_yaml.get("ONOS").get("general").get(
72             "runtimeout")
73         self.OCT = str(functest_yaml.get("ONOS").get("environment").get("OCT"))
74         self.OC1 = str(functest_yaml.get("ONOS").get("environment").get("OC1"))
75         self.OC2 = str(functest_yaml.get("ONOS").get("environment").get("OC2"))
76         self.OC3 = str(functest_yaml.get("ONOS").get("environment").get("OC3"))
77         self.OCN = str(functest_yaml.get("ONOS").get("environment").get("OCN"))
78         self.OCN2 = str(functest_yaml.get("ONOS").
79                         get("environment").get("OCN2"))
80         self.installer_master = str(functest_yaml.get("ONOS").
81                                     get("environment").get("installer_master"))
82         self.installer_master_username = str(functest_yaml.get("ONOS").
83                                              get("environment").
84                                              get("installer_master_username"))
85         self.installer_master_password = str(functest_yaml.get("ONOS").
86                                              get("environment").
87                                              get("installer_master_password"))
88         self.hosts = [self.OC1, self.OCN, self.OCN2]
89         self.localhost = self.OCT
90
91     def GetResult(self):
92         cmd = "cat " + self.logfilepath + " | grep Fail"
93         Resultbuffer = os.popen(cmd).read()
94         duration = datetime.datetime.now() - self.starttime
95         time.sleep(2)
96
97         if re.search("[1-9]+", Resultbuffer):
98             self.log("Testcase Fails\n" + Resultbuffer)
99             Result = "POK"
100         else:
101             self.log("Testcases Pass")
102             Result = "OK"
103         payload = {'timestart': str(self.starttime),
104                    'duration': str(duration), 'status': Result}
105
106         return payload