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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import os, time, threading, sys, os, errno, select
from connection import connection
import utils
from utils import log
#this class is to communicate between reader and sender threads
#the reader increments a count each time it receives the prompt
#and wakes up the sender
#it also indicates when it exits so the sender can exit too
#the sender (calling get) waits for the count to be greated than passed
#argument or if reader exited
#it returns argument+1 if things okay, -1 if reader exited
class Counter:
def __init__(self):
self.count = 0
self.cond = threading.Condition()
def inc(self):
self.cond.acquire()
self.count = self.count + 1
self.cond.notify()
self.cond.release()
def set(self, value):
self.cond.acquire()
self.count = value
self.cond.notify()
self.cond.release()
def get(self, current):
self.cond.acquire()
while True:
if self.count == -1:
ret = -1
break
if self.count >= current + 1:
ret = current + 1
break
self.cond.wait()
self.cond.release()
return ret
#this class is used for the main application to wait for some specific
#output from the remote program (typically the ALU HSS prompt, indicating
#it is ready and we can proceed further)
#(too much classes...)
class ProducerConsumer:
def __init__(self):
self.count = 0
self.cond = threading.Condition()
def add(self, v):
self.cond.acquire()
self.count = self.count + v
self.cond.notify()
self.cond.release()
def set(self, v):
self.cond.acquire()
self.count = v
self.cond.notify()
self.cond.release()
def get(self, old):
self.cond.acquire()
while True:
if self.count == -1:
ret = -1
break
if self.count > old:
ret = self.count
break
self.cond.wait()
self.cond.release()
return ret
#this thread gets input from the child process of the task
#it removes the prompts it gets (be carefully to use a prompt
#that will not be output of any command otherwise you dead)
class ReaderThread(threading.Thread):
def __init__(self, fdin, logfile, prompt, prompt_counter, producer):
threading.Thread.__init__(self)
self.fdin = fdin
self.logfile = logfile
self.prompt_counter = prompt_counter
self.prompt = prompt
self.promptsize = len(prompt)
self.stack = ""
self.stacksize = 0
self.producer = producer
def run(self):
try:
outfile = open(self.logfile, "w")
except BaseException, e:
log("ERROR: ReaderThread: " + self.logfile + ": " + str(e))
os._exit(1)
while True:
try:
(a, b, c) = select.select([ self.fdin ], [], [ self.fdin ])
except BaseException, e:
log("ERROR: ReaderThread: " + self.logfile +
": select failed: " + str(e))
os._exit(1)
try:
z = os.read(self.fdin, 1024)
except OSError, e:
if e.errno == errno.EIO:
#pipe has died, quit the thread
break
else:
log("ERROR: ReaderThread: " + self.logfile +
": unhandled error: " + str(e))
except BaseException, e:
log("ERROR: ReaderThread: " + self.logfile +
": unhandled error: " + str(e))
break
try:
produced = 0
#this part is to remove the prompt
for x in z:
if x == self.prompt[self.stacksize]:
self.stack = self.stack + x
self.stacksize = self.stacksize + 1
if self.stacksize == self.promptsize:
self.prompt_counter.inc()
self.stack = ""
self.stacksize = 0
else:
outfile.write(self.stack)
outfile.write(x)
produced = produced + len(self.stack) + len(x)
self.stack = ""
self.stacksize = 0
outfile.flush()
self.producer.add(produced)
except BaseException, e:
log("ERROR: ReaderThread: " + self.logfile + ": " + str(e))
os._exit(1)
try:
outfile.close()
except BaseException, e:
log("ERROR: ReaderThread: " + self.logfile + ": " + str(e))
os._exit(1)
#close the pipe, don't care about errors
try:
os.close(self.fdin)
except:
pass
#signal sender to quit
self.prompt_counter.set(-1)
self.producer.set(-1)
class SenderQuit(Exception):
pass
#this thread sends commands to the child process of the task
#it waits for the prompt between each command
#'event' is used for the main thread to wait for one of several tasks
#to quit, meaning error or end-of-test
class SenderThread(threading.Thread):
def __init__(self, fdout, prompt_counter, connection, env, action,
description, prompt, event=None):
threading.Thread.__init__(self)
self.fdin = fdout
self.prompt_counter = prompt_counter
self.connection = connection
self.env = env
self.action = action
self.description = description
self.prompt = prompt
self.count = 0
self.event = event
self.alive = True
def wait_prompt(self):
self.count = self.prompt_counter.get(self.count)
if self.count == -1:
raise SenderQuit()
def _run(self):
self.connection.send('export PS1=' + self.prompt + '\n')
self.wait_prompt()
self.connection.send('set +o emacs\n')
self.wait_prompt()
self.connection.send('echo\n')
self.wait_prompt()
self.connection.send('echo\n')
self.wait_prompt()
self.connection.send("echo -e '" + utils.GREEN +
'---------------------------------------------'
+ utils.RESET + "'\n")
self.wait_prompt()
self.connection.send('echo\n')
self.wait_prompt()
self.connection.send("echo -n -e '" + utils.YELLOW +
"COMMANDS START: " +
utils.RESET + "'\n")
self.wait_prompt()
self.connection.send('date\n')
self.wait_prompt()
for l in self.env:
self.connection.send('export ' + l + '\n')
self.wait_prompt()
with open(self.action) as f:
for line in f:
self.connection.send("echo -n -e '" + utils.GREEN +
"RUNNING: " + utils.RESET + "'\n")
self.wait_prompt()
self.connection.send("echo '" +
line.replace('\n','')
.replace("'", "'\\''") + "'\n")
self.wait_prompt()
self.connection.send(line)
self.wait_prompt()
self.connection.send("if [ $? != 0 ]; then " +
"echo -e '" + utils.RED +
"TEST_SETUP_ERROR: " + utils.RESET +
"last command failed, exiting'; " +
"date; exit 1; fi\n")
self.wait_prompt()
self.connection.send("echo -n -e '" + utils.YELLOW +
"COMMANDS DONE: " +
utils.RESET + "'\n")
self.wait_prompt()
self.connection.send('date\n')
self.wait_prompt()
self.connection.send("echo -e '" + utils.GREEN +
"TEST_SETUP_SUCCESS" + utils.RESET + "'\n")
self.wait_prompt()
self.connection.send('exit\n')
def run(self):
try:
self._run()
except SenderQuit:
log("WARNING: '" + self.description + "' exits too early?")
pass
except BaseException, e:
log("ERROR: task failed: " + str(e))
log("ERROR: action is: " + self.action)
log("ERROR: description is: " + self.description)
os._exit(1)
self.alive = False
if self.event != None:
self.event.set()
WAITLOG_RUNNING = 0
WAITLOG_SUCCESS = 1
WAITLOG_FAILURE = 2
class WaitlogFailed(Exception):
pass
#'event' is used by main thread to wait for any of several threads to quit.
#'Task' passes it the 'SenderThread' above, which sets it when it finishes.
class Task:
def __init__(self, action, description, machine, user, password, env,
logfile, post_action = None, event=None):
self.action = action
self.description = description
self.machine = machine
self.user = user
self.password = password
self.post_action = post_action
self.producer = ProducerConsumer()
self.logfile = logfile
prompt = "__OAI_TEST_SETUP_PROMPT__:"
prompt_counter = Counter()
self.connection = connection(description, machine, user, password)
self.reader = ReaderThread(self.connection.fd, logfile, prompt,
prompt_counter, self.producer)
self.reader.start()
self.sender = SenderThread(self.connection.fd, prompt_counter,
self.connection, env, action, description,
prompt, event)
self.sender.start()
def wait(self, timeout=-1):
if self.connection.active == False:
return self.connection.retcode
try:
(pid, ret) = os.waitpid(self.connection.pid, 0)
except KeyboardInterrupt, e:
log("ERROR: ctrl+c catched! " + str(e))
os._exit(1)
except BaseException, e:
log("ERROR: " + str(e))
os._exit(1)
self.sender.join()
self.reader.join()
return ret
#this function should not be called, it is used internally by 'waitlog'
#in mode 'background thread'
#TODO: join() the thread at some point
def _waitlog_thread(self, task, s):
consumed = 0
while True:
consumed = task.producer.get(consumed)
if consumed == -1:
log("ERROR: string '" + s + "' not found in logfile " +
task.logfile)
task.waitlog_state = WAITLOG_FAILURE
task.waitlog_event.set()
return
if s in open(task.logfile).read():
task.waitlog_state = WAITLOG_SUCCESS
task.waitlog_event.set()
return
#two ways to wait for a string in the log file:
# - blocking way
# - background thread, using an Event to signal success/failure
def waitlog(self, s, event=None):
if event != None:
self.waitlog_state = WAITLOG_RUNNING
self.waitlog_event = event
self.waitlog_thread = \
threading.Thread(target=self._waitlog_thread,
args=(self, s))
self.waitlog_thread.start()
return
#TODO: optimize, do not process all the file at each wakeup
consumed = 0
while True:
consumed = self.producer.get(consumed)
if consumed == -1:
log("ERROR: string '" + s + "' not found in logfile " +
self.logfile)
raise WaitlogFailed()
if s in open(self.logfile).read():
return
def sendnow(self, x):
self.connection.send(x)
def alive(self):
return self.sender.alive
def kill(self):
self.connection.kill()
#put some error log in logfile, for verbosity
try:
f = open(self.logfile, "a+")
f.write("\n\n" + utils.RED + "TEST_SETUP_ERROR: " + utils.RESET +
"task killed by test setup\n\n");
close(f)
except BaseException, e:
pass
def postaction(self):
if self.post_action != None:
out = utils.quickshell(self.post_action)
if len(out):
log("INFO: task '" + self.description +
"' post_action '" + self.post_action + "' says: ")
for l in out.splitlines():
log("INFO: " + l)