-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Card ID: CCT-414
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |