use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class PeerFinderMessagesTests method testPeersResponseEqualsHashCodeSerialization.
public void testPeersResponseEqualsHashCodeSerialization() {
final long initialTerm = randomNonNegativeLong();
final PeersResponse initialPeersResponse;
if (randomBoolean()) {
initialPeersResponse = new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), initialTerm);
} else {
initialPeersResponse = new PeersResponse(Optional.empty(), Arrays.stream(generateRandomStringArray(10, 10, false, false)).map(this::createNode).collect(Collectors.toList()), initialTerm);
}
// Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialPeersResponse, (CopyFunction<PeersResponse>) publishResponse -> copyWriteable(publishResponse, writableRegistry(), PeersResponse::new), in -> {
final long term = in.getTerm();
if (randomBoolean()) {
return new PeersResponse(in.getMasterNode(), in.getKnownPeers(), randomValueOtherThan(term, OpenSearchTestCase::randomNonNegativeLong));
} else {
if (in.getMasterNode().isPresent()) {
if (randomBoolean()) {
return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), in.getKnownPeers(), term);
} else {
return new PeersResponse(Optional.empty(), singletonList(createNode(randomAlphaOfLength(10))), term);
}
} else {
if (randomBoolean()) {
return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), term);
} else {
return new PeersResponse(in.getMasterNode(), modifyDiscoveryNodesList(in.getKnownPeers(), false), term);
}
}
}
});
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class GatewayMetaStatePersistedStateTests method testSetLastAcceptedStateTermChanged.
public void testSetLastAcceptedStateTermChanged() throws IOException {
CoordinationState.PersistedState gateway = null;
try {
gateway = newGatewayPersistedState();
final String indexName = randomAlphaOfLength(10);
final int numberOfShards = randomIntBetween(1, 5);
final long version = randomNonNegativeLong();
final long term = randomValueOtherThan(Long.MAX_VALUE, OpenSearchTestCase::randomNonNegativeLong);
final IndexMetadata indexMetadata = createIndexMetadata(indexName, numberOfShards, version);
final ClusterState state = createClusterState(randomNonNegativeLong(), Metadata.builder().coordinationMetadata(createCoordinationMetadata(term)).put(indexMetadata, false).build());
gateway.setLastAcceptedState(state);
gateway = maybeNew(gateway);
final long newTerm = randomLongBetween(term + 1, Long.MAX_VALUE);
final int newNumberOfShards = randomValueOtherThan(numberOfShards, () -> randomIntBetween(1, 5));
final IndexMetadata newIndexMetadata = createIndexMetadata(indexName, newNumberOfShards, version);
final ClusterState newClusterState = createClusterState(randomNonNegativeLong(), Metadata.builder().coordinationMetadata(createCoordinationMetadata(newTerm)).put(newIndexMetadata, false).build());
gateway.setLastAcceptedState(newClusterState);
gateway = maybeNew(gateway);
assertThat(gateway.getLastAcceptedState().metadata().index(indexName), equalTo(newIndexMetadata));
} finally {
IOUtils.close(gateway);
}
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class InstallPluginCommandTests method testFailedSignatureVerification.
public void testFailedSignatureVerification() throws Exception {
final String icu = "analysis-icu";
final String url = "https://artifacts.opensearch.org/releases/plugins/analysis-icu/" + Version.CURRENT + "/" + icu + "-" + Build.CURRENT.getQualifiedVersion() + ".zip";
final MessageDigest digest = MessageDigest.getInstance("SHA-512");
/*
* To setup a situation where signature verification fails, we will mutate the input byte array by modifying a single byte to some
* random byte value other than the actual value. This is enough to change the signature and cause verification to intentionally
* fail.
*/
final BiFunction<byte[], PGPSecretKey, String> signature = (b, p) -> {
final byte[] bytes = Arrays.copyOf(b, b.length);
bytes[0] = randomValueOtherThan(b[0], OpenSearchTestCase::randomByte);
return signature(bytes, p);
};
final IllegalStateException e = expectThrows(IllegalStateException.class, () -> assertInstallPluginFromUrl(icu, icu, url, null, false, ".sha512", checksumAndFilename(digest, url), newSecretKey(), signature));
assertThat(e, hasToString(equalTo("java.lang.IllegalStateException: signature verification for [" + url + "] failed")));
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class UpdateRequestTests method runTimeoutTest.
private void runTimeoutTest(final GetResult getResult, final UpdateRequest updateRequest) {
final UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "", 0), updateRequest, getResult, OpenSearchTestCase::randomNonNegativeLong);
final Writeable action = result.action();
assertThat(action, instanceOf(ReplicationRequest.class));
final ReplicationRequest<?> request = (ReplicationRequest<?>) action;
assertThat(request.timeout(), equalTo(updateRequest.timeout()));
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class RecoverySourceHandlerTests method generateOperations.
private static List<Translog.Operation> generateOperations(int numOps) {
final List<Translog.Operation> operations = new ArrayList<>(numOps);
final byte[] source = "{}".getBytes(StandardCharsets.UTF_8);
final Set<Long> seqNos = new HashSet<>();
for (int i = 0; i < numOps; i++) {
final long seqNo = randomValueOtherThanMany(n -> seqNos.add(n) == false, OpenSearchTestCase::randomNonNegativeLong);
final Translog.Operation op;
if (randomBoolean()) {
op = new Translog.Index("_doc", "id", seqNo, randomNonNegativeLong(), randomNonNegativeLong(), source, null, -1);
} else if (randomBoolean()) {
op = new Translog.Delete("_doc", "id", new Term("_id", Uid.encodeId("id")), seqNo, randomNonNegativeLong(), randomNonNegativeLong());
} else {
op = new Translog.NoOp(seqNo, randomNonNegativeLong(), "test");
}
operations.add(op);
}
return operations;
}
Aggregations