This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
XSConsoleRemoteTest.py
289 lines (240 loc) · 10.8 KB
/
XSConsoleRemoteTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright (c) 2008-2009 Citrix Systems Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os, socket, xmlrpclib
from XSConsoleBases import *
from XSConsoleImporter import *
from XSConsoleLang import *
from XSConsoleLog import *
from XSConsoleLayout import *
import SocketServer
import SimpleXMLRPCServer
class UnixSimpleXMLRPCRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
# Python 2.7's SimpleXMLRPCRequestHandler enables Nagle's algorithm by default
# which fails because we're working with Unix domain sockets so disable it.
disable_nagle_algorithm = False
class UDSXMLRPCServer(SocketServer.UnixStreamServer, SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
def __init__(self, inAddr, inRequestHandler = None):
self.logRequests = False
SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self)
SocketServer.UnixStreamServer.__init__(self, inAddr,
FirstValue(inRequestHandler, UnixSimpleXMLRPCRequestHandler))
def handle_request(self):
# Same as base class, but returns True if a request was handled
try:
request, client_address = self.get_request()
except socket.error:
return False
if self.verify_request(request, client_address):
try:
self.process_request(request, client_address)
except:
self.handle_error(request, client_address)
self.close_request(request)
return True
class XMLRPCRemoteTest:
LOCAL_SOCKET_PATH = '/var/xapi/xmlrpcsocket.xsconsole'
def __init__(self):
if os.path.exists(self.LOCAL_SOCKET_PATH):
os.unlink(self.LOCAL_SOCKET_PATH)
self.server = UDSXMLRPCServer(self.LOCAL_SOCKET_PATH)
self.server.register_introspection_functions()
for name in dir(self):
match = re.match(r'HandleXMLRPC(.+)', name)
if match:
self.server.register_function(getattr(self, name), match.group(1).lower())
self.SetSocketTimeout(0.0)
self.ResetStrings()
Language.SetStringHook(self.StringHook)
Language.SetErrorHook(self.ErrorHook)
def SetSocketTimeout(self, inTimeout):
self.server.socket.settimeout(inTimeout)
def ResetStrings(self):
self.strings = []
self.errors = []
self.params = []
def StringHook(self, inString):
self.strings.append(inString)
def ErrorHook(self, inString):
self.errors.append(inString)
def SetApp(self, inApp):
self.app = inApp
def Poll(self):
retVal = self.server.handle_request() # True if the server handled a request, False if timed out
# The socket timeout is different for:
# 1. XMLRPC idle, where the wait mustn't delay real keypress handling.
# 2. Ongoing XML test, where the wait must allow time for the next XML command to arrive
# before allowing the next one-second wait for a real keypress, to avoid delays in the test.
if retVal:
self.SetSocketTimeout(1.0)
else:
self.SetSocketTimeout(0.0)
return retVal
def ErrorString(self, inException = None):
retVal = time.strftime("%Y%m%d%H%M%S", time.gmtime())+'Z ********* Exception *********'
# The log contains every string translated via Lang, so is verbose and disabled for normal use
# retVal += "\n\nLog\n\n" + "\n".join(self.strings)
retVal += "\n\nSnapshot\n\n"
try:
snapshot = Layout.Inst().TopDialogue().Snapshot()
for i, pane in enumerate(snapshot):
for line in pane:
retVal += 'Pane '+str(i) + ':' + line + '\n'
except Exception, e:
retVal += 'Failed: '+Lang(e)
if len(self.errors) > 0:
retVal += "\n\nExceptions process by Lang()\n\n" + "\n".join(self.errors)
retVal += "\n\n**************************************\n\nThe command\n\n"
retVal += ":".join(self.params) + "\n\nfailed with:\n\n"
if inException is not None:
retVal += Lang(inException)+"\n\n"
return retVal
def WrapProcedure(self, inProc): # Any return value of inProc is discarded
try:
inProc()
except Exception, e:
raise xmlrpclib.Fault(1, self.ErrorString(e))
return None
def WrapFunction(self, inFunc): # inFunc returns a value
try:
retVal = inFunc()
except Exception, e:
raise xmlrpclib.Fault(1, self.ErrorString(e))
return retVal
def StandardReturn(self, inInfix = None):
retVal = time.strftime("%Y%m%d%H%M%S", time.gmtime())+'Z '
retVal += ':'.join(self.params)
if inInfix is not None:
retVal += ' ' + inInfix
retVal +=' -> OK'
return retVal
def HandleXMLRPCNew(self, inTestname):
"""Function: new(<test name>)
Clears current dialogues and resets xsconsole to the first menu item in the root
menu. Requires a test name as a parameter, and returns a banner suitable for
logging.
"""
self.ResetStrings()
self.params = ['new', inTestname]
self.testname = inTestname
self.WrapProcedure(lambda: Layout.Inst().Reset())
data = Data.Inst()
retVal = "\nTest: "+inTestname
retVal += "\nHost Version: "+data.derived.fullversion()
retVal += "\nHostname: "+data.host.hostname()
retVal += "\nManufacturer: "+data.dmi.system_manufacturer()
retVal += "\nProduct Name: "+data.dmi.system_product_name()
retVal += "\nManagement IP: "+data.ManagementIP()
retVal += "\n"+self.StandardReturn()
return retVal
def HandleXMLRPCKeypress(self, inKeypress):
"""Function: keypress(<key name>)
Simulates a keypress. The parameter should be the ncurses name of a single key,
e.g. one of 'H', 'KEY_ESCAPE', 'KEY_ENTER', 'KEY_UP', 'KEY_F(8)' or similar
Return a string suitable for logging.
"""
self.params = ['keypress', inKeypress]
if self.WrapFunction(lambda: self.app.HandleKeypress(inKeypress)):
retVal = self.StandardReturn()
else:
retVal = self.StandardReturn('(keypress ignored)')
return retVal
def HandleXMLRPCVerify(self, inString):
"""Function: verify(<search regexp>)
Searches for the input regexp in the log of every string passed to Lang during
this test. Raises an exception if not found. Prefer assertsuccess/assertfail
to this function if possible.'
"""
self.params = ['verify', inString]
result = None
regExp = re.compile(inString)
for line in self.strings:
if regExp.match(line):
result = line
break
if result is None:
raise xmlrpclib.Fault(1, self.ErrorString()+"\n\nSearch string '"+inString+"' not found.")
return self.StandardReturn("found '"+result+"'")
def HandleXMLRPCActivate(self, inName):
""" Function: activate(<plug in name>)
Activates a plug in feature, as if the menu item had been selected from
the menu. The same thing can be achieved by sending keypresses of arrow
keys and enter, but this method avoids dependence on the ordering of items
within menus.
The name parameter should match the name passed to Importer.RegisterNamedPlugIn
by the intended plug in. Returns a string suitable for logging.
"""
self.params = ['activate', inName]
self.WrapProcedure(lambda: Importer.ActivateNamedPlugIn(inName))
return self.StandardReturn()
def HandleXMLRPCAuthenticate(self, inPassword):
"""Function: authenticate(<password>)
Authenticates within xsconsole, as if the user had entered the password in
the login dialogue. Returns a string suitable for logging.
"""
self.params = ['authenticate', '*' * len(inPassword)]
self.WrapProcedure(lambda: Auth.Inst().ProcessLogin('root', inPassword))
return self.StandardReturn()
def HandleXMLRPCGetData(self):
"""Function: getdata
Returns xsconsole's internal cache of xapi data.
"""
self.params = ['getdata']
self.WrapProcedure(lambda: Data.Inst().Update())
retVal = str(Data.Inst().DataCache())
return retVal
def HandleXMLRPCSnapshot(self):
"""Function: snapshot
Returns the contents of the foreground pane(s) on xsconsole's terminal,
as a list of lists of strings.
"""
self.params = ['snapshot']
retVal = self.WrapFunction(lambda: Layout.Inst().TopDialogue().Snapshot())
return retVal
def HandleXMLRPCAssertFailure(self):
"""Function: assertfailure
Raises an exception if all operations since the start of the test have
succeeded. Otherwise returns a string suitable for logging.
"""
self.params = ['assertfailure']
if len(self.errors) == 0:
raise xmlrpclib.Fault(1, self.ErrorString())
return self.StandardReturn()
def HandleXMLRPCAssertSuccess(self):
"""Function: assertsuccess
Raises an exception if any of the operations since the start of the test have
failed. Otherwise returns a string suitable for logging.
"""
self.params = ['assertsuccess']
if len(self.errors) > 0:
raise xmlrpclib.Fault(1, self.ErrorString())
return self.StandardReturn()
class NullRemoteTest:
def __init__(*inParams):
pass
def Poll(self):
return False
def SetApp(self, *inParams):
pass
class RemoteTest:
__instance = None
@classmethod
def Inst(cls):
if cls.__instance is None:
if os.path.exists('/etc/xsconsole/activatexmlrpc'):
cls.__instance = XMLRPCRemoteTest()
XSLog('xsconsole XMLRPC interface activated')
else:
cls.__instance = NullRemoteTest()
return cls.__instance