forked from seveas/python-hpilo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpilo_test_matrix
executable file
·143 lines (131 loc) · 5.59 KB
/
hpilo_test_matrix
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
#!/usr/bin/python -u
#
# This is my personal test matrix script that tries to test as many variants as
# possible on various systems I have access to.
#
# This script makes a few assumptions:
# - All iLO's use the same username/password
# - $HOME/hpilo_test/ is safe to wipe and recreate on all relevant remote
# machines
# - It is safe to overwrite ~/.ilo.conf on all relevan remote machines
#
# The tests can be configured in your ~/.ilo.conf as follows:
#
# [test_1]
# ilo = server1.ilo.example.com
# host = server1.example.com
#
# [test_remote_1]
# host = server2.example.com
# tests = 3,4
#
# A [test_n] block defines a host to be tested. ilo and host should point to
# ilo and data IP respectively. The test suite will be run twice per host: once
# from the local machine and once via ssh. The current checkout is copied and
# the testsuite is executed in local mode which uses hponcfg. If you do not
# specify a data ip, the second test will not be performed.
#
# A [test_remote_n] block specifies a remote server on which to also run the
# test matrix for all [test_n] hosts. It will not redo the hponcfg based tests
# as that'd be useless.
#
# (c) 2011-2013 Dennis Kaarsemaker <[email protected]>
# see COPYING for license details
import ConfigParser
import glob
import optparse
import os
from pprint import pprint
import re
import subprocess
H = lambda x: '.' in x and x[:x.find('.')] or x
def main():
usage = """%prog [options]"""
p = optparse.OptionParser(usage=usage, add_help_option=False)
p.add_option("-c", "--config", dest="config", default="~/.ilo.conf",
help="File containing authentication and config details", metavar="FILE")
p.add_option("-d", "--debug", dest="debug", action="count", default=0,
help="Output debug information, repeat to see all XML data")
p.add_option('--enable-write-tests', dest="do_write_tests", action="store_true", default=False,
help="Do tests that change the iLO configuration too")
opts, args = p.parse_args()
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser(opts.config))
login = config.get('ilo', 'login')
password = config.get('ilo', 'password')
local_tests = []
remote_tests = []
for section in config.sections():
if re.match(r'^test_\d+$', section):
local_tests.append(section)
elif re.match(r'^test_remote_\d+$', section):
remote_tests.append(section)
local_tests.sort()
remote_tests.sort()
results = []
for proxy in [None] + remote_tests:
if not proxy:
proxy = 'localhost'
my_tests = local_tests
else:
my_tests = ['test_' + x for x in config.get(proxy, 'tests').split(',')]
proxy = config.get(proxy, 'host')
run_on_host('localhost', ['rsync', '-av'] + glob.glob('./hpilo*') + [proxy + ':hpilo_test/'])
run_on_host('localhost', ['rsync', '-av', os.path.join(os.path.expanduser('~'), '.ilo.conf'), proxy + ':.ilo.conf'])
print("Running all tests on %s" % proxy)
_, py2, _ = run_on_host(proxy, ['which', 'python2'], True)
_, py3, _ = run_on_host(proxy, ['which', 'python3'], True)
py2 = py2.strip()
py3 = py3.strip()
for test in my_tests:
ilo = config.get(test, 'ilo')
host = config.get(test, 'host')
print("Testing iLO connection to %s" % ilo)
if py2:
results.append((proxy, 2, H(ilo), 'remote', run_test(ilo, py2, login, password, proxy, opts)))
if py3:
results.append((proxy, 3, H(ilo), 'remote', run_test(ilo, py3, login, password, proxy, opts)))
if proxy == 'localhost':
print("Testing hponcfg on %s" % host)
run_on_host('localhost', ['rsync', '-av'] + glob.glob('./hpilo*') + [host + ':hpilo_test/'])
run_on_host('localhost', ['rsync', '-av', os.path.join(os.path.expanduser('~'), '.ilo.conf'), host + ':.ilo.conf'])
results.append((H(ilo), 2, 'localhost', 'local', run_test('localhost', '/usr/bin/python', login, password, host, opts, 'local')))
ok = '\033[32mOK\033[0m'
fail = '\033[31mFAIL\033[0m'
print("Test results:")
plen = max([len(x[0]) for x in results])
hlen = max([len(x[2]) for x in results])
msgformat = "%-*s %-*s %-*s %-*s %-*s"
print(msgformat % (plen, 'proxy', 6, 'python', hlen, 'subject', 8, 'protocol', 6, 'result'))
print("=" * (28 + hlen + plen))
for result in results:
print(msgformat % (plen, result[0], 6, result[1], hlen, result[2], 8, result[3], 6, result[4][0] and fail or ok))
def run_test(host, python, login, password, proxy, opts, protocol=None):
hpilo_cli = './hpilo_cli'
if proxy != 'localhost':
hpilo_cli = 'hpilo_test/hpilo_cli'
command = [python, '-u', hpilo_cli]
if protocol:
command += ['--protocol', protocol]
if protocol == 'local':
command = ['sudo', 'PATH=/bin:/usr/bin:/sbin:/usr/sbin'] + command
for _ in range(opts.debug):
command.append('-d')
command.append(host)
if opts.do_write_tests:
command.append('_test_writes')
else:
command.append('_test')
return run_on_host(proxy, command)
def run_on_host(host, command, redirect=False):
if host != 'localhost':
command = ['ssh', host] + command
if redirect:
redirect = subprocess.PIPE
else:
redirect = None
sp = subprocess.Popen(command, stdout=redirect, stderr = redirect)
out, err = sp.communicate()
return sp.returncode, out, err
if __name__ == '__main__':
main()