adding TestScripts class
[tests.git] / qaapi / qa / Config.py
1 import xmlrpclib
2 import os
3 import sys
4 import re
5 import socket
6 import utils
7 import copy
8 from logger import logfile
9 from PLCs import PLC, PLCs
10 from Sites import Site, Sites   
11 from Nodes import Node, Nodes
12 from Slices import Slice, Slices
13 from Persons import Person, Persons     
14
15 path = os.path.dirname(os.path.abspath(__file__))
16
17 class Config:
18
19     path = os.path.dirname(os.path.abspath(__file__))
20     tests_path = path + os.sep + 'tests' + os.sep
21     node_tests_path = tests_path + os.sep + 'node' + os.sep
22     slice_tests_path = tests_path + os.sep + 'slice' + os.sep                           
23     vserver_scripts_path = path + os.sep + 'vserver' + os.sep
24     log_filename = logfile.filename
25     
26     def update_api(self, plc = None):
27         # Set up API acccess
28         # If plc is specified, find its configuration
29         # and use its API
30         if plc is not None:
31             protocol, host, path, port  = 'http', plc['host'], plc['api_path'], plc['port']
32             if port in ['443']:
33                 protocol = 'https'
34             api_server = "%(protocol)s://%(host)s:%(port)s/%(path)s" % locals()
35             self.api = xmlrpclib.Server(api_server, allow_none = 1)
36             self.api_type = 'xmlrpc'    
37         else:
38
39             # Try importing the API shell for direct api access.
40             # If that fails fall back to using xmlrpm
41             try:
42                 sys.path.append('/usr/share/plc_api')
43                 from PLC.Shell import Shell
44                 shell = Shell(globals = globals())
45             
46                 # test it
47                 shell.GetRoles()
48                 self.api = shell
49                 self.api_type = 'direct'
50             except:
51                 self.api = xmlrpclib.Server('https://%s/PLCAPI/' % self.PLC_API_HOST, allow_none = 1)
52                 self.api_type = 'xmlrpc'
53
54     def __init__(self, config_file = path+os.sep+'qa_config'):
55         # Load config file
56         try:
57             execfile(config_file, self.__dict__)
58         except:
59             raise "Could not find system config in %s" % config_file
60         
61         self.logfile = logfile
62         self.auth = {}
63         self.auth['Username'] = self.PLC_ROOT_USER
64         self.auth['AuthString'] = self.PLC_ROOT_PASSWORD
65         self.auth['AuthMethod'] = 'password'
66         self.verbose = self.VERBOSE     
67         
68         # try setting hostname and ip
69         self.hostname = socket.gethostname()
70         try:
71             command = "/sbin/ifconfig eth0 | grep -v inet6 | grep inet | awk '{print$2;}'"
72             (status, output) = utils.commands(command)            
73             self.ip = re.findall(r'[0-9\.]+', output)[0]
74         except:
75             self.ip = '127.0.0.1'
76         
77         api_host = self.__dict__.get("PLC_API_HOST",self.ip)
78         self.PLC_API_HOST=api_host
79
80         self.update_api()
81
82         # Load list of node tests
83         valid_node_test_files = lambda name: not name.startswith('__init__') \
84                                              and not name.endswith('pyc')
85         node_test_files = os.listdir(self.node_tests_path)
86         self.node_test_files = filter(valid_node_test_files, node_test_files) 
87
88     def get_plc(self, plc_name):
89         plc = PLC(self)
90         if hasattr(self, 'plcs')  and plc_name in self.plcs.keys():
91             plc.update(self.plcs[plc_name])
92             plc.update_api()
93         return plc
94
95     def get_node(self, hostname):
96         node = Node(self)
97         if hasattr(self, 'nodes') and hostname in self.nodes.keys():
98             node.update(self.nodes[hostname])
99             node.__init_logfile__()
100         return node                     
101
102     def load(self, conffile):
103         
104         confdata = {}
105         try: execfile(conffile, confdata)
106         except: raise 
107
108         from Nodes import Nodes
109         from PLCs import PLCs   
110         loadables = ['plcs', 'sites', 'nodes', 'slices', 'persons']
111         config = Config()
112         for loadable in loadables:
113             if loadable in confdata and loadable in ['plcs']:
114                 setattr(self, loadable, PLCs(config, confdata[loadable]).dict('name'))
115             elif loadable in confdata and loadable in ['nodes']:
116                 setattr(self, loadable, Nodes(config, confdata[loadable]).dict('hostname'))     
117             elif loadable in confdata and loadable in ['sites']:
118                 setattr(self, loadable, Sites(confdata[loadable]).dict('login_base'))
119             elif loadable in confdata and loadable in ['slices']:
120                 setattr(self, loadable, Slices(confdata[loadable]).dict('name'))
121             elif loadable in confdata and loadable in ['persons']:
122                 setattr(self, loadable, Persons(confdata[loadable]).dict('email')) 
123             
124