-
Notifications
You must be signed in to change notification settings - Fork 53
/
misc.py
166 lines (141 loc) · 4.82 KB
/
misc.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
""" tlog tests """
import os
import ast
import stat
import time
import inspect
import json
from shutil import copyfile
from systemd import journal
import pexpect
from pexpect.pxssh import pxssh
def journal_find_last():
""" Find the last TLOG_REC journal entry """
j = journal.Reader()
j.seek_tail()
while True:
entry = j.get_previous()
if '_COMM' in entry:
matchfield = '_COMM'
elif 'SYSLOG_IDENTIFIER' in entry:
matchfield = 'SYSLOG_IDENTIFIER'
else:
continue
if 'tlog' in entry[matchfield]:
return entry
elif not entry:
raise ValueError('Did not find TLOG_REC entry in journal')
def check_journal(pattern):
""" Check that last journal entry contains pattern """
time.sleep(1)
for _ in range(0, 10):
entry = journal_find_last()
message = entry['MESSAGE']
out_txt = ast.literal_eval(message)['out_txt']
if pattern in out_txt:
break
else:
time.sleep(5)
assert pattern in out_txt
def check_outfile(pattern, filename, maxchecks=10):
""" Check that file contains pattern """
time.sleep(1)
for _ in range(0, maxchecks):
file1 = open(filename, 'r')
content = file1.read()
file1.close()
if pattern in content:
break
else:
time.sleep(5)
assert pattern in content
def check_recording(shell, pattern, filename=None):
""" Check that recording contains pattern """
time.sleep(1)
if filename is not None:
cmd = 'tlog-play -i {}'.format(filename)
else:
entry = journal_find_last()
message = entry['MESSAGE']
rec = ast.literal_eval(message)['rec']
tlog_rec = 'TLOG_REC={}'.format(rec)
cmd = 'tlog-play -r journal -M {}'.format(tlog_rec)
shell.sendline(cmd)
out = shell.expect([pexpect.TIMEOUT, pattern], timeout=10)
# print(shell.before)
# print(shell.after)
if out == 0:
print('\ncheck_recording TIMEOUT')
assert out == 1
def check_recording_missing(shell, pattern, filename=None):
""" Check that recording does not contain pattern """
time.sleep(1)
if filename is not None:
cmd = 'tlog-play -g end -i {}'.format(filename)
else:
entry = journal_find_last()
message = entry['MESSAGE']
rec = ast.literal_eval(message)['rec']
tlog_rec = 'TLOG_REC={}'.format(rec)
cmd = 'tlog-play -g end -r journal -M {}'.format(tlog_rec)
shell.sendline(cmd)
out = shell.expect([pexpect.TIMEOUT, pattern], timeout=5)
if out == 1:
print('\ncheck_recording_missing found: {}'.format(pattern))
assert out == 0
def ssh_pexpect(username, password, hostname, encoding='utf-8', control_persist=False):
""" Setup an ssh connection to remote host """
opts = {
"StrictHostKeyChecking": "no",
"UserKnownHostsFile": "/dev/null"
}
if control_persist:
opts['ControlMaster'] = 'auto',
opts['ControlPath'] = "~/.ssh/control-%h-%p-%r"
ssh = pxssh(options=opts, encoding=encoding, codec_errors='replace')
ssh.force_password = True
ssh.login(hostname, username, password)
ssh.sendline('echo loggedin')
ssh.expect('loggedin')
# ssh.logfile = sys.stdout
return ssh
def mklogfile(directory, filename=None):
""" Create temporary logfile """
# if filename not given, we use the name of the calling frame
if filename is None:
filename = '{}.tlog'.format(inspect.stack()[1][3])
logfile = '{}/{}'.format(directory, filename)
if not os.path.isdir(directory):
os.makedirs(directory)
os.chmod(directory, stat.S_IRWXU + stat.S_IRWXG + stat.S_IRWXO +
stat.S_ISUID + stat.S_ISGID + stat.S_ISVTX)
return logfile
def mkcfgfile(filename, content):
""" Create config file from string content """
bkup = '{}.origtest'.format(filename)
content = '\n'.join([line.lstrip() for line in content.split('\n')])
if os.path.isfile(filename) and not os.path.isfile(bkup):
copyfile(filename, bkup)
with open(filename, 'w') as out:
out.write(content)
def mkrecording(shell, filename=None, sleep=3):
""" Create a standard recording for tests to use """
if filename is None:
opts = '-w journal'
else:
opts = '-o {}'.format(filename)
shell.sendline('tlog-rec {}'.format(opts))
time.sleep(sleep)
shell.sendline('id')
shell.sendline('cat /etc/hosts')
shell.sendline('exit')
def read_tlog_recording_file(recording_file):
""" Read tlog message(s) from file. JSON Objects are
decoded and returned into list of dictionaries.
"""
messages = []
with open(recording_file) as f:
for line in f:
obj = json.loads(line)
messages.append(obj)
return messages