Skip to content

Commit b9f8720

Browse files
committed
make test python2 compilant
fix user and oa regions test
1 parent 7b7da37 commit b9f8720

1 file changed

Lines changed: 127 additions & 72 deletions

File tree

test/test.py

Lines changed: 127 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,45 @@
44
import subprocess
55
import os
66
import shutil
7+
import sys
8+
import pkgutil
9+
10+
def call(arguments, env = os.environ.copy()):
11+
"""
12+
return a triple with (returncode, stdout, stderr) from the call to subprocess
13+
"""
14+
result = ()
15+
if sys.version_info > (3, 5):
16+
out = subprocess.run(
17+
arguments,
18+
env=env,
19+
stdout=subprocess.PIPE,
20+
stderr=subprocess.PIPE)
21+
result = (
22+
out.returncode,
23+
out.stdout.decode("utf-8"),
24+
out.stderr.decode("utf-8"))
25+
else:
26+
p = subprocess.Popen(
27+
arguments,
28+
env=env,
29+
stdout=subprocess.PIPE,
30+
stderr=subprocess.PIPE)
31+
stdout, stderr = p.communicate()
32+
p.wait()
33+
result = (p.returncode, stdout.decode("utf-8"), stderr.decode("utf-8"))
34+
return result
735

836

937
class TestScorepBindingsPython(unittest.TestCase):
1038
maxDiff = None
39+
python = sys.executable
40+
41+
def assertRegex(self, in1, in2):
42+
if sys.version_info > (3, 5):
43+
super().assertRegex(in1, in2)
44+
else:
45+
super(TestScorepBindingsPython, self).assertRegexpMatches(in1, in2)
1146

1247
def setUp(self):
1348
self.env = os.environ.copy()
@@ -29,109 +64,129 @@ def test_user_regions(self):
2964
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_user_regions"
3065
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
3166

