def test_ca_cert_files_checks_are_skipped_under_dev_and_msi_install(self): # Skip those checks under dev and msi install because those final generated certs files # are not present under dev install import scalyr_agent.agent_main from scalyr_agent.agent_main import ScalyrAgent from scalyr_agent.platform_controller import PlatformController # 1. Dev install (boths checks should be skipped) scalyr_agent.agent_main.INSTALL_TYPE = DEV_INSTALL # ca_cert_path file doesn't exist config = mock.Mock() config.scalyr_server = "foo.bar.com" config.compression_level = 1 config.verify_server_certificate = True config.ca_cert_path = "/tmp/doesnt.exist" agent = ScalyrAgent(PlatformController()) agent._ScalyrAgent__config = config self.assertTrue(agent._ScalyrAgent__create_client()) # intermediate_certs_path file doesn't exist config.verify_server_certificate = True config.ca_cert_path = __file__ config.intermediate_certs_path = "/tmp/doesnt.exist" self.assertTrue(agent._ScalyrAgent__create_client()) # 2. MSI install (only intermediate_certs_path check should be skipped) scalyr_agent.agent_main.INSTALL_TYPE = MSI_INSTALL config = mock.Mock() config.scalyr_server = "foo.bar.com" config.compression_level = 1 config.verify_server_certificate = True config.ca_cert_path = "/tmp/doesnt.exist" agent = ScalyrAgent(PlatformController()) agent._ScalyrAgent__config = config expected_msg = ( r'Invalid path "/tmp/doesnt.exist" specified for the "ca_cert_path" config ' "option: file does not exist") self.assertRaisesRegexp(ValueError, expected_msg, agent._ScalyrAgent__create_client) # intermediate_certs_path file doesn't exist config.verify_server_certificate = True config.ca_cert_path = __file__ config.intermediate_certs_path = "/tmp/doesnt.exist" self.assertTrue(agent._ScalyrAgent__create_client())
def test_create_client_ca_file_and_intermediate_certs_file_doesnt_exist(self): from scalyr_agent.agent_main import ScalyrAgent from scalyr_agent.platform_controller import PlatformController # 1. file doesn't exist but cert verification is disabled config = mock.Mock() config.scalyr_server = "foo.bar.com" config.verify_server_certificate = False config.ca_cert_path = "/tmp/doesnt.exist" agent = ScalyrAgent(PlatformController()) agent._ScalyrAgent__config = config self.assertTrue(agent._ScalyrAgent__create_client()) # ca_cert_path file doesn't exist config.verify_server_certificate = True config.ca_cert_path = "/tmp/doesnt.exist" agent = ScalyrAgent(PlatformController()) agent._ScalyrAgent__config = config expected_msg = ( r'Invalid path "/tmp/doesnt.exist" specified for the "ca_cert_path" config ' "option: file does not exist" ) self.assertRaisesRegexp( ValueError, expected_msg, agent._ScalyrAgent__create_client ) # intermediate_certs_path file doesn't exist config.verify_server_certificate = True config.ca_cert_path = __file__ config.intermediate_certs_path = "/tmp/doesnt.exist" agent = ScalyrAgent(PlatformController()) agent._ScalyrAgent__config = config expected_msg = ( r'Invalid path "/tmp/doesnt.exist" specified for the ' '"intermediate_certs_path" config option: file does not exist' ) self.assertRaisesRegexp( ValueError, expected_msg, agent._ScalyrAgent__create_client )
def setUp(self): super(AgentMainStatusHandlerTestCase, self).setUp() self.data_path = tempfile.mkdtemp(suffix="agent-data-path") self.status_format_file = os.path.join(self.data_path, STATUS_FORMAT_FILE) default_paths = mock.Mock() default_paths.agent_data_path = self.data_path default_paths.agent_log_path = "agent.log" config_file = os.path.join(BASE_DIR, "fixtures/configs/agent1.json") config = Configuration(config_file, default_paths, None) config.parse() self.agent = ScalyrAgent(PlatformController()) self.agent._ScalyrAgent__config = config
def start(self): from scalyr_agent.agent_main import ScalyrAgent self.controller = WindowsPlatformController() config_path = _get_config_path_registry_entry( self.controller.default_paths.config_file_path) try: ScalyrAgent.agent_run_method(self.controller, config_path, perform_config_check=True) except Exception as e: self.error( "Error seen while starting the Scalyr Agent: {}".format(e)) self.error( "Still attempting to run agent, but you must fix error. Agent will re-read configuration file " "without restarting it.") ScalyrAgent.agent_run_method(self.controller, config_path, perform_config_check=False)
class AgentMainStatusHandlerTestCase(ScalyrTestCase): def setUp(self): super(AgentMainStatusHandlerTestCase, self).setUp() self.data_path = tempfile.mkdtemp(suffix="agent-data-path") self.status_format_file = os.path.join(self.data_path, STATUS_FORMAT_FILE) default_paths = mock.Mock() default_paths.agent_data_path = self.data_path default_paths.agent_log_path = "agent.log" config_file = os.path.join(BASE_DIR, "fixtures/configs/agent1.json") config = Configuration(config_file, default_paths, None) config.parse() self.agent = ScalyrAgent(PlatformController()) self.agent._ScalyrAgent__config = config def test_report_status_to_file_no_format_specified(self): # No format is provided, should default to "text" status_file = self.agent._ScalyrAgent__report_status_to_file() content = self._read_status_file(status_file) self.assertTrue("Current time:" in content) self.assertTrue("ServerHost:" in content) self.assertTrue("Agent configuration:" in content) def test_report_status_to_file_text_format_specified(self): # "text" format is explicitly provided self._write_status_format_file("text") status_file = self.agent._ScalyrAgent__report_status_to_file() content = self._read_status_file(status_file) self.assertTrue("Current time:" in content) self.assertTrue("ServerHost:" in content) self.assertTrue("Agent configuration:" in content) def test_report_status_to_file_invalid_format_specified(self): # invalid format is explicitly provided, should fall back to text self._write_status_format_file("invalid") status_file = self.agent._ScalyrAgent__report_status_to_file() content = self._read_status_file(status_file) self.assertTrue("Current time:" in content) self.assertTrue("ServerHost:" in content) self.assertTrue("Agent configuration:" in content) def test_report_status_to_file_json_format_specified(self): # "json" format is explicitly provided self._write_status_format_file("json") status_file = self.agent._ScalyrAgent__report_status_to_file() content = self._read_status_file(status_file) self.assertFalse("Current time:" in content) self.assertFalse("ServerHost:" in content) self.assertFalse("Agent configuration:" in content) parsed = json.loads(content) self.assertTrue("config_status" in parsed) self.assertTrue("user" in parsed) self.assertTrue("scalyr_server" in parsed) def _write_status_format_file(self, status_format): with open(self.status_format_file, "w") as fp: fp.write(status_format.strip()) def _read_status_file(self, status_file_path): with open(status_file_path, "r") as fp: content = fp.read() return content
def test_check_remote_if_no_tty(self): from scalyr_agent.agent_main import ScalyrAgent mock_config = mock.Mock() platform_controller = mock.Mock() platform_controller.default_paths = mock.Mock() agent = ScalyrAgent(platform_controller) agent._ScalyrAgent__config = mock_config agent._ScalyrAgent__read_and_verify_config = mock.Mock() agent._ScalyrAgent__perform_config_checks = mock.Mock() agent._ScalyrAgent__run = mock.Mock() agent._ScalyrAgent__read_and_verify_config.return_value = mock_config agent._ScalyrAgent__run.return_value = 7 mock_config_path = self._write_mock_config() # tty is available, should use command line option value when specified mock_command = "inner_run_with_checks" mock_command_options = mock.Mock() mock_command_options.no_check_remote = True return_code = agent.main(mock_config_path, mock_command, mock_command_options) self.assertEqual(return_code, 7) agent._ScalyrAgent__perform_config_checks.assert_called_with(True) # tty is available, command option is not set, should default to False agent._ScalyrAgent__perform_config_checks.reset_mock() mock_command = "inner_run_with_checks" mock_command_options = mock.Mock() mock_command_options.no_check_remote = None return_code = agent.main(mock_config_path, mock_command, mock_command_options) self.assertEqual(return_code, 7) agent._ScalyrAgent__perform_config_checks.assert_called_with(False) # tty is not available (stdout.isatty returns False), should use check_remote_if_no_tty # config option value (config option is set to True) agent._ScalyrAgent__perform_config_checks.reset_mock() mock_command_options.no_check_remote = None mock_stdout = mock.Mock() mock_isatty = mock.Mock() mock_isatty.return_value = False mock_stdout.isatty = mock_isatty mock_config.check_remote_if_no_tty = True with mock.patch("scalyr_agent.agent_main.sys.stdout", mock_stdout): return_code = agent.main(mock_config_path, mock_command, mock_command_options) self.assertEqual(return_code, 7) agent._ScalyrAgent__perform_config_checks.assert_called_with(False) # tty is not available (stdout.isatty returns False), should use check_remote_if_no_tty # config option value (config option is set to False) agent._ScalyrAgent__perform_config_checks.reset_mock() mock_command_options.no_check_remote = None mock_stdout = mock.Mock() mock_isatty = mock.Mock() mock_isatty.return_value = False mock_stdout.isatty = mock_isatty mock_config.check_remote_if_no_tty = False with mock.patch("scalyr_agent.agent_main.sys.stdout", mock_stdout): return_code = agent.main(mock_config_path, mock_command, mock_command_options) self.assertEqual(return_code, 7) agent._ScalyrAgent__perform_config_checks.assert_called_with(True) # tty is not available (stdout.isatty is not set), should use check_remote_if_no_tty # config option value (config option is set to False) agent._ScalyrAgent__perform_config_checks.reset_mock() mock_command_options.no_check_remote = None mock_stdout = mock.Mock() mock_isatty = None mock_stdout.isatty = mock_isatty mock_config.check_remote_if_no_tty = True with mock.patch("scalyr_agent.agent_main.sys.stdout", mock_stdout): return_code = agent.main(mock_config_path, mock_command, mock_command_options) self.assertEqual(return_code, 7) agent._ScalyrAgent__perform_config_checks.assert_called_with(False) # tty is not available (stdout.isatty is not set), should use check_remote_if_no_tty # config option value (config option is set to True) agent._ScalyrAgent__perform_config_checks.reset_mock() mock_command_options.no_check_remote = None mock_stdout = mock.Mock() mock_isatty = None mock_stdout.isatty = mock_isatty mock_config.check_remote_if_no_tty = False with mock.patch("scalyr_agent.agent_main.sys.stdout", mock_stdout): return_code = agent.main(mock_config_path, mock_command, mock_command_options) self.assertEqual(return_code, 7) agent._ScalyrAgent__perform_config_checks.assert_called_with(True)
def test_skipped_bytes_warnings(self): from scalyr_agent.agent_main import ScalyrAgent from scalyr_agent.platform_controller import PlatformController with mock.patch("scalyr_agent.__scalyr__.INSTALL_TYPE", __scalyr__.DEV_INSTALL): config = mock.Mock() config.scalyr_server = "foo.bar.com" config.server_attributes = {"serverHost": "test"} config.additional_file_paths = [] config.compression_level = 1 config.copying_manager_stats_log_interval = 60 config.parsed_max_send_rate_enforcement = 12345 config.verify_server_certificate = True config.ca_cert_path = "/tmp/doesnt.exist" platform_controller = PlatformController() platform_controller.get_usage_info = AgentMainTestCase.fake_get_useage_info agent = ScalyrAgent(platform_controller) agent._ScalyrAgent__config = config client = create_client(config) def get_worker_session_statuses_mock(*args, **kwargs): return [client] with mock.patch.object(agent, "_ScalyrAgent__copying_manager") as m: m.generate_status = mock.MagicMock(return_value=None) m.get_worker_session_statuses = get_worker_session_statuses_mock base_stats = agent_status.OverallStats() base_stats.total_bytes_skipped = 500 test = agent._ScalyrAgent__calculate_overall_stats( base_stats, copy_manager_warnings=True) self.assertIsNotNone(test) self.assertLogFileContainsLineRegex(".*" + re.escape( "Warning, skipping copying log lines. Only copied 0.0 MB/s log bytes when 0.0 MB/s were generated over the last 1.0 minutes. This may be due to max_send_rate_enforcement. Log upload has been delayed 0.0 seconds in the last 1.0 minutes This may be desired (due to excessive bytes from a problematic log file). Please contact [email protected] for additional help." )) config = mock.Mock() config.scalyr_server = "foo.bar.com" config.server_attributes = {"serverHost": "test"} config.additional_file_paths = [] config.compression_level = 1 config.copying_manager_stats_log_interval = 60 config.parsed_max_send_rate_enforcement = None config.verify_server_certificate = True config.ca_cert_path = "/tmp/doesnt.exist" platform_controller = PlatformController() platform_controller.get_usage_info = AgentMainTestCase.fake_get_useage_info agent = ScalyrAgent(platform_controller) agent._ScalyrAgent__config = config client = create_client(config) with mock.patch.object(agent, "_ScalyrAgent__copying_manager") as m: m.generate_status = mock.MagicMock(return_value=None) m.get_worker_session_statuses = get_worker_session_statuses_mock base_stats.total_bytes_skipped = 1000 test = agent._ScalyrAgent__calculate_overall_stats( base_stats, copy_manager_warnings=True) self.assertIsNotNone(test) with open(self.agent_log_path, "r") as f: print((f.read())) self.assertLogFileContainsLineRegex(".*" + re.escape( "Warning, skipping copying log lines. Only copied 0.0 MB/s log bytes when 0.0 MB/s were generated over the last 1.0 minutes. This may be desired (due to excessive bytes from a problematic log file). Please contact [email protected] for additional help." ))