@@ -15,41 +15,53 @@ const ORG_NAME = 'SomeAwesomeCoder';
15
15
const REPO_NAME = `${ ORG_NAME } /some-amazing-library` ;
16
16
const ENVIRONMENT = 'unit-test-environment' ;
17
17
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 : [
25
24
{
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' } ,
45
31
] ,
46
32
} ,
47
33
] ,
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 ( ) ;
50
59
} ) ;
51
60
52
61
it ( 'returns a list of instances' , async ( ) => {
62
+ mockDescribeInstances . promise
63
+ . mockReturnValueOnce ( mockRunningInstances )
64
+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
53
65
const resp = await listEC2Runners ( ) ;
54
66
expect ( resp . length ) . toBe ( 2 ) ;
55
67
expect ( resp ) . toContainEqual ( {
@@ -67,41 +79,61 @@ describe('list instances', () => {
67
79
} ) ;
68
80
69
81
it ( 'calls EC2 describe instances' , async ( ) => {
82
+ mockDescribeInstances . promise
83
+ . mockReturnValueOnce ( mockRunningInstances )
84
+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
70
85
await listEC2Runners ( ) ;
71
86
expect ( mockEC2 . describeInstances ) . toBeCalled ( ) ;
72
87
} ) ;
73
88
74
89
it ( 'filters instances on repo name' , async ( ) => {
90
+ mockDescribeInstances . promise
91
+ . mockReturnValueOnce ( mockRunningInstances )
92
+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
75
93
await listEC2Runners ( { runnerType : 'Repo' , runnerOwner : REPO_NAME , environment : undefined } ) ;
76
94
expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
77
95
Filters : [
78
- { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
79
96
{ Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
80
97
{ Name : 'tag:Type' , Values : [ 'Repo' ] } ,
81
98
{ 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' ] } ,
82
108
] ,
83
109
} ) ;
84
110
} ) ;
85
111
86
112
it ( 'filters instances on org name' , async ( ) => {
113
+ mockDescribeInstances . promise
114
+ . mockReturnValueOnce ( mockRunningInstances )
115
+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
87
116
await listEC2Runners ( { runnerType : 'Org' , runnerOwner : ORG_NAME , environment : undefined } ) ;
88
117
expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
89
118
Filters : [
90
- { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
91
119
{ Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
92
120
{ Name : 'tag:Type' , Values : [ 'Org' ] } ,
93
121
{ Name : 'tag:Owner' , Values : [ ORG_NAME ] } ,
122
+ { Name : 'tag:ghr:Application' , Values : [ 'github-action-runner' ] } ,
94
123
] ,
95
124
} ) ;
96
125
} ) ;
97
126
98
127
it ( 'filters instances on environment' , async ( ) => {
128
+ mockDescribeInstances . promise
129
+ . mockReturnValueOnce ( mockRunningInstances )
130
+ . mockReturnValueOnce ( mockRunningInstancesLegacy ) ;
99
131
await listEC2Runners ( { environment : ENVIRONMENT } ) ;
100
132
expect ( mockEC2 . describeInstances ) . toBeCalledWith ( {
101
133
Filters : [
102
- { Name : 'tag:Application' , Values : [ 'github-action-runner' ] } ,
103
134
{ Name : 'instance-state-name' , Values : [ 'running' , 'pending' ] } ,
104
135
{ Name : 'tag:ghr:environment' , Values : [ ENVIRONMENT ] } ,
136
+ { Name : 'tag:ghr:Application' , Values : [ 'github-action-runner' ] } ,
105
137
] ,
106
138
} ) ;
107
139
} ) ;
@@ -123,7 +155,7 @@ describe('list instances', () => {
123
155
} ,
124
156
] ,
125
157
} ;
126
- mockDescribeInstances . promise . mockReturnValue ( noInstances ) ;
158
+ mockDescribeInstances . promise . mockReturnValueOnce ( noInstances ) . mockReturnValueOnce ( noInstances ) ;
127
159
const resp = await listEC2Runners ( ) ;
128
160
expect ( resp . length ) . toBe ( 0 ) ;
129
161
} ) ;
@@ -142,7 +174,7 @@ describe('list instances', () => {
142
174
} ,
143
175
] ,
144
176
} ;
145
- mockDescribeInstances . promise . mockReturnValue ( noInstances ) ;
177
+ mockDescribeInstances . promise . mockReturnValueOnce ( noInstances ) . mockReturnValue ( { } ) ;
146
178
const resp = await listEC2Runners ( ) ;
147
179
expect ( resp . length ) . toBe ( 1 ) ;
148
180
} ) ;
@@ -459,7 +491,7 @@ function expectedCreateFleetRequest(expectedValues: ExpectedFleetRequestValues):
459
491
{
460
492
ResourceType : 'instance' ,
461
493
Tags : [
462
- { Key : 'Application' , Value : 'github-action-runner' } ,
494
+ { Key : 'ghr: Application' , Value : 'github-action-runner' } ,
463
495
{ Key : 'Type' , Value : expectedValues . type } ,
464
496
{ Key : 'Owner' , Value : REPO_NAME } ,
465
497
] ,
0 commit comments