blob: 2e81bdbbd3e8ef7e8ea0a6c1958f9db4b27a8f0f [file]
// 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 static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.DEFAULT_USERNAME;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.KEY_PASSWORD;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.KEY_PREFIX;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.KEY_SERVER;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.KEY_TRUST_STORE_PASSWORD;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.KEY_TRUST_STORE_PATH;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.KEY_USERNAME;
import static com.google.gerrit.opensearch.OpenSearchConfiguration.SECTION_OPENSEARCH;
import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static java.util.stream.Collectors.toList;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.index.IndexConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.ProvisionException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.util.Arrays;
import org.apache.hc.core5.http.HttpHost;
import org.eclipse.jgit.lib.Config;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class OpenSearchConfigurationTest {
@Test
public void singleServerNoOtherConfig() throws Exception {
Config cfg = newConfig();
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertHosts(esCfg, "http://open:1234");
assertThat(esCfg.username).isNull();
assertThat(esCfg.password).isNull();
assertThat(esCfg.prefix).isEmpty();
}
@Test
public void serverWithoutPortSpecified() throws Exception {
Config cfg = new Config();
cfg.setString(SECTION_OPENSEARCH, null, KEY_SERVER, "http://open");
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertHosts(esCfg, "http://open:9200");
}
@Test
public void prefix() throws Exception {
Config cfg = newConfig();
cfg.setString(SECTION_OPENSEARCH, null, KEY_PREFIX, "myprefix");
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertThat(esCfg.prefix).isEqualTo("myprefix");
}
@Test
public void withAuthentication() throws Exception {
Config cfg = newConfig();
cfg.setString(SECTION_OPENSEARCH, null, KEY_USERNAME, "myself");
cfg.setString(SECTION_OPENSEARCH, null, KEY_PASSWORD, "s3kr3t");
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertThat(esCfg.username).isEqualTo("myself");
assertThat(esCfg.password).isEqualTo("s3kr3t");
}
@Test
public void withAuthenticationPasswordOnlyUsesDefaultUsername() throws Exception {
Config cfg = newConfig();
cfg.setString(SECTION_OPENSEARCH, null, KEY_PASSWORD, "s3kr3t");
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertThat(esCfg.username).isEqualTo(DEFAULT_USERNAME);
assertThat(esCfg.password).isEqualTo("s3kr3t");
}
@Test
public void multipleServers() throws Exception {
Config cfg = new Config();
cfg.setStringList(
SECTION_OPENSEARCH,
null,
KEY_SERVER,
ImmutableList.of("http://open1:1234", "http://open2:1234"));
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertHosts(esCfg, "http://open1:1234", "http://open2:1234");
}
@Test
public void noServers() throws Exception {
assertProvisionException(new Config(), "No valid OpenSearch servers configured");
}
@Test
public void singleServerInvalid() throws Exception {
Config cfg = new Config();
cfg.setString(SECTION_OPENSEARCH, null, KEY_SERVER, "foo");
assertProvisionException(cfg, "No valid OpenSearch servers configured");
}
@Test
public void multipleServersIncludingInvalid() throws Exception {
Config cfg = new Config();
cfg.setStringList(
SECTION_OPENSEARCH, null, KEY_SERVER, ImmutableList.of("http://open1:1234", "foo"));
OpenSearchConfiguration esCfg = newOpenSearchConfig(cfg);
assertHosts(esCfg, "http://open1:1234");
}
@Test
public void unsupportedPaginationTypeNone() {
Config cfg = new Config();
cfg.setString("index", null, "paginationType", "NONE");
assertProvisionException(
cfg, "The 'index.paginationType = NONE' configuration is not supported by OpenSearch");
}
@Rule public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void trustStorePathIsReadFromConfig() throws Exception {
// Generate a throwaway truststore in a temp dir
Path trustStore = tempFolder.newFile("test.jks").toPath();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null, null);
try (OutputStream os = Files.newOutputStream(trustStore)) {
ks.store(os, "changeit".toCharArray());
}
Config cfg = new Config();
cfg.setString(SECTION_OPENSEARCH, null, KEY_SERVER, "http://localhost:9200");
cfg.setString(SECTION_OPENSEARCH, null, KEY_TRUST_STORE_PATH, trustStore.toString());
cfg.setString(SECTION_OPENSEARCH, null, KEY_TRUST_STORE_PASSWORD, "changeit");
OpenSearchConfiguration openCfg = newOpenSearchConfig(cfg);
assertThat(openCfg.trustStorePath).isEqualTo(trustStore);
assertThat(openCfg.trustStorePassword).isEqualTo("changeit");
}
private static Config newConfig() {
Config config = new Config();
config.setString(SECTION_OPENSEARCH, null, KEY_SERVER, "http://open:1234");
return config;
}
private OpenSearchConfiguration newOpenSearchConfig(Config cfg) throws Exception {
return new OpenSearchConfiguration(
cfg,
IndexConfig.fromConfig(cfg).build(),
new SitePaths(tempFolder.newFolder("site").toPath()));
}
private void assertHosts(OpenSearchConfiguration cfg, Object... hostURIs) throws Exception {
assertThat(Arrays.asList(cfg.getHosts()).stream().map(HttpHost::toURI).collect(toList()))
.containsExactly(hostURIs);
}
private void assertProvisionException(Config cfg, String msg) {
ProvisionException thrown =
assertThrows(ProvisionException.class, () -> newOpenSearchConfig(cfg));
assertThat(thrown).hasMessageThat().contains(msg);
}
}