Ejemplo n.º 1
0
    def test_wait_for_service_always_fail_with_exception(
        self,
        mock_process_stderr_data: Mock,
        mock_process_stdout_data: Mock,
        mock_service_alive: Mock,
        mock_time_sleep: Mock
    ) -> None:

        service = ServiceOpenSearch(
            self.version,
            self.distribution,
            self.additional_config,
            True,
            self.dependency_installer,
            self.work_dir
        )

        with self.assertRaises(ClusterCreationException) as ctx:
            service.wait_for_service()

        self.assertEqual(str(ctx.exception), "Cluster is not available after 10 attempts")

        self.assertEqual(mock_service_alive.call_count, 10)
        self.assertEqual(mock_time_sleep.call_count, 10)
        self.assertEqual(mock_process_stdout_data.call_count, 10)
        self.assertEqual(mock_process_stderr_data.call_count, 10)
    def test_terminate_process_not_started(self, mock_process_started,
                                           mock_process_terminate):
        service = ServiceOpenSearch(self.version, self.additional_config, True,
                                    self.dependency_installer, self.work_dir)

        service.terminate()

        mock_process_terminate.assert_not_called()
        mock_process_started.assert_called_once()
    def test_start_security_disabled(self, mock_tarfile_open, mock_dump,
                                     mock_file, mock_pid, mock_process):

        dependency_installer = MagicMock()

        service = ServiceOpenSearch(self.version, self.additional_config,
                                    False, dependency_installer, self.work_dir)

        bundle_full_name = "test_bundle_name"
        dependency_installer.download_dist.return_value = bundle_full_name

        mock_dump_result_for_security = MagicMock()
        mock_dump_result_for_additional_config = MagicMock()

        mock_dump.side_effect = [
            mock_dump_result_for_security,
            mock_dump_result_for_additional_config
        ]

        mock_bundle_tar = MagicMock()
        mock_tarfile_open.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
        ]

        # call test target function
        service.start()

        mock_dump.assert_has_calls([
            call({"plugins.security.disabled": "true"}),
            call(self.additional_config)
        ])

        mock_file.assert_has_calls(
            [
                call(
                    os.path.join(self.work_dir, "opensearch-1.1.0", "config",
                                 "opensearch.yml"), "a")
            ],
            [
                call(
                    os.path.join(self.work_dir, "opensearch-1.1.0", "config",
                                 "opensearch.yml"), "a")
            ],
        )
        mock_file_hanlder_for_security.write.assert_called_once_with(
            mock_dump_result_for_security)
        mock_file_hanlder_for_additional_config.write.assert_called_once_with(
            mock_dump_result_for_additional_config)
Ejemplo n.º 4
0
    def test_url_security_disabled(self) -> None:
        service = ServiceOpenSearch(
            self.version,
            self.distribution,
            self.additional_config,
            False,
            self.dependency_installer,
            self.work_dir
        )

        self.assertEqual(service.url(), "http://localhost:9200")
