Example #1
0
def _test_standalone_smoke(agent_installation_type, python_version=None):
    """
    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)

    if python_version:
        runner.switch_version(python_version)

    agent_log_verifier = AgentLogVerifier(
        runner, compat.os_environ_unicode["SCALYR_SERVER"])
    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"])

    # NOTE: It's important that the file exists before starting the agent otherwise it won't be
    # consumed by the agent and the tests will fail.
    agent_logs_dir_path = six.text_type(runner.agent_logs_dir_path)
    data_log_path = six.text_type(data_json_verifier._data_json_log_path)

    if os.path.exists(data_log_path):
        os.utime(data_log_path, None)
    else:
        try:
            os.makedirs(agent_logs_dir_path)
        except OSError:
            pass

        with open(data_log_path, "a") as _:
            pass

    runner.start()

    time.sleep(1)

    print("Verify 'agent.log'")
    assert agent_log_verifier.verify(
    ), "Verification of the file: 'agent.log' 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"

    runner.stop()
Example #2
0
def common_test_switch_default_to_python2(install_package_fn,
                                          install_next_version_fn):
    """
    Test package installation on machine with python and python2.
    Package installer should pick 'python' by default and then we switch it to python2 and get back to default.
    :param install_package_fn: callable that installs package with appropriate type to the current machine OS.
    """

    # let python2 be default.
    _link_to_default_python("python2")
    _remove_python("python3")

    stdout, _ = install_package_fn()

    # make sure that installer has found 'python' mapped on to python2.
    # this is signaled by the install script not trying to switch the interpreter
    assert "Switching the Python interpreter used by the Scalyr Agent" not in stdout

    assert _get_current_config_script_name() == "config_main.py"

    runner = AgentRunner(PACKAGE_INSTALL)
    runner.start()
    time.sleep(1)

    assert _get_python_major_version(runner) == 2

    # switching to python2
    runner.stop()
    runner.switch_version("python2")
    assert _get_current_config_script_name() == "config_main_py2.py"
    runner.start()
    time.sleep(1)
    assert _get_python_major_version(runner) == 2
    runner.stop()

    # install next version of the package and check that links are the same.
    stdout, _ = install_next_version_fn()
    # Installer must not switch to default python.
    assert "Switching the Python interpreter used by the Scalyr Agent" not in stdout
    # the source file should be "config_main_py3.py"
    assert _get_current_config_script_name() == "config_main_py2.py"

    # switching back to default python
    runner.switch_version("default")
    assert _get_current_config_script_name() == "config_main.py"
    runner.start()
    time.sleep(1)
    assert _get_python_major_version(runner) == 2
Example #3
0
def _test_standalone_smoke(agent_installation_type, python_version=None):
    """
    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)

    if python_version:
        runner.switch_version(python_version)

    agent_log_verifier = AgentLogVerifier(
        runner, compat.os_environ_unicode["SCALYR_SERVER"])
    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"
    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"

    runner.stop()
Example #4
0
def common_test_switch_python2_to_python3(install_package_fn,
                                          install_next_version_fn):
    """
    Test package installation on machine with both python2 and python3 but without default 'python'.
    Package installer should pick python2 by default and then we switch to the python3.
    :param install_package_fn: callable that installs package with appropriate type to the current machine OS.
    """
    _remove_python("python")

    install_package_fn()
    assert _get_current_config_script_name() == "config_main_py2.py"

    runner = AgentRunner(PACKAGE_INSTALL)
    runner.start()
    time.sleep(1)

    assert _get_python_major_version(runner) == 2

    # switching to python3
    runner.stop()
    runner.switch_version("python3")
    assert _get_current_config_script_name() == "config_main_py3.py"
    runner.start()
    time.sleep(1)
    assert _get_python_major_version(runner) == 3
    runner.stop()

    # install next version of the package and check that links are the same.
    stdout, _ = install_next_version_fn()
    # Installer must not switch to default python.
    assert "Switching the Python interpreter used by the Scalyr Agent" not in stdout
    # the source file should be "config_main_py3.py"
    assert _get_current_config_script_name() == "config_main_py3.py"

    # switching bach to python2
    runner.switch_version("python2")
    assert _get_current_config_script_name() == "config_main_py2.py"
    runner.start()
    time.sleep(1)
    assert _get_python_major_version(runner) == 2
