Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions tests/unittests/distros/test_hosts.py
Original file line number Diff line number Diff line change
@@ -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 = """
Expand All @@ -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") == []