32-
out = subprocess.run(["python3",
33-
"test_user_regions.py"],
34-
stdout=subprocess.PIPE,
35-
stderr=subprocess.PIPE,
36-
env=env)
37-
self.assertEqual(out.stderr.decode("utf-8"), self.expected_std_err)
38-
self.assertEqual(out.stdout.decode("utf-8"), "hello world\n")
39-
40-
out = subprocess.run(["otf2-print", trace_path],
41-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42-
self.assertEqual(out.stderr.decode("utf-8"), "")
43-
self.assertRegex(out.stdout.decode("utf-8"),
67+
out = call([self.python,
68+
"-m",
69+
"scorep",
70+
"--nopython",
71+
"test_user_regions.py"],
72+
env=env)
73+
std_out = out[1]
74+
std_err = out[2]
75+
76+
self.assertEqual(std_err, self.expected_std_err)
77+
self.assertEqual(std_out, "hello world\n")
78+
79+
out = call(["otf2-print", trace_path])
80+
std_out = out[1]
81+
std_err = out[2]
82+
83+
self.assertRegex(std_out,
4484
'ENTER[ ]*[0-9 ]*[0-9 ]*Region: "user:test_region"')
45-
self.assertRegex(out.stdout.decode("utf-8"),
85+
self.assertRegex(std_out,
4686
'LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "user:test_region"')
4787

4888
def test_oa_regions(self):
4989
env = self.env
5090
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_oa_regions"
5191
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
52-
53-
out = subprocess.run(["python3",
54-
"test_oa_regions.py"],
55-
stdout=subprocess.PIPE,
56-
stderr=subprocess.PIPE,
57-
env=env)
58-
self.assertEqual(out.stderr.decode("utf-8"), self.expected_std_err)
59-
self.assertEqual(out.stdout.decode("utf-8"), "hello world\n")
60-
61-
out = subprocess.run(["otf2-print", trace_path],
62-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63-
self.assertEqual(out.stderr.decode("utf-8"), "")
64-
self.assertRegex(out.stdout.decode("utf-8"),
92+
93+
out = call([self.python,
94+
"-m",
95+
"scorep",
96+
"--nopython",
97+
"test_oa_regions.py"],
98+
env=env)
99+
std_out = out[1]
100+
std_err = out[2]
101+
102+
self.assertEqual(std_err, self.expected_std_err)
103+
self.assertEqual(std_out, "hello world\n")
104+
105+
out = call(["otf2-print", trace_path])
106+
std_out = out[1]
107+
std_err = out[2]
108+
109+
self.assertEqual(std_err, "")
110+
self.assertRegex(std_out,
65111
'ENTER[ ]*[0-9 ]*[0-9 ]*Region: "test_region"')
66-
self.assertRegex(out.stdout.decode("utf-8"),
112+
self.assertRegex(std_out,
67113
'LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "test_region"')
68114

69115
def test_instrumentation(self):
70116
env = self.env
71117
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_instrumentation"
72118
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
73119

74-
out = subprocess.run(["python3",
75-
"-m",
76-
"scorep",
77-
"--nocompiler",
78-
"test_instrumentation.py"],
79-
stdout=subprocess.PIPE,
80-
stderr=subprocess.PIPE,
81-
env=env)
82-
self.assertEqual(out.stderr.decode("utf-8"), self.expected_std_err)
83-
self.assertEqual(out.stdout.decode("utf-8"), "hello world\nbaz\nbar\n")
84-
85-
out = subprocess.run(["otf2-print", trace_path],
86-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
87-
self.assertEqual(out.stderr.decode("utf-8"), "")
88-
self.assertRegex(out.stdout.decode("utf-8"),
120+
out = call([self.python,
121+
"-m",
122+
"scorep",
123+
"--nocompiler",
124+
"test_instrumentation.py"],
125+
env=env)
126+
std_out = out[1]
127+
std_err = out[2]
128+
129+
self.assertEqual(std_err, self.expected_std_err)
130+
self.assertEqual(std_out, "hello world\nbaz\nbar\n")
131+
132+
out = call(["otf2-print", trace_path])
133+
std_out = out[1]
134+
std_err = out[2]
135+
136+
self.assertEqual(std_err, "")
137+
self.assertRegex(std_out,
89138
'ENTER[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo"')
90-
self.assertRegex(out.stdout.decode("utf-8"),
139+
self.assertRegex(std_out,
91140
'LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo"')
92141

142+
@unittest.skipIf(len(pkgutil.extend_path([], "mpi4py")) == 0 or
143+
len(pkgutil.extend_path([], "numpy")) == 0,
144+
"no mpi4py present")
93145
def test_mpi(self):
146+
94147
env = self.env
95148
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_mpi"
96149
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
97-
out = subprocess.run(["mpirun",
98-
"-n",
99-
"2",
100-
"python3",
101-
"-m",
102-
"scorep",
103-
"--mpp=mpi",
104-
"--nocompiler",
105-
"test_mpi.py"],
106-
stdout=subprocess.PIPE,
107-
stderr=subprocess.PIPE,
108-
env=env)
109-
expected_std_err = ""
150+
out = call(["mpirun",
151+
"-n",
152+
"2",
153+
self.python,
154+
"-m",
155+
"scorep",
156+
"--mpp=mpi",
157+
"--nocompiler",
158+
"test_mpi.py"],
159+
env=env)
160+
161+
std_out = out[1]
162+
std_err = out[2]
110163

111-
expected_std_out = \
112-
"[00] [0. 1. 2. 3. 4.]\n" +\
113-
"[01] [0. 1. 2. 3. 4.]\n"
164+
expected_std_err = ""
165+
expected_std_out = "\[0[0-9]\] \[0. 1. 2. 3. 4.\]\\n\[0[0-9]] \[0. 1. 2. 3. 4.\]\\n"
114166

115-
# TODO
116-
#self.assertEqual(out.stderr.decode("utf-8"), expected_std_err)
117-
#self.assertEqual(out.stdout.decode("utf-8"), "hello world\n")
167+
self.assertRegex(std_err,
168+
'\[Score-P\] [\w/.: ]*MPI_THREAD_FUNNELED')
169+
self.assertRegex(std_out, expected_std_out)
170+
171+
expected_std_out
118172

119173
def test_call_main(self):
120174
env = self.env
121175
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_call_main"
122176
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
123-
out = subprocess.run(["python3",
124-
"-m",
125-
"scorep",
126-
"--nocompiler",
127-
"test_call_main.py"],
128-
stdout=subprocess.PIPE,
129-
stderr=subprocess.PIPE,
130-
env=env)
177+
out = call([self.python,
178+
"-m",
179+
"scorep",
180+
"--nocompiler",
181+
"test_call_main.py"],
182+
env=env)
183+
std_out = out[1]
184+
std_err = out[2]
185+
131186
expected_std_err = "scorep: Someone called scorep\.__main__\.main"
132187
expected_std_out = ""
133-
self.assertRegex(out.stderr.decode("utf-8"), expected_std_err)
134-
self.assertEqual(out.stdout.decode("utf-8"), expected_std_out)
188+
self.assertRegex(std_err, expected_std_err)
189+
self.assertEqual(std_out, expected_std_out)
135190

136191
def tearDown(self):
137192
shutil.rmtree(

0 commit comments

Comments
 (0)