-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEc2Service.test.ts
More file actions
88 lines (74 loc) · 3.11 KB
/
Ec2Service.test.ts
File metadata and controls
88 lines (74 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import 'reflect-metadata';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { mockClient } from 'aws-sdk-client-mock';
import {
EC2Client,
DescribeNetworkInterfacesCommand,
} from '@aws-sdk/client-ec2';
vi.mock('../logger.js', () => ({
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
import { Ec2Service } from './Ec2Service.js';
import type { ConfigService } from './ConfigService.js';
/** Typed stand-in for the AWS EC2 SDK client. */
const ec2Mock = mockClient(EC2Client);
/**
* Build a minimal ConfigService stub exposing only the members Ec2Service
* actually reads at runtime.
*/
function makeConfig(): ConfigService {
const stub: Partial<ConfigService> = { getRegion: () => 'us-east-1' };
return stub as ConfigService;
}
describe('Ec2Service', () => {
/** Service under test, freshly constructed per test. */
let service: Ec2Service;
beforeEach(() => {
ec2Mock.reset();
service = new Ec2Service(makeConfig());
});
describe('getPublicIp', () => {
it('should return the public IP when attached', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).resolves({
NetworkInterfaces: [{ Association: { PublicIp: '54.1.2.3' } }],
});
expect(await service.getPublicIp('eni-abc')).toBe('54.1.2.3');
});
it('should return null when there is no association or public IP', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).resolves({
NetworkInterfaces: [{}],
});
expect(await service.getPublicIp('eni-abc')).toBeNull();
});
it('should return null when no interfaces are returned', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).resolves({ NetworkInterfaces: [] });
expect(await service.getPublicIp('eni-abc')).toBeNull();
});
it('should return null on API error', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).rejects(new Error('boom'));
expect(await service.getPublicIp('eni-abc')).toBeNull();
});
it('should pass the ENI id through to the SDK command', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).resolves({ NetworkInterfaces: [] });
await service.getPublicIp('eni-123');
const input = ec2Mock.commandCalls(DescribeNetworkInterfacesCommand)[0]!.args[0].input;
expect(input.NetworkInterfaceIds).toEqual(['eni-123']);
});
});
describe('getPrivateIp', () => {
it('should return the private IP when present', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).resolves({
NetworkInterfaces: [{ PrivateIpAddress: '10.0.1.5' }],
});
expect(await service.getPrivateIp('eni-abc')).toBe('10.0.1.5');
});
it('should return null when the private IP is absent', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).resolves({ NetworkInterfaces: [{}] });
expect(await service.getPrivateIp('eni-abc')).toBeNull();
});
it('should return null on API error', async () => {
ec2Mock.on(DescribeNetworkInterfacesCommand).rejects(new Error('nope'));
expect(await service.getPrivateIp('eni-abc')).toBeNull();
});
});
});