def load_monitor(monitor_module, additional_python_paths): """Loads the module for the specified monitor. @param monitor_module: The module for the monitor. @param additional_python_paths: A list of paths (separate by os.pathsep) to add to the PYTHONPATH when instantiating the module in case it needs to packages in other directories. @type monitor_module: str @type additional_python_paths: str @return: The class for the ScalyrMonitor in the module. @rtype: class """ # Augment the PYTHONPATH if requested to locate the module. paths_to_pass = [] # Also add in scalyr_agent/../monitors/local and scalyr_agent/../monitors/contrib to the Python path to search # for monitors. (They are always in the parent directory of the scalyr_agent package. path_to_package_parent = os.path.dirname(get_package_root()) paths_to_pass.append( os.path.join(path_to_package_parent, "monitors", "local")) paths_to_pass.append( os.path.join(path_to_package_parent, "monitors", "contrib")) # Add in the additional paths. if additional_python_paths is not None and len( additional_python_paths) > 0: for x in additional_python_paths.split(os.pathsep): paths_to_pass.append(x) return load_monitor_class(monitor_module, os.pathsep.join(paths_to_pass))[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()
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()
def __init__(self): self._docker = None # type: Optional # dict with files which need to be copied to build_context. # New paths can be added by using 'add_to_build_context' method. self._things_copy_to_build_context = dict() # type: Dict[Path, Dict] # copy agent course code if needed. if type(self).COPY_AGENT_SOURCE: root_path = Path(get_package_root()).parent self.add_to_build_context(root_path, "agent_source", custom_copy_function=_copy_agent_source) # the value of this attribute is the path to the file to be copied to the image build # context. for path in self.INCLUDE_PATHS: self.add_to_build_context(path, path.name)
def copy_agent_source(dest_path): root_path = Path(get_package_root()).parent shutil.copytree( six.text_type(root_path), six.text_type(dest_path), # TODO: We should probably juse use .gitignore values here ignore=shutil.ignore_patterns( "*.pyc", "__pycache__", "*.egg-info", ".tox", ".pytest*", ".mypy*", ".idea", ".git", ".virtualenv*", ), )
import json import pprint import subprocess from distutils.spawn import find_executable from scalyr_agent.__scalyr__ import PACKAGE_INSTALL, DEV_INSTALL, get_package_root from scalyr_agent import compat from scalyr_agent.platform_controller import PlatformController from tests.utils.compat import Path from tests.utils.common import get_env import six _AGENT_MAIN_PATH = Path(get_package_root(), "agent_main.py") _CONFIG_MAIN_PATH = Path(get_package_root(), "config_main.py") def _make_or_clear_directory(path): # type: (Path) -> None """ Create directory or clear it if exests.. """ if path.exists(): shutil.rmtree(six.text_type(path), ignore_errors=True) path.mkdir(exist_ok=True, parents=True) def _path_or_text(fn): def wrapper(self, path, *args, **kwargs): if isinstance(path, six.text_type):
def test_get_package_root(self): self.assertEquals(os.path.basename(get_package_root()), "scalyr_agent")
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()
def test_get_package_root(self): self.assertEquals(os.path.basename(get_package_root()), 'scalyr_agent')