@@ -15,41 +15,53 @@ const ORG_NAME = 'SomeAwesomeCoder';
1515const REPO_NAME = `${ ORG_NAME } /some-amazing-library` ;
1616const ENVIRONMENT = 'unit-test-environment' ;
1717
18- describe ( 'list instances' , ( ) => {
19- const mockDescribeInstances = { promise : jest . fn ( ) } ;
20- beforeEach ( ( ) => {
21- jest . clearAllMocks ( ) ;
22- mockEC2 . describeInstances . mockImplementation ( ( ) => mockDescribeInstances ) ;
23- const mockRunningInstances : AWS . EC2 . DescribeInstancesResult = {
24- Reservations : [
18+ const mockDescribeInstances = { promise : jest . fn ( ) } ;
19+ mockEC2 . describeInstances . mockImplementation ( ( ) => mockDescribeInstances ) ;
20+ const mockRunningInstances : AWS . EC2 . DescribeInstancesResult = {
21+ Reservations : [
22+ {
23+ Instances : [
2524 {
26- Instances : [
27- {
28- LaunchTime : new Date ( '2020-10-10T14:48:00.000+09:00' ) ,
29- InstanceId : 'i-1234' ,
30- Tags : [
31- { Key : 'Application' , Value : 'github-action-runner' } ,
32- { Key : 'Type' , Value : 'Org' } ,
33- { Key : 'Owner' , Value : 'CoderToCat' } ,
34- ] ,
35- } ,
36- {
37- LaunchTime : new Date ( '2020-10-11T14:48:00.000+09:00' ) ,
38- InstanceId : 'i-5678' ,
39- Tags : [
40- { Key : 'Owner' , Value : REPO_NAME } ,
41- { Key : 'Type' , Value : 'Repo' } ,
42- { Key : 'Application' , Value : 'github-action-runner' } ,
43- ] ,
44- } ,
25+ LaunchTime : new Date ( '2020-10-10T14:48:00.000+09:00' ) ,
26+ InstanceId : 'i-1234' ,
27+ Tags : [
28+ { Key : 'ghr:Application' , Value : 'github-action-runner' } ,
29+ { Key : 'Type' , Value : 'Org' } ,
30+ { Key : 'Owner' , Value : 'CoderToCat' } ,
4531 ] ,
4632 } ,
4733 ] ,
48- } ;
49- mockDescribeInstances . promise . mockReturnValue ( mockRunningInstances ) ;
34+ } ,
35+ ] ,
36+ } ;
37+ const mockRunningInstancesLegacy : AWS . EC2 . DescribeInstancesResult = {
38+ Reservations : [
39+ {
40+ Instances : [
41+ {
42+ LaunchTime : new Date ( '2020-10-11T14:48:00.000+09:00' ) ,
43+ InstanceId : 'i-5678' ,
44+ Tags : [
45+ { Key : 'Owner' , Value : REPO_NAME } ,
46+ { Key : 'Type' , Value : 'Repo' } ,
47+ { Key : 'Application' , Value : 'github-action-runner' } ,
48+ ] ,
49+ } ,
50+ ] ,
51+ } ,
52+ ] ,
53+ } ;
54+
55+ describe ( 'list instances' , ( ) => {
56+ beforeEach ( ( ) => {
57+ jest . resetModules ( ) ;
58+ jest . clearAllMocks ( ) ;
5059 } ) ;
5160
5261 it ( 'returns a list of instances' , async ( ) => {
62+ mockDescribeInstances . promise
63+ . mockReturnValueOnce ( mockRunningInstances )
64+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
5365 const resp = await listEC2Runners ( ) ;
5466 expect ( resp . length ) . toBe ( 2 ) ;
5567 expect ( resp ) . toContainEqual ( {
@@ -67,41 +79,61 @@ describe('list instances', () => {
6779 } ) ;
6880
6981 it ( 'calls EC2 describe instances' , async ( ) => {
82+ mockDescribeInstances . promise
83+ . mockReturnValueOnce ( mockRunningInstances )
84+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
7085 await listEC2Runners ( ) ;
7186 expect ( mockEC2 . describeInstances ) . toBeCalled ( ) ;
7287 } ) ;
7388
7489 it ( 'filters instances on repo name' , async ( ) => {
90+ mockDescribeInstances . promise
91+ . mockReturnValueOnce ( mockRunningInstances )
92+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
7593 await listEC2Runners ( { runnerType : 'Repo' , runnerOwner : REPO_NAME , environment : undefined } ) ;
7694 expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
7795 Filters : [
78- { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
7996 { Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
8097 { Name : 'tag:Type' , Values : [ 'Repo' ] } ,
8198 { Name : 'tag:Owner' , Values : [ REPO_NAME ] } ,
99+ { Name : 'tag:ghr:Application' , Values : [ 'github-action-runner' ] } ,
100+ ] ,
101+ } ) ;
102+ expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
103+ Filters : [
104+ { Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
105+ { Name : 'tag:Type' , Values : [ 'Repo' ] } ,
106+ { Name : 'tag:Owner' , Values : [ REPO_NAME ] } ,
107+ { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
82108 ] ,
83109 } ) ;
84110 } ) ;
85111
86112 it ( 'filters instances on org name' , async ( ) => {
113+ mockDescribeInstances . promise
114+ . mockReturnValueOnce ( mockRunningInstances )
115+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
87116 await listEC2Runners ( { runnerType : 'Org' , runnerOwner : ORG_NAME , environment : undefined } ) ;
88117 expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
89118 Filters : [
90- { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
91119 { Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
92120 { Name : 'tag:Type' , Values : [ 'Org' ] } ,
93121 { Name : 'tag:Owner' , Values : [ ORG_NAME ] } ,
122+ { Name : 'tag:ghr:Application' , Values : [ 'github-action-runner' ] } ,
94123 ] ,
95124 } ) ;
96125 } ) ;
97126
98127 it ( 'filters instances on environment' , async ( ) => {
128+ mockDescribeInstances . promise
129+ . mockReturnValueOnce ( mockRunningInstances )
130+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
99131 await listEC2Runners ( { environment : ENVIRONMENT } ) ;
100132 expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
101133 Filters : [
102- { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
103134 { Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
104135 { Name : 'tag:ghr:environment' , Values : [ ENVIRONMENT ] } ,
136+ { Name : 'tag:ghr:Application' , Values : [ 'github-action-runner' ] } ,
105137 ] ,
106138 } ) ;
107139 } ) ;
@@ -123,7 +155,7 @@ describe('list instances', () => {
123155 } ,
124156 ] ,
125157 } ;
126- mockDescribeInstances . promise . mockReturnValue ( noInstances ) ;
158+ mockDescribeInstances . promise . mockReturnValueOnce ( noInstances ) . mockReturnValueOnce ( noInstances ) ;
127159 const resp = await listEC2Runners ( ) ;
128160 expect ( resp . length ) . toBe ( 0 ) ;
129161 } ) ;
@@ -142,7 +174,7 @@ describe('list instances', () => {
142174 } ,
143175 ] ,
144176 } ;
145- mockDescribeInstances . promise . mockReturnValue ( noInstances ) ;
177+ mockDescribeInstances . promise . mockReturnValueOnce ( noInstances ) . mockReturnValue ( { } ) ;
146178 const resp = await listEC2Runners ( ) ;
147179 expect ( resp . length ) . toBe ( 1 ) ;
148180 } ) ;
@@ -459,7 +491,7 @@ function expectedCreateFleetRequest(expectedValues: ExpectedFleetRequestValues):
459491 {
460492 ResourceType : 'instance' ,
461493 Tags : [
462- { Key : 'Application' , Value : 'github-action-runner' } ,
494+ { Key : 'ghr: Application' , Value : 'github-action-runner' } ,
463495 { Key : 'Type' , Value : expectedValues . type } ,
464496 { Key : 'Owner' , Value : REPO_NAME } ,
465497 ] ,
0 commit comments