|
@@ -8,6 +8,7 @@
|
|
|
package org.elasticsearch.xpack.inference.services.elastic.authorization;
|
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
+import org.elasticsearch.ElasticsearchException;
|
|
|
import org.elasticsearch.action.ActionListener;
|
|
|
import org.elasticsearch.action.support.PlainActionFuture;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
@@ -18,6 +19,7 @@ import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.test.http.MockResponse;
|
|
|
import org.elasticsearch.test.http.MockWebServer;
|
|
|
import org.elasticsearch.threadpool.ThreadPool;
|
|
|
+import org.elasticsearch.xcontent.XContentParseException;
|
|
|
import org.elasticsearch.xpack.core.inference.results.ChatCompletionResults;
|
|
|
import org.elasticsearch.xpack.inference.external.http.HttpClientManager;
|
|
|
import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSender;
|
|
@@ -38,13 +40,14 @@ import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool;
|
|
|
import static org.elasticsearch.xpack.inference.Utils.mockClusterServiceEmpty;
|
|
|
import static org.elasticsearch.xpack.inference.external.http.Utils.getUrl;
|
|
|
import static org.elasticsearch.xpack.inference.external.http.retry.RetryingHttpSender.MAX_RETIES;
|
|
|
+import static org.hamcrest.Matchers.containsString;
|
|
|
+import static org.hamcrest.Matchers.instanceOf;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.Mockito.doAnswer;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.times;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
-import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
public class ElasticInferenceServiceAuthorizationRequestHandlerTests extends ESTestCase {
|
|
@@ -135,22 +138,17 @@ public class ElasticInferenceServiceAuthorizationRequestHandlerTests extends EST
|
|
|
PlainActionFuture<ElasticInferenceServiceAuthorizationModel> listener = new PlainActionFuture<>();
|
|
|
authHandler.getAuthorization(listener, sender);
|
|
|
|
|
|
- var authResponse = listener.actionGet(TIMEOUT);
|
|
|
- assertTrue(authResponse.getAuthorizedTaskTypes().isEmpty());
|
|
|
- assertTrue(authResponse.getAuthorizedModelIds().isEmpty());
|
|
|
- assertFalse(authResponse.isAuthorized());
|
|
|
+ var exception = expectThrows(XContentParseException.class, () -> listener.actionGet(TIMEOUT));
|
|
|
+ assertThat(exception.getMessage(), containsString("failed to parse field [models]"));
|
|
|
|
|
|
- var loggerArgsCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
- verify(logger).warn(loggerArgsCaptor.capture());
|
|
|
- var message = loggerArgsCaptor.getValue();
|
|
|
- assertThat(
|
|
|
- message,
|
|
|
- is(
|
|
|
- "Failed to retrieve the authorization information from the Elastic Inference Service."
|
|
|
- + " Encountered an exception: org.elasticsearch.xcontent.XContentParseException: [4:28] "
|
|
|
- + "[ElasticInferenceServiceAuthorizationResponseEntity] failed to parse field [models]"
|
|
|
- )
|
|
|
- );
|
|
|
+ var stringCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
+ var exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
|
|
|
+ verify(logger).warn(stringCaptor.capture(), exceptionCaptor.capture());
|
|
|
+ var message = stringCaptor.getValue();
|
|
|
+ assertThat(message, containsString("failed to parse field [models]"));
|
|
|
+
|
|
|
+ var capturedException = exceptionCaptor.getValue();
|
|
|
+ assertThat(capturedException, instanceOf(XContentParseException.class));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -196,7 +194,6 @@ public class ElasticInferenceServiceAuthorizationRequestHandlerTests extends EST
|
|
|
|
|
|
var message = loggerArgsCaptor.getValue();
|
|
|
assertThat(message, is("Retrieving authorization information from the Elastic Inference Service."));
|
|
|
- verifyNoMoreInteractions(logger);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -230,7 +227,6 @@ public class ElasticInferenceServiceAuthorizationRequestHandlerTests extends EST
|
|
|
|
|
|
var message = loggerArgsCaptor.getValue();
|
|
|
assertThat(message, is("Retrieving authorization information from the Elastic Inference Service."));
|
|
|
- verifyNoMoreInteractions(logger);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -252,20 +248,14 @@ public class ElasticInferenceServiceAuthorizationRequestHandlerTests extends EST
|
|
|
PlainActionFuture<ElasticInferenceServiceAuthorizationModel> listener = new PlainActionFuture<>();
|
|
|
|
|
|
authHandler.getAuthorization(listener, sender);
|
|
|
- var result = listener.actionGet(TIMEOUT);
|
|
|
+ var exception = expectThrows(ElasticsearchException.class, () -> listener.actionGet(TIMEOUT));
|
|
|
|
|
|
- assertThat(result, is(ElasticInferenceServiceAuthorizationModel.newDisabledService()));
|
|
|
+ assertThat(exception.getMessage(), containsString("Received an invalid response type from the Elastic Inference Service"));
|
|
|
|
|
|
var loggerArgsCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
verify(logger).warn(loggerArgsCaptor.capture());
|
|
|
var message = loggerArgsCaptor.getValue();
|
|
|
- assertThat(
|
|
|
- message,
|
|
|
- is(
|
|
|
- "Failed to retrieve the authorization information from the Elastic Inference Service."
|
|
|
- + " Received an invalid response type: ChatCompletionResults"
|
|
|
- )
|
|
|
- );
|
|
|
+ assertThat(message, containsString("Failed to retrieve the authorization information from the Elastic Inference Service."));
|
|
|
}
|
|
|
|
|
|
}
|