From 8df6ab14a98190c080ea48837f1ca41aabbfaa37 Mon Sep 17 00:00:00 2001 From: GautamThorani <107914462+GautamThorani@users.noreply.github.com> Date: Mon, 22 Sep 2025 21:39:46 +0500 Subject: [PATCH] test(distros): Convert test_hosts.py from unittest to pytest Refactored tests/unittests/distros/test_hosts.py to use pytest instead of unittest.TestCase as part of the pytest migration effort. - Removed TestCase inheritance - Converted self.assert* methods to bare assert statements - Maintained all original test functionality Related: #6427 --- tests/unittests/distros/test_hosts.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/tests/unittests/distros/test_hosts.py b/tests/unittests/distros/test_hosts.py index 2a705568a21..7fd5abf2bd9 100644 --- a/tests/unittests/distros/test_hosts.py +++ b/tests/unittests/distros/test_hosts.py @@ -1,7 +1,5 @@ # This file is part of cloud-init. See LICENSE file for license information. -import unittest - from cloudinit.distros.parsers import hosts BASE_ETC = """ @@ -15,30 +13,28 @@ BASE_ETC = BASE_ETC.strip() -class TestHostsHelper(unittest.TestCase): +class TestHostsHelper: def test_parse(self): eh = hosts.HostsConf(BASE_ETC) - self.assertEqual(eh.get_entry("127.0.0.1"), [["localhost"]]) - self.assertEqual( - eh.get_entry("192.168.1.10"), - [["foo.mydomain.org", "foo"], ["bar.mydomain.org", "bar"]], - ) + assert eh.get_entry("127.0.0.1") == [["localhost"]] + assert eh.get_entry("192.168.1.10") == [ + ["foo.mydomain.org", "foo"], + ["bar.mydomain.org", "bar"], + ] eh = str(eh) - self.assertTrue(eh.startswith("# Example")) + assert eh.startswith("# Example") def test_add(self): eh = hosts.HostsConf(BASE_ETC) eh.add_entry("127.0.0.0", "blah") - self.assertEqual(eh.get_entry("127.0.0.0"), [["blah"]]) + assert eh.get_entry("127.0.0.0") == [["blah"]] eh.add_entry("127.0.0.3", "blah", "blah2", "blah3") - self.assertEqual( - eh.get_entry("127.0.0.3"), [["blah", "blah2", "blah3"]] - ) + assert eh.get_entry("127.0.0.3") == [["blah", "blah2", "blah3"]] def test_del(self): eh = hosts.HostsConf(BASE_ETC) eh.add_entry("127.0.0.0", "blah") - self.assertEqual(eh.get_entry("127.0.0.0"), [["blah"]]) + assert eh.get_entry("127.0.0.0") == [["blah"]] eh.del_entries("127.0.0.0") - self.assertEqual(eh.get_entry("127.0.0.0"), []) + assert eh.get_entry("127.0.0.0") == []