Skip to content

Commit

Permalink
test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pgomulka committed Oct 30, 2018
1 parent 5ecdd85 commit a7f9368
Showing 1 changed file with 23 additions and 44 deletions.
Expand Up @@ -22,17 +22,12 @@
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.LatchedActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest;
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoResponse;
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeRequest;
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeTaskResponse;
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
Expand All @@ -41,45 +36,44 @@
import java.util.function.BooleanSupplier;

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

public class MigrationIT extends ESRestHighLevelClientTestCase {

public void testGetAssistance() throws IOException {
RestHighLevelClient client = highLevelClient();
{
IndexUpgradeInfoResponse response = client.migration()
IndexUpgradeInfoResponse response = highLevelClient().migration()
.getAssistance(new IndexUpgradeInfoRequest(), RequestOptions.DEFAULT);
assertEquals(0, response.getActions().size());
}
{
client.indices().create(new CreateIndexRequest("test"), RequestOptions.DEFAULT);
IndexUpgradeInfoResponse response = client.migration().getAssistance(
createIndex("test", Settings.EMPTY);
IndexUpgradeInfoResponse response = highLevelClient().migration().getAssistance(
new IndexUpgradeInfoRequest("test"), RequestOptions.DEFAULT);
assertEquals(0, response.getActions().size());
}
}

public void testGetAssistanceForWatchesIndex() throws IOException {
createWatch();
createIndex("test", Settings.EMPTY);

IndexUpgradeInfoResponse response = highLevelClient().migration()
.getAssistance(new IndexUpgradeInfoRequest(), RequestOptions.DEFAULT);
assertEquals(0, response.getActions().size());
}

public void testUpgrade() throws IOException {
createWatch();
createIndex("test", Settings.EMPTY);

// watcher is up to date
ElasticsearchStatusException responseException = expectThrows(ElasticsearchStatusException.class,
() -> highLevelClient().migration()
.upgrade(new IndexUpgradeRequest(".watches"), RequestOptions.DEFAULT));
.upgrade(new IndexUpgradeRequest("test"), RequestOptions.DEFAULT));

assertThat(responseException.getDetailedMessage(), containsString("cannot be upgraded"));
}

public void testSyncWaitingWithTaskApi() throws IOException, InterruptedException {
createWatch();
createIndex("test", Settings.EMPTY);

IndexUpgradeRequest request = new IndexUpgradeRequest(".watches").waitForCompletion(false);

IndexUpgradeTaskResponse upgrade = highLevelClient().migration()
Expand All @@ -91,21 +85,8 @@ public void testSyncWaitingWithTaskApi() throws IOException, InterruptedExceptio
awaitBusy(hasUpgradeCompleted);
}

private BooleanSupplier callUpgrade(IndexUpgradeTaskResponse upgrade) {
return () -> {
try {
Response response = client().performRequest(new Request("GET", "/_tasks/" + upgrade.getTask().toString()));
return (boolean) entityAsMap(response).get("completed");
} catch (IOException e) {
e.printStackTrace();
fail();
return false;
}
};
}

public void testUpgradeAsync() throws IOException, InterruptedException {
createWatch();
createIndex("test", Settings.EMPTY);

CountDownLatch latch = new CountDownLatch(1);

Expand All @@ -115,25 +96,23 @@ public void testUpgradeAsync() throws IOException, InterruptedException {
ActionListener<BulkByScrollResponse> listener = new LatchedActionListener<>(resultWrapper, latch);

highLevelClient().migration()
.upgradeAsync(new IndexUpgradeRequest(".watches"), RequestOptions.DEFAULT, listener);
.upgradeAsync(new IndexUpgradeRequest("test"), RequestOptions.DEFAULT, listener);

latch.await(3, TimeUnit.SECONDS);

assertThat(failReference.get().getMessage(), containsString("cannot be upgraded"));
}

private PutWatchResponse createWatch() throws IOException {
String json = "{ \n" +
" \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" +
" \"input\": { \"none\": {} },\n" +
" \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" +
"}";
BytesReference bytesReference = new BytesArray(json);
String watchId = randomAlphaOfLength(10);

PutWatchRequest putWatchRequest = new PutWatchRequest(watchId, bytesReference, XContentType.JSON);
PutWatchResponse putWatchResponse = highLevelClient().watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT);
assertThat(putWatchResponse.isCreated(), is(true));
return putWatchResponse;
private BooleanSupplier callUpgrade(IndexUpgradeTaskResponse upgrade) {
return () -> {
try {
Response response = client().performRequest(new Request("GET", "/_tasks/" + upgrade.getTask().toString()));
return (boolean) entityAsMap(response).get("completed");
} catch (IOException e) {
e.printStackTrace();
fail();
return false;
}
};
}
}

0 comments on commit a7f9368

Please sign in to comment.