|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.qa.mixed; |
| 9 | + |
| 10 | +import org.apache.http.util.EntityUtils; |
| 11 | +import org.elasticsearch.client.Request; |
| 12 | +import org.elasticsearch.client.Response; |
| 13 | +import org.elasticsearch.common.Strings; |
| 14 | +import org.elasticsearch.common.settings.SecureString; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 17 | +import org.elasticsearch.inference.TaskType; |
| 18 | +import org.elasticsearch.test.ESTestCase; |
| 19 | +import org.elasticsearch.test.http.MockWebServer; |
| 20 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 21 | +import org.hamcrest.Matchers; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +public abstract class BaseMixedTestCase extends MixedClusterSpecTestCase { |
| 28 | + protected static String getUrl(MockWebServer webServer) { |
| 29 | + return Strings.format("http://%s:%s", webServer.getHostName(), webServer.getPort()); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + protected Settings restClientSettings() { |
| 34 | + String token = ESRestTestCase.basicAuthHeaderValue("x_pack_rest_user", new SecureString("x-pack-test-password".toCharArray())); |
| 35 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 36 | + } |
| 37 | + |
| 38 | + protected void delete(String inferenceId, TaskType taskType) throws IOException { |
| 39 | + var request = new Request("DELETE", Strings.format("_inference/%s/%s", taskType, inferenceId)); |
| 40 | + var response = ESRestTestCase.client().performRequest(request); |
| 41 | + ESRestTestCase.assertOK(response); |
| 42 | + } |
| 43 | + |
| 44 | + protected void delete(String inferenceId) throws IOException { |
| 45 | + var request = new Request("DELETE", Strings.format("_inference/%s", inferenceId)); |
| 46 | + var response = ESRestTestCase.client().performRequest(request); |
| 47 | + ESRestTestCase.assertOK(response); |
| 48 | + } |
| 49 | + |
| 50 | + protected Map<String, Object> getAll() throws IOException { |
| 51 | + var request = new Request("GET", "_inference/_all"); |
| 52 | + var response = ESRestTestCase.client().performRequest(request); |
| 53 | + ESRestTestCase.assertOK(response); |
| 54 | + return ESRestTestCase.entityAsMap(response); |
| 55 | + } |
| 56 | + |
| 57 | + protected Map<String, Object> get(String inferenceId) throws IOException { |
| 58 | + var endpoint = Strings.format("_inference/%s", inferenceId); |
| 59 | + var request = new Request("GET", endpoint); |
| 60 | + var response = ESRestTestCase.client().performRequest(request); |
| 61 | + ESRestTestCase.assertOK(response); |
| 62 | + return ESRestTestCase.entityAsMap(response); |
| 63 | + } |
| 64 | + |
| 65 | + protected Map<String, Object> get(TaskType taskType, String inferenceId) throws IOException { |
| 66 | + var endpoint = Strings.format("_inference/%s/%s", taskType, inferenceId); |
| 67 | + var request = new Request("GET", endpoint); |
| 68 | + var response = ESRestTestCase.client().performRequest(request); |
| 69 | + ESRestTestCase.assertOK(response); |
| 70 | + return ESRestTestCase.entityAsMap(response); |
| 71 | + } |
| 72 | + |
| 73 | + protected Map<String, Object> inference(String inferenceId, TaskType taskType, String input) throws IOException { |
| 74 | + var endpoint = Strings.format("_inference/%s/%s", taskType, inferenceId); |
| 75 | + var request = new Request("POST", endpoint); |
| 76 | + request.setJsonEntity("{\"input\": [" + '"' + input + '"' + "]}"); |
| 77 | + |
| 78 | + var response = ESRestTestCase.client().performRequest(request); |
| 79 | + ESRestTestCase.assertOK(response); |
| 80 | + return ESRestTestCase.entityAsMap(response); |
| 81 | + } |
| 82 | + |
| 83 | + protected Map<String, Object> rerank(String inferenceId, List<String> inputs, String query) throws IOException { |
| 84 | + var endpoint = Strings.format("_inference/rerank/%s", inferenceId); |
| 85 | + var request = new Request("POST", endpoint); |
| 86 | + |
| 87 | + StringBuilder body = new StringBuilder("{").append("\"query\":\"").append(query).append("\",").append("\"input\":["); |
| 88 | + |
| 89 | + for (int i = 0; i < inputs.size(); i++) { |
| 90 | + body.append("\"").append(inputs.get(i)).append("\""); |
| 91 | + if (i < inputs.size() - 1) { |
| 92 | + body.append(","); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + body.append("]}"); |
| 97 | + request.setJsonEntity(body.toString()); |
| 98 | + |
| 99 | + var response = ESRestTestCase.client().performRequest(request); |
| 100 | + ESRestTestCase.assertOK(response); |
| 101 | + return ESRestTestCase.entityAsMap(response); |
| 102 | + } |
| 103 | + |
| 104 | + protected void put(String inferenceId, String modelConfig, TaskType taskType) throws IOException { |
| 105 | + String endpoint = Strings.format("_inference/%s/%s?error_trace", taskType, inferenceId); |
| 106 | + var request = new Request("PUT", endpoint); |
| 107 | + request.setJsonEntity(modelConfig); |
| 108 | + var response = ESRestTestCase.client().performRequest(request); |
| 109 | + logger.warn("PUT response: {}", response.toString()); |
| 110 | + System.out.println("PUT response: " + response.toString()); |
| 111 | + ESRestTestCase.assertOKAndConsume(response); |
| 112 | + } |
| 113 | + |
| 114 | + protected static void assertOkOrCreated(Response response) throws IOException { |
| 115 | + int statusCode = response.getStatusLine().getStatusCode(); |
| 116 | + // Once EntityUtils.toString(entity) is called the entity cannot be reused. |
| 117 | + // Avoid that call with check here. |
| 118 | + if (statusCode == 200 || statusCode == 201) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + String responseStr = EntityUtils.toString(response.getEntity()); |
| 123 | + ESTestCase.assertThat( |
| 124 | + responseStr, |
| 125 | + response.getStatusLine().getStatusCode(), |
| 126 | + Matchers.anyOf(Matchers.equalTo(200), Matchers.equalTo(201)) |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments