def __create_test_instance(self, configuration_logs_entry, monitors_log_configs): config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, "agentConfig.json") config_fragments_dir = os.path.join(config_dir, "configs.d") os.makedirs(config_fragments_dir) logs_json_array = JsonArray() for entry in configuration_logs_entry: logs_json_array.add(JsonObject(content=entry)) fp = open(config_file, "w") fp.write(json_lib.serialize(JsonObject(api_key="fake", logs=logs_json_array))) fp.close() default_paths = DefaultPaths( "/var/log/scalyr-agent-2", "/etc/scalyr-agent-2/agent.json", "/var/lib/scalyr-agent-2" ) config = Configuration(config_file, default_paths) config.parse() self.__monitor_fake_instances = [] for monitor_log_config in monitors_log_configs: self.__monitor_fake_instances.append(FakeMonitor(monitor_log_config)) # noinspection PyTypeChecker return CopyingManager(config, self.__monitor_fake_instances)
def __create_test_instance(self, configuration_logs_entry, monitors_log_configs): config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) logs_json_array = JsonArray() for entry in configuration_logs_entry: logs_json_array.add(JsonObject(content=entry)) fp = open(config_file, 'w') fp.write( json_lib.serialize(JsonObject(api_key='fake', logs=logs_json_array))) fp.close() default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') config = Configuration(config_file, default_paths) config.parse() self.__monitor_fake_instances = [] for monitor_log_config in monitors_log_configs: self.__monitor_fake_instances.append( FakeMonitor(monitor_log_config)) # noinspection PyTypeChecker return CopyingManager(config, self.__monitor_fake_instances)
def __create_test_instance(self, config_monitors, platform_monitors): config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) monitors_json_array = JsonArray() for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) fp = open(config_file, 'w') fp.write( json_lib.serialize( JsonObject(api_key='fake', monitors=monitors_json_array))) fp.close() default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') config = Configuration(config_file, default_paths) config.parse() # noinspection PyTypeChecker return MonitorsManager(config, FakePlatform(platform_monitors))
def __create_test_instance(self, configuration_logs_entry, monitors_log_configs): logs_json_array = JsonArray() for entry in configuration_logs_entry: logs_json_array.add(JsonObject(content=entry)) config = ScalyrTestUtils.create_configuration(extra_toplevel_config={'logs': logs_json_array}) self.__monitor_fake_instances = [] for monitor_log_config in monitors_log_configs: self.__monitor_fake_instances.append(FakeMonitor(monitor_log_config)) # noinspection PyTypeChecker return CopyingManager(config, self.__monitor_fake_instances)
def create_test_monitors_manager( config_monitors=None, platform_monitors=None, extra_toplevel_config=None, null_logger=False, fake_clock=False, ): """Create a test MonitorsManager @param config_monitors: config monitors @param platform_monitors: platform monitors @param extra_toplevel_config: dict of extra top-level key value objects @param null_logger: If True, set all monitors to log to Nullhandler @param fake_clock: If non-null, the manager and all it's monitors' _run_state's will use the provided fake_clock """ monitors_json_array = JsonArray() if config_monitors: for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) extras = {"monitors": monitors_json_array} if extra_toplevel_config: extras.update(extra_toplevel_config) config = ScalyrTestUtils.create_configuration( extra_toplevel_config=extras) config.parse() if not platform_monitors: platform_monitors = [] # noinspection PyTypeChecker test_manager = MonitorsManager(config, FakePlatform(platform_monitors)) if null_logger: # Override Agent Logger to prevent writing to disk for monitor in test_manager.monitors: monitor._logger = FakeAgentLogger("fake_agent_logger") if fake_clock: for monitor in test_manager.monitors + [test_manager]: monitor._run_state = scalyr_util.RunState( fake_clock=fake_clock) # AGENT-113 set all monitors and the monitors_manager threads to daemon to eliminate occasionally hanging tests test_manager.setDaemon(True) for monitor in test_manager.monitors: if isinstance(monitor, threading.Thread): monitor.setDaemon(True) return test_manager, config
def create_test_monitors_manager(config_monitors=None, platform_monitors=None, extra_toplevel_config=None, null_logger=False, fake_clock=False, set_daemon=False): """Create a test MonitorsManager @param config_monitors: config monitors @param platform_monitors: platform monitors @param extra_toplevel_config: dict of extra top-level key value objects @param null_logger: If True, set all monitors to log to Nullhandler @param fake_clock: If non-null, the manager and all it's monitors' _run_state's will use the provided fake_clock """ monitors_json_array = JsonArray() if config_monitors: for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) extras = {'monitors': monitors_json_array} if extra_toplevel_config: extras.update(extra_toplevel_config) config = ScalyrTestUtils.create_configuration(extra_toplevel_config=extras) config.parse() if not platform_monitors: platform_monitors = [] # noinspection PyTypeChecker test_manager = MonitorsManager(config, FakePlatform(platform_monitors)) if null_logger: # Override Agent Logger to prevent writing to disk for monitor in test_manager.monitors: monitor._logger = FakeAgentLogger('fake_agent_logger') if fake_clock: for monitor in test_manager.monitors + [test_manager]: monitor._run_state = scalyr_util.RunState(fake_clock=fake_clock) # AGENT-113 set all monitors and the monitors_manager threads to daemon to eliminate occasionally hanging tests test_manager.setDaemon(True) for monitor in test_manager.monitors: if isinstance(monitor, threading.Thread): monitor.setDaemon(True) return test_manager
def __create_test_instance(self, use_pipelining=False): tmp_dir = tempfile.mkdtemp() config_dir = os.path.join(tmp_dir, "config") data_dir = os.path.join(tmp_dir, "data") log_dir = os.path.join(tmp_dir, "log") os.mkdir(data_dir) os.mkdir(config_dir) os.mkdir(log_dir) self.__test_log_file = os.path.join(tmp_dir, "test.log") fp = open(self.__test_log_file, "w") fp.close() config_file = os.path.join(config_dir, "agentConfig.json") config_fragments_dir = os.path.join(config_dir, "configs.d") os.makedirs(config_fragments_dir) logs_json_array = JsonArray() logs_json_array.add(JsonObject(path=self.__test_log_file)) pipeline_threshold = 1.1 if use_pipelining: pipeline_threshold = 0.0 fp = open(config_file, "w") fp.write( json_lib.serialize( JsonObject( api_key="fake", logs=logs_json_array, pipeline_threshold=pipeline_threshold, ))) fp.close() default_paths = DefaultPaths(log_dir, config_file, data_dir) config = Configuration(config_file, default_paths, None) config.parse() # noinspection PyTypeChecker self._controller = TestableCopyingManager(config, []).controller return self._controller
def __parse_array(self): """Parse a JSON array. The scanner must be at the first '['.""" array_start = self.__scanner.position self.__scanner.read_ubyte() array = JsonArray() while True: # Check for end-of-array. if self.__peek_next_non_whitespace() == ']': self.__scanner.read_ubyte() return array self.__peek_next_non_whitespace() # skip any whitespace # TODO: If we ever want to put in annotated supported, uncomment the pos lines. # value_start_pos = self.__scanner.position array.add(self.parse_value()) # value_end_pos = self.__scanner.position # value_comma_pos = -1 c = self.__peek_next_non_whitespace() if c is None: self.__error("Array has no terminating '['", array_start) elif c == ']': # do nothing we'll process the ']' back around at the top of # the loop. continue elif c == ',': self.__scanner.read_ubyte() # value_comma_pos = self.__scanner.position else: if self.__preceding_line_break() and self.allow_missing_commas: # proceed, inferring a comma continue else: self.__error( "Unexpected character [%s] in array... are you " "missing a comma?" % c)
def __create_test_instance(self, config_monitors, platform_monitors): config_dir = tempfile.mkdtemp() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) monitors_json_array = JsonArray() for entry in config_monitors: monitors_json_array.add(JsonObject(content=entry)) fp = open(config_file, 'w') fp.write(json_lib.serialize(JsonObject(api_key='fake', monitors=monitors_json_array))) fp.close() default_paths = DefaultPaths('/var/log/scalyr-agent-2', '/etc/scalyr-agent-2/agent.json', '/var/lib/scalyr-agent-2') config = Configuration(config_file, default_paths) config.parse() # noinspection PyTypeChecker return MonitorsManager(config, FakePlatform(platform_monitors))
def __create_test_instance(self, use_pipelining=False): tmp_dir = tempfile.mkdtemp() config_dir = os.path.join(tmp_dir, 'config') data_dir = os.path.join(tmp_dir, 'data') log_dir = os.path.join(tmp_dir, 'log') os.mkdir(data_dir) os.mkdir(config_dir) os.mkdir(log_dir) self.__test_log_file = os.path.join(tmp_dir, 'test.log') fp = open(self.__test_log_file, 'w') fp.close() config_file = os.path.join(config_dir, 'agentConfig.json') config_fragments_dir = os.path.join(config_dir, 'configs.d') os.makedirs(config_fragments_dir) logs_json_array = JsonArray() logs_json_array.add(JsonObject(path=self.__test_log_file)) pipeline_threshold = 1.1 if use_pipelining: pipeline_threshold = 0.0 fp = open(config_file, 'w') fp.write(json_lib.serialize(JsonObject(api_key='fake', logs=logs_json_array, pipeline_threshold=pipeline_threshold))) fp.close() default_paths = DefaultPaths(log_dir, config_file, data_dir) config = Configuration(config_file, default_paths) config.parse() # noinspection PyTypeChecker self._controller = TestableCopyingManager(config, []).controller return self._controller
def __parse_array(self): """Parse a JSON array. The scanner must be at the first '['.""" array_start = self.__scanner.position self.__scanner.read_ubyte() array = JsonArray() while True: # Check for end-of-array. if self.__peek_next_non_whitespace() == ']': self.__scanner.read_ubyte() return array self.__peek_next_non_whitespace() # skip any whitespace # TODO: If we ever want to put in annotated supported, uncomment the pos lines. # value_start_pos = self.__scanner.position array.add(self.parse_value()) # value_end_pos = self.__scanner.position # value_comma_pos = -1 c = self.__peek_next_non_whitespace() if c is None: self.__error("Array has no terminating '['", array_start) elif c == ']': # do nothing we'll process the ']' back around at the top of # the loop. continue elif c == ',': self.__scanner.read_ubyte() # value_comma_pos = self.__scanner.position else: if self.__preceding_line_break() and self.allow_missing_commas: # proceed, inferring a comma continue else: self.__error("Unexpected character [%s] in array... are you " "missing a comma?" % c)