1717import org .elasticsearch .cluster .node .DiscoveryNodes ;
1818import org .elasticsearch .cluster .service .ClusterService ;
1919import org .elasticsearch .core .TimeValue ;
20+ import org .elasticsearch .features .FeatureService ;
2021import org .elasticsearch .persistent .ClusterPersistentTasksCustomMetadata ;
2122import org .elasticsearch .persistent .PersistentTasksService ;
2223import org .elasticsearch .test .ESTestCase ;
@@ -49,6 +50,7 @@ public class AuthorizationTaskExecutorTests extends ESTestCase {
4950 private ClusterService clusterService ;
5051 private PersistentTasksService persistentTasksService ;
5152 private String localNodeId ;
53+ private FeatureService enabledFeatureServiceMock ;
5254
5355 @ Before
5456 public void setUp () throws Exception {
@@ -57,6 +59,8 @@ public void setUp() throws Exception {
5759 clusterService = createClusterService (threadPool );
5860 persistentTasksService = mock (PersistentTasksService .class );
5961 localNodeId = clusterService .localNode ().getId ();
62+ enabledFeatureServiceMock = mock (FeatureService .class );
63+ when (enabledFeatureServiceMock .clusterHasFeature (any (), any ())).thenReturn (true );
6064 }
6165
6266 @ After
@@ -72,6 +76,7 @@ public void testMultipleCallsToStart_OnlyRegistersOnce() {
7276 var executor = new AuthorizationTaskExecutor (
7377 mockClusterService ,
7478 persistentTasksService ,
79+ enabledFeatureServiceMock ,
7580 new AuthorizationPoller .Parameters (
7681 createWithEmptySettings (threadPool ),
7782 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -102,6 +107,7 @@ public void testStartLazy_OnlyRegistersOnce_NeverCallsPersistentTaskService() {
102107 var executor = new AuthorizationTaskExecutor (
103108 mockClusterService ,
104109 persistentTasksService ,
110+ enabledFeatureServiceMock ,
105111 new AuthorizationPoller .Parameters (
106112 createWithEmptySettings (threadPool ),
107113 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -138,6 +144,7 @@ public void testDoesNotRegisterListener_IfUrlIsEmpty() {
138144 var executor = new AuthorizationTaskExecutor (
139145 mockClusterService ,
140146 persistentTasksService ,
147+ enabledFeatureServiceMock ,
141148 new AuthorizationPoller .Parameters (
142149 createWithEmptySettings (threadPool ),
143150 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -168,6 +175,7 @@ public void testMultipleCallsToStart_AndStop() {
168175 var executor = new AuthorizationTaskExecutor (
169176 mockClusterService ,
170177 persistentTasksService ,
178+ enabledFeatureServiceMock ,
171179 new AuthorizationPoller .Parameters (
172180 createWithEmptySettings (threadPool ),
173181 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -214,6 +222,7 @@ public void testCallsSendClusterStartRequest_WhenStartIsCalled() {
214222 var executor = new AuthorizationTaskExecutor (
215223 mockClusterService ,
216224 persistentTasksService ,
225+ enabledFeatureServiceMock ,
217226 new AuthorizationPoller .Parameters (
218227 createWithEmptySettings (threadPool ),
219228 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -263,6 +272,7 @@ public void testDoesNotCallSendClusterStartRequest_WhenStartIsCalled_WhenItIsAlr
263272 var executor = new AuthorizationTaskExecutor (
264273 mockClusterService ,
265274 persistentTasksService ,
275+ enabledFeatureServiceMock ,
266276 new AuthorizationPoller .Parameters (
267277 createWithEmptySettings (threadPool ),
268278 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -292,6 +302,7 @@ public void testCreatesTask_WhenItDoesNotExistOnClusterStateChange() {
292302 var executor = new AuthorizationTaskExecutor (
293303 clusterService ,
294304 persistentTasksService ,
305+ enabledFeatureServiceMock ,
295306 new AuthorizationPoller .Parameters (
296307 createWithEmptySettings (threadPool ),
297308 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -340,10 +351,46 @@ private ClusterState initialState() {
340351 return ClusterState .builder (ClusterName .DEFAULT ).nodes (nodes ).metadata (EMPTY_METADATA ).build ();
341352 }
342353
354+ public void testDoesNotCreateTask_WhenFeatureIsNotSupported () {
355+ var eisUrl = "abc" ;
356+ var disabledFeatureServiceMock = mock (FeatureService .class );
357+ when (disabledFeatureServiceMock .clusterHasFeature (any (), any ())).thenReturn (false );
358+
359+ var executor = new AuthorizationTaskExecutor (
360+ clusterService ,
361+ persistentTasksService ,
362+ disabledFeatureServiceMock ,
363+ new AuthorizationPoller .Parameters (
364+ createWithEmptySettings (threadPool ),
365+ mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
366+ mock (Sender .class ),
367+ ElasticInferenceServiceSettingsTests .create (eisUrl , TimeValue .timeValueMillis (1 ), TimeValue .timeValueMillis (1 ), true ),
368+ mock (ModelRegistry .class ),
369+ mock (Client .class ),
370+ createMockCCMFeature (false ),
371+ createMockCCMService (false )
372+ )
373+ );
374+ executor .startAndImmediatelyCreateTask ();
375+
376+ var listener1 = new PlainActionFuture <Void >();
377+ clusterService .getClusterApplierService ().onNewClusterState ("initialization" , this ::initialState , listener1 );
378+ listener1 .actionGet (TimeValue .THIRTY_SECONDS );
379+ // We should never call sendClusterStartRequest since the feature is not supported
380+ verify (persistentTasksService , never ()).sendClusterStartRequest (
381+ eq (AuthorizationPoller .TASK_NAME ),
382+ eq (AuthorizationPoller .TASK_NAME ),
383+ eq (AuthorizationTaskParams .INSTANCE ),
384+ any (),
385+ any ()
386+ );
387+ }
388+
343389 public void testDoesNotRegisterClusterStateListener_DoesNotCreateTask_WhenTheEisUrlIsEmpty () {
344390 var executor = new AuthorizationTaskExecutor (
345391 clusterService ,
346392 persistentTasksService ,
393+ enabledFeatureServiceMock ,
347394 new AuthorizationPoller .Parameters (
348395 createWithEmptySettings (threadPool ),
349396 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -373,6 +420,7 @@ public void testDoesNotRegisterClusterStateListener_DoesNotCreateTask_WhenTheEis
373420 var executor = new AuthorizationTaskExecutor (
374421 clusterService ,
375422 persistentTasksService ,
423+ enabledFeatureServiceMock ,
376424 new AuthorizationPoller .Parameters (
377425 createWithEmptySettings (threadPool ),
378426 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
@@ -425,6 +473,7 @@ public void testDoesNotCreateTask_OnClusterStateChange_WhenItAlreadyExists() {
425473 var executor = new AuthorizationTaskExecutor (
426474 mockClusterService ,
427475 persistentTasksService ,
476+ enabledFeatureServiceMock ,
428477 new AuthorizationPoller .Parameters (
429478 createWithEmptySettings (threadPool ),
430479 mock (ElasticInferenceServiceAuthorizationRequestHandler .class ),
0 commit comments