3636import org .opensearch .core .common .transport .TransportAddress ;
3737import org .opensearch .core .indices .breaker .NoneCircuitBreakerService ;
3838import org .opensearch .core .transport .TransportResponse ;
39+ import org .opensearch .discovery .InitializeExtensionRequest ;
3940import org .opensearch .env .Environment ;
4041import org .opensearch .env .EnvironmentSettingsResponse ;
4142import org .opensearch .extensions .ExtensionsSettings .Extension ;
7778import static org .mockito .ArgumentMatchers .any ;
7879import static org .mockito .ArgumentMatchers .anyBoolean ;
7980import static org .mockito .ArgumentMatchers .anyString ;
81+ import static org .mockito .Mockito .doNothing ;
8082import static org .mockito .Mockito .mock ;
8183import static org .mockito .Mockito .spy ;
8284import static org .mockito .Mockito .times ;
@@ -409,19 +411,94 @@ public void testInitialize() throws Exception {
409411 )
410412 );
411413
412- // Test needs to be changed to mock the connection between the local node and an extension. Assert statment is commented out for
413- // now.
414+ // Test needs to be changed to mock the connection between the local node and an extension.
414415 // Link to issue: https://github.com/opensearch-project/OpenSearch/issues/4045
415416 // mockLogAppender.assertAllExpectationsMatched();
416417 }
417418 }
418419
420+ public void testInitializeExtension () throws Exception {
421+ ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
422+
423+ TransportService mockTransportService = spy (
424+ new TransportService (
425+ Settings .EMPTY ,
426+ mock (Transport .class ),
427+ threadPool ,
428+ TransportService .NOOP_TRANSPORT_INTERCEPTOR ,
429+ x -> null ,
430+ null ,
431+ Collections .emptySet (),
432+ NoopTracer .INSTANCE
433+ )
434+ );
435+
436+ doNothing ().when (mockTransportService ).connectToExtensionNode (any (DiscoveryExtensionNode .class ));
437+
438+ doNothing ().when (mockTransportService )
439+ .sendRequest (any (DiscoveryExtensionNode .class ), anyString (), any (InitializeExtensionRequest .class ), any ());
440+
441+ extensionsManager .initializeServicesAndRestHandler (
442+ actionModule ,
443+ settingsModule ,
444+ mockTransportService ,
445+ clusterService ,
446+ settings ,
447+ client ,
448+ identityService
449+ );
450+
451+ Extension firstExtension = new Extension (
452+ "firstExtension" ,
453+ "uniqueid1" ,
454+ "127.0.0.0" ,
455+ "9301" ,
456+ "0.0.7" ,
457+ "2.0.0" ,
458+ "2.0.0" ,
459+ List .of (),
460+ null
461+ );
462+
463+ extensionsManager .initializeExtension (firstExtension );
464+
465+ Extension secondExtension = new Extension (
466+ "secondExtension" ,
467+ "uniqueid2" ,
468+ "127.0.0.0" ,
469+ "9301" ,
470+ "0.0.7" ,
471+ "2.0.0" ,
472+ "2.0.0" ,
473+ List .of (),
474+ null
475+ );
476+
477+ extensionsManager .initializeExtension (secondExtension );
478+
479+ ThreadPool .terminate (threadPool , 3 , TimeUnit .SECONDS );
480+
481+ verify (mockTransportService , times (2 )).connectToExtensionNode (any (DiscoveryExtensionNode .class ));
482+
483+ verify (mockTransportService , times (2 )).sendRequest (
484+ any (DiscoveryExtensionNode .class ),
485+ anyString (),
486+ any (InitializeExtensionRequest .class ),
487+ any ()
488+ );
489+ }
490+
419491 public void testHandleRegisterRestActionsRequest () throws Exception {
420492
421493 ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
422494 initialize (extensionsManager );
423495
424496 String uniqueIdStr = "uniqueid1" ;
497+
498+ extensionsManager .loadExtension (
499+ new Extension ("firstExtension" , uniqueIdStr , "127.0.0.0" , "9300" , "0.0.7" , "3.0.0" , "3.0.0" , List .of (), null )
500+ );
501+
425502 List <String > actionsList = List .of ("GET /foo foo" , "PUT /bar bar" , "POST /baz baz" );
426503 List <String > deprecatedActionsList = List .of ("GET /deprecated/foo foo_deprecated" , "It's deprecated!" );
427504 RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest (uniqueIdStr , actionsList , deprecatedActionsList );
@@ -431,6 +508,58 @@ public void testHandleRegisterRestActionsRequest() throws Exception {
431508 assertTrue (((AcknowledgedResponse ) response ).getStatus ());
432509 }
433510
511+ public void testHandleRegisterRestActionsRequestRequiresDiscoveryNode () throws Exception {
512+
513+ ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
514+ initialize (extensionsManager );
515+
516+ RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest ("uniqueId1" , List .of (), List .of ());
517+
518+ expectThrows (
519+ IllegalStateException .class ,
520+ () -> extensionsManager .getRestActionsRequestHandler ()
521+ .handleRegisterRestActionsRequest (registerActionsRequest , actionModule .getDynamicActionRegistry ())
522+ );
523+ }
524+
525+ public void testHandleRegisterRestActionsRequestMultiple () throws Exception {
526+
527+ ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
528+ initialize (extensionsManager );
529+
530+ List <String > actionsList = List .of ("GET /foo foo" , "PUT /bar bar" , "POST /baz baz" );
531+ List <String > deprecatedActionsList = List .of ("GET /deprecated/foo foo_deprecated" , "It's deprecated!" );
532+ for (int i = 0 ; i < 2 ; i ++) {
533+ String uniqueIdStr = "uniqueid-%d" + i ;
534+
535+ Set <Setting <?>> additionalSettings = extAwarePlugin .getExtensionSettings ().stream ().collect (Collectors .toSet ());
536+ ExtensionScopedSettings extensionScopedSettings = new ExtensionScopedSettings (additionalSettings );
537+ Extension firstExtension = new Extension (
538+ "Extension %s" + i ,
539+ uniqueIdStr ,
540+ "127.0.0.0" ,
541+ "9300" ,
542+ "0.0.7" ,
543+ "3.0.0" ,
544+ "3.0.0" ,
545+ List .of (),
546+ extensionScopedSettings
547+ );
548+
549+ extensionsManager .loadExtension (firstExtension );
550+
551+ RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest (
552+ uniqueIdStr ,
553+ actionsList ,
554+ deprecatedActionsList
555+ );
556+ TransportResponse response = extensionsManager .getRestActionsRequestHandler ()
557+ .handleRegisterRestActionsRequest (registerActionsRequest , actionModule .getDynamicActionRegistry ());
558+ assertEquals (AcknowledgedResponse .class , response .getClass ());
559+ assertTrue (((AcknowledgedResponse ) response ).getStatus ());
560+ }
561+ }
562+
434563 public void testHandleRegisterSettingsRequest () throws Exception {
435564 ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
436565 initialize (extensionsManager );
@@ -452,6 +581,9 @@ public void testHandleRegisterRestActionsRequestWithInvalidMethod() throws Excep
452581 initialize (extensionsManager );
453582
454583 String uniqueIdStr = "uniqueid1" ;
584+ extensionsManager .loadExtension (
585+ new Extension ("firstExtension" , uniqueIdStr , "127.0.0.0" , "9300" , "0.0.7" , "3.0.0" , "3.0.0" , List .of (), null )
586+ );
455587 List <String > actionsList = List .of ("FOO /foo" , "PUT /bar" , "POST /baz" );
456588 List <String > deprecatedActionsList = List .of ("GET /deprecated/foo" , "It's deprecated!" );
457589 RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest (uniqueIdStr , actionsList , deprecatedActionsList );
@@ -467,6 +599,9 @@ public void testHandleRegisterRestActionsRequestWithInvalidDeprecatedMethod() th
467599 initialize (extensionsManager );
468600
469601 String uniqueIdStr = "uniqueid1" ;
602+ extensionsManager .loadExtension (
603+ new Extension ("firstExtension" , uniqueIdStr , "127.0.0.0" , "9300" , "0.0.7" , "3.0.0" , "3.0.0" , List .of (), null )
604+ );
470605 List <String > actionsList = List .of ("GET /foo" , "PUT /bar" , "POST /baz" );
471606 List <String > deprecatedActionsList = List .of ("FOO /deprecated/foo" , "It's deprecated!" );
472607 RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest (uniqueIdStr , actionsList , deprecatedActionsList );
@@ -481,6 +616,9 @@ public void testHandleRegisterRestActionsRequestWithInvalidUri() throws Exceptio
481616 ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
482617 initialize (extensionsManager );
483618 String uniqueIdStr = "uniqueid1" ;
619+ extensionsManager .loadExtension (
620+ new Extension ("firstExtension" , uniqueIdStr , "127.0.0.0" , "9300" , "0.0.7" , "3.0.0" , "3.0.0" , List .of (), null )
621+ );
484622 List <String > actionsList = List .of ("GET" , "PUT /bar" , "POST /baz" );
485623 List <String > deprecatedActionsList = List .of ("GET /deprecated/foo" , "It's deprecated!" );
486624 RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest (uniqueIdStr , actionsList , deprecatedActionsList );
@@ -495,6 +633,9 @@ public void testHandleRegisterRestActionsRequestWithInvalidDeprecatedUri() throw
495633 ExtensionsManager extensionsManager = new ExtensionsManager (Set .of (), identityService );
496634 initialize (extensionsManager );
497635 String uniqueIdStr = "uniqueid1" ;
636+ extensionsManager .loadExtension (
637+ new Extension ("firstExtension" , uniqueIdStr , "127.0.0.0" , "9300" , "0.0.7" , "3.0.0" , "3.0.0" , List .of (), null )
638+ );
498639 List <String > actionsList = List .of ("GET /foo" , "PUT /bar" , "POST /baz" );
499640 List <String > deprecatedActionsList = List .of ("GET" , "It's deprecated!" );
500641 RegisterRestActionsRequest registerActionsRequest = new RegisterRestActionsRequest (uniqueIdStr , actionsList , deprecatedActionsList );
0 commit comments