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: 11946843 X-Patchwork-Delegate: shuah@kernel.org 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 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:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 11946845 X-Patchwork-Delegate: shuah@kernel.org 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 16D47C83013 for ; Wed, 2 Dec 2020 19:10:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7076622254 for ; Wed, 2 Dec 2020 19:10:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389691AbgLBTJu (ORCPT ); Wed, 2 Dec 2020 14:09:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389529AbgLBTJu (ORCPT ); Wed, 2 Dec 2020 14:09:50 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 293BFC0617A6 for ; Wed, 2 Dec 2020 11:09:04 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id u37so2964822ybi.15 for ; Wed, 02 Dec 2020 11:09:04 -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; bh=LaYOodOz6PHC9js4K1TKag05C3DcrBBHqvjyVy43Jqc=; b=fl3D1QpAZ+NCjnrqMzc2jrvICaUs7Q3ZnbRRtEbl1II19wv8qjw6tmbT9bM6CLqGRo dV9Lx2PqN7k8SlHh02T1m4GW2rlkvpWEydP/F4VNNZr/bBSa77yIh95ba+P7s1oSK89H bLu/aFyveBcpVYG47LS5s1pXg3axmlNheXuyyWAvaDnAWcsBTtPyIgpxptSZxWlO5ekC nFukXuJHMwaKQIfqjIKVOc+86P84V05o0VQmiGgicXDRdKuSXNIPF/U514ae3Tb0A4BX /Dd0od3QHy7/rCD1eUOnFN723t+Hvuf6lIhEwaFrBBDA0d1TVhawqZ06PlkibdEEXW/f JaeA== 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; bh=LaYOodOz6PHC9js4K1TKag05C3DcrBBHqvjyVy43Jqc=; b=mIfbFO9lQjlngVpmoPBCKz26qU8/tUIPwCxAbHoXIpJzx5bUrcFJP0jGZKuCkTEnAw qC7LHskx1iTFJFnSNLd/9X57LnOMECM+4ndc/x5H6hyhkqno3D1mSESJI2SdZTJKZa9H OGVANw7iGdah1KwhSd7jf5fupXsWlJqbbaWVUFvAYIDKn4hpr7vtORuyWK1tbT1VDYoX UBndYSwHIHnLEftIgadFXjgL04S96y/lZUGWCuMPVpfgadDJV48WnE/hSOhW8Phb8hq7 Lq8/r2I+Nu22ikCJmrqiKimvIza9LdphQo3TspeCGw1EFtdHWgOpcLk+ZJRUvWMuZTEL UcKw== X-Gm-Message-State: AOAM530c+bUHDs4HzjWTYPAcLW7WM3PHHKfE1Xrt1DRGqS3q1sNjNKcM esgyp6uEe7kMZem0KD8msLeZOLEpX+eCIw== X-Google-Smtp-Source: ABdhPJziALyfJnuN3AAJNi2ehoDUXGPN2+qhrVRgqdzwMctq3MSPF8RuvMv6/3jDJ7UQwl79wXiQJTHIbOVRTg== 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:a25:aad3:: with SMTP id t77mr5567966ybi.180.1606936143384; Wed, 02 Dec 2020 11:09:03 -0800 (PST) Date: Wed, 2 Dec 2020 11:08:22 -0800 In-Reply-To: <20201202190824.1309398-1-dlatypov@google.com> Message-Id: <20201202190824.1309398-2-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 2/4] kunit: tool: stop using bare asserts in unit test 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 Use self.assertEqual/assertNotEqual() instead. Besides being more appropriate in a unit test, it'll also give a better error message by show the unexpected values. Also * Delete redundant check of exception types. self.assertRaises does this. * s/kall/call. There's no reason to name it this way. * This is probably a misunderstanding from the docs which uses it since `mock.call` is in scope as `call`. Signed-off-by: Daniel Latypov Reviewed-by: David Gow Tested-by: Brendan Higgins Acked-by: Brendan Higgins --- tools/testing/kunit/kunit_tool_test.py | 50 +++++++++++++------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index 3fbe1acd531a..3a74e5612cf9 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -301,26 +301,26 @@ class KUnitMainTest(unittest.TestCase): 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 - assert self.linux_source_mock.run_kernel.call_count == 0 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) def test_build_passes_args_pass(self): kunit.main(['build'], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 0 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, '.kunit', None) - assert self.linux_source_mock.run_kernel.call_count == 0 + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) def test_exec_passes_args_pass(self): kunit.main(['exec'], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 0 - assert self.linux_source_mock.run_kernel.call_count == 1 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='.kunit', timeout=300) self.print_mock.assert_any_call(StrContains('Testing complete.')) def test_run_passes_args_pass(self): kunit.main(['run'], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 1 - assert self.linux_source_mock.run_kernel.call_count == 1 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) self.linux_source_mock.run_kernel.assert_called_once_with( build_dir='.kunit', timeout=300) self.print_mock.assert_any_call(StrContains('Testing complete.')) @@ -329,35 +329,33 @@ class KUnitMainTest(unittest.TestCase): self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) with self.assertRaises(SystemExit) as e: kunit.main(['exec'], self.linux_source_mock) - assert type(e.exception) == SystemExit - assert e.exception.code == 1 + self.assertEqual(e.exception.code, 1) def test_run_passes_args_fail(self): self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) with self.assertRaises(SystemExit) as e: kunit.main(['run'], self.linux_source_mock) - assert type(e.exception) == SystemExit - assert e.exception.code == 1 - assert self.linux_source_mock.build_reconfig.call_count == 1 - assert self.linux_source_mock.run_kernel.call_count == 1 + self.assertEqual(e.exception.code, 1) + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) self.print_mock.assert_any_call(StrContains(' 0 tests run')) def test_exec_raw_output(self): self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) kunit.main(['exec', '--raw_output'], self.linux_source_mock) - assert self.linux_source_mock.run_kernel.call_count == 1 - for kall in self.print_mock.call_args_list: - assert kall != mock.call(StrContains('Testing complete.')) - assert kall != mock.call(StrContains(' 0 tests run')) + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) + for call in self.print_mock.call_args_list: + self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) + self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) def test_run_raw_output(self): self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) kunit.main(['run', '--raw_output'], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 1 - assert self.linux_source_mock.run_kernel.call_count == 1 - for kall in self.print_mock.call_args_list: - assert kall != mock.call(StrContains('Testing complete.')) - assert kall != mock.call(StrContains(' 0 tests run')) + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) + self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) + for call in self.print_mock.call_args_list: + self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) + self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) def test_exec_timeout(self): timeout = 3453 @@ -368,7 +366,7 @@ class KUnitMainTest(unittest.TestCase): def test_run_timeout(self): timeout = 3453 kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 1 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) self.linux_source_mock.run_kernel.assert_called_once_with( build_dir='.kunit', timeout=timeout) self.print_mock.assert_any_call(StrContains('Testing complete.')) @@ -376,7 +374,7 @@ class KUnitMainTest(unittest.TestCase): def test_run_builddir(self): build_dir = '.kunit' kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 1 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) self.linux_source_mock.run_kernel.assert_called_once_with( build_dir=build_dir, timeout=300) self.print_mock.assert_any_call(StrContains('Testing complete.')) @@ -384,7 +382,7 @@ class KUnitMainTest(unittest.TestCase): def test_config_builddir(self): build_dir = '.kunit' kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock) - assert self.linux_source_mock.build_reconfig.call_count == 1 + self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) def test_build_builddir(self): build_dir = '.kunit' From patchwork Wed Dec 2 19:08:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 11946841 X-Patchwork-Delegate: shuah@kernel.org 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 72595C71156 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 0F5A822259 for ; Wed, 2 Dec 2020 19:10:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389676AbgLBTJq (ORCPT ); Wed, 2 Dec 2020 14:09:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389599AbgLBTJq (ORCPT ); Wed, 2 Dec 2020 14:09:46 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E855AC0617A7 for ; Wed, 2 Dec 2020 11:09:05 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id m186so2959551ybm.22 for ; Wed, 02 Dec 2020 11:09:05 -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; bh=d9ODsGy71UxS1scEvGTz4NCozRGDYn/BsJFydzZu0V0=; b=dm5FdKlSIuxUi4iLenNKFWMujqwqankdTmdkODUJuiHoLawGQEiQYIlCsPA196Zy5s rJ03obABD3u8Q3lABqlHBI08yEr3pL8Hwdv1f+NnXPVmFwdZn0VdbbpsxCKacL58cOsf 2K9qn3WjkeW/Vxhz6Z3LMNidcFYPXIh/q8TfQ8aBjP4Cgb+LmgppN4kyjCuo0Gp6ZZ9u 3fuLgKBkULfSfv42PD05FLsXvhPJGUSSex8Sk2qXGJgjhARC6jrne1bB09543UR464Kr Y1nO8tsaSexhUXeMwgV8AVn9NlC53AwAtGqznDMKxTRRTr0hEuzel7fqh67UJxVi+Aw4 D8vA== 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; bh=d9ODsGy71UxS1scEvGTz4NCozRGDYn/BsJFydzZu0V0=; b=sLrYrosf3D3DWBxAhT92DVMoindycQsXyLd8T6C+iAmev+2asHfe645PJydPWNtXfm 7Ap1TpBaf1scvR3BJ6Ev8mobIPR/AlShKVo+vmACaP4aldg2j04Hkk5qkL3T3Hks/2kO EEBOoAm8usNQUVIFuLaPDiEuemg5Cyu15eDZpddI0/p1IlPzYvjwQHqnyj486ovyyDUh s9m7s1RgpUs+gREE6kr1u3yAk/xw3TZTvdR+NZpwqkBUn/kHYnGWxPnxMBR4/xpvFcUY 1BeKAt+AlQbwplkcBG5AWrzxXtd/Nh2NCMibYwB6CJNvSO1bLubwIvQ7Zd5g5U/ReKQx OqAg== X-Gm-Message-State: AOAM532HD+3pQZtIq8/ze6atatJxSKw4izNnuqxp7+YRNI80nLvqwjWJ Id4vymBjafrDfCZCkHQOsBVPCvOq3DCNBw== X-Google-Smtp-Source: ABdhPJwmsBhISZbu0nVZxEfBTeDWYTD4iqql23LY1CiDCmUOfcfU0O8RYn2iUSu71C7BW1OylAiUFpjYE9ItZA== 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:a25:d251:: with SMTP id j78mr5578186ybg.193.1606936145223; Wed, 02 Dec 2020 11:09:05 -0800 (PST) Date: Wed, 2 Dec 2020 11:08:23 -0800 In-Reply-To: <20201202190824.1309398-1-dlatypov@google.com> Message-Id: <20201202190824.1309398-3-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 3/4] kunit: tool: use `with open()` in unit test 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 The use of manual open() and .close() calls seems to be an attempt to keep the contents in scope. But Python doesn't restrict variables like that, so we can introduce new variables inside of a `with` and use them outside. Do so to make the code more Pythonic. Signed-off-by: Daniel Latypov Reviewed-by: David Gow Tested-by: Brendan Higgins Acked-by: Brendan Higgins --- tools/testing/kunit/kunit_tool_test.py | 33 +++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index 3a74e5612cf9..cf160914bc55 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -100,15 +100,14 @@ class KUnitParserTest(unittest.TestCase): def test_output_isolated_correctly(self): log_path = get_absolute_path( 'test_data/test_output_isolated_correctly.log') - file = open(log_path) - result = kunit_parser.isolate_kunit_output(file.readlines()) + with open(log_path) as file: + result = kunit_parser.isolate_kunit_output(file.readlines()) self.assertContains('TAP version 14', result) self.assertContains(' # Subtest: example', result) self.assertContains(' 1..2', result) self.assertContains(' ok 1 - example_simple_test', result) self.assertContains(' ok 2 - example_mock_test', result) self.assertContains('ok 1 - example', result) - file.close() def test_output_with_prefix_isolated_correctly(self): log_path = get_absolute_path( @@ -143,42 +142,39 @@ class KUnitParserTest(unittest.TestCase): def test_parse_successful_test_log(self): all_passed_log = get_absolute_path( 'test_data/test_is_test_passed-all_passed.log') - file = open(all_passed_log) - result = kunit_parser.parse_run_tests(file.readlines()) + with open(all_passed_log) as file: + result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( kunit_parser.TestStatus.SUCCESS, result.status) - file.close() def test_parse_failed_test_log(self): failed_log = get_absolute_path( 'test_data/test_is_test_passed-failure.log') - file = open(failed_log) - result = kunit_parser.parse_run_tests(file.readlines()) + with open(failed_log) as file: + result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( kunit_parser.TestStatus.FAILURE, result.status) - file.close() def test_no_tests(self): empty_log = get_absolute_path( 'test_data/test_is_test_passed-no_tests_run.log') - file = open(empty_log) - result = kunit_parser.parse_run_tests( - kunit_parser.isolate_kunit_output(file.readlines())) + with open(empty_log) as file: + result = kunit_parser.parse_run_tests( + kunit_parser.isolate_kunit_output(file.readlines())) self.assertEqual(0, len(result.suites)) self.assertEqual( kunit_parser.TestStatus.NO_TESTS, result.status) - file.close() def test_no_kunit_output(self): crash_log = get_absolute_path( 'test_data/test_insufficient_memory.log') - file = open(crash_log) print_mock = mock.patch('builtins.print').start() - result = kunit_parser.parse_run_tests( - kunit_parser.isolate_kunit_output(file.readlines())) + with open(crash_log) as file: + result = kunit_parser.parse_run_tests( + kunit_parser.isolate_kunit_output(file.readlines())) print_mock.assert_any_call(StrContains('no tests run!')) print_mock.stop() file.close() @@ -186,12 +182,11 @@ class KUnitParserTest(unittest.TestCase): def test_crashed_test(self): crashed_log = get_absolute_path( 'test_data/test_is_test_passed-crash.log') - file = open(crashed_log) - result = kunit_parser.parse_run_tests(file.readlines()) + with open(crashed_log) as file: + result = kunit_parser.parse_run_tests(file.readlines()) self.assertEqual( kunit_parser.TestStatus.TEST_CRASHED, result.status) - file.close() def test_ignores_prefix_printk_time(self): prefix_log = get_absolute_path( 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: 11946847 X-Patchwork-Delegate: shuah@kernel.org 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()