Example #5
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()
Example #6
0
def common_test_switch_command_works_without_agent_config(install_package_fn):
    """
    Verify that the Python version switch command works if the config file is not present or
    doesn't contain a valid API key.

    This is important because this command may run as part of package postinstall script when the
    key won't be configured yet.

    Basically it asserts that the switch command doesn't rely agent config being present and
    correctly configured.
    """
    install_package_fn()

    runner = AgentRunner(PACKAGE_INSTALL)

    # Make sure the config is not present
    agent_config_path = "/etc/scalyr-agent-2/agent.json"
    agent_config_d_path = "/etc/scalyr-agent-2/agent.d"

    if os.path.isfile(agent_config_path):
        os.unlink(agent_config_path)

    if os.path.isdir(agent_config_d_path):
        shutil.rmtree(agent_config_d_path)

    # Make sure no SCALYR_ environment variables are set
    env = compat.os_environ_unicode.copy()

    for key in list(env.keys()):
        if key.lower().startswith("scalyr"):
            del env[key]

    binary_path = os.path.join("/", "usr", "share", "scalyr-agent-2", "bin")

    scalyr_agent_2_target = os.path.join(binary_path, "scalyr-agent-2")
    scalyr_agent_2_config_target = os.path.join(binary_path,
                                                "scalyr-agent-2-config")

    # Default should be python binary
    shebang_line_main = get_shebang_from_file(scalyr_agent_2_target)
    shebang_line_config = get_shebang_from_file(scalyr_agent_2_config_target)

    # On some newer distros python binary is not available
    if shutil.which("python"):
        is_python_binary_available = True
        expected = "#!/usr/bin/env python"
    else:
        is_python_binary_available = False
        expected = "#!/usr/bin/env python2"

    assert shebang_line_main == expected, "expected %s, got %s" % (
        expected,
        shebang_line_main,
    )
    assert shebang_line_config == expected, "expected %s, got %s" % (
        expected,
        shebang_line_config,
    )

    # Switch to python3
    runner.switch_version("python3", env=env)

    shebang_line_main = get_shebang_from_file(scalyr_agent_2_target)
    shebang_line_config = get_shebang_from_file(scalyr_agent_2_config_target)
    assert shebang_line_main == "#!/usr/bin/env python3"
    assert shebang_line_config == "#!/usr/bin/env python3"

    # Switch back to python2
    runner.switch_version("python2", env=env)

    shebang_line_main = get_shebang_from_file(scalyr_agent_2_target)
    shebang_line_config = get_shebang_from_file(scalyr_agent_2_config_target)
    assert shebang_line_main == "#!/usr/bin/env python2"
    assert shebang_line_config == "#!/usr/bin/env python2"

    # Switch back to python (aka default)
    if is_python_binary_available:
        runner.switch_version("python", env=env)

        shebang_line_main = get_shebang_from_file(scalyr_agent_2_target)
        shebang_line_config = get_shebang_from_file(
            scalyr_agent_2_config_target)
        assert shebang_line_main == "#!/usr/bin/env python"
        assert shebang_line_config == "#!/usr/bin/env python"

    # Write a config with invalid config, this way we ensure config is indeed not parsed by that
    # command even if it's present
    mock_config = {
        "api_key": "",
        "scalyr_server": "agent.scalyr.com",
    }

    with open(agent_config_path, "w") as fp:
        fp.write(json.dumps(mock_config))

    # Switch to python3
    runner.switch_version("python3", env=env)

    shebang_line_main = get_shebang_from_file(scalyr_agent_2_target)
    shebang_line_config = get_shebang_from_file(scalyr_agent_2_config_target)
    assert shebang_line_main == "#!/usr/bin/env python3"
    assert shebang_line_config == "#!/usr/bin/env python3"