forked from SeattleTestbed/dist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestportfiller.py
108 lines (91 loc) · 4.13 KB
/
testportfiller.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
"""
<Program Name>
testportfiller.py
<Started>
November 13, 2008
<Author>
Brent Couvrette
<Purpose>
This module is used to fill in the port numbers in the repy unit tests.
Because the unit tests can be run on any random node you have access to,
hardcoding in a port or even a small set of ports is asking for failure
when the tests are run on a node that does not have those ports open.
Therefore it is best to dynamically determine what ports are available on
the node that is being used, then make all the tests use those ports.
However, we also want to be able to still run the unit tests locally, which
requires that this functionallity be in two places, hence the existence of
this module.
If run on its own, this module will find and replace all of the uses of port
numbers in the repy tests with some default port.
If included, the replacePorts function should be called to replace all the
ports with the given port numbers (more details in the replacePorts doc).
"""
import glob
# Goes through all of the test files and replaces the <messport> and <connport>
# tags with the ports that were found on the actual vessel
def replace_ports(foundMessports, foundConnports):
"""
<Purpose>
Replaces all mess and conn port tags in the repy test files with the given
lists of mess and conn ports. Currently, to completely replace every port,
foundMessports and foundConnports must be of length at least 3. However,
if they are shorter, It will still replace as many as it can, though this
will leave some tests with invalid syntax as they still have some
unreplaced tags.
<Arguments>
foundMessports:
The list of port numbers that should be used to replace the <messport>
tags as shown:
<messport> => foundMessports[0]
<messport1> => foundMessports[1]
<messport2> => foundMessports[2]
If a foundMessports index as given above does not exist, then that tag
will just not get replaced.
foundConnports:
The list of port numbers that should be used to replace the <connport>
tags as shown:
<connport> => foundConnports[0]
<connport1> => foundConnports[1]
<connport2> => foundConnports[2]
If a foundConnports index as given above does not exist, then that tag
will just not get replaced.
<Side Effects>
Changes all of the repy unit tests to include actual port numbers as
possible.
<Returns>
None.
"""
for testfile in glob.glob("rs_*.py") + glob.glob("ut_*.py") + glob.glob("rn_*.py") + \
glob.glob("rz_*.py") + glob.glob("rb_*.py") + glob.glob("ru_*.py") + \
glob.glob("re_*.py") + glob.glob("rl_*.py") +glob.glob("s_*.py") + \
glob.glob("n_*.py") + glob.glob("z_*.py") + glob.glob("b_*.py") + \
glob.glob("u_*.py") + glob.glob("e_*.py") + glob.glob("l_*.py") + \
glob.glob('restrictions.*') + glob.glob("ut_*.mix"):
# read in the initial file
inFile = file(testfile, 'r')
filestring = inFile.read()
inFile.close()
# Replace the instances of messport that we can replace
if len(foundMessports) >= 1:
filestring = filestring.replace('<messport>', foundMessports[0])
if len(foundMessports) >= 2:
filestring = filestring.replace('<messport1>', foundMessports[1])
if len(foundMessports) >= 3:
filestring = filestring.replace('<messport2>', foundMessports[2])
# Replace the instances of connport that we can replace
if len(foundConnports) >= 1:
filestring = filestring.replace('<connport>', foundConnports[0])
if len(foundConnports) >= 2:
filestring = filestring.replace('<connport1>', foundConnports[1])
if len(foundConnports) >= 3:
filestring = filestring.replace('<connport2>', foundConnports[2])
# write out the file with our changes
outFile = file(testfile, 'w')
outFile.write(filestring)
outFile.close()
def main():
# If running separately, just put back in the values that were previously
# hardcoded.
replace_ports(['12345', '12346', '12347'], ['12345', '12346', '12347'])
if __name__ == '__main__':
main()