@@ -10,16 +10,18 @@ import {
1010import {
1111 DescribeParametersCommand ,
1212 DescribeParametersCommandOutput ,
13- GetParametersCommand ,
1413 SSMClient ,
1514} from '@aws-sdk/client-ssm' ;
15+ import { getParameters } from '@aws-github-runner/aws-ssm-util' ;
1616import { mockClient } from 'aws-sdk-client-mock' ;
1717import 'aws-sdk-client-mock-jest/vitest' ;
1818
1919import { AmiCleanupOptions , amiCleanup , defaultAmiCleanupOptions } from './ami' ;
2020import { describe , it , expect , beforeEach , vi } from 'vitest' ;
2121import { fail } from 'assert' ;
2222
23+ vi . mock ( '@aws-github-runner/aws-ssm-util' ) ;
24+
2325process . env . AWS_REGION = 'eu-east-1' ;
2426const deleteAmisOlderThenDays = 30 ;
2527const date31DaysAgo = new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) - ( deleteAmisOlderThenDays + 1 ) ) ) ;
@@ -83,23 +85,12 @@ describe("delete AMI's", () => {
8385 mockSSMClient . reset ( ) ;
8486
8587 mockSSMClient . on ( DescribeParametersCommand ) . resolves ( ssmParameters ) ;
86- mockSSMClient . on ( GetParametersCommand ) . resolves ( {
87- Parameters : [
88- {
89- Name : 'ami-id/ami-ssm0001' ,
90- Type : 'String' ,
91- Value : 'ami-ssm0001' ,
92- Version : 1 ,
93- } ,
94- {
95- Name : 'ami-id/ami-ssm0002' ,
96- Type : 'String' ,
97- Value : 'ami-ssm0002' ,
98- Version : 1 ,
99- } ,
100- ] ,
101- InvalidParameters : [ ] ,
102- } ) ;
88+ vi . mocked ( getParameters ) . mockResolvedValue (
89+ new Map ( [
90+ [ 'ami-id/ami-ssm0001' , 'ami-ssm0001' ] ,
91+ [ 'ami-id/ami-ssm0002' , 'ami-ssm0002' ] ,
92+ ] ) ,
93+ ) ;
10394
10495 mockEC2Client . on ( DescribeLaunchTemplatesCommand ) . resolves ( {
10596 LaunchTemplates : [
@@ -144,11 +135,7 @@ describe("delete AMI's", () => {
144135 expect ( mockEC2Client ) . toHaveReceivedCommand ( DescribeLaunchTemplatesCommand ) ;
145136 expect ( mockEC2Client ) . toHaveReceivedCommand ( DescribeLaunchTemplateVersionsCommand ) ;
146137 expect ( mockSSMClient ) . toHaveReceivedCommand ( DescribeParametersCommand ) ;
147- expect ( mockSSMClient ) . toHaveReceivedCommandTimes ( GetParametersCommand , 1 ) ;
148- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
149- Names : [ 'ami-id/ami-ssm0001' , 'ami-id/ami-ssm0002' ] ,
150- WithDecryption : true ,
151- } ) ;
138+ expect ( getParameters ) . toHaveBeenCalledWith ( [ 'ami-id/ami-ssm0001' , 'ami-id/ami-ssm0002' ] ) ;
152139 } ) ;
153140
154141 it ( 'should NOT delete instances in use.' , async ( ) => {
@@ -484,17 +471,9 @@ describe("delete AMI's", () => {
484471 ] ,
485472 } ) ;
486473
487- mockSSMClient . on ( GetParametersCommand ) . resolves ( {
488- Parameters : [
489- {
490- Name : '/github-runner/config/ami_id' ,
491- Type : 'String' ,
492- Value : 'ami-underscore0001' ,
493- Version : 1 ,
494- } ,
495- ] ,
496- InvalidParameters : [ ] ,
497- } ) ;
474+ vi . mocked ( getParameters ) . mockResolvedValue (
475+ new Map ( [ [ '/github-runner/config/ami_id' , 'ami-underscore0001' ] ] ) ,
476+ ) ;
498477
499478 await amiCleanup ( {
500479 minimumDaysOld : 0 ,
@@ -503,10 +482,7 @@ describe("delete AMI's", () => {
503482
504483 // AMI should not be deleted because it's referenced in SSM
505484 expect ( mockEC2Client ) . not . toHaveReceivedCommand ( DeregisterImageCommand ) ;
506- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
507- Names : [ '/github-runner/config/ami_id' ] ,
508- WithDecryption : true ,
509- } ) ;
485+ expect ( getParameters ) . toHaveBeenCalledWith ( [ '/github-runner/config/ami_id' ] ) ;
510486 expect ( mockSSMClient ) . not . toHaveReceivedCommand ( DescribeParametersCommand ) ;
511487 } ) ;
512488
@@ -521,17 +497,9 @@ describe("delete AMI's", () => {
521497 ] ,
522498 } ) ;
523499
524- mockSSMClient . on ( GetParametersCommand ) . resolves ( {
525- Parameters : [
526- {
527- Name : '/github-runner/config/ami-id' ,
528- Type : 'String' ,
529- Value : 'ami-hyphen0001' ,
530- Version : 1 ,
531- } ,
532- ] ,
533- InvalidParameters : [ ] ,
534- } ) ;
500+ vi . mocked ( getParameters ) . mockResolvedValue (
501+ new Map ( [ [ '/github-runner/config/ami-id' , 'ami-hyphen0001' ] ] ) ,
502+ ) ;
535503
536504 await amiCleanup ( {
537505 minimumDaysOld : 0 ,
@@ -540,10 +508,7 @@ describe("delete AMI's", () => {
540508
541509 // AMI should not be deleted because it's referenced in SSM
542510 expect ( mockEC2Client ) . not . toHaveReceivedCommand ( DeregisterImageCommand ) ;
543- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
544- Names : [ '/github-runner/config/ami-id' ] ,
545- WithDecryption : true ,
546- } ) ;
511+ expect ( getParameters ) . toHaveBeenCalledWith ( [ '/github-runner/config/ami-id' ] ) ;
547512 expect ( mockSSMClient ) . not . toHaveReceivedCommand ( DescribeParametersCommand ) ;
548513 } ) ;
549514
@@ -568,17 +533,9 @@ describe("delete AMI's", () => {
568533 ] ,
569534 } ) ;
570535
571- mockSSMClient . on ( GetParametersCommand ) . resolves ( {
572- Parameters : [
573- {
574- Name : '/some/path/ami-id' ,
575- Type : 'String' ,
576- Value : 'ami-wildcard0001' ,
577- Version : 1 ,
578- } ,
579- ] ,
580- InvalidParameters : [ ] ,
581- } ) ;
536+ vi . mocked ( getParameters ) . mockResolvedValue (
537+ new Map ( [ [ '/some/path/ami-id' , 'ami-wildcard0001' ] ] ) ,
538+ ) ;
582539
583540 await amiCleanup ( {
584541 minimumDaysOld : 0 ,
@@ -590,10 +547,7 @@ describe("delete AMI's", () => {
590547 expect ( mockSSMClient ) . toHaveReceivedCommandWith ( DescribeParametersCommand , {
591548 ParameterFilters : [ { Key : 'Name' , Option : 'Contains' , Values : [ 'ami-id' ] } ] ,
592549 } ) ;
593- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
594- Names : [ '/some/path/ami-id' ] ,
595- WithDecryption : true ,
596- } ) ;
550+ expect ( getParameters ) . toHaveBeenCalledWith ( [ '/some/path/ami-id' ] ) ;
597551 } ) ;
598552
599553 it ( 'handles wildcard SSM parameter patterns (*ami_id)' , async ( ) => {
@@ -617,17 +571,9 @@ describe("delete AMI's", () => {
617571 ] ,
618572 } ) ;
619573
620- mockSSMClient . on ( GetParametersCommand ) . resolves ( {
621- Parameters : [
622- {
623- Name : '/github-runner/config/ami_id' ,
624- Type : 'String' ,
625- Value : 'ami-wildcard-underscore0001' ,
626- Version : 1 ,
627- } ,
628- ] ,
629- InvalidParameters : [ ] ,
630- } ) ;
574+ vi . mocked ( getParameters ) . mockResolvedValue (
575+ new Map ( [ [ '/github-runner/config/ami_id' , 'ami-wildcard-underscore0001' ] ] ) ,
576+ ) ;
631577
632578 await amiCleanup ( {
633579 minimumDaysOld : 0 ,
@@ -639,10 +585,7 @@ describe("delete AMI's", () => {
639585 expect ( mockSSMClient ) . toHaveReceivedCommandWith ( DescribeParametersCommand , {
640586 ParameterFilters : [ { Key : 'Name' , Option : 'Contains' , Values : [ 'ami_id' ] } ] ,
641587 } ) ;
642- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
643- Names : [ '/github-runner/config/ami_id' ] ,
644- WithDecryption : true ,
645- } ) ;
588+ expect ( getParameters ) . toHaveBeenCalledWith ( [ '/github-runner/config/ami_id' ] ) ;
646589 } ) ;
647590
648591 it ( 'handles mixed explicit names and wildcard patterns' , async ( ) => {
@@ -664,17 +607,9 @@ describe("delete AMI's", () => {
664607 ] ,
665608 } ) ;
666609
667- mockSSMClient . on ( GetParametersCommand , { Names : [ '/explicit/ami_id' ] , WithDecryption : true } ) . resolves ( {
668- Parameters : [
669- {
670- Name : '/explicit/ami_id' ,
671- Type : 'String' ,
672- Value : 'ami-explicit0001' ,
673- Version : 1 ,
674- } ,
675- ] ,
676- InvalidParameters : [ ] ,
677- } ) ;
610+ vi . mocked ( getParameters )
611+ . mockResolvedValueOnce ( new Map ( [ [ '/explicit/ami_id' , 'ami-explicit0001' ] ] ) )
612+ . mockResolvedValueOnce ( new Map ( [ [ '/discovered/ami-id' , 'ami-wildcard0001' ] ] ) ) ;
678613
679614 mockSSMClient . on ( DescribeParametersCommand ) . resolves ( {
680615 Parameters : [
@@ -686,18 +621,6 @@ describe("delete AMI's", () => {
686621 ] ,
687622 } ) ;
688623
689- mockSSMClient . on ( GetParametersCommand , { Names : [ '/discovered/ami-id' ] , WithDecryption : true } ) . resolves ( {
690- Parameters : [
691- {
692- Name : '/discovered/ami-id' ,
693- Type : 'String' ,
694- Value : 'ami-wildcard0001' ,
695- Version : 1 ,
696- } ,
697- ] ,
698- InvalidParameters : [ ] ,
699- } ) ;
700-
701624 await amiCleanup ( {
702625 minimumDaysOld : 0 ,
703626 ssmParameterNames : [ '/explicit/ami_id' , '*ami-id' ] ,
@@ -709,17 +632,11 @@ describe("delete AMI's", () => {
709632 ImageId : 'ami-unused0001' ,
710633 } ) ;
711634
712- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
713- Names : [ '/explicit/ami_id' ] ,
714- WithDecryption : true ,
715- } ) ;
635+ expect ( getParameters ) . toHaveBeenCalledWith ( [ '/explicit/ami_id' ] ) ;
716636 expect ( mockSSMClient ) . toHaveReceivedCommandWith ( DescribeParametersCommand , {
717637 ParameterFilters : [ { Key : 'Name' , Option : 'Contains' , Values : [ 'ami-id' ] } ] ,
718638 } ) ;
719- expect ( mockSSMClient ) . toHaveReceivedCommandWith ( GetParametersCommand , {
720- Names : [ '/discovered/ami-id' ] ,
721- WithDecryption : true ,
722- } ) ;
639+ expect ( getParameters ) . toHaveBeenCalledWith ( [ '/discovered/ami-id' ] ) ;
723640 } ) ;
724641
725642 it ( 'handles SSM parameter fetch failures gracefully' , async ( ) => {
@@ -733,7 +650,7 @@ describe("delete AMI's", () => {
733650 ] ,
734651 } ) ;
735652
736- mockSSMClient . on ( GetParametersCommand ) . rejects ( new Error ( 'ParameterNotFound' ) ) ;
653+ vi . mocked ( getParameters ) . mockRejectedValue ( new Error ( 'ParameterNotFound' ) ) ;
737654
738655 // Should not throw and should delete the AMI since SSM reference failed
739656 await amiCleanup ( {
@@ -791,7 +708,7 @@ describe("delete AMI's", () => {
791708 ImageId : 'ami-no-ssm0001' ,
792709 } ) ;
793710 expect ( mockSSMClient ) . not . toHaveReceivedCommand ( DescribeParametersCommand ) ;
794- expect ( mockSSMClient ) . not . toHaveReceivedCommand ( GetParametersCommand ) ;
711+ expect ( getParameters ) . not . toHaveBeenCalled ( ) ;
795712 } ) ;
796713 } ) ;
797714} ) ;
0 commit comments