-support multiple plc instances with different configurations. - added load() method...
[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 PLCs import PLC, PLCs
9 from Sites import Site, Sites   
10 from Nodes import Node, Nodes
11 from Slices import Slice, Slices
12 from Persons import Person, Persons     
13
14 class Config:
15
16     path = os.path.dirname(os.path.abspath(__file__))
17     tests_path = path + os.sep + 'tests' + os.sep
18     node_tests_path = tests_path + os.sep + 'node' + os.sep
19     slice_tests_path = tests_path + os.sep + 'slice' + os.sep                           
20     vserver_scripts_path = path + os.sep + 'vserver' + os.sep
21     qemu_scripts_path = path + os.sep + 'qemu' + os.sep 
22
23     def update_api(self, plc = None):
24         # Set up API acccess
25         # If plc is specified, find its configuration
26         # and use its API
27         if plc is not None:
28             host, path, port  = plc['host'], plc['api_path'], plc['port']
29             api_server = "https://%(host)s:%(port)s/%(path)s" % locals()
30             self.api = xmlrpclib.Server(api_server, allow_none = 1)
31             self.api_type = 'xmlrpc'    
32         else:
33
34             # Try importing the API shell for direct api access.
35             # If that fails fall back to using xmlrpm
36             try:
37                 sys.path.append('/usr/share/plc_api')
38                 from PLC.Shell import Shell
39                 shell = Shell(globals = globals())
40             
41                 # test it
42                 shell.GetRoles()
43                 self.api = shell
44                 self.api_type = 'direct'
45             except:
46                 self.api = xmlrpclib.Server('https://%s/PLCAPI/' % self.PLC_API_HOST, allow_none = 1)
47                 self.api_type = 'xmlrpc'
48
49     def __init__(self, config_file = path+os.sep+'qa_config'):
50         # Load config file
51         try:
52             execfile(config_file, self.__dict__)
53         except:
54             raise "Could not find system config in %s" % config_file
55
56         self.auth = {}
57         self.auth['Username'] = self.PLC_ROOT_USER
58         self.auth['AuthString'] = self.PLC_ROOT_PASSWORD
59         self.auth['AuthMethod'] = 'password'
60         self.verbose = self.VERBOSE     
61         
62         # try setting hostname and ip
63         self.hostname = socket.gethostname()
64         try:
65             (stdout, stderr) = utils.popen("/sbin/ifconfig eth0 | grep 'inet addr'")
66             inet_addr = re.findall('inet addr:[0-9\.^\w]*', stdout[0])[0]
67             parts = inet_addr.split(":")
68             self.ip = parts[1]
69         except:
70             self.ip = '127.0.0.1'
71         
72         api_host = self.__dict__.get("PLC_API_HOST",self.ip)
73         self.PLC_API_HOST=api_host
74
75         self.update_api()
76
77         # Load list of node tests
78         valid_node_test_files = lambda name: not name.startswith('__init__') \
79                                              and not name.endswith('pyc')
80         node_test_files = os.listdir(self.node_tests_path)
81         self.node_test_files = filter(valid_node_test_files, node_test_files) 
82
83     def load(self, conffile):
84         
85         confdata = {}
86         try: execfile(conffile, confdata)
87         except: raise 
88
89         from Nodes import Nodes
90         from PLCs import PLCs   
91         loadables = ['plcs', 'sites', 'nodes', 'slices', 'persons']
92         config = Config()
93         for loadable in loadables:
94             if loadable in confdata and loadable in ['plcs']:
95                 setattr(self, loadable, PLCs(config, confdata[loadable]))
96             elif loadable in confdata and loadable in ['nodes']:
97                 setattr(self, loadable, Nodes(config, confdata[loadable]))              
98             elif loadable in confdata and loadable in ['sites']:
99                 setattr(self, loadable, Sites(confdata[loadable]))
100             elif loadable in confdata and loadable in ['slices']:
101                 setattr(self, loadable, Slices(confdata[loadable]))
102             elif loadable in confdata and loadable in ['persons']:
103                 setattr(self, loadable, Persons(confdata[loadable])) 
104             
105