def test_start_job(request_context): job = DummyBackgroundJob() job.set_function(job.execute_hello) status = job.get_status() assert status["state"] == background_job.JobStatusStates.INITIALIZED job.start() testlib.wait_until(job.is_active, timeout=5, interval=0.1) with pytest.raises(background_job.BackgroundJobAlreadyRunning): job.start() assert job.is_active() job.finish_hello_event.set() testlib.wait_until( lambda: job.get_status()["state"] not in [ background_job.JobStatusStates.INITIALIZED, background_job. JobStatusStates.RUNNING ], timeout=5, interval=0.1, ) status = job.get_status() assert status["state"] == background_job.JobStatusStates.FINISHED output = "\n".join(status["loginfo"]["JobProgressUpdate"]) assert "Initialized background job" in output assert "Hallo :-)" in output
def test_load_dashboard_plugin(request, site: Site): plugin_path = "local/lib/check_mk/gui/plugins/dashboard/test_plugin.py" def cleanup(): site.delete_file(plugin_path) request.addfinalizer(cleanup) assert not site.file_exists("tmp/dashboard_test") site.write_text_file( plugin_path, """ with open("%s", "w") as f: f.write("ding") """ % site.path("tmp/dashboard_test"), ) # Reload site apache to trigger the reload of our plugin site.omd("reload", "apache") def file_created(): return site.file_exists("tmp/dashboard_test") # We need to wait some time for apache to initialize our application wait_until(file_created, timeout=60, interval=1)
def _wait_for_waiting_lock(): """Use /proc/locks to wait until one lock is in waiting state https://man7.org/linux/man-pages/man5/proc.5.html """ def has_waiting_lock(): pid = os.getpid() with Path("/proc/locks").open() as f: for line in f: p = line.strip().split() if p[1] == "->" and p[2] == "FLOCK" and p[4] == "WRITE" and p[ 5] == str(pid): return True return False wait_until(has_waiting_lock, timeout=1, interval=0.01)
def _start(request, client, version=None, is_update=False, **kwargs): if version is None: version = build_version() try: if version.version == build_version().version: _image, _build_logs = _build(request, client, version) else: # In case the given version is not the current branch version, don't # try to build it. Download it instead! _image = _pull(client, version) except requests.exceptions.ConnectionError as e: raise Exception( "Failed to access docker socket (Permission denied). You need to be member of the " 'docker group to get access to the socket (e.g. use "make -C docker setup") to ' "fix this, then restart your computer and try again.") from e c = client.containers.run(image=_image.id, detach=True, **kwargs) logger.info("Starting container %s from image %s", c.short_id, _image.short_id) try: site_id = kwargs.get("environment", {}).get("CMK_SITE_ID", "cmk") request.addfinalizer(lambda: c.remove(force=True)) testlib.wait_until( lambda: "### CONTAINER STARTED" in c.logs().decode("utf-8"), timeout=120) output = c.logs().decode("utf-8") if not is_update: assert "Created new site" in output assert "cmkadmin with password:"******"Created new site" not in output assert "cmkadmin with password:"******"STARTING SITE" in output exit_code, status_output = _exec_run(c, ["omd", "status"], user=site_id) assert exit_code == 0, f"Status is {exit_code}. Output: {status_output}" finally: logger.debug("Log so far: %s", c.logs().decode("utf-8")) return c
def test_stop_job(request_context): job = DummyBackgroundJob() job.set_function(job.execute_endless) job.start() testlib.wait_until( lambda: "Hanging loop" in job.get_status()["loginfo"]["JobProgressUpdate"], timeout=5, interval=0.1, ) status = job.get_status() assert status["state"] == background_job.JobStatusStates.RUNNING job.stop() status = job.get_status() assert status["state"] == background_job.JobStatusStates.STOPPED output = "\n".join(status["loginfo"]["JobProgressUpdate"]) assert "Job was stopped" in output
def initial_state_fixture(site: Site, scenario): # Before each test: Set to initial state: Both UP site.send_host_check_result("notify-test-child", 0, "UP") site.send_host_check_result("notify-test-parent", 0, "UP") # Before each test: Clear logs if scenario.core == "cmc": # The command is processed asynchronously -> Wait for completion inode_before = os.stat(site.path("var/check_mk/core/history")).st_ino site.live.command("[%d] ROTATE_LOGFILE" % time.time()) def rotated_log(): try: return inode_before != os.stat(site.path("var/check_mk/core/history")).st_ino except OSError as e: if e.errno == errno.ENOENT: return False raise e wait_until(rotated_log, timeout=10) else: site.delete_file("var/nagios/nagios.log")
def snmpsim_fixture(site: Site, snmp_data_dir, tmp_path_factory): tmp_path = tmp_path_factory.getbasetemp() log.logger.setLevel(logging.DEBUG) debug.enable() process_definitions = [ _define_process(idx, auth, tmp_path, snmp_data_dir) for idx, auth in enumerate(_create_auth_list()) ] for process_def in process_definitions: wait_until(_create_listening_condition(process_def), timeout=20) yield log.logger.setLevel(logging.INFO) debug.disable() logger.debug("Stopping snmpsimd...") for process_def in process_definitions: process_def.process.terminate() process_def.process.wait() logger.debug("Stopped snmpsimd.")