@@ -370,6 +370,142 @@ func TestDescribeInstanceBatching(t *testing.T) {
370370 mockedEC2API .AssertNumberOfCalls (t , "DescribeInstances" , 1 )
371371}
372372
373+ // TestDescribeInstanceBatchingWithInstanceDoesntExist will test where one of the instance doesn't exist
374+ func TestDescribeInstanceBatchingWithInstanceDoesntExist (t * testing.T ) {
375+ mockedEC2API := newMockedEC2API ()
376+ batcher := newdescribeInstanceBatcher (context .Background (), & awsSdkEC2 {ec2 : mockedEC2API })
377+
378+ mockedEC2API .On ("DescribeInstances" , mock .Anything ).Return (& ec2.DescribeInstancesOutput {
379+ Reservations : []ec2types.Reservation {
380+ {
381+ Instances : []ec2types.Instance {
382+ {
383+ InstanceId : aws .String ("Test-1" ),
384+ },
385+ {
386+ InstanceId : aws .String ("Test-2" ),
387+ },
388+ {
389+ InstanceId : aws .String ("Test-3" ),
390+ },
391+ },
392+ },
393+ },
394+ }, nil )
395+
396+ type result struct {
397+ input string
398+ output []* ec2types.Instance
399+ err error
400+ isInstanceNotFound bool
401+ }
402+
403+ // Add extra space to channel so that we can ensure there were only 3 responses
404+ resCh := make (chan result , 5 )
405+ helper := func (wg * sync.WaitGroup , input string , isInstanceNotFound bool ) {
406+ defer wg .Done ()
407+ res , err := batcher .DescribeInstances (context .Background (), & ec2.DescribeInstancesInput {InstanceIds : []string {input }})
408+ resCh <- result {input : input , output : res , err : err , isInstanceNotFound : isInstanceNotFound }
409+ }
410+
411+ wg := sync.WaitGroup {}
412+ wg .Add (3 )
413+ go helper (& wg , "Test-1" , false )
414+ go helper (& wg , "Test-2" , false )
415+ go helper (& wg , "Test-4" , true )
416+ wg .Wait ()
417+ close (resCh )
418+
419+ assert .Len (t , resCh , 3 )
420+ for res := range resCh {
421+ assert .NoError (t , res .err )
422+ if ! res .isInstanceNotFound {
423+ assert .Len (t , res .output , 1 )
424+ assert .Equal (t , res .input , * res .output [0 ].InstanceId )
425+ } else {
426+ assert .Len (t , res .output , 0 )
427+ }
428+ }
429+
430+ mockedEC2API .AssertNumberOfCalls (t , "DescribeInstances" , 1 )
431+ }
432+
433+ // TestDescribeInstanceBatchingWithBatchedRequestFail will test where batched request fails but individual request succeeds
434+ func TestDescribeInstanceBatchingWithBatchedRequestFail (t * testing.T ) {
435+ mockedEC2API := newMockedEC2API ()
436+ batcher := newdescribeInstanceBatcher (context .Background (), & awsSdkEC2 {ec2 : mockedEC2API })
437+ var nilResp * ec2.DescribeInstancesOutput
438+ mockedEC2API .On ("DescribeInstances" , & ec2.DescribeInstancesInput {
439+ InstanceIds : []string {"Test-1" , "Test-2" },
440+ }).Return (nilResp , errors .New ("instances not found" ))
441+ mockedEC2API .On ("DescribeInstances" , & ec2.DescribeInstancesInput {
442+ InstanceIds : []string {"Test-2" , "Test-1" },
443+ }).Return (nilResp , errors .New ("instances not found" ))
444+ mockedEC2API .On ("DescribeInstances" ,
445+ & ec2.DescribeInstancesInput {
446+ InstanceIds : []string {"Test-1" },
447+ },
448+ ).Return (
449+ & ec2.DescribeInstancesOutput {
450+ Reservations : []ec2types.Reservation {
451+ {
452+ Instances : []ec2types.Instance {
453+ {
454+ InstanceId : aws .String ("Test-1" ),
455+ },
456+ },
457+ },
458+ },
459+ },
460+ nil ,
461+ )
462+ mockedEC2API .On ("DescribeInstances" ,
463+ & ec2.DescribeInstancesInput {
464+ InstanceIds : []string {"Test-2" },
465+ },
466+ ).Return (
467+ & ec2.DescribeInstancesOutput {
468+ Reservations : []ec2types.Reservation {},
469+ },
470+ nil ,
471+ )
472+
473+ type result struct {
474+ input string
475+ output []* ec2types.Instance
476+ err error
477+ isInstanceNotFound bool
478+ }
479+
480+ // Add extra space to channel so that we can ensure there were only 3 responses
481+ resCh := make (chan result , 5 )
482+ helper := func (wg * sync.WaitGroup , input string , isInstanceNotFound bool ) {
483+ defer wg .Done ()
484+ res , err := batcher .DescribeInstances (context .Background (), & ec2.DescribeInstancesInput {InstanceIds : []string {input }})
485+ resCh <- result {input : input , output : res , err : err , isInstanceNotFound : isInstanceNotFound }
486+ }
487+
488+ wg := sync.WaitGroup {}
489+ wg .Add (2 )
490+ go helper (& wg , "Test-1" , false )
491+ go helper (& wg , "Test-2" , true )
492+ wg .Wait ()
493+ close (resCh )
494+
495+ assert .Len (t , resCh , 2 )
496+ for res := range resCh {
497+ assert .NoError (t , res .err )
498+ if ! res .isInstanceNotFound {
499+ assert .Len (t , res .output , 1 )
500+ assert .Equal (t , res .input , * res .output [0 ].InstanceId )
501+ } else {
502+ assert .Len (t , res .output , 0 )
503+ }
504+ }
505+
506+ mockedEC2API .AssertNumberOfCalls (t , "DescribeInstances" , 3 )
507+ }
508+
373509func getCloudWithMockedDescribeInstances (instanceExists bool , instanceState ec2types.InstanceStateName , instanceID string ) * Cloud {
374510 mockedEC2API := newMockedEC2API ()
375511 c := & Cloud {ec2 : & awsSdkEC2 {ec2 : mockedEC2API }, describeInstanceBatcher : newdescribeInstanceBatcher (context .Background (), & awsSdkEC2 {ec2 : mockedEC2API })}
0 commit comments