Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Unregister option removes machine-id #3449

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions insights/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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


Expand All @@ -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


Expand Down
64 changes: 51 additions & 13 deletions insights/tests/client/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import contextmanager
from shutil import rmtree
import sys
import os
import pytest
Expand All @@ -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():
Expand Down Expand Up @@ -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")
Expand Down