Skip to content

Commit

Permalink
feat(test): Add unit tests for MOTD
Browse files Browse the repository at this point in the history
* Card ID: CCT-414
  • Loading branch information
m-horky committed Jun 10, 2024
1 parent 0261c75 commit 0dbf17c
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/insights_client/tests/test_motd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import os
import pathlib
import tempfile
import typing
import unittest
import unittest.mock

import pytest

import insights_client


class MockFS:
"""Mock filesystem.
Ensures that the files required for the MOTD manipulation are created
in a temporary directory instead of working with actual system files.
"""

_dir: tempfile.TemporaryDirectory

def __init__(self):
self._dir = tempfile.TemporaryDirectory(prefix="test-chroot-")

paths: typing.Dict[str, str] = {
"insights_client.MOTD_SRC": "/etc/insights-client/insights-client.motd",
"insights_client.MOTD_FILE": "/etc/motd.d/insights-client",
"insights_client.REGISTERED_FILE": "/etc/insights-client/.registered",
"insights_client.UNREGISTERED_FILE": "/etc/insights-client/.unregistered",
}

self.patches: typing.List[unittest.mock.patch] = []
for target, path in paths.items():
mocked_path = f"{self.chroot}{path}"
pathlib.Path(mocked_path).parent.mkdir(parents=True, exist_ok=True)
patch = unittest.mock.patch(target, mocked_path)
self.patches.append(patch)
patch.start()

with (self.chroot / "etc/insights-client/insights-client.motd").open("w") as f:
f.write("This is the content of the MOTD file.")

@property
def chroot(self) -> pathlib.Path:
return pathlib.Path(self._dir.name)

def __del__(self):
for patch in self.patches:
patch.stop()
self._dir.cleanup()


@pytest.fixture(scope="function")
def mock_fs():
fs = MockFS()
yield fs
del fs


def test_present(mock_fs):
assert not (mock_fs.chroot / "etc/motd.d/insights-client").exists()

# The file gets created/symlinked when .registered & .unregistered do not exist
insights_client.update_motd_message()
assert (mock_fs.chroot / "etc/insights-client/insights-client.motd").samefile(
mock_fs.chroot / "etc/motd.d/insights-client"
)
assert (mock_fs.chroot / "etc/motd.d/insights-client").exists()


def test_absent_on_dot_registered(mock_fs):
# The MOTD file exists by default...
insights_client.update_motd_message()
assert (mock_fs.chroot / "etc/motd.d/insights-client")
# ...and gets removed when .registered exists.
(mock_fs.chroot / "etc/insights-client/.registered").open("w").close()

insights_client.update_motd_message()
assert not (mock_fs.chroot / "etc/motd.d/insights-client").exists()

# It stays absent when run multiple times.
insights_client.update_motd_message()
assert not (mock_fs.chroot / "etc/motd.d/insights-client").exists()


def test_absent_on_dot_unregistered(mock_fs):
# The MOTD file exists by default...
insights_client.update_motd_message()
assert (mock_fs.chroot / "etc/motd.d/insights-client").exists()
# ...and gets removed when .unregistered exists.
(mock_fs.chroot / "etc/insights-client/.unregistered").open("w").close()

insights_client.update_motd_message()
assert not (mock_fs.chroot / "etc/motd.d/insights-client").exists()

# It stays absent when run multiple times.
insights_client.update_motd_message()
assert not (mock_fs.chroot / "etc/motd.d/insights-client").exists()


def test_ignored_on_dev_null(mock_fs):
# When the /etc/motd.d/insights-client is a symbolic link to /dev/null...
(mock_fs.chroot / "etc/motd.d/insights-client").symlink_to(pathlib.Path(os.devnull))

# ...it should not be overwritten...
insights_client.update_motd_message()
assert (mock_fs.chroot / "etc/motd.d/insights-client").samefile(pathlib.Path(os.devnull))

# ...whether the MOTD file should be present or not.
(mock_fs.chroot / "etc/insights-client/.registered").open("w").close()
insights_client.update_motd_message()
assert (mock_fs.chroot / "etc/motd.d/insights-client").samefile(pathlib.Path(os.devnull))

0 comments on commit 0dbf17c

Please sign in to comment.