Ejemplo n.º 5
0
    def test_endpoint_port(self) -> None:
        service = ServiceOpenSearch(
            self.version,
            self.distribution,
            self.additional_config,
            True,
            self.dependency_installer,
            self.work_dir
        )

        self.assertEqual(service.endpoint(), "localhost")
        self.assertEqual(service.port(), 9200)
    def test_service_alive_unavailable(self, mock_get_service_response):
        service = ServiceOpenSearch(self.version, self.additional_config, True,
                                    self.dependency_installer, self.work_dir)

        mock_response = MagicMock()

        mock_response.status_code = 300
        mock_response.text = '"status": "red"'

        mock_get_service_response.return_value = mock_response

        self.assertFalse(service.service_alive())
    def test_get_service_response(self, mock_url, mock_requests_get):
        service = ServiceOpenSearch(self.version, 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("/_cluster/health")
        mock_requests_get.assert_called_once_with(mock_url_result,
                                                  verify=False,
                                                  auth=('admin', 'admin'))
    def test_wait_for_service_suceed_on_third_attempt(self,
                                                      mock_process_stderr_data,
                                                      mock_process_stdout_data,
                                                      mock_service_alive,
                                                      mock_time_sleep):
        service = ServiceOpenSearch(self.version, self.additional_config, True,
                                    self.dependency_installer, self.work_dir)

        service.wait_for_service()

        self.assertEqual(mock_service_alive.call_count, 3)
        self.assertEqual(mock_time_sleep.call_count, 2)
        self.assertEqual(mock_process_stdout_data.call_count, 2)
        self.assertEqual(mock_process_stderr_data.call_count, 2)
    def test_wait_for_service_succeed_on_first_attemp(self,
                                                      mock_process_stderr_data,
                                                      mock_process_stdout_data,
                                                      mock_service_alive,
                                                      mock_time_sleep):
        service = ServiceOpenSearch(self.version, self.additional_config, True,
                                    self.dependency_installer, self.work_dir)

        service.wait_for_service()

        mock_service_alive.assert_called_once()
        mock_time_sleep.assert_not_called()
        mock_process_stdout_data.assert_not_called()
        mock_process_stderr_data.assert_not_called()
Ejemplo n.º 10
0
    def test_service_alive_yellow_available(self, mock_get_service_response: Mock) -> None:
        service = ServiceOpenSearch(
            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 = '"status":"yellow"'

        mock_get_service_response.return_value = mock_response

        self.assertTrue(service.service_alive())
    def test_terminate(self, mock_process_stderr_data,
                       mock_process_stdout_data, mock_process_started,
                       mock_process_terminate):
        service = ServiceOpenSearch(self.version, self.additional_config, True,
                                    self.dependency_installer, self.work_dir)

        termination_result = service.terminate()

        mock_process_terminate.assert_called_once()

        self.assertEqual(termination_result.return_code, 123)
        self.assertEqual(termination_result.stdout_data, "test stdout_data")
        self.assertEqual(termination_result.stderr_data, "test stderr_data")
        self.assertEqual(
            termination_result.log_files, {
                "opensearch-service-logs":
                os.path.join("test_work_dir", "opensearch-1.1.0", "logs")
            })
Ejemplo n.º 12
0
    def test_start(self, mock_tarfile_open: Mock, mock_dump: Mock, mock_file: Mock, mock_pid: Mock, mock_process: Mock) -> None:

        dependency_installer = MagicMock()

        service = ServiceOpenSearch(
            self.version,
            self.distribution,
            self.additional_config,
            True,
            dependency_installer,
            self.work_dir
        )

        bundle_full_name = "test_bundle_name"
        dependency_installer.download_dist.return_value = bundle_full_name

        mock_dump_result = MagicMock()
        mock_dump.return_value = mock_dump_result

        mock_bundle_tar = MagicMock()
        mock_tarfile_open.return_value.__enter__.return_value = mock_bundle_tar

        # call test target function
        service.start()

        mock_process.assert_called_once_with("./opensearch-tar-install.sh", os.path.join(self.work_dir, "opensearch-1.1.0"))

        mock_dump.assert_called_once_with(self.additional_config)

        mock_file.assert_called_once_with(os.path.join(self.work_dir, "opensearch-1.1.0", "config", "opensearch.yml"), "a")
        mock_file.return_value.write.assert_called_once_with(mock_dump_result)

        dependency_installer.download_dist.assert_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)

        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)
Ejemplo n.º 15
0
    def __init__(
        self,
        dependency_installer,
        work_dir,
        component_name,
        additional_cluster_config,
        bundle_manifest,
        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 = bundle_manifest
        self.dependency_installer = dependency_installer

        self.service_opensearch = ServiceOpenSearch(
            self.manifest.build.version, self.additional_cluster_config,
            self.security_enabled, self.dependency_installer, self.work_dir)
    def test_url_security_enabled(self):
        service = ServiceOpenSearch(self.version, self.additional_config, True,
                                    self.dependency_installer, self.work_dir)

        self.assertEqual(service.url(), "https://localhost:9200")