Skip to content

Commit d8ee685

Browse files
committed
Factor out duplicated code
1 parent f0f3054 commit d8ee685

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/integration/SemanticTextIndexOptionsIT.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,7 @@ public void testValidateIndexOptionsWithBasicLicense() throws Exception {
115115
final String inferenceId = "test-inference-id-1";
116116
final String inferenceFieldName = "inference_field";
117117
createInferenceEndpoint(TaskType.TEXT_EMBEDDING, inferenceId, BBQ_COMPATIBLE_SERVICE_SETTINGS);
118-
119-
// Downgrade the license and restart the cluster to force the model registry to rebuild
120-
setLicense(License.LicenseType.BASIC);
121-
internalCluster().fullRestart(new InternalTestCluster.RestartCallback());
122-
ensureGreen(InferenceIndex.INDEX_NAME);
123-
assertLicense(License.LicenseType.BASIC);
118+
downgradeLicenseAndRestartCluster();
124119

125120
IndexOptions indexOptions = new DenseVectorFieldMapper.Int8HnswIndexOptions(
126121
randomIntBetween(1, 100),
@@ -133,22 +128,14 @@ public void testValidateIndexOptionsWithBasicLicense() throws Exception {
133128
);
134129

135130
final Map<String, Object> expectedFieldMapping = generateExpectedFieldMapping(inferenceFieldName, inferenceId, indexOptions);
136-
var getFieldMappingsResponse = safeGet(
137-
client().execute(GetFieldMappingsAction.INSTANCE, new GetFieldMappingsRequest().indices(INDEX_NAME).fields(inferenceFieldName))
138-
);
139-
assertThat(getFieldMappingsResponse.fieldMappings(INDEX_NAME, inferenceFieldName).sourceAsMap(), equalTo(expectedFieldMapping));
131+
assertThat(getFieldMappings(inferenceFieldName, false), equalTo(expectedFieldMapping));
140132
}
141133

142134
public void testSetDefaultBBQIndexOptionsWithBasicLicense() throws Exception {
143135
final String inferenceId = "test-inference-id-2";
144136
final String inferenceFieldName = "inference_field";
145137
createInferenceEndpoint(TaskType.TEXT_EMBEDDING, inferenceId, BBQ_COMPATIBLE_SERVICE_SETTINGS);
146-
147-
// Downgrade the license and restart the cluster to force the model registry to rebuild
148-
setLicense(License.LicenseType.BASIC);
149-
internalCluster().fullRestart(new InternalTestCluster.RestartCallback());
150-
ensureGreen(InferenceIndex.INDEX_NAME);
151-
assertLicense(License.LicenseType.BASIC);
138+
downgradeLicenseAndRestartCluster();
152139

153140
assertAcked(safeGet(prepareCreate(INDEX_NAME).setMapping(generateMapping(inferenceFieldName, inferenceId, null)).execute()));
154141

@@ -157,17 +144,9 @@ public void testSetDefaultBBQIndexOptionsWithBasicLicense() throws Exception {
157144
inferenceId,
158145
SemanticTextFieldMapper.defaultBbqHnswDenseVectorIndexOptions()
159146
);
160-
var getFieldMappingsResponse = safeGet(
161-
client().execute(
162-
GetFieldMappingsAction.INSTANCE,
163-
new GetFieldMappingsRequest().indices(INDEX_NAME).fields(inferenceFieldName).includeDefaults(true)
164-
)
165-
);
166147

167148
// Filter out null/empty values from params we didn't set to make comparison easier
168-
Map<String, Object> actualFieldMappings = filterNullOrEmptyValues(
169-
getFieldMappingsResponse.fieldMappings(INDEX_NAME, inferenceFieldName).sourceAsMap()
170-
);
149+
Map<String, Object> actualFieldMappings = filterNullOrEmptyValues(getFieldMappings(inferenceFieldName, true));
171150
assertThat(actualFieldMappings, equalTo(expectedFieldMapping));
172151
}
173152

@@ -269,6 +248,11 @@ private static Map<String, Object> filterNullOrEmptyValues(Map<String, Object> m
269248
return filteredMap;
270249
}
271250

251+
private static Map<String, Object> getFieldMappings(String fieldName, boolean includeDefaults) {
252+
var request = new GetFieldMappingsRequest().indices(INDEX_NAME).fields(fieldName).includeDefaults(includeDefaults);
253+
return safeGet(client().execute(GetFieldMappingsAction.INSTANCE, request)).fieldMappings(INDEX_NAME, fieldName).sourceAsMap();
254+
}
255+
272256
private static void setLicense(License.LicenseType type) throws Exception {
273257
if (type == License.LicenseType.BASIC) {
274258
assertAcked(
@@ -301,4 +285,12 @@ private static void assertLicense(License.LicenseType type) {
301285
var getLicenseResponse = safeGet(client().execute(GetLicenseAction.INSTANCE, new GetLicenseRequest(TEST_REQUEST_TIMEOUT)));
302286
assertThat(getLicenseResponse.license().type(), equalTo(type.getTypeName()));
303287
}
288+
289+
private void downgradeLicenseAndRestartCluster() throws Exception {
290+
// Downgrade the license and restart the cluster to force the model registry to rebuild
291+
setLicense(License.LicenseType.BASIC);
292+
internalCluster().fullRestart(new InternalTestCluster.RestartCallback());
293+
ensureGreen(InferenceIndex.INDEX_NAME);
294+
assertLicense(License.LicenseType.BASIC);
295+
}
304296
}

0 commit comments

Comments
 (0)