Skip to content

Commit ad9cdeb

Browse files
committed
Lift some common test code to shared parent class§
1 parent 6cf016b commit ad9cdeb

File tree

3 files changed

+114
-169
lines changed

3 files changed

+114
-169
lines changed

qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeDeprecatedSettingsIT.java

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,7 @@
2525
import java.io.IOException;
2626
import java.util.List;
2727

28-
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK;
29-
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_WRITE_BLOCK;
30-
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK;
31-
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING;
32-
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING;
33-
import static org.hamcrest.Matchers.allOf;
34-
import static org.hamcrest.Matchers.contains;
35-
import static org.hamcrest.Matchers.either;
3628
import static org.hamcrest.Matchers.equalTo;
37-
import static org.hamcrest.Matchers.hasItem;
38-
import static org.hamcrest.Matchers.is;
39-
import static org.hamcrest.Matchers.not;
4029

4130
public class RollingUpgradeDeprecatedSettingsIT extends RollingUpgradeIndexCompatibilityTestCase {
4231

@@ -88,88 +77,14 @@ public void testIndexUpgrade() throws Exception {
8877
closeIndex(index);
8978
}
9079

91-
final var randomBlocks = randomFrom(
92-
List.of(IndexMetadata.APIBlock.WRITE, IndexMetadata.APIBlock.READ_ONLY),
93-
List.of(IndexMetadata.APIBlock.READ_ONLY),
94-
List.of(IndexMetadata.APIBlock.WRITE)
95-
);
96-
for (var randomBlock : randomBlocks) {
97-
addIndexBlock(index, randomBlock);
98-
assertThat(indexBlocks(index), hasItem(randomBlock.getBlock()));
99-
}
100-
101-
assertThat(indexBlocks(index), maybeClose ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK)));
102-
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(maybeClose));
103-
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
80+
addAndAssertIndexBlocks(index, maybeClose);
10481
return;
10582
}
10683

10784
if (nodesVersions().values().stream().anyMatch(v -> v.onOrAfter(VERSION_CURRENT))) {
10885
final var isClosed = isIndexClosed(index);
109-
logger.debug("--> upgraded index [{}] is now in [{}] state", index, isClosed ? "closed" : "open");
110-
assertThat(
111-
indexBlocks(index),
112-
allOf(
113-
either(hasItem(INDEX_READ_ONLY_BLOCK)).or(hasItem(INDEX_WRITE_BLOCK)),
114-
isClosed ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK))
115-
)
116-
);
117-
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed));
118-
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
119-
120-
var blocks = indexBlocks(index).stream().filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)).toList();
121-
if (blocks.size() == 2) {
122-
switch (randomInt(2)) {
123-
case 0:
124-
updateIndexSettings(
125-
index,
126-
Settings.builder()
127-
.putNull(IndexMetadata.APIBlock.WRITE.settingName())
128-
.put(IndexMetadata.APIBlock.READ_ONLY.settingName(), true)
129-
);
130-
assertThat(
131-
indexBlocks(index),
132-
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_READ_ONLY_BLOCK) : contains(INDEX_READ_ONLY_BLOCK)
133-
);
134-
break;
135-
case 1:
136-
updateIndexSettings(
137-
index,
138-
Settings.builder()
139-
.putNull(IndexMetadata.APIBlock.READ_ONLY.settingName())
140-
.put(IndexMetadata.APIBlock.WRITE.settingName(), true)
141-
);
142-
assertThat(
143-
indexBlocks(index),
144-
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)
145-
);
146-
break;
147-
case 2:
148-
updateIndexSettings(index, Settings.builder().put(IndexMetadata.APIBlock.READ_ONLY.settingName(), false));
149-
assertThat(
150-
indexBlocks(index),
151-
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)
152-
);
153-
break;
154-
default:
155-
throw new AssertionError();
156-
}
157-
}
158-
159-
blocks = indexBlocks(index).stream().filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)).toList();
160-
if (blocks.contains(INDEX_READ_ONLY_BLOCK)) {
161-
logger.debug("--> read_only API block can be replaced by a write block (required for the remaining tests)");
162-
updateIndexSettings(
163-
index,
164-
Settings.builder()
165-
.putNull(IndexMetadata.APIBlock.READ_ONLY.settingName())
166-
.put(IndexMetadata.APIBlock.WRITE.settingName(), true)
167-
);
168-
}
169-
170-
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
171-
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed));
172-
assertThat(indexBlocks(index), isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK));
86+
assertAndModifyIndexBlocks(index, isClosed);
87+
ensureWriteBlock(index, isClosed);
17388

