| // Copyright (C) 2026 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package com.google.gerrit.opensearch; |
| |
| import com.google.common.flogger.FluentLogger; |
| import java.net.URISyntaxException; |
| import org.apache.hc.core5.http.HttpHost; |
| import org.opensearch.testcontainers.OpenSearchContainer; |
| import org.testcontainers.containers.ContainerLaunchException; |
| import org.testcontainers.utility.DockerImageName; |
| |
| public class Container extends OpenSearchContainer<Container> { |
| private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| |
| public static Container createAndStart(OpenSearchVersion version) { |
| Container container = new Container(version); |
| try { |
| container.start(); |
| } catch (ContainerLaunchException e) { |
| logger.atSevere().log( |
| "Failed to launch OpenSearch container. Logs from container:\n%s", container.getLogs()); |
| throw e; |
| } |
| return container; |
| } |
| |
| private static DockerImageName getImageName(OpenSearchVersion version) { |
| DockerImageName image = DockerImageName.parse("opensearchproject/opensearch"); |
| switch (version) { |
| case V3: |
| return image.withTag("3.5.0"); |
| } |
| throw new IllegalStateException("No tests for version: " + version.name()); |
| } |
| |
| private Container(OpenSearchVersion version) { |
| super(getImageName(version)); |
| withEnv("action.destructive_requires_name", "false"); |
| } |
| |
| public HttpHost getHttpHost() { |
| try { |
| return HttpHost.create(getHttpHostAddress()); |
| } catch (URISyntaxException e) { |
| throw new IllegalStateException("Invalid OpenSearch host address", e); |
| } |
| } |
| } |