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

Fix registration tests [ESSNTL-3070] #3519

Merged
merged 2 commits into from
Oct 3, 2022
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
258 changes: 128 additions & 130 deletions insights/tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
import sys
import os
import pytest
import time

from insights.client import InsightsClient
from insights.client.archive import InsightsArchive
from insights.client.config import InsightsConfig
from insights import package_info
from insights.client.constants import InsightsConstants as constants
from insights.client.utilities import generate_machine_id
from mock.mock import patch, Mock, call
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"
TEMP_TEST_REG_DIR = "/tmp/insights-client-registration"
TEMP_TEST_REG_DIR2 = "/tmp/redhat-access-insights-registration"


@pytest.fixture(autouse=True)
Expand All @@ -32,7 +30,7 @@ def mock_os_umask():
yield os_umask


class FakeConnection(object):
class _mock_InsightsConnection(object):
'''
For stubbing out network calls
'''
Expand All @@ -41,7 +39,7 @@ def __init__(self, registered=None):

def api_registration_check(self):
# True = registered
# None or string = unregistered
# None = unregistered
# False = unreachable
return self.registered

Expand Down Expand Up @@ -69,25 +67,51 @@ def test_version():
sys.argv = tmp


@pytest.mark.skip(reason="Mocked paths not working in QE jenkins")
@contextmanager
def _mock_no_register_files():
# mock a directory with the fresh install 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:
unregistered_path = os.path.join(TEMP_TEST_REG_DIR, ".unregistered")
with open(unregistered_path, "w") as unregistered_file:
unregistered_file.write("date")
unregistered_path2 = os.path.join(TEMP_TEST_REG_DIR2, ".unregistered")
with open(unregistered_path2, "w") as unregistered_file:
unregistered_file.write("date")
yield
finally:
rmtree(TEMP_TEST_REG_DIR)
rmtree(TEMP_TEST_REG_DIR2)


@patch('insights.client.client.generate_machine_id')
@patch('insights.client.utilities.delete_unregistered_file')
@patch('insights.client.utilities.write_to_disk')
@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_register():
config = InsightsConfig(register=True)
TEMP_TEST_REG_DIR + '/machine-id')
def test_register_legacy(utilities_write, delete_unregistered_file, generate_machine_id):
config = InsightsConfig(register=True, legacy_upload=True)
client = InsightsClient(config)
client.connection = FakeConnection()
client.connection = _mock_InsightsConnection(registered=None)
client.connection.config = config
client.session = True
assert client.register() is True
for r in constants.registered_files:
assert os.path.isfile(r) is True
for u in constants.unregistered_files:
assert os.path.isfile(u) is False
with _mock_no_register_files():
client.register() is True
delete_unregistered_file.assert_called_once()
generate_machine_id.assert_called_once_with(new=False)
utilities_write.assert_has_calls((
call(constants.registered_files[0]),
call(constants.registered_files[1])
))


@contextmanager
Expand All @@ -114,8 +138,8 @@ def _mock_registered_files():
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_unregistered_file')
@patch('insights.client.utilities.delete_cache_files')
@patch('insights.client.utilities.write_to_disk')
@patch('insights.client.client.write_to_disk')
@patch('insights.client.utilities.get_time', return_value='now')
Expand All @@ -127,67 +151,51 @@ def _mock_registered_files():
TEMP_TEST_REG_DIR2 + '/.unregistered'])
@patch('insights.client.utilities.constants.machine_id_file',
TEMP_TEST_REG_DIR + '/machine-id')
def test_new_unregister(date, write_to_disk, write):
config = InsightsConfig(unregister=True, legacy_upload=False)
def test_unregister_legacy(date, client_write, utilities_write, _delete_cache_file, _write_unregistered_file):
config = InsightsConfig(unregister=True, legacy_upload=True)
client = InsightsClient(config)
client.connection = FakeConnection(registered=True)
client.connection = _mock_InsightsConnection(registered=True)
client.connection.config = config
client.session = 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((
client_write.assert_called_once_with(constants.machine_id_file, delete=True)
utilities_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")
@patch('insights.client.utilities.delete_unregistered_file')
@patch('insights.client.utilities.write_to_disk')
@patch('insights.client.client.generate_machine_id')
@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_force_reregister():
config = InsightsConfig(reregister=True)
TEMP_TEST_REG_DIR + '/machine-id')
def test_force_reregister_legacy(generate_machine_id, utilities_write, delete_unregistered_file):
config = InsightsConfig(reregister=True, legacy_upload=True)
client = InsightsClient(config)
client.connection = FakeConnection(registered=None)
client.connection = _mock_InsightsConnection(registered=None)
client.connection.config = config
client.session = True

# initialize comparisons
old_machine_id = None
new_machine_id = None

# register first
assert client.register() is True
for r in constants.registered_files:
assert os.path.isfile(r) is True

# get modified time of .registered to ensure it's regenerated
old_reg_file1_ts = os.path.getmtime(constants.registered_files[0])
old_reg_file2_ts = os.path.getmtime(constants.registered_files[1])

old_machine_id = generate_machine_id()

# wait to allow for timestamp difference
time.sleep(3)

# reregister with new machine-id
client.connection = FakeConnection(registered=True)
config.reregister = True
assert client.register() is True

new_machine_id = generate_machine_id()
new_reg_file1_ts = os.path.getmtime(constants.registered_files[0])
new_reg_file2_ts = os.path.getmtime(constants.registered_files[1])

assert old_machine_id != new_machine_id
assert old_reg_file1_ts != new_reg_file1_ts
assert old_reg_file2_ts != new_reg_file2_ts
with _mock_registered_files():
assert client.register() is True
delete_unregistered_file.assert_called_once()
generate_machine_id.assert_called_once_with(new=True)
utilities_write.assert_has_calls((
call(constants.registered_files[0], delete=True),
call(constants.registered_files[1], delete=True),
call(constants.unregistered_files[0], delete=True),
call(constants.unregistered_files[1], delete=True)
))


def test_register_container():
Expand All @@ -205,110 +213,100 @@ def test_force_reregister_container():
InsightsConfig(reregister=True, analyze_container=True)


@pytest.mark.skip(reason="Mocked paths not working in QE jenkins")
@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')
TEMP_TEST_REG_DIR + '/machine-id')
def test_reg_check_registered():
# register the machine first

