Skip to content

Commit cd4a9bc

Browse files
dlatypovshuahkh
authored andcommitted
minor: kunit: tool: fix unit test so it can run from non-root dir
Also take this time to rename get_absolute_path() to test_data_path(). 1. the name is currently a lie. It gives relative paths, e.g. if I run from the same dir as the test file, it gives './test_data/<file>' See https://docs.python.org/3/reference/import.html#__file__, which doesn't stipulate that implementations provide absolute paths. 2. it's only used for generating paths to tools/testing/kunit/test_data/ So we can tersen things by making it less general. Cache the absolute path to the test data files per suggestion from [1]. Using relative paths, the tests break because of this code in kunit.py if get_kernel_root_path():         os.chdir(get_kernel_root_path()) [1] https://lore.kernel.org/linux-kselftest/CABVgOSnH0gz7z5JhRCGyG1wg0zDDBTLoSUCoB-gWMeXLgVTo2w@mail.gmail.com/ Fixes: 5578d00 ("kunit: tool: fix running kunit_tool from outside kernel tree") Signed-off-by: Daniel Latypov <[email protected]> Reviewed-by: David Gow <[email protected]> Tested-by: Brendan Higgins <[email protected]> Acked-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent a3ece07 commit cd4a9bc

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

tools/testing/kunit/kunit_tool_test.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121
import kunit
2222

2323
test_tmpdir = ''
24+
abs_test_data_dir = ''
2425

2526
def setUpModule():
26-
global test_tmpdir
27+
global test_tmpdir, abs_test_data_dir
2728
test_tmpdir = tempfile.mkdtemp()
29+
abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data'))
2830

2931
def tearDownModule():
3032
shutil.rmtree(test_tmpdir)
3133

32-
def get_absolute_path(path):
33-
return os.path.join(os.path.dirname(__file__), path)
34+
def test_data_path(path):
35+
return os.path.join(abs_test_data_dir, path)
3436

3537
class KconfigTest(unittest.TestCase):
3638

@@ -46,8 +48,7 @@ def test_is_subset_of(self):
4648

4749
def test_read_from_file(self):
4850
kconfig = kunit_config.Kconfig()
49-
kconfig_path = get_absolute_path(
50-
'test_data/test_read_from_file.kconfig')
51+
kconfig_path = test_data_path('test_read_from_file.kconfig')
5152

5253
kconfig.read_from_file(kconfig_path)
5354

@@ -98,8 +99,7 @@ def assertContains(self, needle, haystack):
9899
str(needle) + '" not found in "' + str(haystack) + '"!')
99100

100101
def test_output_isolated_correctly(self):
101-
log_path = get_absolute_path(
102-
'test_data/test_output_isolated_correctly.log')
102+
log_path = test_data_path('test_output_isolated_correctly.log')
103103
with open(log_path) as file:
104104
result = kunit_parser.isolate_kunit_output(file.readlines())
105105
self.assertContains('TAP version 14', result)
@@ -110,8 +110,7 @@ def test_output_isolated_correctly(self):
110110
self.assertContains('ok 1 - example', result)
111111

112112
def test_output_with_prefix_isolated_correctly(self):
113-
log_path = get_absolute_path(
114-
'test_data/test_pound_sign.log')
113+
log_path = test_data_path('test_pound_sign.log')
115114
with open(log_path) as file:
116115
result = kunit_parser.isolate_kunit_output(file.readlines())
117116
self.assertContains('TAP version 14', result)
@@ -140,26 +139,23 @@ def test_output_with_prefix_isolated_correctly(self):
140139
self.assertContains('ok 3 - string-stream-test', result)
141140

142141
def test_parse_successful_test_log(self):
143-
all_passed_log = get_absolute_path(
144-
'test_data/test_is_test_passed-all_passed.log')
142+
all_passed_log = test_data_path('test_is_test_passed-all_passed.log')
145143
with open(all_passed_log) as file:
146144
result = kunit_parser.parse_run_tests(file.readlines())
147145
self.assertEqual(
148146
kunit_parser.TestStatus.SUCCESS,
149147
result.status)
150148

151149
def test_parse_failed_test_log(self):
152-
failed_log = get_absolute_path(
153-
'test_data/test_is_test_passed-failure.log')
150+
failed_log = test_data_path('test_is_test_passed-failure.log')
154151
with open(failed_log) as file:
155152
result = kunit_parser.parse_run_tests(file.readlines())
156153
self.assertEqual(
157154
kunit_parser.TestStatus.FAILURE,
158155
result.status)
159156

160157
def test_no_tests(self):
161-
empty_log = get_absolute_path(
162-
'test_data/test_is_test_passed-no_tests_run.log')
158+
empty_log = test_data_path('test_is_test_passed-no_tests_run.log')
163159
with open(empty_log) as file:
164160
result = kunit_parser.parse_run_tests(
165161
kunit_parser.isolate_kunit_output(file.readlines()))
@@ -169,8 +165,7 @@ def test_no_tests(self):
169165
result.status)
170166

