35
35
36
36
public class OldMappingsIT extends ESRestTestCase {
37
37
38
+ private static final List <String > indices = Arrays .asList ("filebeat" , "custom" , "nested" );
39
+
38
40
public static TemporaryFolder repoDirectory = new TemporaryFolder ();
39
41
40
42
public static ElasticsearchCluster currentCluster = ElasticsearchCluster .local ()
@@ -57,10 +59,11 @@ public class OldMappingsIT extends ESRestTestCase {
57
59
58
60
@ ClassRule
59
61
public static TestRule ruleChain = RuleChain .outerRule (repoDirectory ).around (oldCluster ).around (currentCluster );
62
+ private static boolean repoSetup = false ;
60
63
61
64
@ Override
62
65
protected String getTestRestCluster () {
63
- return oldCluster .getHttpAddresses ();
66
+ return currentCluster .getHttpAddresses ();
64
67
}
65
68
66
69
private Request createIndex (String indexName , String file ) throws IOException {
@@ -71,9 +74,10 @@ private Request createIndex(String indexName, String file) throws IOException {
71
74
.startObject ()
72
75
.startObject ("settings" )
73
76
.field ("index.number_of_shards" , numberOfShards )
77
+ .field ("index.number_of_replicas" , 0 )
74
78
.endObject ()
75
79
.startObject ("mappings" );
76
- builder .rawValue (getClass () .getResourceAsStream (file ), XContentType .JSON );
80
+ builder .rawValue (OldMappingsIT . class .getResourceAsStream (file ), XContentType .JSON );
77
81
builder .endObject ().endObject ();
78
82
79
83
createIndex .setJsonEntity (Strings .toString (builder ));
@@ -82,106 +86,108 @@ private Request createIndex(String indexName, String file) throws IOException {
82
86
83
87
@ Before
84
88
public void setupIndex () throws IOException {
85
- // String repoLocation = PathUtils.get(System.getProperty("tests.repo.location"))
86
- // .resolve(RandomizedTest.getContext().getTargetClass().getName())
87
- // .toString();
88
-
89
- String repoLocation = repoDirectory .getRoot ().getPath ();
90
-
91
- String repoName = "old_mappings_repo" ;
92
- String snapshotName = "snap" ;
93
- List <String > indices ;
94
- indices = Arrays .asList ("filebeat" , "custom" , "nested" );
95
-
96
- List <HttpHost > oldClusterHosts = parseClusterHosts (oldCluster .getHttpAddresses ());
97
- List <HttpHost > clusterHosts = parseClusterHosts (currentCluster .getHttpAddresses ());
98
- // try (RestClient oldEsClient = client()) {
99
- try (
100
- RestClient oldEsClient = RestClient .builder (oldClusterHosts .toArray (new HttpHost [oldClusterHosts .size ()])).build ();
101
- RestClient esClient = RestClient .builder (clusterHosts .toArray (new HttpHost [clusterHosts .size ()])).build ();
102
- ) {
103
- assertOK (oldEsClient .performRequest (createIndex ("filebeat" , "filebeat.json" )));
104
-
105
- assertOK (oldEsClient .performRequest (createIndex ("custom" , "custom.json" )));
106
- assertOK (oldEsClient .performRequest (createIndex ("nested" , "nested.json" )));
107
-
108
- Request doc1 = new Request ("PUT" , "/" + "custom" + "/" + "_doc" + "/" + "1" );
109
- doc1 .addParameter ("refresh" , "true" );
110
- XContentBuilder bodyDoc1 = XContentFactory .jsonBuilder ()
111
- .startObject ()
112
- .startObject ("apache2" )
113
- .startObject ("access" )
114
- .field ("url" , "myurl1" )
115
- .field ("agent" , "agent1" )
116
- .endObject ()
117
- .endObject ()
118
- .endObject ();
119
- doc1 .setJsonEntity (Strings .toString (bodyDoc1 ));
120
- assertOK (oldEsClient .performRequest (doc1 ));
121
-
122
- Request doc2 = new Request ("PUT" , "/" + "custom" + "/" + "_doc" + "/" + "2" );
123
- doc2 .addParameter ("refresh" , "true" );
124
- XContentBuilder bodyDoc2 = XContentFactory .jsonBuilder ()
125
- .startObject ()
126
- .startObject ("apache2" )
127
- .startObject ("access" )
128
- .field ("url" , "myurl2" )
129
- .field ("agent" , "agent2 agent2" )
130
- .endObject ()
131
- .endObject ()
132
- .field ("completion" , "some_value" )
133
- .endObject ();
134
- doc2 .setJsonEntity (Strings .toString (bodyDoc2 ));
135
- assertOK (oldEsClient .performRequest (doc2 ));
136
-
137
- Request doc3 = new Request ("PUT" , "/" + "nested" + "/" + "_doc" + "/" + "1" );
138
- doc3 .addParameter ("refresh" , "true" );
139
- XContentBuilder bodyDoc3 = XContentFactory .jsonBuilder ()
140
- .startObject ()
141
- .field ("group" , "fans" )
142
- .startArray ("user" )
143
- .startObject ()
144
- .field ("first" , "John" )
145
- .field ("last" , "Smith" )
146
- .endObject ()
147
- .startObject ()
148
- .field ("first" , "Alice" )
149
- .field ("last" , "White" )
150
- .endObject ()
151
- .endArray ()
152
- .endObject ();
153
- doc3 .setJsonEntity (Strings .toString (bodyDoc3 ));
154
- assertOK (oldEsClient .performRequest (doc3 ));
155
-
156
- Request getSettingsRequest = new Request ("GET" , "/_cluster/settings?include_defaults=true" );
157
- Map <String , Object > response = entityAsMap (oldEsClient .performRequest (getSettingsRequest ));
158
- assertEquals (repoLocation , ((List <?>) (XContentMapValues .extractValue ("defaults.path.repo" , response ))).get (0 ));
159
-
160
- // register repo on old ES and take snapshot
161
- Request createRepoRequest = new Request ("PUT" , "/_snapshot/" + repoName );
162
- createRepoRequest .setJsonEntity (Strings .format ("""
163
- {"type":"fs","settings":{"location":"%s"}}
164
- """ , repoLocation ));
165
- assertOK (oldEsClient .performRequest (createRepoRequest ));
166
-
167
- Request createSnapshotRequest = new Request ("PUT" , "/_snapshot/" + repoName + "/" + snapshotName );
168
- createSnapshotRequest .addParameter ("wait_for_completion" , "true" );
169
- createSnapshotRequest .setJsonEntity ("{\" indices\" :\" " + indices .stream ().collect (Collectors .joining ("," )) + "\" }" );
170
- assertOK (oldEsClient .performRequest (createSnapshotRequest ));
171
- // }
172
-
173
- // register repo on new ES and restore snapshot
174
- Request createRepoRequest2 = new Request ("PUT" , "/_snapshot/" + repoName );
175
- createRepoRequest2 .setJsonEntity (Strings .format ("""
176
- {"type":"fs","settings":{"location":"%s"}}
177
- """ , repoLocation ));
178
- assertOK (esClient .performRequest (createRepoRequest2 ));
179
-
180
- final Request createRestoreRequest = new Request ("POST" , "/_snapshot/" + repoName + "/" + snapshotName + "/_restore" );
181
- createRestoreRequest .addParameter ("wait_for_completion" , "true" );
182
- createRestoreRequest .setJsonEntity ("{\" indices\" :\" " + indices .stream ().collect (Collectors .joining ("," )) + "\" }" );
183
- createRestoreRequest .setOptions (RequestOptions .DEFAULT .toBuilder ().setWarningsHandler (WarningsHandler .PERMISSIVE ));
184
- assertOK (esClient .performRequest (createRestoreRequest ));
89
+ if (repoSetup == false ) {
90
+ // String repoLocation = PathUtils.get(System.getProperty("tests.repo.location"))
91
+ // .resolve(RandomizedTest.getContext().getTargetClass().getName())
92
+ // .toString();
93
+
94
+ String repoLocation = repoDirectory .getRoot ().getPath ();
95
+
96
+ String repoName = "old_mappings_repo" ;
97
+ String snapshotName = "snap" ;
98
+
99
+ List <HttpHost > oldClusterHosts = parseClusterHosts (oldCluster .getHttpAddresses ());
100
+ List <HttpHost > clusterHosts = parseClusterHosts (currentCluster .getHttpAddresses ());
101
+ // try (RestClient oldEsClient = client()) {
102
+ try (
103
+ RestClient oldEsClient = RestClient .builder (oldClusterHosts .toArray (new HttpHost [oldClusterHosts .size ()])).build ();
104
+ RestClient esClient = RestClient .builder (clusterHosts .toArray (new HttpHost [clusterHosts .size ()])).build ();
105
+ ) {
106
+ assertOK (oldEsClient .performRequest (createIndex ("filebeat" , "filebeat.json" )));
107
+
108
+ assertOK (oldEsClient .performRequest (createIndex ("custom" , "custom.json" )));
109
+ assertOK (oldEsClient .performRequest (createIndex ("nested" , "nested.json" )));
110
+
111
+ Request doc1 = new Request ("PUT" , "/" + "custom" + "/" + "_doc" + "/" + "1" );
112
+ doc1 .addParameter ("refresh" , "true" );
113
+ XContentBuilder bodyDoc1 = XContentFactory .jsonBuilder ()
114
+ .startObject ()
115
+ .startObject ("apache2" )
116
+ .startObject ("access" )
117
+ .field ("url" , "myurl1" )
118
+ .field ("agent" , "agent1" )
119
+ .endObject ()
120
+ .endObject ()
121
+ .endObject ();
122
+ doc1 .setJsonEntity (Strings .toString (bodyDoc1 ));
123
+ assertOK (oldEsClient .performRequest (doc1 ));
124
+
125
+ Request doc2 = new Request ("PUT" , "/" + "custom" + "/" + "_doc" + "/" + "2" );
126
+ doc2 .addParameter ("refresh" , "true" );
127
+ XContentBuilder bodyDoc2 = XContentFactory .jsonBuilder ()
128
+ .startObject ()
129
+ .startObject ("apache2" )
130
+ .startObject ("access" )
131
+ .field ("url" , "myurl2" )
132
+ .field ("agent" , "agent2 agent2" )
133
+ .endObject ()
134
+ .endObject ()
135
+ .field ("completion" , "some_value" )
136
+ .endObject ();
137
+ doc2 .setJsonEntity (Strings .toString (bodyDoc2 ));
138
+ assertOK (oldEsClient .performRequest (doc2 ));
139
+
140
+ Request doc3 = new Request ("PUT" , "/" + "nested" + "/" + "_doc" + "/" + "1" );
141
+ doc3 .addParameter ("refresh" , "true" );
142
+ XContentBuilder bodyDoc3 = XContentFactory .jsonBuilder ()
143
+ .startObject ()
144
+ .field ("group" , "fans" )
145
+ .startArray ("user" )
146
+ .startObject ()
147
+ .field ("first" , "John" )
148
+ .field ("last" , "Smith" )
149
+ .endObject ()
150
+ .startObject ()
151
+ .field ("first" , "Alice" )
152
+ .field ("last" , "White" )
153
+ .endObject ()
154
+ .endArray ()
155
+ .endObject ();
156
+ doc3 .setJsonEntity (Strings .toString (bodyDoc3 ));
157
+ assertOK (oldEsClient .performRequest (doc3 ));
158
+
159
+ Request getSettingsRequest = new Request ("GET" , "/_cluster/settings?include_defaults=true" );
160
+ Map <String , Object > response = entityAsMap (oldEsClient .performRequest (getSettingsRequest ));
161
+ assertEquals (repoLocation , ((List <?>) (XContentMapValues .extractValue ("defaults.path.repo" , response ))).get (0 ));
162
+
163
+ // register repo on old ES and take snapshot
164
+ Request createRepoRequest = new Request ("PUT" , "/_snapshot/" + repoName );
165
+ createRepoRequest .setJsonEntity (Strings .format ("""
166
+ {"type":"fs","settings":{"location":"%s"}}
167
+ """ , repoLocation ));
168
+ assertOK (oldEsClient .performRequest (createRepoRequest ));
169
+
170
+ Request createSnapshotRequest = new Request ("PUT" , "/_snapshot/" + repoName + "/" + snapshotName );
171
+ createSnapshotRequest .addParameter ("wait_for_completion" , "true" );
172
+ createSnapshotRequest .setJsonEntity ("{\" indices\" :\" " + indices .stream ().collect (Collectors .joining ("," )) + "\" }" );
173
+ assertOK (oldEsClient .performRequest (createSnapshotRequest ));
174
+ // }
175
+
176
+ // register repo on new ES and restore snapshot
177
+ Request createRepoRequest2 = new Request ("PUT" , "/_snapshot/" + repoName );
178
+ createRepoRequest2 .setJsonEntity (Strings .format ("""
179
+ {"type":"fs","settings":{"location":"%s"}}
180
+ """ , repoLocation ));
181
+ assertOK (esClient .performRequest (createRepoRequest2 ));
182
+
183
+ final Request createRestoreRequest = new Request ("POST" , "/_snapshot/" + repoName + "/" + snapshotName + "/_restore" );
184
+ createRestoreRequest .addParameter ("wait_for_completion" , "true" );
185
+ createRestoreRequest .setJsonEntity ("{\" indices\" :\" " + indices .stream ().collect (Collectors .joining ("," )) + "\" }" );
186
+ createRestoreRequest .setOptions (RequestOptions .DEFAULT .toBuilder ().setWarningsHandler (WarningsHandler .PERMISSIVE ));
187
+ assertOK (esClient .performRequest (createRestoreRequest ));
188
+
189
+ repoSetup = true ;
190
+ }
185
191
}
186
192
}
187
193
@@ -191,4 +197,33 @@ public void testMappingOk() throws IOException {
191
197
// assertNotNull(XContentMapValues.extractValue(mapping, "filebeat", "mappings", "properties", "apache2"));
192
198
}
193
199
200
+ public void testBasicSearchOk () throws IOException {
201
+ System .out .println ("TestBasicSearchOk" );
202
+ List <HttpHost > clusterHosts = parseClusterHosts (currentCluster .getHttpAddresses ());
203
+ try (RestClient esClient = RestClient .builder (clusterHosts .toArray (new HttpHost [clusterHosts .size ()])).build ();) {
204
+ for (String index : indices ) {
205
+ ensureGreen (esClient , index );
206
+ }
207
+ Request searchRequest = new Request ("POST" , "/custom/_search" );
208
+ Map <String , Object > searchResponse = entityAsMap (esClient .performRequest (searchRequest ));
209
+ assertEquals (2 , XContentMapValues .extractValue (searchResponse , "hits" , "total" , "value" ));
210
+ }
211
+ }
212
+
213
+ protected boolean resetFeatureStates () {
214
+ return false ;
215
+ }
216
+
217
+ protected boolean preserveIndicesUponCompletion () {
218
+ return true ;
219
+ }
220
+
221
+ protected void wipeSnapshots () throws IOException {
222
+ // we want to preserve snapshots between individual tests
223
+ }
224
+
225
+ @ Override
226
+ protected String getEnsureGreenTimeout () {
227
+ return "5s" ;
228
+ }
194
229
}
0 commit comments