Skip to content

Commit

Permalink
testlib_refactor: extract changes API requests into a separate class
Browse files Browse the repository at this point in the history
Jira ticket: CMK-20440

Change-Id: I01c97e0ee44fb6acb59b952ceb3e30045764cbf6
  • Loading branch information
asyash26 committed Dec 17, 2024
1 parent 88a1f32 commit 8ecb9bb
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 119 deletions.
8 changes: 4 additions & 4 deletions tests/composition/cmk/piggyback/piggyback_test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ def disable_piggyback_hub_globally(central_site: Site, remote_site_id: str) -> I
):
_write_replication_changes(central_site, central_site.id)
_write_replication_changes(central_site, remote_site_id)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()
yield
finally:
_write_replication_changes(central_site, central_site.id)
_write_replication_changes(central_site, remote_site_id)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()


@contextmanager
Expand All @@ -143,11 +143,11 @@ def disable_piggyback_hub_remote_site(central_site: Site, remote_site_id: str) -
# fake changes to trigger replication
with _write_sitespecific_config_file(central_site):
_write_replication_changes(central_site, remote_site_id)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()
yield
finally:
_write_replication_changes(central_site, remote_site_id)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()


@contextmanager
Expand Down
20 changes: 10 additions & 10 deletions tests/composition/cmk/piggyback/test_piggyback.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def _setup_piggyback_host(
"tag_piggyback": "piggyback",
},
)
source_site.openapi.activate_changes_and_wait_for_completion()
source_site.openapi.changes.activate_and_wait_for_completion()
yield
finally:
source_site.openapi.hosts.delete(hostname_piggyback)
source_site.openapi.activate_changes_and_wait_for_completion()
source_site.openapi.changes.activate_and_wait_for_completion()


@pytest.fixture(name="prepare_piggyback_environment", scope="module")
Expand All @@ -83,10 +83,10 @@ def _prepare_piggyback_environment(central_site: Site, remote_site: Site) -> Ite
[_HOSTNAME_PIGGYBACKED_B],
),
):
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()
yield
finally:
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()


def _schedule_check_and_discover(site: Site, hostname_source: str, hostname_piggyback: str) -> None:
Expand Down Expand Up @@ -146,16 +146,16 @@ def _create_and_rename_host(
},
)

source_site.openapi.activate_changes_and_wait_for_completion()
source_site.openapi.changes.activate_and_wait_for_completion()

source_site.openapi.hosts.rename_and_wait_for_completion(
hostname_old="other_host", hostname_new=hostname_piggyback, etag="*"
)
source_site.openapi.activate_changes_and_wait_for_completion()
source_site.openapi.changes.activate_and_wait_for_completion()
yield
finally:
source_site.openapi.hosts.delete(hostname_piggyback)
source_site.openapi.activate_changes_and_wait_for_completion()
source_site.openapi.changes.activate_and_wait_for_completion()


