|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.cluster.coordination; |
| 20 | + |
| 21 | +import joptsimple.OptionSet; |
| 22 | +import org.elasticsearch.ElasticsearchException; |
| 23 | +import org.elasticsearch.cli.MockTerminal; |
| 24 | +import org.elasticsearch.cli.UserException; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.env.Environment; |
| 27 | +import org.elasticsearch.env.TestEnvironment; |
| 28 | +import org.elasticsearch.test.ESIntegTestCase; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.containsString; |
| 31 | + |
| 32 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false) |
| 33 | +public class RemoveCustomsCommandIT extends ESIntegTestCase { |
| 34 | + |
| 35 | + public void testRemoveCustomsAbortedByUser() throws Exception { |
| 36 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 37 | + String node = internalCluster().startNode(); |
| 38 | + Settings dataPathSettings = internalCluster().dataPathSettings(node); |
| 39 | + ensureStableCluster(1); |
| 40 | + internalCluster().stopRandomDataNode(); |
| 41 | + |
| 42 | + Environment environment = TestEnvironment.newEnvironment( |
| 43 | + Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()); |
| 44 | + expectThrows(() -> removeCustoms(environment, true, new String[]{ "index-graveyard" }), |
| 45 | + ElasticsearchNodeCommand.ABORTED_BY_USER_MSG); |
| 46 | + } |
| 47 | + |
| 48 | + public void testRemoveCustomsSuccessful() throws Exception { |
| 49 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 50 | + String node = internalCluster().startNode(); |
| 51 | + createIndex("test"); |
| 52 | + client().admin().indices().prepareDelete("test").get(); |
| 53 | + assertEquals(1, client().admin().cluster().prepareState().get().getState().metaData().indexGraveyard().getTombstones().size()); |
| 54 | + Settings dataPathSettings = internalCluster().dataPathSettings(node); |
| 55 | + ensureStableCluster(1); |
| 56 | + internalCluster().stopRandomDataNode(); |
| 57 | + |
| 58 | + Environment environment = TestEnvironment.newEnvironment( |
| 59 | + Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()); |
| 60 | + MockTerminal terminal = removeCustoms(environment, false, |
| 61 | + randomBoolean() ? |
| 62 | + new String[]{ "index-graveyard" } : |
| 63 | + new String[]{ "index-*" } |
| 64 | + ); |
| 65 | + assertThat(terminal.getOutput(), containsString(RemoveCustomsCommand.CUSTOMS_REMOVED_MSG)); |
| 66 | + assertThat(terminal.getOutput(), containsString("The following customs will be removed:")); |
| 67 | + assertThat(terminal.getOutput(), containsString("index-graveyard")); |
| 68 | + |
| 69 | + internalCluster().startNode(dataPathSettings); |
| 70 | + assertEquals(0, client().admin().cluster().prepareState().get().getState().metaData().indexGraveyard().getTombstones().size()); |
| 71 | + } |
| 72 | + |
| 73 | + public void testCustomDoesNotMatch() throws Exception { |
| 74 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 75 | + String node = internalCluster().startNode(); |
| 76 | + createIndex("test"); |
| 77 | + client().admin().indices().prepareDelete("test").get(); |
| 78 | + assertEquals(1, client().admin().cluster().prepareState().get().getState().metaData().indexGraveyard().getTombstones().size()); |
| 79 | + Settings dataPathSettings = internalCluster().dataPathSettings(node); |
| 80 | + ensureStableCluster(1); |
| 81 | + internalCluster().stopRandomDataNode(); |
| 82 | + |
| 83 | + Environment environment = TestEnvironment.newEnvironment( |
| 84 | + Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()); |
| 85 | + UserException ex = expectThrows(UserException.class, () -> removeCustoms(environment, false, |
| 86 | + new String[]{ "index-greveyard-with-typos" })); |
| 87 | + assertThat(ex.getMessage(), containsString("No custom metadata matching [index-greveyard-with-typos] were " + |
| 88 | + "found on this node")); |
| 89 | + } |
| 90 | + |
| 91 | + private MockTerminal executeCommand(ElasticsearchNodeCommand command, Environment environment, boolean abort, String... args) |
| 92 | + throws Exception { |
| 93 | + final MockTerminal terminal = new MockTerminal(); |
| 94 | + final OptionSet options = command.getParser().parse(args); |
| 95 | + final String input; |
| 96 | + |
| 97 | + if (abort) { |
| 98 | + input = randomValueOtherThanMany(c -> c.equalsIgnoreCase("y"), () -> randomAlphaOfLength(1)); |
| 99 | + } else { |
| 100 | + input = randomBoolean() ? "y" : "Y"; |
| 101 | + } |
| 102 | + |
| 103 | + terminal.addTextInput(input); |
| 104 | + |
| 105 | + try { |
| 106 | + command.execute(terminal, options, environment); |
| 107 | + } finally { |
| 108 | + assertThat(terminal.getOutput(), containsString(ElasticsearchNodeCommand.STOP_WARNING_MSG)); |
| 109 | + } |
| 110 | + |
| 111 | + return terminal; |
| 112 | + } |
| 113 | + |
| 114 | + private MockTerminal removeCustoms(Environment environment, boolean abort, String... args) throws Exception { |
| 115 | + final MockTerminal terminal = executeCommand(new RemoveCustomsCommand(), environment, abort, args); |
| 116 | + assertThat(terminal.getOutput(), containsString(RemoveCustomsCommand.CONFIRMATION_MSG)); |
| 117 | + assertThat(terminal.getOutput(), containsString(RemoveCustomsCommand.CUSTOMS_REMOVED_MSG)); |
| 118 | + return terminal; |
| 119 | + } |
| 120 | + |
| 121 | + private void expectThrows(ThrowingRunnable runnable, String message) { |
| 122 | + ElasticsearchException ex = expectThrows(ElasticsearchException.class, runnable); |
| 123 | + assertThat(ex.getMessage(), containsString(message)); |
| 124 | + } |
| 125 | +} |
0 commit comments