Skip to content

Commit e26dc37

Browse files
committed
refactor: move tests from __tests__ to src directory, fix AWS SDK Filter properties
1 parent 1c8a393 commit e26dc37

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

lambdas/functions/ami-updater/src/__tests__/ami.test.ts renamed to lambdas/functions/ami-updater/src/ami.test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
2-
import { EC2Client, DescribeImagesCommand, DescribeLaunchTemplatesCommand, DescribeLaunchTemplateVersionsCommand, CreateLaunchTemplateVersionCommand, ModifyLaunchTemplateCommand } from '@aws-sdk/client-ec2';
3-
import { AMIManager } from '../ami';
2+
import {
3+
EC2Client,
4+
DescribeImagesCommand,
5+
DescribeLaunchTemplatesCommand,
6+
DescribeLaunchTemplateVersionsCommand,
7+
CreateLaunchTemplateVersionCommand,
8+
ModifyLaunchTemplateCommand,
9+
} from '@aws-sdk/client-ec2';
10+
import { AMIManager } from './ami';
411

512
vi.mock('@aws-sdk/client-ec2');
613
vi.mock('../../shared/aws-powertools-util', () => ({
@@ -34,7 +41,7 @@ describe('AMIManager', () => {
3441

3542
const config = {
3643
owners: ['self'],
37-
filters: [{ name: 'tag:Environment', values: ['prod'] }],
44+
filters: [{ Name: 'tag:Environment', Values: ['prod'] }],
3845
};
3946

4047
const result = await amiManager.getLatestAmi(config);
@@ -47,7 +54,7 @@ describe('AMIManager', () => {
4754

4855
const config = {
4956
owners: ['self'],
50-
filters: [{ name: 'tag:Environment', values: ['prod'] }],
57+
filters: [{ Name: 'tag:Environment', Values: ['prod'] }],
5158
};
5259

5360
await expect(amiManager.getLatestAmi(config)).rejects.toThrow('No matching AMIs found');
@@ -57,13 +64,16 @@ describe('AMIManager', () => {
5764
describe('updateLaunchTemplate', () => {
5865
it('should update launch template with new AMI ID', async () => {
5966
vi.mocked(ec2Client.send)
60-
.mockResolvedValueOnce({ // getCurrentAmiId - DescribeLaunchTemplatesCommand
67+
.mockResolvedValueOnce({
68+
// getCurrentAmiId - DescribeLaunchTemplatesCommand
6169
LaunchTemplates: [{ LatestVersionNumber: 1 }],
6270
})
63-
.mockResolvedValueOnce({ // getCurrentAmiId - DescribeLaunchTemplateVersionsCommand
71+
.mockResolvedValueOnce({
72+
// getCurrentAmiId - DescribeLaunchTemplateVersionsCommand
6473
LaunchTemplateVersions: [{ LaunchTemplateData: { ImageId: 'ami-old' } }],
6574
})
66-
.mockResolvedValueOnce({ // updateLaunchTemplate - DescribeLaunchTemplatesCommand
75+
.mockResolvedValueOnce({
76+
// updateLaunchTemplate - DescribeLaunchTemplatesCommand
6777
LaunchTemplates: [{ LatestVersionNumber: 1 }],
6878
});
6979

@@ -77,10 +87,12 @@ describe('AMIManager', () => {
7787

7888
it('should not update if AMI ID is the same', async () => {
7989
vi.mocked(ec2Client.send)
80-
.mockResolvedValueOnce({ // getCurrentAmiId - DescribeLaunchTemplatesCommand
90+
.mockResolvedValueOnce({
91+
// getCurrentAmiId - DescribeLaunchTemplatesCommand
8192
LaunchTemplates: [{ LatestVersionNumber: 1 }],
8293
})
83-
.mockResolvedValueOnce({ // getCurrentAmiId - DescribeLaunchTemplateVersionsCommand
94+
.mockResolvedValueOnce({
95+
// getCurrentAmiId - DescribeLaunchTemplateVersionsCommand
8496
LaunchTemplateVersions: [{ LaunchTemplateData: { ImageId: 'ami-1' } }],
8597
});
8698

@@ -93,10 +105,12 @@ describe('AMIManager', () => {
93105

94106
it('should handle dry run mode', async () => {
95107
vi.mocked(ec2Client.send)
96-
.mockResolvedValueOnce({ // getCurrentAmiId - DescribeLaunchTemplatesCommand
108+
.mockResolvedValueOnce({
109+
// getCurrentAmiId - DescribeLaunchTemplatesCommand
97110
LaunchTemplates: [{ LatestVersionNumber: 1 }],
98111
})
99-
.mockResolvedValueOnce({ // getCurrentAmiId - DescribeLaunchTemplateVersionsCommand
112+
.mockResolvedValueOnce({
113+
// getCurrentAmiId - DescribeLaunchTemplateVersionsCommand
100114
LaunchTemplateVersions: [{ LaunchTemplateData: { ImageId: 'ami-old' } }],
101115
});
102116

@@ -107,4 +121,4 @@ describe('AMIManager', () => {
107121
expect(ec2Client.send).not.toHaveBeenCalledWith(expect.any(CreateLaunchTemplateVersionCommand));
108122
});
109123
});
110-
});
124+
});

lambdas/functions/ami-updater/src/__tests__/config.test.ts renamed to lambdas/functions/ami-updater/src/config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
2-
import { getConfig } from '../config';
2+
import { getConfig } from './config';
33

44
vi.mock('../../shared/aws-powertools-util', () => ({
55
logger: {
@@ -76,4 +76,4 @@ describe('getConfig', () => {
7676

7777
expect(() => getConfig()).toThrow('AMI_FILTER must contain owners (array) and filters (array)');
7878
});
79-
});
79+
});

lambdas/functions/ami-updater/src/__tests__/index.test.ts renamed to lambdas/functions/ami-updater/src/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
2-
import { handler } from '../lambda';
3-
import { AMIManager } from '../ami';
4-
import { getConfig } from '../config';
2+
import { handler } from './lambda';
3+
import { AMIManager } from './ami';
4+
import { getConfig } from './config';
55

6-
vi.mock('../ami');
7-
vi.mock('../config');
6+
vi.mock('./ami');
7+
vi.mock('./config');
88
vi.mock('@aws-github-runner/aws-powertools-util', () => ({
99
logger: {
1010
addContext: vi.fn(),
@@ -24,7 +24,7 @@ describe('Lambda Handler', () => {
2424
dryRun: false,
2525
amiFilter: {
2626
owners: ['self'],
27-
filters: [{ name: 'tag:Environment', values: ['prod'] }],
27+
filters: [{ Name: 'tag:Environment', Values: ['prod'] }],
2828
},
2929
};
3030

0 commit comments

Comments
 (0)