17489
if (isClosed) {
17590
logger.debug("--> re-opening index [{}] after upgrade", index);

qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeIndexCompatibilityTestCase.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,28 @@
1414
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
1515
import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering;
1616

17+
import org.elasticsearch.cluster.metadata.IndexMetadata;
18+
import org.elasticsearch.common.settings.Settings;
1719
import org.elasticsearch.test.cluster.util.Version;
1820

1921
import java.util.Comparator;
2022
import java.util.List;
2123
import java.util.stream.Stream;
2224

25+
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK;
26+
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_WRITE_BLOCK;
27+
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK;
28+
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING;
29+
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING;
2330
import static org.elasticsearch.test.cluster.util.Version.CURRENT;
31+
import static org.hamcrest.Matchers.allOf;
32+
import static org.hamcrest.Matchers.contains;
33+
import static org.hamcrest.Matchers.either;
2434
import static org.hamcrest.Matchers.equalTo;
35+
import static org.hamcrest.Matchers.hasItem;
2536
import static org.hamcrest.Matchers.hasSize;
37+
import static org.hamcrest.Matchers.is;
38+
import static org.hamcrest.Matchers.not;
2639
import static org.hamcrest.Matchers.notNullValue;
2740

2841
/**
@@ -86,6 +99,101 @@ protected void maybeUpgrade() throws Exception {
8699
}
87100
}
88101

102+
protected void addAndAssertIndexBlocks(String index, boolean maybeClose) throws Exception {
103+
final var randomBlocks = randomFrom(
104+
List.of(IndexMetadata.APIBlock.WRITE, IndexMetadata.APIBlock.READ_ONLY),
105+
List.of(IndexMetadata.APIBlock.READ_ONLY),
106+
List.of(IndexMetadata.APIBlock.WRITE)
107+
);
108+
for (var randomBlock : randomBlocks) {
109+
addIndexBlock(index, randomBlock);
110+
assertThat(indexBlocks(index), hasItem(randomBlock.getBlock()));
111+
}
112+
113+
assertThat(indexBlocks(index), maybeClose ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK)));
114+
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(maybeClose));
115+
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
116+
return;
117+
}
118+
119+
/**
120+
* assert that index has either a read-only block or write block, if index is closed also a cloded-block.
121+
* In case both blocks are present, randomly modify one of them.
122+
*/
123+
protected void assertAndModifyIndexBlocks(String index, boolean isClosed) throws Exception {
124+
logger.debug("--> upgraded index [{}] is now in [{}] state", index, isClosed ? "closed" : "open");
125+
assertThat(
126+
indexBlocks(index),
127+
allOf(
128+
either(hasItem(INDEX_READ_ONLY_BLOCK)).or(hasItem(INDEX_WRITE_BLOCK)),
129+
isClosed ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK))
130+
)
131+
);
132+
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed));
133+
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
134+
135+
var blocks = indexBlocks(index).stream().filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)).toList();
136+
if (blocks.size() == 2) {
137+
switch (randomInt(2)) {
138+
case 0:
139+
updateIndexSettings(
140+
index,
141+
Settings.builder()
142+
.putNull(IndexMetadata.APIBlock.WRITE.settingName())
143+
.put(IndexMetadata.APIBlock.READ_ONLY.settingName(), true)
144+
);
145+
assertThat(
146+
indexBlocks(index),
147+
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_READ_ONLY_BLOCK) : contains(INDEX_READ_ONLY_BLOCK)
148+
);
149+
break;
150+
case 1:
151+
updateIndexSettings(
152+
index,
153+
Settings.builder()
154+
.putNull(IndexMetadata.APIBlock.READ_ONLY.settingName())
155+
.put(IndexMetadata.APIBlock.WRITE.settingName(), true)
156+
);
157+
assertThat(
158+
indexBlocks(index),
159+
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)
160+
);
161+
break;
162+
case 2:
163+
updateIndexSettings(index, Settings.builder().put(IndexMetadata.APIBlock.READ_ONLY.settingName(), false));
164+
assertThat(
165+
indexBlocks(index),
166+
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)
167+
);
168+
break;
169+
default:
170+
throw new AssertionError();
171+
}
172+
}
173+
}
174+
175+
/**
176+
* ensure we have a write-block. If this is currently not the case, set it.
177+
*/
178+
protected void ensureWriteBlock(String index, boolean isClosed) throws Exception {
179+
List<org.elasticsearch.cluster.block.ClusterBlock> blocks = indexBlocks(index).stream()
180+
.filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK))
181+
.toList();
182+
if (blocks.contains(INDEX_READ_ONLY_BLOCK)) {
183+
logger.debug("--> read_only API block can be replaced by a write block (required for the remaining tests)");
184+
updateIndexSettings(
185+
index,
186+
Settings.builder()
187+
.putNull(IndexMetadata.APIBlock.READ_ONLY.settingName())
188+
.put(IndexMetadata.APIBlock.WRITE.settingName(), true)
189+
);
190+
}
191+
192+
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
193+
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed));
194+
assertThat(indexBlocks(index), isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK));
195+
}
196+
89197
/**
90198
* Execute the test suite with the parameters provided by the {@link #parameters()} in nodes versions order.
91199
*/

qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeLuceneIndexCompatibilityTestCase.java

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@
1818

1919
import java.util.List;
2020

21-
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK;
2221
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_WRITE_BLOCK;
2322
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK;
2423
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING;
2524
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING;
26-
import static org.hamcrest.Matchers.allOf;
2725
import static org.hamcrest.Matchers.contains;
2826
import static org.hamcrest.Matchers.containsString;
29-
import static org.hamcrest.Matchers.either;
3027
import static org.hamcrest.Matchers.equalTo;
31-
import static org.hamcrest.Matchers.hasItem;
3228
import static org.hamcrest.Matchers.is;
33-
import static org.hamcrest.Matchers.not;
3429

3530
public class RollingUpgradeLuceneIndexCompatibilityTestCase extends RollingUpgradeIndexCompatibilityTestCase {
3631

@@ -73,88 +68,15 @@ public void testIndexUpgrade() throws Exception {
7368
closeIndex(index);
7469
}
7570

76-
final var randomBlocks = randomFrom(
77-
List.of(IndexMetadata.APIBlock.WRITE, IndexMetadata.APIBlock.READ_ONLY),
78-
List.of(IndexMetadata.APIBlock.READ_ONLY),
79-
List.of(IndexMetadata.APIBlock.WRITE)
80-
);
81-
for (var randomBlock : randomBlocks) {
82-
addIndexBlock(index, randomBlock);
83-
assertThat(indexBlocks(index), hasItem(randomBlock.getBlock()));
84-
}
85-
86-
assertThat(indexBlocks(index), maybeClose ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK)));
87-
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(maybeClose));
88-
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
71+
addAndAssertIndexBlocks(index, maybeClose);
8972
return;
9073
}
9174

9275
if (nodesVersions().values().stream().anyMatch(v -> v.onOrAfter(VERSION_CURRENT))) {
9376
final var isClosed = isIndexClosed(index);
94-
logger.debug("--> upgraded index [{}] is now in [{}] state", index, isClosed ? "closed" : "open");
95-
assertThat(
96-
indexBlocks(index),
97-
allOf(
98-
either(hasItem(INDEX_READ_ONLY_BLOCK)).or(hasItem(INDEX_WRITE_BLOCK)),
99-
isClosed ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK))
100-
)
101-
);
102-
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed));
103-
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
77+
assertAndModifyIndexBlocks(index, isClosed);
10478

105-
var blocks = indexBlocks(index).stream().filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)).toList();
106-
if (blocks.size() == 2) {
107-
switch (randomInt(2)) {
108-
case 0:
109-
updateIndexSettings(
110-
index,
111-
Settings.builder()
112-
.putNull(IndexMetadata.APIBlock.WRITE.settingName())
113-
.put(IndexMetadata.APIBlock.READ_ONLY.settingName(), true)
114-
);
115-
assertThat(
116-
indexBlocks(index),
117-
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_READ_ONLY_BLOCK) : contains(INDEX_READ_ONLY_BLOCK)
118-
);
119-
break;
120-
case 1:
121-
updateIndexSettings(
122-
index,
123-
Settings.builder()
124-
.putNull(IndexMetadata.APIBlock.READ_ONLY.settingName())
125-
.put(IndexMetadata.APIBlock.WRITE.settingName(), true)
126-
);
127-
assertThat(
128-
indexBlocks(index),
129-
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)
130-
);
131-
break;
132-
case 2:
133-
updateIndexSettings(index, Settings.builder().put(IndexMetadata.APIBlock.READ_ONLY.settingName(), false));
134-
assertThat(
135-
indexBlocks(index),
136-
isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)
137-
);
138-
break;
139-
default:
140-
throw new AssertionError();
141-
}
142-
}
143-
144-
blocks = indexBlocks(index).stream().filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)).toList();
145-
if (blocks.contains(INDEX_READ_ONLY_BLOCK)) {
146-
logger.debug("--> read_only API block can be replaced by a write block (required for the remaining tests)");
147-
updateIndexSettings(
148-
index,
149-
Settings.builder()
150-
.putNull(IndexMetadata.APIBlock.READ_ONLY.settingName())
151-
.put(IndexMetadata.APIBlock.WRITE.settingName(), true)
152-
);
153-
}
154-
155-
assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true));
156-
assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed));
157-
assertThat(indexBlocks(index), isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK));
79+
ensureWriteBlock(index, isClosed);
15880

15981
if (isClosed) {
16082
logger.debug("--> re-opening index [{}] after upgrade", index);

0 commit comments

Comments
 (0)