From a4547208d82b263026b4d7afbce9e120f55e6acc Mon Sep 17 00:00:00 2001 From: mhorky Date: Fri, 8 Mar 2024 15:44:45 +0100 Subject: [PATCH] feat(test): Add tests for registration and unregistration * Card ID: CCT-161 --- integration-tests/test_registration.py | 68 ++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 integration-tests/test_registration.py diff --git a/integration-tests/test_registration.py b/integration-tests/test_registration.py new file mode 100644 index 00000000..ec47be2b --- /dev/null +++ b/integration-tests/test_registration.py @@ -0,0 +1,68 @@ +import os +import subprocess + +import pytest + + +pytestmark = pytest.mark.usefixtures("register_subman") + +MACHINE_ID_FILE: str = "/etc/insights-client/machine-id" + + +def test_machineid_exists_only_when_registered(insights_client): + """`machine-id` is only present when insights-client is registered.""" + assert not insights_client.is_registered + assert not os.path.exists(MACHINE_ID_FILE) + + res = insights_client.run(check=False) + assert ( + "This host has not been registered. Use --register to register this host." + ) in res.stdout + assert res.returncode != 0 + assert not os.path.exists(MACHINE_ID_FILE) + + insights_client.register() + assert os.path.exists(MACHINE_ID_FILE) + + insights_client.unregister() + assert not os.path.exists(MACHINE_ID_FILE) + + +def test_machineid_changes_on_new_registration(insights_client): + """machine-id content changes when insights-client is un- & registered.""" + insights_client.register() + with open(MACHINE_ID_FILE, "r") as f: + machine_id_old = f.read() + + insights_client.unregister() + assert not os.path.exists(MACHINE_ID_FILE) + + insights_client.register() + with open(MACHINE_ID_FILE, "r") as f: + machine_id_new = f.read() + + assert machine_id_new != machine_id_old + + +def test_double_registration(insights_client): + """`--register` can be passed multiple times. + + Even system that is already registered should allow the `--register` flag to be + passed in, without resulting in non-zero exit code. + + This behavior has changed multiple times during the package lifetime. + """ + assert not insights_client.is_registered + + insights_client.register() + assert os.path.exists(MACHINE_ID_FILE) + with open(MACHINE_ID_FILE, "r") as f: + machine_id_old = f.read() + + res = insights_client.register() + assert "This host has already been registered" in res.stdout + assert os.path.exists(MACHINE_ID_FILE) + with open(MACHINE_ID_FILE, "r") as f: + machine_id_new = f.read() + + assert machine_id_new == machine_id_old