def test_start_without_security(self, mock_tarfile_open, mock_dump, mock_file, mock_pid, mock_process, mock_check_call): mock_dependency_installer = MagicMock() service = ServiceOpenSearchDashboards(self.version, {}, False, mock_dependency_installer, self.work_dir) bundle_full_name = "test_bundle_name" mock_dependency_installer.download_dist.return_value = bundle_full_name mock_bundle_tar = MagicMock() mock_tarfile_open.return_value.__enter__.return_value = mock_bundle_tar mock_file_hanlder_for_security = mock_open().return_value mock_file_hanlder_for_additional_config = mock_open().return_value # open() will be called twice, one for disabling security, second for additional_config mock_file.side_effect = [ mock_file_hanlder_for_security, mock_file_hanlder_for_additional_config ] mock_dump_result = MagicMock() mock_dump.return_value = mock_dump_result # call the target test function service.start() mock_file.assert_has_calls( [ call( os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "config", "opensearch_dashboards.yml"), "w") ], [ call( os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "config", "opensearch_dashboards.yml"), "a") ], ) mock_check_call.assert_called_once_with( "./opensearch-dashboards-plugin remove securityDashboards", cwd=os.path.join("test_work_dir", "opensearch-dashboards-1.1.0", "bin"), shell=True) mock_dump.assert_called_once_with({ "logging.dest": os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "logs", "opensearch_dashboards.log") }) mock_file_hanlder_for_security.close.assert_called_once() mock_file_hanlder_for_additional_config.write.assert_called_once_with( mock_dump_result)
def test_start_without_security_and_not_installed( self, mock_tarfile_open: Mock, mock_dump: Mock, mock_file: Any, mock_pid: Mock, mock_process: Mock, mock_check_call: Mock, mock_os_isdir: Mock) -> None: mock_dependency_installer = MagicMock() service = ServiceOpenSearchDashboards(self.version, self.distribution, {}, False, mock_dependency_installer, self.work_dir) bundle_full_name = "test_bundle_name" mock_dependency_installer.download_dist.return_value = bundle_full_name mock_bundle_tar = MagicMock() mock_tarfile_open.return_value.__enter__.return_value = mock_bundle_tar mock_file_handler_for_security = mock_open().return_value mock_file_handler_for_additional_config = mock_open().return_value # open() will be called twice, one for disabling security, second for additional_config mock_file.side_effect = [ mock_file_handler_for_security, mock_file_handler_for_additional_config ] mock_dump_result = MagicMock() mock_dump.return_value = mock_dump_result mock_os_isdir.side_effect = [False, True] # call the target test function service.start() mock_check_call.assert_not_called() mock_file.assert_has_calls( [ call( os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "config", "opensearch_dashboards.yml"), "w") ], [ call( os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "config", "opensearch_dashboards.yml"), "a") ], ) mock_dump.assert_called_once_with({ "logging.dest": os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "logs", "opensearch_dashboards.log") }) mock_file_handler_for_security.close.assert_called_once() mock_file_handler_for_additional_config.write.assert_called_once_with( mock_dump_result)
def test_endpoint_port_url(self): service = ServiceOpenSearchDashboards(self.version, self.additional_config, True, self.dependency_installer, self.work_dir) self.assertEqual(service.endpoint(), "localhost") self.assertEqual(service.port(), 5601) self.assertEqual(service.url(), "http://localhost:5601")
def test_service_alive_red_unavailable(self, mock_get_service_response): service = ServiceOpenSearchDashboards(self.version, self.additional_config, True, self.dependency_installer, self.work_dir) mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = '"state":"red"' mock_get_service_response.return_value = mock_response self.assertFalse(service.service_alive())
def test_service_alive_yellow_available( self, mock_get_service_response: Mock) -> None: service = ServiceOpenSearchDashboards(self.version, self.distribution, self.additional_config, True, self.dependency_installer, self.work_dir) mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = '"state":"yellow"' mock_get_service_response.return_value = mock_response self.assertTrue(service.service_alive())
def test_get_service_response_without_security(self, mock_url, mock_requests_get): service = ServiceOpenSearchDashboards(self.version, self.additional_config, False, self.dependency_installer, self.work_dir) mock_url_result = MagicMock() mock_url.return_value = mock_url_result service.get_service_response() mock_url.assert_called_once_with("/api/status") mock_requests_get.assert_called_once_with(mock_url_result, auth=None, verify=False)
def test_get_service_response_with_security( self, mock_url: Mock, mock_requests_get: Mock) -> None: service = ServiceOpenSearchDashboards(self.version, self.distribution, self.additional_config, True, self.dependency_installer, self.work_dir) mock_url_result = MagicMock() mock_url.return_value = mock_url_result service.get_service_response() mock_url.assert_called_once_with("/api/status") mock_requests_get.assert_called_once_with(mock_url_result, verify=False, auth=("admin", "admin"))
def test_start(self, mock_tarfile_open: Mock, mock_dump: Mock, mock_file: Mock, mock_pid: Mock, mock_process: Mock) -> None: mock_dependency_installer = MagicMock() service = ServiceOpenSearchDashboards(self.version, self.distribution, self.additional_config, True, mock_dependency_installer, self.work_dir) bundle_full_name = "test_bundle_name" mock_dependency_installer.download_dist.return_value = bundle_full_name mock_bundle_tar = MagicMock() mock_tarfile_open.return_value.__enter__.return_value = mock_bundle_tar mock_dump_result = MagicMock() mock_dump.return_value = mock_dump_result # call the target test function service.start() mock_dependency_installer.download_dist.called_once_with(self.work_dir) mock_tarfile_open.assert_called_once_with(bundle_full_name, "r:gz") mock_bundle_tar.extractall.assert_called_once_with(self.work_dir) mock_file.assert_called_once_with( os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "config", "opensearch_dashboards.yml"), "a") mock_dump.assert_called_once_with({ "script.context.field.max_compilations_rate": "1000/1m", "logging.dest": os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "logs", "opensearch_dashboards.log") }) mock_file.return_value.write.assert_called_once_with(mock_dump_result) mock_process.assert_called_once_with( "./opensearch-dashboards", os.path.join(self.work_dir, "opensearch-dashboards-1.1.0", "bin")) self.assertEqual(mock_pid.call_count, 1)
def __init__( self, dependency_installer_opensearch: DependencyInstallerOpenSearch, dependency_installer_opensearch_dashboards: DependencyInstallerOpenSearchDashboards, work_dir: str, component_name: str, additional_cluster_config: dict, bundle_manifest_opensearch: BundleManifest, bundle_manifest_opensearch_dashboards: BundleManifest, security_enabled: bool, component_test_config: str, test_recorder: TestRecorder, ) -> None: super().__init__( work_dir, component_name, component_test_config, security_enabled, additional_cluster_config, test_recorder.local_cluster_logs ) self.manifest_opensearch = bundle_manifest_opensearch self.manifest_opensearch_dashboards = bundle_manifest_opensearch_dashboards self.dependency_installer_opensearch = dependency_installer_opensearch self.dependency_installer_opensearch_dashboards = dependency_installer_opensearch_dashboards self.service_opensearch = ServiceOpenSearch( self.manifest_opensearch.build.version, self.manifest_opensearch.build.distribution, {}, self.security_enabled, self.dependency_installer_opensearch, self.work_dir) self.service_opensearch_dashboards = ServiceOpenSearchDashboards( self.manifest_opensearch_dashboards.build.version, self.manifest_opensearch_dashboards.build.distribution, self.additional_cluster_config, self.security_enabled, self.dependency_installer_opensearch_dashboards, self.work_dir)
def __init__( self, dependency_installer_opensearch, dependency_installer_opensearch_dashboards, work_dir, component_name, additional_cluster_config, bundle_manifest_opensearch, bundle_manifest_opensearch_dashboards, security_enabled, component_test_config, test_recorder: TestRecorder, ): super().__init__( work_dir, component_name, component_test_config, security_enabled, additional_cluster_config, test_recorder.local_cluster_logs ) self.manifest_opensearch = bundle_manifest_opensearch self.manifest_opensearch_dashboards = bundle_manifest_opensearch_dashboards self.dependency_installer_opensearch = dependency_installer_opensearch self.dependency_installer_opensearch_dashboards = dependency_installer_opensearch_dashboards self.service_opensearch = ServiceOpenSearch( self.manifest_opensearch.build.version, {}, self.security_enabled, self.dependency_installer_opensearch, self.work_dir) build = self.manifest_opensearch_dashboards.build self.service_opensearch_dashboards = ServiceOpenSearchDashboards( build.version, self.additional_cluster_config, self.security_enabled, self.dependency_installer_opensearch_dashboards, self.work_dir)