Skip to content

Commit fc4af9e

Browse files
committed
refactor test file to use mocking framework
1 parent 0a26c22 commit fc4af9e

File tree

2 files changed

+43
-88
lines changed

2 files changed

+43
-88
lines changed

src/ec2/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class Ec2ConnectionManager {
254254
})
255255
}
256256

257-
protected async getRemoteUser(instanceId: string) {
257+
public async getRemoteUser(instanceId: string) {
258258
const osName = await this.ssmClient.getTargetPlatformName(instanceId)
259259
if (osName === 'Amazon Linux') {
260260
return 'ec2-user'

src/test/ec2/model.test.ts

Lines changed: 42 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,104 +10,62 @@ import { SsmClient } from '../../shared/clients/ssmClient'
1010
import { Ec2Client } from '../../shared/clients/ec2Client'
1111
import { Ec2Selection } from '../../ec2/utils'
1212
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'
1614
import { mock } from 'ts-mockito'
1715
import { SshKeyPair } from '../../ec2/sshKeyPair'
1816
import { DefaultIamClient } from '../../shared/clients/iamClient'
1917

2018
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
6520

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+
})
7224

25+
describe('isInstanceRunning', async function () {
7326
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')
7631

7732
assert.strictEqual(true, actualFirstResult)
7833
assert.strictEqual(false, actualSecondResult)
34+
sinon.restore()
7935
})
8036
})
8137

8238
describe('handleStartSessionError', async function () {
83-
let client: Ec2ConnectionManager
8439
let instanceSelection: Ec2Selection
8540

8641
before(function () {
87-
client = new Ec2ConnectionManager('test-region')
8842
instanceSelection = { instanceId: 'testInstance', region: 'testRegion' }
8943
})
9044

9145
it('throws EC2SSMStatus error if instance is not running', async function () {
9246
sinon.stub(Ec2ConnectionManager.prototype, 'isInstanceRunning').resolves(false)
47+
9348
try {
9449
await client.checkForStartSessionError(instanceSelection)
9550
assert.ok(false)
9651
} catch (err) {
9752
assert.strictEqual((err as ToolkitError).code, 'EC2SSMStatus')
9853
}
54+
9955
sinon.restore()
10056
})
10157

10258
it('throws EC2SSMPermission error if instance is running but has no role', async function () {
10359
sinon.stub(Ec2ConnectionManager.prototype, 'isInstanceRunning').resolves(true)
10460
sinon.stub(Ec2ConnectionManager.prototype, 'getAttachedIamRole').resolves(undefined)
61+
10562
try {
10663
await client.checkForStartSessionError(instanceSelection)
10764
assert.ok(false)
10865
} catch (err) {
10966
assert.strictEqual((err as ToolkitError).code, 'EC2SSMPermission')
11067
}
68+
11169
sinon.restore()
11270
})
11371

@@ -116,12 +74,14 @@ describe('Ec2ConnectClient', function () {
11674
sinon.stub(Ec2ConnectionManager.prototype, 'getAttachedIamRole').resolves({ Arn: 'testRole' } as IAM.Role)
11775
sinon.stub(Ec2ConnectionManager.prototype, 'hasProperPolicies').resolves(true)
11876
sinon.stub(SsmClient.prototype, 'getInstanceAgentPingStatus').resolves('offline')
77+
11978
try {
12079
await client.checkForStartSessionError(instanceSelection)
12180
assert.ok(false)
12281
} catch (err) {
12382
assert.strictEqual((err as ToolkitError).code, 'EC2SSMAgentStatus')
12483
}
84+
12585
sinon.restore()
12686
})
12787

@@ -130,23 +90,21 @@ describe('Ec2ConnectClient', function () {
13090
sinon.stub(Ec2ConnectionManager.prototype, 'getAttachedIamRole').resolves({ Arn: 'testRole' } as IAM.Role)
13191
sinon.stub(Ec2ConnectionManager.prototype, 'hasProperPolicies').resolves(true)
13292
sinon.stub(SsmClient.prototype, 'getInstanceAgentPingStatus').resolves('Online')
93+
13394
assert.doesNotThrow(async () => await client.checkForStartSessionError(instanceSelection))
95+
13496
sinon.restore()
13597
})
13698
})
13799

138100
describe('hasProperPolicies', async function () {
139-
let realClient: Ec2ConnectionManager
140-
141-
before(async function () {
142-
realClient = new Ec2ConnectionManager('test-region')
143-
})
144-
145101
it('correctly determines if proper policies are included', async function () {
146102
async function assertAcceptsPolicies(policies: IAM.Policy[], expectedResult: boolean) {
147103
sinon.stub(DefaultIamClient.prototype, 'listAttachedRolePolicies').resolves(policies)
148-
const result = await realClient.hasProperPolicies('')
104+
105+
const result = await client.hasProperPolicies('')
149106
assert.strictEqual(result, expectedResult)
107+
150108
sinon.restore()
151109
}
152110
await assertAcceptsPolicies(
@@ -166,63 +124,60 @@ describe('Ec2ConnectClient', function () {
166124

167125
it('throws error when sdk throws error', async function () {
168126
sinon.stub(DefaultIamClient.prototype, 'listAttachedRolePolicies').throws(new ToolkitError('error'))
127+
169128
try {
170-
await realClient.hasProperPolicies('')
129+
await client.hasProperPolicies('')
171130
assert.ok(false)
172131
} catch {
173132
assert.ok(true)
174133
}
134+
175135
sinon.restore()
176136
})
177137
})
178138

179139
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-
195140
it('calls the sdk with the proper parameters', async function () {
141+
const sendCommandStub = sinon.stub(SsmClient.prototype, 'sendCommandAndWait')
142+
196143
const testSelection = {
197144
instanceId: 'test-id',
198145
region: 'test-region',
199146
}
200147
const mockKeys = mock() as SshKeyPair
201148
await client.sendSshKeyToInstance(testSelection, mockKeys, '')
202149
sinon.assert.calledWith(sendCommandStub, testSelection.instanceId, 'AWS-RunShellScript')
150+
sinon.restore()
203151
})
204152
})
205153

206-
describe('determineRemoteUser', async function () {
207-
let client: MockEc2ConnectClient
154+
describe('getRemoteUser', async function () {
155+
let getTargetPlatformNameStub: sinon.SinonStub<[target: string], Promise<string>>
208156

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()
211163
})
212164

213165
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')
215168
assert.strictEqual(remoteUser, 'ubuntu')
216169
})
217170

218171
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')
220174
assert.strictEqual(remoteUser, 'ec2-user')
221175
})
222176

223177
it('throws error when not given known OS', async function () {
178+
getTargetPlatformNameStub.resolves('ThisIsNotARealOs!')
224179
try {
225-
await client.testGetRemoteUser('testInstance', 'ThisIsNotARealOs!')
180+
await client.getRemoteUser('testInstance')
226181
assert.ok(false)
227182
} catch (exception) {
228183
assert.ok(true)

0 commit comments

Comments
 (0)