@@ -499,6 +499,118 @@ func TestImports_Validate(t *testing.T) {
499499 }
500500}
501501
502+ func TestImports_Validate_ServiceConflicts (t * testing.T ) {
503+ acctA := publicKey (createAccountNKey (t ), t )
504+ acctB := publicKey (createAccountNKey (t ), t )
505+
506+ testCases := []struct {
507+ test string
508+ imports []* Import
509+ valid bool
510+ }{
511+ {
512+ test : "Same subject from same account" ,
513+ imports : []* Import {
514+ {Account : acctA , Subject : "order.create" , Type : Service },
515+ {Account : acctA , Subject : "order.create" , Type : Service },
516+ },
517+ valid : false ,
518+ },
519+ {
520+ test : "Same subject from same account, to same local subject" ,
521+ imports : []* Import {
522+ {Account : acctA , Subject : "order.create" , LocalSubject : "external.order.create" , Type : Service },
523+ {Account : acctA , Subject : "order.create" , LocalSubject : "external.order.create" , Type : Service },
524+ },
525+ valid : false ,
526+ },
527+ {
528+ test : "Same subject from same account, to same local subject (using To)" ,
529+ imports : []* Import {
530+ {Account : acctA , Subject : "order.create" , To : "external.order.create" , Type : Service },
531+ {Account : acctA , Subject : "order.create" , To : "external.order.create" , Type : Service },
532+ },
533+ valid : false ,
534+ },
535+ {
536+ test : "Same subject from same account, to same local subject (using To and LocalSubject)" ,
537+ imports : []* Import {
538+ {Account : acctA , Subject : "order.create" , To : "external.order.create" , Type : Service },
539+ {Account : acctA , Subject : "order.create" , LocalSubject : "external.order.create" , Type : Service },
540+ },
541+ valid : false ,
542+ },
543+ {
544+ test : "Same subject from same account, but different local subject" ,
545+ imports : []* Import {
546+ {Account : acctA , Subject : "order.create" , LocalSubject : "service-a.order.create" , Type : Service },
547+ {Account : acctA , Subject : "order.create" , LocalSubject : "service-b.order.create" , Type : Service },
548+ },
549+ valid : true ,
550+ },
551+ {
552+ test : "Different subjects from same account, to same local subject" ,
553+ imports : []* Import {
554+ {Account : acctA , Subject : "service-a.ping" , LocalSubject : "deps.ping" , Type : Service },
555+ {Account : acctA , Subject : "service-b.ping" , LocalSubject : "deps.ping" , Type : Service },
556+ },
557+ valid : false , // nats-server does not allow this today, hence it should be rejected here as well.
558+ },
559+ {
560+ test : "Same subject from different accounts" ,
561+ imports : []* Import {
562+ {Account : acctA , Subject : "$JS.FC.>" , Type : Service },
563+ {Account : acctB , Subject : "$JS.FC.>" , Type : Service },
564+ },
565+ valid : true ,
566+ },
567+ {
568+ test : "Same subject from different accounts, to same local subject" ,
569+ imports : []* Import {
570+ {Account : acctA , Subject : "order.create" , LocalSubject : "external.order.create" , Type : Service },
571+ {Account : acctB , Subject : "order.create" , LocalSubject : "external.order.create" , Type : Service },
572+ },
573+ valid : true ,
574+ },
575+ {
576+ test : "Same subject from different accounts, but different local subject" ,
577+ imports : []* Import {
578+ {Account : acctA , Subject : "order.create" , LocalSubject : "external.a.order.create" , Type : Service },
579+ {Account : acctB , Subject : "order.create" , LocalSubject : "external.b.order.create" , Type : Service },
580+ },
581+ valid : true ,
582+ },
583+ {
584+ test : "Different subjects from different accounts, to same local subject" ,
585+ imports : []* Import {
586+ {Account : acctA , Subject : "service-a.ping" , LocalSubject : "deps.ping" , Type : Service },
587+ {Account : acctB , Subject : "service-a.ping" , LocalSubject : "deps.ping" , Type : Service },
588+ },
589+ valid : true ,
590+ },
591+ }
592+
593+ for _ , testCase := range testCases {
594+ t .Run (testCase .test , func (t * testing.T ) {
595+ // Given
596+ var imports Imports
597+ imports .Add (testCase .imports ... )
598+ vr := ValidationResults {}
599+
600+ // When
601+ imports .Validate ("" , & vr )
602+
603+ // Then
604+ valid := ! vr .IsBlocking (true )
605+ if testCase .valid && ! valid {
606+ t .Fatalf ("expected valid, got: %+v" , vr .Issues )
607+ } else if ! testCase .valid && valid {
608+ t .Fatalf ("expected error, got no blocking validation issues" )
609+ }
610+ })
611+ }
612+ }
613+
502614func TestImportAllowTrace (t * testing.T ) {
503615 ak2 := createAccountNKey (t )
504616 akp2 := publicKey (ak2 , t )
0 commit comments