Skip to content

Commit

Permalink
Fix toString() in SnapshotStatus (#26852)
Browse files Browse the repository at this point in the history
Closes #26851
  • Loading branch information
kel authored and cbuescher committed Oct 5, 2017
1 parent 24359c1 commit a978ddf
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
package org.elasticsearch.action.admin.cluster.snapshots.status;

import org.elasticsearch.cluster.SnapshotsInProgress.State;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.snapshots.Snapshot;

import java.io.IOException;
Expand Down Expand Up @@ -160,15 +159,7 @@ public static SnapshotStatus readSnapshotStatus(StreamInput in) throws IOExcepti

@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
builder.startObject();
toXContent(builder, EMPTY_PARAMS);
builder.endObject();
return builder.string();
} catch (IOException e) {
return "{ \"error\" : \"" + e.getMessage() + "\"}";
}
return Strings.toString(this, true, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package org.elasticsearch.action.admin.cluster.snapshots.status;

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/

import org.elasticsearch.cluster.SnapshotsInProgress;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.snapshots.Snapshot;
import org.elasticsearch.snapshots.SnapshotId;
import org.elasticsearch.test.ESTestCase;

import java.util.ArrayList;
import java.util.List;


public class SnapshotStatusTests extends ESTestCase {


public void testToString() throws Exception {
SnapshotsInProgress.State state = randomFrom(SnapshotsInProgress.State.values());
String uuid = UUIDs.randomBase64UUID();
SnapshotId id = new SnapshotId("test-snap", uuid);
Snapshot snapshot = new Snapshot("test-repo", id);

String indexName = randomAlphaOfLengthBetween(3, 50);
int shardId = randomInt();
ShardId testShardId = ShardId.fromString("[" + indexName + "][" + shardId + "]");
SnapshotIndexShardStage shardStage = randomFrom(SnapshotIndexShardStage.values());
SnapshotIndexShardStatus snapshotIndexShardStatus = new SnapshotIndexShardStatus(testShardId, shardStage);
List<SnapshotIndexShardStatus> snapshotIndexShardStatuses = new ArrayList<>();
snapshotIndexShardStatuses.add(snapshotIndexShardStatus);
SnapshotStatus status = new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses);

int initializingShards = 0;
int startedShards = 0;
int finalizingShards = 0;
int doneShards = 0;
int failedShards = 0;
int totalShards = 1;

switch (shardStage) {
case INIT:
initializingShards++;
break;
case STARTED:
startedShards++;
break;
case FINALIZE:
finalizingShards++;
break;
case DONE:
doneShards++;
break;
case FAILURE:
failedShards++;
break;
default:
break;
}

String expected = "{\n" +
" \"snapshot\" : \"test-snap\",\n" +
" \"repository\" : \"test-repo\",\n" +
" \"uuid\" : \"" + uuid + "\",\n" +
" \"state\" : \"" + state.toString() + "\",\n" +
" \"shards_stats\" : {\n" +
" \"initializing\" : " + initializingShards + ",\n" +
" \"started\" : " + startedShards + ",\n" +
" \"finalizing\" : " + finalizingShards + ",\n" +
" \"done\" : " + doneShards + ",\n" +
" \"failed\" : " + failedShards + ",\n" +
" \"total\" : " + totalShards + "\n" +
" },\n" +
" \"stats\" : {\n" +
" \"number_of_files\" : 0,\n" +
" \"processed_files\" : 0,\n" +
" \"total_size_in_bytes\" : 0,\n" +
" \"processed_size_in_bytes\" : 0,\n" +
" \"start_time_in_millis\" : 0,\n" +
" \"time_in_millis\" : 0\n" +
" },\n" +
" \"indices\" : {\n" +
" \"" + indexName + "\" : {\n" +
" \"shards_stats\" : {\n" +
" \"initializing\" : " + initializingShards + ",\n" +
" \"started\" : " + startedShards + ",\n" +
" \"finalizing\" : " + finalizingShards + ",\n" +
" \"done\" : " + doneShards + ",\n" +
" \"failed\" : " + failedShards + ",\n" +
" \"total\" : " + totalShards + "\n" +
" },\n" +
" \"stats\" : {\n" +
" \"number_of_files\" : 0,\n" +
" \"processed_files\" : 0,\n" +
" \"total_size_in_bytes\" : 0,\n" +
" \"processed_size_in_bytes\" : 0,\n" +
" \"start_time_in_millis\" : 0,\n" +
" \"time_in_millis\" : 0\n" +
" },\n" +
" \"shards\" : {\n" +
" \"" + shardId + "\" : {\n" +
" \"stage\" : \"" + shardStage.toString() + "\",\n" +
" \"stats\" : {\n" +
" \"number_of_files\" : 0,\n" +
" \"processed_files\" : 0,\n" +
" \"total_size_in_bytes\" : 0,\n" +
" \"processed_size_in_bytes\" : 0,\n" +
" \"start_time_in_millis\" : 0,\n" +
" \"time_in_millis\" : 0\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
assertEquals(expected, status.toString());
}
}

0 comments on commit a978ddf

Please sign in to comment.