|
| 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.services.validation; |
| 9 | + |
| 10 | +import org.elasticsearch.ElasticsearchStatusException; |
| 11 | +import org.elasticsearch.action.ActionListener; |
| 12 | +import org.elasticsearch.core.TimeValue; |
| 13 | +import org.elasticsearch.inference.InferenceService; |
| 14 | +import org.elasticsearch.inference.Model; |
| 15 | +import org.elasticsearch.rest.RestStatus; |
| 16 | +import org.elasticsearch.test.ESTestCase; |
| 17 | +import org.junit.Before; |
| 18 | +import org.mockito.ArgumentCaptor; |
| 19 | +import org.mockito.Mock; |
| 20 | + |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.doAnswer; |
| 24 | +import static org.mockito.Mockito.doThrow; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | +import static org.mockito.MockitoAnnotations.openMocks; |
| 29 | + |
| 30 | +public class ElasticsearchInternalServiceModelValidatorTests extends ESTestCase { |
| 31 | + |
| 32 | + private static final TimeValue TIMEOUT = TimeValue.ONE_MINUTE; |
| 33 | + private static final String MODEL_VALIDATION_AND_STOP_FAILED_MESSAGE = |
| 34 | + "Model validation failed and model deployment could not be stopped"; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private ModelValidator mockModelValidator; |
| 38 | + @Mock |
| 39 | + private InferenceService mockInferenceService; |
| 40 | + @Mock |
| 41 | + private Model mockModel; |
| 42 | + @Mock |
| 43 | + private ActionListener<Model> mockActionListener; |
| 44 | + |
| 45 | + private ElasticsearchInternalServiceModelValidator underTest; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() { |
| 49 | + openMocks(this); |
| 50 | + |
| 51 | + underTest = new ElasticsearchInternalServiceModelValidator(mockModelValidator); |
| 52 | + |
| 53 | + when(mockActionListener.delegateResponse(any())).thenCallRealMethod(); |
| 54 | + } |
| 55 | + |
| 56 | + public void testValidate_ModelDeploymentThrowsException() { |
| 57 | + doThrow(ElasticsearchStatusException.class).when(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 58 | + |
| 59 | + assertThrows( |
| 60 | + ElasticsearchStatusException.class, |
| 61 | + () -> { underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); } |
| 62 | + ); |
| 63 | + |
| 64 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 65 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 66 | + } |
| 67 | + |
| 68 | + public void testValidate_ModelDeploymentReturnsFalse() { |
| 69 | + mockModelDeployment(false); |
| 70 | + |
| 71 | + underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); |
| 72 | + |
| 73 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 74 | + verify(mockActionListener).onFailure(any(ElasticsearchStatusException.class)); |
| 75 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 76 | + } |
| 77 | + |
| 78 | + public void testValidate_ModelValidatorThrowsExceptionAndModelDeploymentIsStopped() { |
| 79 | + mockModelDeployment(true); |
| 80 | + doThrow(new ElasticsearchStatusException("Model Validator Exception", RestStatus.INTERNAL_SERVER_ERROR)).when(mockModelValidator) |
| 81 | + .validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 82 | + mockModelStop(true); |
| 83 | + |
| 84 | + underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); |
| 85 | + |
| 86 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 87 | + verify(mockInferenceService).stop(eq(mockModel), any()); |
| 88 | + verify(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 89 | + verify(mockActionListener).delegateResponse(any()); |
| 90 | + verifyMockActionListenerAfterStopModelDeployment(true); |
| 91 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 92 | + } |
| 93 | + |
| 94 | + public void testValidate_ModelValidatorThrowsExceptionAndModelDeploymentIsNotStopped() { |
| 95 | + mockModelDeployment(true); |
| 96 | + doThrow(new ElasticsearchStatusException("Model Validator Exception", RestStatus.INTERNAL_SERVER_ERROR)).when(mockModelValidator) |
| 97 | + .validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 98 | + mockModelStop(false); |
| 99 | + |
| 100 | + underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); |
| 101 | + |
| 102 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 103 | + verify(mockInferenceService).stop(eq(mockModel), any()); |
| 104 | + verify(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 105 | + verify(mockActionListener).delegateResponse(any()); |
| 106 | + verifyMockActionListenerAfterStopModelDeployment(false); |
| 107 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 108 | + } |
| 109 | + |
| 110 | + public void testValidate_ModelValidationFailsAndModelDeploymentIsStopped() { |
| 111 | + mockModelDeployment(true); |
| 112 | + doAnswer(ans -> { |
| 113 | + ActionListener<Model> responseListener = ans.getArgument(3); |
| 114 | + responseListener.onFailure(new ElasticsearchStatusException("Model validation failed", RestStatus.INTERNAL_SERVER_ERROR)); |
| 115 | + return null; |
| 116 | + }).when(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 117 | + mockModelStop(true); |
| 118 | + |
| 119 | + underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); |
| 120 | + |
| 121 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 122 | + verify(mockInferenceService).stop(eq(mockModel), any()); |
| 123 | + verify(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 124 | + verify(mockActionListener).delegateResponse(any()); |
| 125 | + verifyMockActionListenerAfterStopModelDeployment(true); |
| 126 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 127 | + } |
| 128 | + |
| 129 | + public void testValidate_ModelValidationFailsAndModelDeploymentIsNotStopped() { |
| 130 | + mockModelDeployment(true); |
| 131 | + doAnswer(ans -> { |
| 132 | + ActionListener<Model> responseListener = ans.getArgument(3); |
| 133 | + responseListener.onFailure(new ElasticsearchStatusException("Model validation failed", RestStatus.INTERNAL_SERVER_ERROR)); |
| 134 | + return null; |
| 135 | + }).when(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 136 | + mockModelStop(false); |
| 137 | + |
| 138 | + underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); |
| 139 | + |
| 140 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 141 | + verify(mockInferenceService).stop(eq(mockModel), any()); |
| 142 | + verify(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 143 | + verify(mockActionListener).delegateResponse(any()); |
| 144 | + verifyMockActionListenerAfterStopModelDeployment(false); |
| 145 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 146 | + } |
| 147 | + |
| 148 | + public void testValidate_ModelValidationSucceeds() { |
| 149 | + mockModelDeployment(true); |
| 150 | + mockModelStop(true); |
| 151 | + |
| 152 | + underTest.validate(mockInferenceService, mockModel, TIMEOUT, mockActionListener); |
| 153 | + |
| 154 | + verify(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 155 | + verify(mockModelValidator).validate(eq(mockInferenceService), eq(mockModel), eq(TIMEOUT), any()); |
| 156 | + verify(mockActionListener).delegateResponse(any()); |
| 157 | + verifyNoMoreInteractions(mockModelValidator, mockInferenceService, mockModel, mockActionListener); |
| 158 | + } |
| 159 | + |
| 160 | + private void mockModelDeployment(boolean modelDeploymentStarted) { |
| 161 | + doAnswer(ans -> { |
| 162 | + ActionListener<Boolean> responseListener = ans.getArgument(2); |
| 163 | + responseListener.onResponse(modelDeploymentStarted); |
| 164 | + return null; |
| 165 | + }).when(mockInferenceService).start(eq(mockModel), eq(TIMEOUT), any()); |
| 166 | + } |
| 167 | + |
| 168 | + private void mockModelStop(boolean modelDeploymentStopped) { |
| 169 | + if (modelDeploymentStopped) { |
| 170 | + doAnswer(ans -> { |
| 171 | + ActionListener<Void> responseListener = ans.getArgument(1); |
| 172 | + responseListener.onResponse(null); |
| 173 | + return null; |
| 174 | + }).when(mockInferenceService).stop(eq(mockModel), any()); |
| 175 | + } else { |
| 176 | + doAnswer(ans -> { |
| 177 | + ActionListener<Void> responseListener = ans.getArgument(1); |
| 178 | + responseListener.onFailure(new ElasticsearchStatusException("Model stop failed", RestStatus.INTERNAL_SERVER_ERROR)); |
| 179 | + return null; |
| 180 | + }).when(mockInferenceService).stop(eq(mockModel), any()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private void verifyMockActionListenerAfterStopModelDeployment(boolean modelDeploymentStopped) { |
| 185 | + verify(mockInferenceService).stop(eq(mockModel), any()); |
| 186 | + ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class); |
| 187 | + verify(mockActionListener).onFailure(exceptionCaptor.capture()); |
| 188 | + assertTrue(exceptionCaptor.getValue() instanceof ElasticsearchStatusException); |
| 189 | + assertEquals(RestStatus.INTERNAL_SERVER_ERROR, ((ElasticsearchStatusException) exceptionCaptor.getValue()).status()); |
| 190 | + |
| 191 | + if (modelDeploymentStopped) { |
| 192 | + assertFalse(exceptionCaptor.getValue().getMessage().contains(MODEL_VALIDATION_AND_STOP_FAILED_MESSAGE)); |
| 193 | + } else { |
| 194 | + assertTrue(exceptionCaptor.getValue().getMessage().contains(MODEL_VALIDATION_AND_STOP_FAILED_MESSAGE)); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments