def test_standalone_agent_config_reload():
    runner = AgentRunner(
        DEV_INSTALL,
        enable_debug_log=True,
        workers_type="process",
        workers_session_count=2,
    )

    runner.start()
    logging.info("Agent started.")
    time.sleep(1)

    process, worker_pids, children, workers_processes = _perform_workers_check(runner)

    _check_workers_gracefull_stop(runner, worker_pids, occurrences=0)

    logging.info(
        "Increase the worker count in the config and wait for agent config reload."
    )

    old_worker_pids = worker_pids.copy()
    config = runner.config
    config_worker_count = config["default_sessions_per_worker"]
    config["default_sessions_per_worker"] = config_worker_count + 1
    runner.write_config(config)

    logging.info("checking in the loop until all old worker processes are gone")
    while True:
        assert process.is_running(), "The agent process must not be terminated."

        children_ids = [p.pid for p in process.children()]

        still_running_old_ids = []
        for old_id in old_worker_pids:
            if old_id in children_ids:
                still_running_old_ids.append(old_id)

        if not still_running_old_ids:
            logging.info("All old worker processes are terminated.")
            break

        logging.info(
            "Wait for old worker processes are terminated. Now alive: {0}".format(
                still_running_old_ids
            )
        )
        time.sleep(1)

    time.sleep(1)

    logging.info("checking in loop until the  workers number is increased")
    while True:
        try:
            process, worker_pids, children, workers_processes = _perform_workers_check(
                runner
            )

            assert process.is_running()
            assert len(workers_processes) == config_worker_count + 1

        except:

            time.sleep(1)
        else:
            break

    _check_workers_gracefull_stop(runner, old_worker_pids, occurrences=1)

    _stop_and_perform_checks(runner)

    _check_workers_gracefull_stop(runner, old_worker_pids, occurrences=2)

    new_workers = {}
    for worker_id, worker_pid in worker_pids.items():
        if worker_id not in old_worker_pids:
            new_workers[worker_id] = worker_pid

    _check_workers_gracefull_stop(runner, new_workers, occurrences=1)
Exemple #2
0
def _test_standalone_smoke(
    agent_installation_type,
    python_version=None,
    rate_limited=False,
    workers_type="thread",
    workers_sessions_count=1,
):
    """
    Agent standalone test to run within the same machine.
    """
    if agent_installation_type == DEV_INSTALL:
        os.chdir(get_package_root())

    print("Agent host name: {0}".format(compat.os_environ_unicode["AGENT_HOST_NAME"]))

    runner = AgentRunner(
        agent_installation_type,
        enable_debug_log=True,
        workers_type=workers_type,
        workers_session_count=workers_sessions_count,
    )

    if python_version:
        runner.switch_version(python_version)

    agent_log_verifier = AgentLogVerifier(
        runner, compat.os_environ_unicode["SCALYR_SERVER"]
    )
    worker_sessions_verifier = None

    if workers_type == "process":
        worker_sessions_verifier = AgentWorkerSessionLogVerifier(
            runner, compat.os_environ_unicode["SCALYR_SERVER"]
        )

    if rate_limited:
        data_json_verifier = DataJsonVerifierRateLimited(
            runner, compat.os_environ_unicode["SCALYR_SERVER"]
        )
    else:
        data_json_verifier = DataJsonVerifier(
            runner, compat.os_environ_unicode["SCALYR_SERVER"]
        )
    system_metrics_verifier = SystemMetricsVerifier(
        runner, compat.os_environ_unicode["SCALYR_SERVER"]
    )
    process_metrics_verifier = ProcessMetricsVerifier(
        runner, compat.os_environ_unicode["SCALYR_SERVER"]
    )

    _create_data_json_file(runner=runner, data_json_verifier=data_json_verifier)

    runner.start()

    time.sleep(1)

    print("Verify 'agent.log'")
    assert agent_log_verifier.verify(), "Verification of the file: 'agent.log' failed"

    if workers_type == "process":
        # if workers are multiprocess, then we check if worker session logs are ingested.
        print("Verify worker sessions log files.")
        assert (
            worker_sessions_verifier.verify()
        ), "Verification of the worker session log files is failed"

    print("Verify 'linux_system_metrics.log'")
    assert (
        system_metrics_verifier.verify()
    ), "Verification of the file: 'linux_system_metrics.log' failed"
    print("Verify 'linux_process_metrics.log'")
    assert (
        process_metrics_verifier.verify()
    ), "Verification of the file: 'linux_process_metrics.log' failed"

    assert data_json_verifier.verify(), "Verification of the file: 'data.json' failed"

    if workers_type == "process":
        # increase worker sessions count, restart and also check if all worker logs are ingested.
        config = runner.config
        sessions_count = config["default_sessions_per_worker"]
        sessions_count += 1

        worker_sessions_verifier = AgentWorkerSessionLogVerifier(
            runner, compat.os_environ_unicode["SCALYR_SERVER"]
        )

        config["default_sessions_per_worker"] = sessions_count
        runner.write_config(config)
        runner.restart()

        agent_status = json.loads(runner.status_json())
        assert (
            len(agent_status["copying_manager_status"]["workers"][-1]["sessions"])
            == sessions_count
        )

        print("Verify worker sessions log files.")

        assert (
            worker_sessions_verifier.verify()
        ), "Verification of the worker session log files is failed"

    runner.stop()