From patchwork Wed Dec 2 19:08:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 336275 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CB82C71155 for ; Wed, 2 Dec 2020 19:10:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3E5FE2225B for ; Wed, 2 Dec 2020 19:10:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389599AbgLBTJs (ORCPT ); Wed, 2 Dec 2020 14:09:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389529AbgLBTJs (ORCPT ); Wed, 2 Dec 2020 14:09:48 -0500 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50E85C0613D4 for ; Wed, 2 Dec 2020 11:09:02 -0800 (PST) Received: by mail-qt1-x849.google.com with SMTP id n12so2177392qta.9 for ; Wed, 02 Dec 2020 11:09:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=VgZZRtoNWqKAzcK9/aAfu4oisqmQ1m2yKtDTP72pRS8=; b=WHGSfdYRlSlhEGGMVMYPPPYzHlhiGDbLAp4lTztktEMvDDFZ351hKvDI6ak0IDYL0K 4vM/yC7E+M7FoBCkjdIifeu6O2iRQRwaG7Ld/4krDfHMGst0omr4oMCLOoKTT2QPDP9D +0o1Fcbb8+cpQWpUlwzjWI/cYW5KuNTV0ctEy0h1YevLnm7UBz9LZlDVl4snGzEOiSTs 5tTiv05ymTPVdI7WbWA3EsNbch6yz6KMHKj3V0Rc5F78ESCZ0WSFViwXxD5EOKDmo5TL DIS/JHw0zf2dE5rhT2kh+cGz1aSPm3i812q1sCnP1XU/D91aGBaWhTbSMMQbdrlU5L/M 6EXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=VgZZRtoNWqKAzcK9/aAfu4oisqmQ1m2yKtDTP72pRS8=; b=KpbOoUh5D4SLJMsEbhq7ujSfsyZQEqQBWqS1G0xUk3VchkkvjHz9pOxef7qKBNnv9O ouO28riNXdZXrh+fpsHPs0Ba0gsvj1qZX/OJegVi9Qc/zunPeuDcSjjovBC6rVgeoPUC +tBnJApLuOcoQA/v1aQUEnwrwRaNtOWs/Z6jG2cQmgixY/LXCPo0pXmFHoeToqvRm/WK GmgabySxdc+uFSKO8NB21Vr3pa8FFkLKaT631Au02/FJeL+cOPnHj5EhLGhURdX9LKlh oWJ5v+RLcNGc4S6tZvwQpOKZTjLNLh/m1j2CHlmsaAq767IJ4Fw+2nZyDfwa/SjwzHj3 7dLQ== X-Gm-Message-State: AOAM533qrNTu8BTPDwk0ibEQkFGmG2TznSpb3W/kjugf5ZyNEZsm4pih Gy7FS1nSnvM56mDsLCvZgOQv+ias5DPegA== X-Google-Smtp-Source: ABdhPJzOiX4EKL0rcePoFns9dZ95KoLwyDkAgaSYDwB0MwIZJTO6Yn0TRQWFmbIj+I30lsMbuvMXWYpXAdxD1w== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:ad4:42d2:: with SMTP id f18mr4135893qvr.23.1606936141444; Wed, 02 Dec 2020 11:09:01 -0800 (PST) Date: Wed, 2 Dec 2020 11:08:21 -0800 Message-Id: <20201202190824.1309398-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.29.2.576.ga3fc446d84-goog Subject: [PATCH v2 1/4] kunit: tool: fix unit test cleanup handling From: Daniel Latypov To: davidgow@google.com Cc: brendanhiggins@google.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org * Stop leaking file objects. * Use self.addCleanup() to ensure we call cleanup functions even if setUp() fails. * use mock.patch.stopall instead of more error-prone manual approach Signed-off-by: Daniel Latypov Reviewed-by: David Gow Reviewed-by: David Gow Reviewed-by: David Gow Tested-by: Brendan Higgins Acked-by: Brendan Higgins Tested-by: Brendan Higgins Acked-by: Brendan Higgins Tested-by: Brendan Higgins Acked-by: Brendan Higgins --- tools/testing/kunit/kunit_tool_test.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) base-commit: 509a15421674b9e1a3e1916939d0d0efd3e578da diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index 497ab51bc170..3fbe1acd531a 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -288,19 +288,17 @@ class StrContains(str): class KUnitMainTest(unittest.TestCase): def setUp(self): path = get_absolute_path('test_data/test_is_test_passed-all_passed.log') - file = open(path) - all_passed_log = file.readlines() - self.print_patch = mock.patch('builtins.print') - self.print_mock = self.print_patch.start() + with open(path) as file: + all_passed_log = file.readlines() + + self.print_mock = mock.patch('builtins.print').start() + self.addCleanup(mock.patch.stopall) + self.linux_source_mock = mock.Mock() self.linux_source_mock.build_reconfig = mock.Mock(return_value=True) self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True) self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log) - def tearDown(self): - self.print_patch.stop() - pass - def test_config_passes_args_pass(self): kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock) assert self.linux_source_mock.build_reconfig.call_count == 1 From patchwork Wed Dec 2 19:08:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 336274 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAABFC71155 for ; Wed, 2 Dec 2020 19:10:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5F2DE2225B for ; Wed, 2 Dec 2020 19:10:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389529AbgLBTKZ (ORCPT ); Wed, 2 Dec 2020 14:10:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387967AbgLBTKY (ORCPT ); Wed, 2 Dec 2020 14:10:24 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDD84C061A48 for ; Wed, 2 Dec 2020 11:09:07 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id h189so2020640qke.19 for ; Wed, 02 Dec 2020 11:09:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc:content-transfer-encoding; bh=XfUwdLBf1qrMT7Mo76z45utfEuCPvJ6tn0QOg+7yY44=; b=D4JV3G6WUyCqsuacWw8GzqoiJcY1MVj6QVkWwFtVrCC3HTiB5KarrybY9sxKABGm85 LEvkzwkiGJUWx9ca0iAdCdwb4Yomi1qt8kfZMT9whlM1saWv/9oAhhyzP6vga5BX0Ii6 qpmS3S0kJopMMZ9EoOxgOlfVwDhbZ/C0twzx2M/W3dqIduArX9ClzR/dTtU3GgtYXuQv DedoEPSAc0XrOlH8hGDjmUvGI7srBcn1okWbVn7AlRGdnpjnX8QMykUB3AgWRsnvdePZ 0GPaMZ//LBGvgatvCF8Zrzn4sZ49yhJBSObtAGDeFymhqj6+oUMC4AxX1kgeKhnRfgRa wJYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=XfUwdLBf1qrMT7Mo76z45utfEuCPvJ6tn0QOg+7yY44=; b=Mljwn51r7Ia/loW+t1rhPK3ynxWudXqpuMYxY4saUPWVpesYOxPjG8AgY64sTSYMG0 sSUnityTOU0e481nExed7jKFS/mVHJ/36ONq1vf0MRnfkL/J5MSgqKAAz13ngiPgLDdj v1A9J5B1LRQViP2AGU0plDN668tfQsDUwFzfy6PVHT6QMRwlwxo8WQoWUJ+TAlp8eS8c DWmu9RI9gdyPFkC+g9b/HzikW7Mav4s2UqyaO/JWli/by06Kukm4/kIdmSfA+C4Uya2x MkWwGgQC0dGeYdxbfsRLYhOS6TqrpnesoSfcUgzHZmh8nhi2+lzERpPixTjjS4lVN8yi 0LdQ== X-Gm-Message-State: AOAM531RyAkZX3mtkUNRBY83wdKt7xZkY3BaXuto7liHQRgaeIJP9maq pLgR64bQGZxjFEazMjkmsILJQH+X0wDA2w== X-Google-Smtp-Source: ABdhPJxVEvgUcIFLR1PPQYBty2WAMvkxuvAvnow8NaiOjNi9FQ5ebQQ9Z/aJCsAgbs1yB1kFOS9kyv155dlBlw== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a05:6214:18d0:: with SMTP id cy16mr3954245qvb.3.1606936146978; Wed, 02 Dec 2020 11:09:06 -0800 (PST) Date: Wed, 2 Dec 2020 11:08:24 -0800 In-Reply-To: <20201202190824.1309398-1-dlatypov@google.com> Message-Id: <20201202190824.1309398-4-dlatypov@google.com> Mime-Version: 1.0 References: <20201202190824.1309398-1-dlatypov@google.com> X-Mailer: git-send-email 2.29.2.576.ga3fc446d84-goog Subject: [PATCH v2 4/4] minor: kunit: tool: fix unit test so it can run from non-root dir From: Daniel Latypov To: davidgow@google.com Cc: brendanhiggins@google.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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/' 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: 5578d008d9e0 ("kunit: tool: fix running kunit_tool from outside kernel tree") Signed-off-by: Daniel Latypov Reviewed-by: David Gow Tested-by: Brendan Higgins Acked-by: Brendan Higgins --- tools/testing/kunit/kunit_tool_test.py | 60 +++++++++++--------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index cf160914bc55..1cd127b225a9 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -21,16 +21,18 @@ import kunit_json import kunit test_tmpdir = '' +abs_test_data_dir = '' def setUpModule(): - global test_tmpdir + global test_tmpdir, abs_test_data_dir test_tmpdir = tempfile.mkdtemp() + abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data')) def tearDownModule(): shutil.rmtree(test_tmpdir) -def get_absolute_path(path): - return os.path.join(os.path.dirname(__file__), path) +def test_data_path(path): + return os.path.join(abs_test_data_dir, path) class KconfigTest(unittest.TestCase): @@ -46,8 +48,7 @@ class KconfigTest(unittest.TestCase): def test_read_from_file(self): kconfig = kunit_config.Kconfig() - kconfig_path = get_absolute_path( - 'test_data/test_read_from_file.kconfig') + kconfig_path = test_data_path('test_read_from_file.kconfig') kconfig.read_from_file(kconfig_path) @@ -98,8 +99,7 @@ class KUnitParserTest(unittest.TestCase): str(needle) + '" not found in "' + str(haystack) + '"!') def test_output_isolated_correctly(self): - log_path = get_absolute_path( - 'test_data/test_output_isolated_correctly.log') + log_path = test_data_path('test_output_isolated_correctly.log') with open(log_path) as file: result = kunit_parser.isolate_kunit_output(file.readlines()) self.assertContains('TAP version 14', result) @@ -110,8 +110,7 @@ class KUnitParserTest(unittest.TestCase): self.assertContains('ok 1 - example', result) def test_output_with_prefix_isolated_correctly(self): - log_path = get_absolute_path( - 'test_data/test_pound_sign.log') + log_path = test_data_path('test_pound_sign.log') with open(log_path) as file: result = kunit_parser.isolate_kunit_output(file.readlines()) self.assertContains('TAP version 14', result) @@ -140,8 +139,7 @@ class KUnitParserTest(unittest.TestCase): self.assertContains('ok 3 - string-stream-test', result) def test_parse_successful_test_log(self): - all_passed_log = get_absolute_path( - 'test_data/test_is_test_passed-all_passed.log') + all_passed_log = test_data_path('test_is_test_passed-all_passed.log') with open(all_passed_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -149,8 +147,7 @@ class KUnitParserTest(unittest.TestCase): result.status) def test_parse_failed_test_log(self): - failed_log = get_absolute_path( - 'test_data/test_is_test_passed-failure.log') + failed_log = test_data_path('test_is_test_passed-failure.log') with open(failed_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -158,8 +155,7 @@ class KUnitParserTest(unittest.TestCase): result.status) def test_no_tests(self): - empty_log = get_absolute_path( - 'test_data/test_is_test_passed-no_tests_run.log') + empty_log = test_data_path('test_is_test_passed-no_tests_run.log') with open(empty_log) as file: result = kunit_parser.parse_run_tests( kunit_parser.isolate_kunit_output(file.readlines())) @@ -169,8 +165,7 @@ class KUnitParserTest(unittest.TestCase): result.status) def test_no_kunit_output(self): - crash_log = get_absolute_path( - 'test_data/test_insufficient_memory.log') + crash_log = test_data_path('test_insufficient_memory.log') print_mock = mock.patch('builtins.print').start() with open(crash_log) as file: result = kunit_parser.parse_run_tests( @@ -180,8 +175,7 @@ class KUnitParserTest(unittest.TestCase): file.close() def test_crashed_test(self): - crashed_log = get_absolute_path( - 'test_data/test_is_test_passed-crash.log') + crashed_log = test_data_path('test_is_test_passed-crash.log') with open(crashed_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -189,8 +183,7 @@ class KUnitParserTest(unittest.TestCase): result.status) def test_ignores_prefix_printk_time(self): - prefix_log = get_absolute_path( - 'test_data/test_config_printk_time.log') + prefix_log = test_data_path('test_config_printk_time.log') with open(prefix_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -199,8 +192,7 @@ class KUnitParserTest(unittest.TestCase): self.assertEqual('kunit-resource-test', result.suites[0].name) def test_ignores_multiple_prefixes(self): - prefix_log = get_absolute_path( - 'test_data/test_multiple_prefixes.log') + prefix_log = test_data_path('test_multiple_prefixes.log') with open(prefix_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -209,8 +201,7 @@ class KUnitParserTest(unittest.TestCase): self.assertEqual('kunit-resource-test', result.suites[0].name) def test_prefix_mixed_kernel_output(self): - mixed_prefix_log = get_absolute_path( - 'test_data/test_interrupted_tap_output.log') + mixed_prefix_log = test_data_path('test_interrupted_tap_output.log') with open(mixed_prefix_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -219,7 +210,7 @@ class KUnitParserTest(unittest.TestCase): self.assertEqual('kunit-resource-test', result.suites[0].name) def test_prefix_poundsign(self): - pound_log = get_absolute_path('test_data/test_pound_sign.log') + pound_log = test_data_path('test_pound_sign.log') with open(pound_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -228,7 +219,7 @@ class KUnitParserTest(unittest.TestCase): self.assertEqual('kunit-resource-test', result.suites[0].name) def test_kernel_panic_end(self): - panic_log = get_absolute_path('test_data/test_kernel_panic_interrupt.log') + panic_log = test_data_path('test_kernel_panic_interrupt.log') with open(panic_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -237,7 +228,7 @@ class KUnitParserTest(unittest.TestCase): self.assertEqual('kunit-resource-test', result.suites[0].name) def test_pound_no_prefix(self): - pound_log = get_absolute_path('test_data/test_pound_no_prefix.log') + pound_log = test_data_path('test_pound_no_prefix.log') with open(pound_log) as file: result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( @@ -248,7 +239,7 @@ class KUnitParserTest(unittest.TestCase): class KUnitJsonTest(unittest.TestCase): def _json_for(self, log_file): - with(open(get_absolute_path(log_file))) as file: + with open(test_data_path(log_file)) as file: test_result = kunit_parser.parse_run_tests(file) json_obj = kunit_json.get_json_result( test_result=test_result, @@ -258,22 +249,19 @@ class KUnitJsonTest(unittest.TestCase): return json.loads(json_obj) def test_failed_test_json(self): - result = self._json_for( - 'test_data/test_is_test_passed-failure.log') + result = self._json_for('test_is_test_passed-failure.log') self.assertEqual( {'name': 'example_simple_test', 'status': 'FAIL'}, result["sub_groups"][1]["test_cases"][0]) def test_crashed_test_json(self): - result = self._json_for( - 'test_data/test_is_test_passed-crash.log') + result = self._json_for('test_is_test_passed-crash.log') self.assertEqual( {'name': 'example_simple_test', 'status': 'ERROR'}, result["sub_groups"][1]["test_cases"][0]) def test_no_tests_json(self): - result = self._json_for( - 'test_data/test_is_test_passed-no_tests_run.log') + result = self._json_for('test_is_test_passed-no_tests_run.log') self.assertEqual(0, len(result['sub_groups'])) class StrContains(str): @@ -282,7 +270,7 @@ class StrContains(str): class KUnitMainTest(unittest.TestCase): def setUp(self): - path = get_absolute_path('test_data/test_is_test_passed-all_passed.log') + path = test_data_path('test_is_test_passed-all_passed.log') with open(path) as file: all_passed_log = file.readlines()