Skip to content

Commit

Permalink
fix test failure
Browse files Browse the repository at this point in the history
Signed-off-by: Xue Zhou <xuezhou@amazon.com>
  • Loading branch information
xuezhou25 committed Oct 20, 2022
1 parent 6ec459b commit 982e29f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.opensearch.cloud.azure.classic.management.AzureComputeService.Discovery;
import org.opensearch.cloud.azure.classic.management.AzureComputeService.Management;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.test.OpenSearchIntegTestCase;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
public class AzureSimpleTests extends AbstractAzureComputeServiceTestCase {
Expand Down Expand Up @@ -78,7 +80,8 @@ public void testOneNodeShouldRunUsingWrongSettings() {
.put(Management.SERVICE_NAME_SETTING.getKey(), "dummy")
.put(Discovery.HOST_TYPE_SETTING.getKey(), "do_not_exist");

IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> internalCluster().startNode(settings));
assertThat(e.getMessage(), containsString("invalid value for host type [do_not_exist]"));
SettingsException e = expectThrows(SettingsException.class, () -> internalCluster().startNode(settings));
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
assertThat(e.getCause().getMessage(), containsString("invalid value for host type [do_not_exist]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

package org.opensearch.example.customsettings;

import org.hamcrest.Matchers;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.test.OpenSearchTestCase;

import static org.opensearch.example.customsettings.ExampleCustomSettingsConfig.VALIDATED_SETTING;
Expand All @@ -50,10 +52,11 @@ public void testValidatedSetting() {
final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build());
assertEquals(expected, actual);

final IllegalArgumentException exception = expectThrows(
IllegalArgumentException.class,
final SettingsException exception = expectThrows(
SettingsException.class,
() -> VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build())
);
assertEquals("Setting must not contain [forbidden]", exception.getMessage());
assertThat(exception.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
assertEquals("Setting must not contain [forbidden]", exception.getCause().getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.opensearch.cluster.metadata.RepositoryMetadata;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.common.unit.ByteSizeUnit;
import org.opensearch.common.unit.ByteSizeValue;
import org.opensearch.common.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -88,17 +89,19 @@ public void testInvalidChunkBufferSizeSettings() {
createS3Repo(getRepositoryMetadata(s3)).close();
// buffer < 5mb should fail
final Settings s4 = bufferAndChunkSettings(4, 10);
final IllegalArgumentException e2 = expectThrows(
IllegalArgumentException.class,
final SettingsException e2 = expectThrows(
SettingsException.class,
() -> createS3Repo(getRepositoryMetadata(s4)).close()
);
assertThat(e2.getMessage(), containsString("failed to parse value [4mb] for setting [buffer_size], must be >= [5mb]"));
assertThat(e2.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
assertThat(e2.getCause().getMessage(), containsString("failed to parse value [4mb] for setting [buffer_size], must be >= [5mb]"));
final Settings s5 = bufferAndChunkSettings(5, 6000000);
final IllegalArgumentException e3 = expectThrows(
IllegalArgumentException.class,
final SettingsException e3 = expectThrows(
SettingsException.class,
() -> createS3Repo(getRepositoryMetadata(s5)).close()
);
assertThat(e3.getMessage(), containsString("failed to parse value [6000000mb] for setting [chunk_size], must be <= [5tb]"));
assertThat(e3.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
assertThat(e3.getCause().getMessage(), containsString("failed to parse value [6000000mb] for setting [chunk_size], must be <= [5tb]"));
}

private Settings bufferAndChunkSettings(long buffer, long chunk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@

package org.opensearch.cluster.metadata;

import org.hamcrest.Matchers;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.test.OpenSearchTestCase;

import static org.opensearch.cluster.metadata.IndexMetadata.DEFAULT_NUMBER_OF_SHARDS;
Expand All @@ -43,20 +45,22 @@ public class EvilSystemPropertyTests extends OpenSearchTestCase {

@SuppressForbidden(reason = "manipulates system properties for testing")
public void testNumShards() {
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () ->
SettingsException exception = expectThrows(SettingsException.class, () ->
IndexMetadata.buildNumberOfShardsSetting()
.get(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1025).build()));
assertEquals("Failed to parse value [1025] for setting [" + SETTING_NUMBER_OF_SHARDS + "] must be <= 1024", exception.getMessage());
assertEquals("Failed to parse value [1025] for setting [" + SETTING_NUMBER_OF_SHARDS + "] must be <= 1024", exception.getCause().getMessage());

Integer numShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 100).build());
assertEquals(100, numShards.intValue());
int limit = randomIntBetween(1, 10);
System.setProperty(MAX_NUMBER_OF_SHARDS, Integer.toString(limit));
try {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () ->
SettingsException e = expectThrows(SettingsException.class, () ->
IndexMetadata.buildNumberOfShardsSetting()
.get(Settings.builder().put("index.number_of_shards", 11).build()));
assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, e.getMessage());
Throwable cause = e.getCause();
assertThat(cause, Matchers.instanceOf(IllegalArgumentException.class));
assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, cause.getMessage());
System.clearProperty(MAX_NUMBER_OF_SHARDS);

Integer defaultFromSetting = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getDefault(Settings.EMPTY);
Expand All @@ -70,9 +74,10 @@ public void testNumShards() {
randomDefault = randomIntBetween(1, 10);
System.setProperty(MAX_NUMBER_OF_SHARDS, Integer.toString(randomDefault));
System.setProperty(DEFAULT_NUMBER_OF_SHARDS, Integer.toString(randomDefault + 1));
e = expectThrows(IllegalArgumentException.class, IndexMetadata::buildNumberOfShardsSetting);

cause = expectThrows(IllegalArgumentException.class, IndexMetadata::buildNumberOfShardsSetting);
assertEquals(DEFAULT_NUMBER_OF_SHARDS + " value [" + (randomDefault + 1) + "] must between " +
"1 and " + MAX_NUMBER_OF_SHARDS + " [" + randomDefault + "]", e.getMessage());
"1 and " + MAX_NUMBER_OF_SHARDS + " [" + randomDefault + "]", cause.getMessage());
} finally {
System.clearProperty(MAX_NUMBER_OF_SHARDS);
System.clearProperty(DEFAULT_NUMBER_OF_SHARDS);
Expand Down

0 comments on commit 982e29f

Please sign in to comment.