config = InsightsConfig()
client = InsightsClient(config)
client.connection = FakeConnection(registered=True)
client.connection = _mock_InsightsConnection(registered=True)
client.connection.config = config
client.session = True

# test function and integration in .register()
assert client.get_registration_status()['status'] is True
assert client.register() is True
for r in constants.registered_files:
assert os.path.isfile(r) is True
for u in constants.unregistered_files:
assert os.path.isfile(u) is False
with _mock_registered_files():
assert client.get_registration_status()['status'] is True
for r in constants.registered_files:
assert os.path.isfile(r) is True
for u in constants.unregistered_files:
assert os.path.isfile(u) is False


@pytest.mark.skip(reason="Mocked paths not working in QE jenkins")
@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')
TEMP_TEST_REG_DIR + '/machine-id')
def test_reg_check_unregistered():
# unregister the machine first
config = InsightsConfig()
client = InsightsClient(config)
client.connection = FakeConnection(registered='unregistered')
client.connection = _mock_InsightsConnection(registered='unregistered')
client.connection.config = config
client.session = True

# test function and integration in .register()
assert client.get_registration_status()['status'] is False
assert client.register() is False
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_no_register_files():
assert client.get_registration_status()['status'] is False
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


@pytest.mark.skip(reason="Mocked paths not working in QE jenkins")
@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')
TEMP_TEST_REG_DIR + '/machine-id')
def test_reg_check_registered_unreachable():
# register the machine first
config = InsightsConfig(register=True)
client = InsightsClient(config)
client.connection = FakeConnection(registered=None)
client.connection = _mock_InsightsConnection(registered=False)
client.connection.config = config
client.session = True
assert client.register() is True

# reset config and try to check registration
config.register = False
client.connection = FakeConnection(registered=False)
assert client.get_registration_status()['unreachable'] is True
assert client.register() is None
for r in constants.registered_files:
assert os.path.isfile(r) is True
for u in constants.unregistered_files:
assert os.path.isfile(u) is False
with _mock_registered_files():
assert client.get_registration_status()['unreachable'] is True
for r in constants.registered_files:
assert os.path.isfile(r) is True
for u in constants.unregistered_files:
assert os.path.isfile(u) is False


@pytest.mark.skip(reason="Mocked paths not working in QE jenkins")
@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')
TEMP_TEST_REG_DIR + '/machine-id')
def test_reg_check_unregistered_unreachable():
# unregister the machine first
config = InsightsConfig(unregister=True)
client = InsightsClient(config)
client.connection = FakeConnection(registered=True)
client.connection = _mock_InsightsConnection(registered=False)
client.connection.config = config
client.session = True
assert client.unregister() is True

# reset config and try to check registration
config.unregister = False
client.connection = FakeConnection(registered=False)
assert client.get_registration_status()['unreachable'] is True
assert client.register() is None
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_no_register_files():
assert client.get_registration_status()['unreachable'] 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


@patch('insights.client.client.constants.sleep_time', 0)
Expand Down