def test_piggyback_rename_host(
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_piggyback_hub_disabled_globally(
_schedule_check_and_discover(
central_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()

assert _piggybacked_service_gets_updated(
central_site, remote_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_piggyback_hub_disabled_remote_site(
_schedule_check_and_discover(
central_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()

assert _piggybacked_service_gets_updated(
central_site, remote_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
Expand All @@ -266,7 +266,7 @@ def _move_host(central_site: Site, to_remote_site: str, hostname_piggyback: str)
hostname_piggyback,
update_attributes={"site": to_remote_site},
)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()


def test_piggyback_services_move_host(
Expand Down
4 changes: 2 additions & 2 deletions tests/composition/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _connection(
}
)
central_site.openapi.sites.login(remote_site.id)
central_site.openapi.activate_changes_and_wait_for_completion(
central_site.openapi.changes.activate_and_wait_for_completion(
# this seems to be necessary to avoid sporadic CI failures
force_foreign_changes=True,
)
Expand All @@ -173,7 +173,7 @@ def _connection(
finally:
logger.warning("Hosts left: %s", central_site.openapi.hosts.get_all_names())
central_site.openapi.sites.delete(remote_site.id)
central_site.openapi.activate_changes_and_wait_for_completion(
central_site.openapi.changes.activate_and_wait_for_completion(
# this seems to be necessary to avoid sporadic CI failures
force_foreign_changes=True,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_proxy_register_import_workflow(
) -> None:
hostname = HostName("proxy-host")
central_site.openapi.hosts.create(hostname=hostname, attributes={"ipaddress": "127.0.0.1"})
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()

try:
proxy_registration_proc = run(
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_proxy_register_import_workflow(
wait_until_host_receives_data(central_site, hostname)

central_site.openapi.service_discovery.run_discovery_and_wait_for_completion(hostname)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()

wait_until_host_has_services(
central_site,
Expand All @@ -66,4 +66,4 @@ def test_proxy_register_import_workflow(
)
finally:
central_site.openapi.hosts.delete(hostname=hostname)
central_site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
central_site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def _test_register_workflow(
) -> None:
try:
site.openapi.hosts.create(hostname=hostname, attributes=dict(host_attributes))
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()

register_controller(ctl_path, site, hostname)

logger.info("Waiting for controller to open TCP socket or push data")
wait_until_host_receives_data(site, hostname)

site.openapi.service_discovery.run_discovery_and_wait_for_completion(hostname)
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()

wait_until_host_has_services(
site,
Expand All @@ -48,7 +48,7 @@ def _test_register_workflow(
)
finally:
site.openapi.hosts.delete(hostname=hostname)
site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)


@skip_if_not_containerized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _test_rename_preserves_registration(
"site": registration_site.id,
},
)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()
register_controller(
ctl_path,
registration_site,
Expand All @@ -53,7 +53,7 @@ def _test_rename_preserves_registration(
hostnames = set(central_site.openapi.hosts.get_all_names())
for hostname_ in hostnames.intersection({hostname, new_hostname}):
central_site.openapi.hosts.delete(hostname_)
central_site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
central_site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)


@skip_if_not_containerized
Expand Down
4 changes: 2 additions & 2 deletions tests/composition/controller_site_interactions/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def _get_status_output_json(
) -> Iterator[Mapping[str, Any]]:
try:
site.openapi.hosts.create(hostname=hostname, attributes=dict(host_attributes))
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()

register_controller(ctl_path, site, hostname)
yield controller_status_json(ctl_path)
finally:
site.openapi.hosts.delete(hostname=hostname)
site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)


@skip_if_not_containerized
Expand Down
4 changes: 2 additions & 2 deletions tests/composition/message_broker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ def p2p_connection(central_site: Site, remote_site: Site, remote_site_2: Site) -
central_site.openapi.broker_connections.create(
connection_id, connecter=remote_site.id, connectee=remote_site_2.id
)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()
yield
finally:
central_site.openapi.broker_connections.delete(connection_id)
central_site.openapi.activate_changes_and_wait_for_completion()
central_site.openapi.changes.activate_and_wait_for_completion()
8 changes: 4 additions & 4 deletions tests/composition/test_automatic_host_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_automatic_host_removal(
central_site: Site,
remote_site: Site,
) -> None:
assert not central_site.openapi.pending_changes()
assert not central_site.openapi.changes.get_pending()

unresolvable_host_central = "not-dns-resolvable-central"
unresolvable_host_remote = "not-dns-resolvable-remote"
Expand All @@ -49,7 +49,7 @@ def test_automatic_host_removal(
},
)

central_site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
central_site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)

try:

Expand All @@ -66,7 +66,7 @@ def _host_removal_done() -> bool:

logger.info("Waiting for changes to be activated")
wait_until(
lambda: not central_site.openapi.pending_changes(),
lambda: not central_site.openapi.changes.get_pending(),
timeout=180,
interval=20,
)
Expand All @@ -81,4 +81,4 @@ def _host_removal_done() -> bool:
central_site.openapi.hosts.delete(unresolvable_host_central)
if unresolvable_host_remote in central_site.openapi.hosts.get_all_names():
central_site.openapi.hosts.delete(unresolvable_host_remote)
central_site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
central_site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)
4 changes: 2 additions & 2 deletions tests/gui_e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _create_hosts_using_data_from_agent_dump(test_site: Site) -> Iterator:
test_site.openapi.service_discovery.run_bulk_discovery_and_wait_for_completion(
created_hosts_list
)
test_site.openapi.activate_changes_and_wait_for_completion()
test_site.openapi.changes.activate_and_wait_for_completion()

logger.info("Schedule the 'Check_MK' service")
for host_name in created_hosts_list:
Expand All @@ -258,7 +258,7 @@ def _create_hosts_using_data_from_agent_dump(test_site: Site) -> Iterator:
logger.info("Clean up: delete the host(s) and the rule")
test_site.openapi.hosts.bulk_delete(created_hosts_list)
test_site.openapi.rules.delete(rule_id)
test_site.openapi.activate_changes_and_wait_for_completion()
test_site.openapi.changes.activate_and_wait_for_completion()
test_site.delete_dir(test_site_dump_path)


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/checks/test_checks_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _host_services(
wait_until_host_receives_data(site, hostname)
wait_for_agent_cache_omd_status(site)
site.openapi.service_discovery.run_bulk_discovery_and_wait_for_completion([str(hostname)])
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()
if active_mode:
site.reschedule_services(hostname)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cmk/ec/test_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _write_ec_rule(site: Site, rule: list | None) -> None:
def _activate_ec_changes(site: Site) -> None:
replication_changes_path = site.path(f"var/check_mk/wato/replication_changes_{site.id}.mk")
site.write_text_file(str(replication_changes_path), str(_get_replication_change()))
site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)


def _generate_message_via_events_pipe(site: Site, message: str, end_of_line: bool = True) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/core/test_core_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def test_core_switch(site: Site, switch_core: Iterator[None]) -> None:
site.openapi.hosts.create(
"test_host_core_switch", attributes={"ipaddress": site.http_address, "site": site.id}
)
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()
except Exception as e:
logger.error(e)
raise

finally:
site.openapi.hosts.delete("test_host_core_switch")
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()
8 changes: 3 additions & 5 deletions tests/plugins_integration/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,7 @@ def wait_for_dcd_pend_changes(site: Site) -> None:
"""Wait up to 60 seconds for DCD to activate changes."""
max_count = 60
count = 0
while (
n_pending_changes := len(site.openapi.pending_changes([site.id]))
) > 0 and count < max_count:
while (n_pending_changes := len(site.openapi.changes.get_pending())) > 0 and count < max_count:
logger.info(
"Waiting for changes to be activated by the DCD connector. Count: %s/%s",
count,
Expand Down Expand Up @@ -668,12 +666,12 @@ def _dcd_connector(test_site_piggyback: Site) -> Iterator[None]:
delete_hosts=True,
no_deletion_time_after_init=60,
)
test_site_piggyback.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
test_site_piggyback.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)
try:
yield
finally:
if not config.skip_cleanup:
test_site_piggyback.openapi.dcd.delete(dcd_id)
test_site_piggyback.openapi.activate_changes_and_wait_for_completion(
test_site_piggyback.openapi.changes.activate_and_wait_for_completion(
force_foreign_changes=True
)
4 changes: 2 additions & 2 deletions tests/plugins_integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ def _create_periodic_service_discovery_rule(test_site_update: Site) -> Iterator[
ruleset_name="periodic_discovery",
value=_periodic_service_discovery_rule(),
)
test_site_update.openapi.activate_changes_and_wait_for_completion()
test_site_update.openapi.changes.activate_and_wait_for_completion()

yield

for rule in test_site_update.openapi.rules.get_all("periodic_discovery"):
if rule["id"] not in existing_rules_ids:
test_site_update.openapi.rules.delete(rule["id"])
test_site_update.openapi.activate_changes_and_wait_for_completion()
test_site_update.openapi.changes.activate_and_wait_for_completion()
2 changes: 1 addition & 1 deletion tests/testlib/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,4 @@ def clean_up_host(site: Site, hostname: HostName) -> Iterator[None]:
deleted = True

if deleted:
site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)
site.openapi.changes.activate_and_wait_for_completion(force_foreign_changes=True)
4 changes: 2 additions & 2 deletions tests/testlib/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def create_notification_user(site: Site, admin: bool = False) -> Iterator[tuple[
customer="global" if site.version.is_managed_edition() else None,
roles=["admin"] if admin else [],
)
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()
yield user_name, email_address
site.openapi.users.delete(user_name)
site.openapi.activate_changes_and_wait_for_completion()
site.openapi.changes.activate_and_wait_for_completion()
Loading

0 comments on commit 8ecb9bb

Please sign in to comment.