|
9 | 9 | import com.fasterxml.jackson.core.JsonParseException;
|
10 | 10 | import com.fasterxml.jackson.core.JsonParser;
|
11 | 11 | import com.fasterxml.jackson.core.JsonToken;
|
| 12 | +import org.elasticsearch.Version; |
| 13 | +import org.elasticsearch.cluster.ClusterName; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 16 | +import org.elasticsearch.cluster.metadata.MappingMetaData; |
| 17 | +import org.elasticsearch.cluster.metadata.MetaData; |
12 | 18 | import org.elasticsearch.common.Strings;
|
| 19 | +import org.elasticsearch.common.settings.Settings; |
13 | 20 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
14 | 21 | import org.elasticsearch.common.xcontent.XContentParser;
|
15 | 22 | import org.elasticsearch.test.ESTestCase;
|
| 23 | +import org.elasticsearch.test.VersionUtils; |
16 | 24 | import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig;
|
17 | 25 | import org.elasticsearch.xpack.core.ml.job.config.Job;
|
18 | 26 | import org.elasticsearch.xpack.core.ml.job.config.ModelPlotConfig;
|
|
30 | 38 | import java.io.IOException;
|
31 | 39 | import java.nio.charset.StandardCharsets;
|
32 | 40 | import java.util.Arrays;
|
| 41 | +import java.util.Collections; |
| 42 | +import java.util.HashMap; |
33 | 43 | import java.util.HashSet;
|
34 | 44 | import java.util.List;
|
35 | 45 | import java.util.Map;
|
@@ -128,6 +138,96 @@ public void testTermFieldMapping() throws IOException {
|
128 | 138 | assertNull(instanceMapping);
|
129 | 139 | }
|
130 | 140 |
|
| 141 | + |
| 142 | + public void testMappingRequiresUpdateNoMapping() throws IOException { |
| 143 | + ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("_name")); |
| 144 | + ClusterState cs = csBuilder.build(); |
| 145 | + String[] indices = new String[] { "no_index" }; |
| 146 | + |
| 147 | + assertArrayEquals(new String[] { "no_index" }, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); |
| 148 | + } |
| 149 | + |
| 150 | + public void testMappingRequiresUpdateNullMapping() throws IOException { |
| 151 | + ClusterState cs = getClusterStateWithMappingsWithMetaData(Collections.singletonMap("null_mapping", null)); |
| 152 | + String[] indices = new String[] { "null_index" }; |
| 153 | + assertArrayEquals(indices, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); |
| 154 | + } |
| 155 | + |
| 156 | + public void testMappingRequiresUpdateNoVersion() throws IOException { |
| 157 | + ClusterState cs = getClusterStateWithMappingsWithMetaData(Collections.singletonMap("no_version_field", "NO_VERSION_FIELD")); |
| 158 | + String[] indices = new String[] { "no_version_field" }; |
| 159 | + assertArrayEquals(indices, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); |
| 160 | + } |
| 161 | + |
| 162 | + public void testMappingRequiresUpdateRecentMappingVersion() throws IOException { |
| 163 | + ClusterState cs = getClusterStateWithMappingsWithMetaData(Collections.singletonMap("version_current", Version.CURRENT.toString())); |
| 164 | + String[] indices = new String[] { "version_current" }; |
| 165 | + assertArrayEquals(new String[] {}, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); |
| 166 | + } |
| 167 | + |
| 168 | + public void testMappingRequiresUpdateMaliciousMappingVersion() throws IOException { |
| 169 | + ClusterState cs = getClusterStateWithMappingsWithMetaData( |
| 170 | + Collections.singletonMap("version_current", Collections.singletonMap("nested", "1.0"))); |
| 171 | + String[] indices = new String[] { "version_nested" }; |
| 172 | + assertArrayEquals(indices, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); |
| 173 | + } |
| 174 | + |
| 175 | + public void testMappingRequiresUpdateBogusMappingVersion() throws IOException { |
| 176 | + ClusterState cs = getClusterStateWithMappingsWithMetaData(Collections.singletonMap("version_bogus", "0.0")); |
| 177 | + String[] indices = new String[] { "version_bogus" }; |
| 178 | + assertArrayEquals(indices, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); |
| 179 | + } |
| 180 | + |
| 181 | + public void testMappingRequiresUpdateNewerMappingVersion() throws IOException { |
| 182 | + ClusterState cs = getClusterStateWithMappingsWithMetaData(Collections.singletonMap("version_newer", Version.CURRENT)); |
| 183 | + String[] indices = new String[] { "version_newer" }; |
| 184 | + assertArrayEquals(new String[] {}, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, VersionUtils.getPreviousVersion())); |
| 185 | + } |
| 186 | + |
| 187 | + public void testMappingRequiresUpdateNewerMappingVersionMinor() throws IOException { |
| 188 | + ClusterState cs = getClusterStateWithMappingsWithMetaData(Collections.singletonMap("version_newer_minor", Version.CURRENT)); |
| 189 | + String[] indices = new String[] { "version_newer_minor" }; |
| 190 | + assertArrayEquals(new String[] {}, |
| 191 | + ElasticsearchMappings.mappingRequiresUpdate(cs, indices, VersionUtils.getPreviousMinorVersion())); |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + private ClusterState getClusterStateWithMappingsWithMetaData(Map<String, Object> namesAndVersions) throws IOException { |
| 196 | + MetaData.Builder metaDataBuilder = MetaData.builder(); |
| 197 | + |
| 198 | + for (Map.Entry<String, Object> entry : namesAndVersions.entrySet()) { |
| 199 | + |
| 200 | + String indexName = entry.getKey(); |
| 201 | + Object version = entry.getValue(); |
| 202 | + |
| 203 | + IndexMetaData.Builder indexMetaData = IndexMetaData.builder(indexName); |
| 204 | + indexMetaData.settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 205 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)); |
| 206 | + |
| 207 | + Map<String, Object> mapping = new HashMap<>(); |
| 208 | + Map<String, Object> properties = new HashMap<>(); |
| 209 | + for (int i = 0; i < 10; i++) { |
| 210 | + properties.put("field" + i, Collections.singletonMap("type", "string")); |
| 211 | + } |
| 212 | + mapping.put("properties", properties); |
| 213 | + |
| 214 | + Map<String, Object> meta = new HashMap<>(); |
| 215 | + if (version != null && version.equals("NO_VERSION_FIELD") == false) { |
| 216 | + meta.put("version", version); |
| 217 | + } |
| 218 | + mapping.put("_meta", meta); |
| 219 | + |
| 220 | + indexMetaData.putMapping(new MappingMetaData(ElasticsearchMappings.DOC_TYPE, mapping)); |
| 221 | + |
| 222 | + metaDataBuilder.put(indexMetaData); |
| 223 | + } |
| 224 | + MetaData metaData = metaDataBuilder.build(); |
| 225 | + |
| 226 | + ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("_name")); |
| 227 | + csBuilder.metaData(metaData); |
| 228 | + return csBuilder.build(); |
| 229 | + } |
| 230 | + |
131 | 231 | private Set<String> collectResultsDocFieldNames() throws IOException {
|
132 | 232 | // Only the mappings for the results index should be added below. Do NOT add mappings for other indexes here.
|
133 | 233 | return collectFieldNames(ElasticsearchMappings.resultsMapping());
|
|
0 commit comments