171167
def test_no_kunit_output(self):
172-
crash_log = get_absolute_path(
173-
'test_data/test_insufficient_memory.log')
168+
crash_log = test_data_path('test_insufficient_memory.log')
174169
print_mock = mock.patch('builtins.print').start()
175170
with open(crash_log) as file:
176171
result = kunit_parser.parse_run_tests(
@@ -180,17 +175,15 @@ def test_no_kunit_output(self):
180175
file.close()
181176

182177
def test_crashed_test(self):
183-
crashed_log = get_absolute_path(
184-
'test_data/test_is_test_passed-crash.log')
178+
crashed_log = test_data_path('test_is_test_passed-crash.log')
185179
with open(crashed_log) as file:
186180
result = kunit_parser.parse_run_tests(file.readlines())
187181
self.assertEqual(
188182
kunit_parser.TestStatus.TEST_CRASHED,
189183
result.status)
190184

191185
def test_ignores_prefix_printk_time(self):
192-
prefix_log = get_absolute_path(
193-
'test_data/test_config_printk_time.log')
186+
prefix_log = test_data_path('test_config_printk_time.log')
194187
with open(prefix_log) as file:
195188
result = kunit_parser.parse_run_tests(file.readlines())
196189
self.assertEqual(
@@ -199,8 +192,7 @@ def test_ignores_prefix_printk_time(self):
199192
self.assertEqual('kunit-resource-test', result.suites[0].name)
200193

201194
def test_ignores_multiple_prefixes(self):
202-
prefix_log = get_absolute_path(
203-
'test_data/test_multiple_prefixes.log')
195+
prefix_log = test_data_path('test_multiple_prefixes.log')
204196
with open(prefix_log) as file:
205197
result = kunit_parser.parse_run_tests(file.readlines())
206198
self.assertEqual(
@@ -209,8 +201,7 @@ def test_ignores_multiple_prefixes(self):
209201
self.assertEqual('kunit-resource-test', result.suites[0].name)
210202

211203
def test_prefix_mixed_kernel_output(self):
212-
mixed_prefix_log = get_absolute_path(
213-
'test_data/test_interrupted_tap_output.log')
204+
mixed_prefix_log = test_data_path('test_interrupted_tap_output.log')
214205
with open(mixed_prefix_log) as file:
215206
result = kunit_parser.parse_run_tests(file.readlines())
216207
self.assertEqual(
@@ -219,7 +210,7 @@ def test_prefix_mixed_kernel_output(self):
219210
self.assertEqual('kunit-resource-test', result.suites[0].name)
220211

221212
def test_prefix_poundsign(self):
222-
pound_log = get_absolute_path('test_data/test_pound_sign.log')
213+
pound_log = test_data_path('test_pound_sign.log')
223214
with open(pound_log) as file:
224215
result = kunit_parser.parse_run_tests(file.readlines())
225216
self.assertEqual(
@@ -228,7 +219,7 @@ def test_prefix_poundsign(self):
228219
self.assertEqual('kunit-resource-test', result.suites[0].name)
229220

230221
def test_kernel_panic_end(self):
231-
panic_log = get_absolute_path('test_data/test_kernel_panic_interrupt.log')
222+
panic_log = test_data_path('test_kernel_panic_interrupt.log')
232223
with open(panic_log) as file:
233224
result = kunit_parser.parse_run_tests(file.readlines())
234225
self.assertEqual(
@@ -237,7 +228,7 @@ def test_kernel_panic_end(self):
237228
self.assertEqual('kunit-resource-test', result.suites[0].name)
238229

239230
def test_pound_no_prefix(self):
240-
pound_log = get_absolute_path('test_data/test_pound_no_prefix.log')
231+
pound_log = test_data_path('test_pound_no_prefix.log')
241232
with open(pound_log) as file:
242233
result = kunit_parser.parse_run_tests(file.readlines())
243234
self.assertEqual(
@@ -248,7 +239,7 @@ def test_pound_no_prefix(self):
248239
class KUnitJsonTest(unittest.TestCase):
249240

250241
def _json_for(self, log_file):
251-
with(open(get_absolute_path(log_file))) as file:
242+
with open(test_data_path(log_file)) as file:
252243
test_result = kunit_parser.parse_run_tests(file)
253244
json_obj = kunit_json.get_json_result(
254245
test_result=test_result,
@@ -258,22 +249,19 @@ def _json_for(self, log_file):
258249
return json.loads(json_obj)
259250

260251
def test_failed_test_json(self):
261-
result = self._json_for(
262-
'test_data/test_is_test_passed-failure.log')
252+
result = self._json_for('test_is_test_passed-failure.log')
263253
self.assertEqual(
264254
{'name': 'example_simple_test', 'status': 'FAIL'},
265255
result["sub_groups"][1]["test_cases"][0])
266256

267257
def test_crashed_test_json(self):
268-
result = self._json_for(
269-
'test_data/test_is_test_passed-crash.log')
258+
result = self._json_for('test_is_test_passed-crash.log')
270259
self.assertEqual(
271260
{'name': 'example_simple_test', 'status': 'ERROR'},
272261
result["sub_groups"][1]["test_cases"][0])
273262

274263
def test_no_tests_json(self):
275-
result = self._json_for(
276-
'test_data/test_is_test_passed-no_tests_run.log')
264+
result = self._json_for('test_is_test_passed-no_tests_run.log')
277265
self.assertEqual(0, len(result['sub_groups']))
278266

279267
class StrContains(str):
@@ -282,7 +270,7 @@ def __eq__(self, other):
282270

283271
class KUnitMainTest(unittest.TestCase):
284272
def setUp(self):
285-
path = get_absolute_path('test_data/test_is_test_passed-all_passed.log')
273+
path = test_data_path('test_is_test_passed-all_passed.log')
286274
with open(path) as file:
287275
all_passed_log = file.readlines()
288276

0 commit comments

Comments
 (0)