From 600364bbcd80fffbe4a55b9a6240436038e5b71c Mon Sep 17 00:00:00 2001 From: Alba Hita <93577878+ahitacat@users.noreply.github.com> Date: Thu, 14 Jul 2022 09:45:46 +0200 Subject: [PATCH] Unregister option removes machine-id (#3449) Resolves: rhbz#1919570 Signed-off-by: ahitacat Co-authored-by: Glutexo --- insights/client/client.py | 4 ++ insights/tests/client/test_client.py | 64 ++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/insights/client/client.py b/insights/client/client.py index f81cee9f2d..7cef444750 100644 --- a/insights/client/client.py +++ b/insights/client/client.py @@ -209,6 +209,7 @@ def __cleanup_local_files(): write_unregistered_file() get_scheduler(config).remove_scheduling() delete_cache_files() + write_to_disk(constants.machine_id_file, delete=True) check = get_registration_status(config, pconn) @@ -230,6 +231,7 @@ def __cleanup_local_files(): if unreg: # only set if unreg was successful __cleanup_local_files() + logger.debug('Legacy unregistration') return unreg @@ -248,6 +250,8 @@ def handle_unregistration(config, pconn): # only set if unreg was successful or --force was set write_unregistered_file() delete_cache_files() + write_to_disk(constants.machine_id_file, delete=True) + logger.debug('Unregistered and removed machine-id') return unreg diff --git a/insights/tests/client/test_client.py b/insights/tests/client/test_client.py index 8d7b65b511..506920103c 100644 --- a/insights/tests/client/test_client.py +++ b/insights/tests/client/test_client.py @@ -1,3 +1,5 @@ +from contextlib import contextmanager +from shutil import rmtree import sys import os import pytest @@ -13,6 +15,10 @@ from pytest import mark from pytest import raises +# Temporary directory to mock registration files +TEMP_TEST_REG_DIR = "/tmp/insights-client-register" +TEMP_TEST_REG_DIR2 = "/tmp/redhat-access-insights-register" + @pytest.fixture(autouse=True) def mock_os_chmod(): @@ -84,25 +90,57 @@ def test_register(): assert os.path.isfile(u) is False -@pytest.mark.skip(reason="Mocked paths not working in QE jenkins") +@contextmanager +def _mock_registered_files(): + # mock a directory with the registered files + if not os.path.exists(TEMP_TEST_REG_DIR): + os.mkdir(TEMP_TEST_REG_DIR) + if not os.path.exists(TEMP_TEST_REG_DIR2): + os.mkdir(TEMP_TEST_REG_DIR2) + try: + registered_path = os.path.join(TEMP_TEST_REG_DIR, ".registered") + with open(registered_path, "w") as registered_file: + registered_file.write("date") + machine_id_path = os.path.join(TEMP_TEST_REG_DIR, "machine-id") + with open(machine_id_path, "w") as machine_id_file: + machine_id_file.write("id") + registered_path2 = os.path.join(TEMP_TEST_REG_DIR2, ".registered") + with open(registered_path2, "w") as registered_file: + registered_file.write("date") + + yield + finally: + rmtree(TEMP_TEST_REG_DIR) + rmtree(TEMP_TEST_REG_DIR2) + + +@patch('insights.client.utilities.write_unregistered_file', Mock()) +@patch('insights.client.utilities.delete_cache_files', Mock()) +@patch('insights.client.utilities.write_to_disk') +@patch('insights.client.client.write_to_disk') +@patch('insights.client.utilities.get_time', return_value='now') @patch('insights.client.utilities.constants.registered_files', - ['/tmp/insights-client.registered', - '/tmp/redhat-access-insights.registered']) + [TEMP_TEST_REG_DIR + '/.registered', + TEMP_TEST_REG_DIR2 + '/.registered']) @patch('insights.client.utilities.constants.unregistered_files', - ['/tmp/insights-client.unregistered', - '/tmp/redhat-access-insights.unregistered']) + [TEMP_TEST_REG_DIR + '/.unregistered', + TEMP_TEST_REG_DIR2 + '/.unregistered']) @patch('insights.client.utilities.constants.machine_id_file', - '/tmp/machine-id') -def test_unregister(): - config = InsightsConfig(unregister=True) + TEMP_TEST_REG_DIR + '/machine-id') +def test_new_unregister(date, write_to_disk, write): + config = InsightsConfig(unregister=True, legacy_upload=False) client = InsightsClient(config) client.connection = FakeConnection(registered=True) client.session = True - assert client.unregister() is True - for r in constants.registered_files: - assert os.path.isfile(r) is False - for u in constants.unregistered_files: - assert os.path.isfile(u) is True + with _mock_registered_files(): + assert client.unregister() is True + write_to_disk.assert_called_once_with(constants.machine_id_file, delete=True) + write.assert_has_calls(( + call(constants.registered_files[0], delete=True), + call(constants.registered_files[1], delete=True), + call(constants.unregistered_files[0], content=date.return_value), + call(constants.unregistered_files[1], content=date.return_value) + )) @pytest.mark.skip(reason="Mocked paths not working in QE jenkins")