@@ -10,104 +10,62 @@ import { SsmClient } from '../../shared/clients/ssmClient'
10
10
import { Ec2Client } from '../../shared/clients/ec2Client'
11
11
import { Ec2Selection } from '../../ec2/utils'
12
12
import { ToolkitError } from '../../shared/errors'
13
- import { AWSError , EC2 , IAM , SSM } from 'aws-sdk'
14
- import { PromiseResult } from 'aws-sdk/lib/request'
15
- import { GetCommandInvocationResult } from 'aws-sdk/clients/ssm'
13
+ import { IAM } from 'aws-sdk'
16
14
import { mock } from 'ts-mockito'
17
15
import { SshKeyPair } from '../../ec2/sshKeyPair'
18
16
import { DefaultIamClient } from '../../shared/clients/iamClient'
19
17
20
18
describe ( 'Ec2ConnectClient' , function ( ) {
21
- let currentInstanceOs : string
22
- class MockSsmClient extends SsmClient {
23
- public constructor ( ) {
24
- super ( 'test-region' )
25
- }
26
-
27
- public override async getInstanceAgentPingStatus ( target : string ) : Promise < string > {
28
- return target . split ( ':' ) [ 2 ]
29
- }
30
-
31
- public override async getTargetPlatformName ( target : string ) : Promise < string > {
32
- return currentInstanceOs
33
- }
34
- }
35
-
36
- class MockEc2Client extends Ec2Client {
37
- public constructor ( ) {
38
- super ( 'test-region' )
39
- }
40
-
41
- public override async getInstanceStatus ( instanceId : string ) : Promise < EC2 . InstanceStateName > {
42
- return instanceId . split ( ':' ) [ 0 ] as EC2 . InstanceStateName
43
- }
44
- }
45
-
46
- class MockEc2ConnectClient extends Ec2ConnectionManager {
47
- public constructor ( ) {
48
- super ( 'test-region' )
49
- }
50
-
51
- protected override createSsmSdkClient ( ) : SsmClient {
52
- return new MockSsmClient ( )
53
- }
54
-
55
- protected override createEc2SdkClient ( ) : Ec2Client {
56
- return new MockEc2Client ( )
57
- }
58
-
59
- public async testGetRemoteUser ( instanceId : string , osName : string ) {
60
- currentInstanceOs = osName
61
- const remoteUser = await this . getRemoteUser ( instanceId )
62
- return remoteUser
63
- }
64
- }
19
+ let client : Ec2ConnectionManager
65
20
66
- describe ( 'isInstanceRunning' , async function ( ) {
67
- let client : MockEc2ConnectClient
68
-
69
- before ( function ( ) {
70
- client = new MockEc2ConnectClient ( )
71
- } )
21
+ before ( function ( ) {
22
+ client = new Ec2ConnectionManager ( 'test-region' )
23
+ } )
72
24
25
+ describe ( 'isInstanceRunning' , async function ( ) {
73
26
it ( 'only returns true with the instance is running' , async function ( ) {
74
- const actualFirstResult = await client . isInstanceRunning ( 'running:noPolicies' )
75
- const actualSecondResult = await client . isInstanceRunning ( 'stopped:noPolicies' )
27
+ sinon . stub ( Ec2Client . prototype , 'getInstanceStatus' ) . callsFake ( async ( input : string ) => input . split ( ':' ) [ 0 ] )
28
+
29
+ const actualFirstResult = await client . isInstanceRunning ( 'running:instance' )
30
+ const actualSecondResult = await client . isInstanceRunning ( 'stopped:instance' )
76
31
77
32
assert . strictEqual ( true , actualFirstResult )
78
33
assert . strictEqual ( false , actualSecondResult )
34
+ sinon . restore ( )
79
35
} )
80
36
} )
81
37
82
38
describe ( 'handleStartSessionError' , async function ( ) {
83
- let client : Ec2ConnectionManager
84
39
let instanceSelection : Ec2Selection
85
40
86
41
before ( function ( ) {
87
- client = new Ec2ConnectionManager ( 'test-region' )
88
42
instanceSelection = { instanceId : 'testInstance' , region : 'testRegion' }
89
43
} )
90
44
91
45
it ( 'throws EC2SSMStatus error if instance is not running' , async function ( ) {
92
46
sinon . stub ( Ec2ConnectionManager . prototype , 'isInstanceRunning' ) . resolves ( false )
47
+
93
48
try {
94
49
await client . checkForStartSessionError ( instanceSelection )
95
50
assert . ok ( false )
96
51
} catch ( err ) {
97
52
assert . strictEqual ( ( err as ToolkitError ) . code , 'EC2SSMStatus' )
98
53
}
54
+
99
55
sinon . restore ( )
100
56
} )
101
57
102
58
it ( 'throws EC2SSMPermission error if instance is running but has no role' , async function ( ) {
103
59
sinon . stub ( Ec2ConnectionManager . prototype , 'isInstanceRunning' ) . resolves ( true )
104
60
sinon . stub ( Ec2ConnectionManager . prototype , 'getAttachedIamRole' ) . resolves ( undefined )
61
+
105
62
try {
106
63
await client . checkForStartSessionError ( instanceSelection )
107
64
assert . ok ( false )
108
65
} catch ( err ) {
109
66
assert . strictEqual ( ( err as ToolkitError ) . code , 'EC2SSMPermission' )
110
67
}
68
+
111
69
sinon . restore ( )
112
70
} )
113
71
@@ -116,12 +74,14 @@ describe('Ec2ConnectClient', function () {
116
74
sinon . stub ( Ec2ConnectionManager . prototype , 'getAttachedIamRole' ) . resolves ( { Arn : 'testRole' } as IAM . Role )
117
75
sinon . stub ( Ec2ConnectionManager . prototype , 'hasProperPolicies' ) . resolves ( true )
118
76
sinon . stub ( SsmClient . prototype , 'getInstanceAgentPingStatus' ) . resolves ( 'offline' )
77
+
119
78
try {
120
79
await client . checkForStartSessionError ( instanceSelection )
121
80
assert . ok ( false )
122
81
} catch ( err ) {
123
82
assert . strictEqual ( ( err as ToolkitError ) . code , 'EC2SSMAgentStatus' )
124
83
}
84
+
125
85
sinon . restore ( )
126
86
} )
127
87
@@ -130,23 +90,21 @@ describe('Ec2ConnectClient', function () {
130
90
sinon . stub ( Ec2ConnectionManager . prototype , 'getAttachedIamRole' ) . resolves ( { Arn : 'testRole' } as IAM . Role )
131
91
sinon . stub ( Ec2ConnectionManager . prototype , 'hasProperPolicies' ) . resolves ( true )
132
92
sinon . stub ( SsmClient . prototype , 'getInstanceAgentPingStatus' ) . resolves ( 'Online' )
93
+
133
94
assert . doesNotThrow ( async ( ) => await client . checkForStartSessionError ( instanceSelection ) )
95
+
134
96
sinon . restore ( )
135
97
} )
136
98
} )
137
99
138
100
describe ( 'hasProperPolicies' , async function ( ) {
139
- let realClient : Ec2ConnectionManager
140
-
141
- before ( async function ( ) {
142
- realClient = new Ec2ConnectionManager ( 'test-region' )
143
- } )
144
-
145
101
it ( 'correctly determines if proper policies are included' , async function ( ) {
146
102
async function assertAcceptsPolicies ( policies : IAM . Policy [ ] , expectedResult : boolean ) {
147
103
sinon . stub ( DefaultIamClient . prototype , 'listAttachedRolePolicies' ) . resolves ( policies )
148
- const result = await realClient . hasProperPolicies ( '' )
104
+
105
+ const result = await client . hasProperPolicies ( '' )
149
106
assert . strictEqual ( result , expectedResult )
107
+
150
108
sinon . restore ( )
151
109
}
152
110
await assertAcceptsPolicies (
@@ -166,63 +124,60 @@ describe('Ec2ConnectClient', function () {
166
124
167
125
it ( 'throws error when sdk throws error' , async function ( ) {
168
126
sinon . stub ( DefaultIamClient . prototype , 'listAttachedRolePolicies' ) . throws ( new ToolkitError ( 'error' ) )
127
+
169
128
try {
170
- await realClient . hasProperPolicies ( '' )
129
+ await client . hasProperPolicies ( '' )
171
130
assert . ok ( false )
172
131
} catch {
173
132
assert . ok ( true )
174
133
}
134
+
175
135
sinon . restore ( )
176
136
} )
177
137
} )
178
138
179
139
describe ( 'sendSshKeysToInstance' , async function ( ) {
180
- let client : MockEc2ConnectClient
181
- let sendCommandStub : sinon . SinonStub <
182
- [ target : string , documentName : string , parameters : SSM . Parameters ] ,
183
- Promise < PromiseResult < GetCommandInvocationResult , AWSError > >
184
- >
185
-
186
- before ( function ( ) {
187
- client = new MockEc2ConnectClient ( )
188
- sendCommandStub = sinon . stub ( MockSsmClient . prototype , 'sendCommandAndWait' )
189
- } )
190
-
191
- after ( function ( ) {
192
- sinon . restore ( )
193
- } )
194
-
195
140
it ( 'calls the sdk with the proper parameters' , async function ( ) {
141
+ const sendCommandStub = sinon . stub ( SsmClient . prototype , 'sendCommandAndWait' )
142
+
196
143
const testSelection = {
197
144
instanceId : 'test-id' ,
198
145
region : 'test-region' ,
199
146
}
200
147
const mockKeys = mock ( ) as SshKeyPair
201
148
await client . sendSshKeyToInstance ( testSelection , mockKeys , '' )
202
149
sinon . assert . calledWith ( sendCommandStub , testSelection . instanceId , 'AWS-RunShellScript' )
150
+ sinon . restore ( )
203
151
} )
204
152
} )
205
153
206
- describe ( 'determineRemoteUser ' , async function ( ) {
207
- let client : MockEc2ConnectClient
154
+ describe ( 'getRemoteUser ' , async function ( ) {
155
+ let getTargetPlatformNameStub : sinon . SinonStub < [ target : string ] , Promise < string > >
208
156
209
- before ( function ( ) {
210
- client = new MockEc2ConnectClient ( )
157
+ before ( async function ( ) {
158
+ getTargetPlatformNameStub = sinon . stub ( SsmClient . prototype , 'getTargetPlatformName' )
159
+ } )
160
+
161
+ after ( async function ( ) {
162
+ sinon . restore ( )
211
163
} )
212
164
213
165
it ( 'identifies the user for ubuntu as ubuntu' , async function ( ) {
214
- const remoteUser = await client . testGetRemoteUser ( 'testInstance' , 'Ubuntu' )
166
+ getTargetPlatformNameStub . resolves ( 'Ubuntu' )
167
+ const remoteUser = await client . getRemoteUser ( 'testInstance' )
215
168
assert . strictEqual ( remoteUser , 'ubuntu' )
216
169
} )
217
170
218
171
it ( 'identifies the user for amazon linux as ec2-user' , async function ( ) {
219
- const remoteUser = await client . testGetRemoteUser ( 'testInstance' , 'Amazon Linux' )
172
+ getTargetPlatformNameStub . resolves ( 'Amazon Linux' )
173
+ const remoteUser = await client . getRemoteUser ( 'testInstance' )
220
174
assert . strictEqual ( remoteUser , 'ec2-user' )
221
175
} )
222
176
223
177
it ( 'throws error when not given known OS' , async function ( ) {
178
+ getTargetPlatformNameStub . resolves ( 'ThisIsNotARealOs!' )
224
179
try {
225
- await client . testGetRemoteUser ( 'testInstance' , 'ThisIsNotARealOs! ')
180
+ await client . getRemoteUser ( 'testInstance' )
226
181
assert . ok ( false )
227
182
} catch ( exception ) {
228
183
assert . ok ( true )
0 commit comments