Skip to content

Commit

Permalink
Merge pull request #8 from Endava/test-formatting
Browse files Browse the repository at this point in the history
Format tests
  • Loading branch information
jcendava authored Aug 23, 2024
2 parents bd2c22f + bbf3362 commit 4ed4541
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 199 deletions.
29 changes: 19 additions & 10 deletions src/__tests__/get-cloudwatch-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// eslint-disable-next-line node/no-extraneous-import
import { CloudWatchClient, GetMetricStatisticsCommand } from '@aws-sdk/client-cloudwatch';
import { getCloudwatchMetrics } from '../api/get-cloudwatch-metrics';
import { getCloudWatchData } from '../api/get-cloudwatch-data';
import {getCloudWatchData} from '../api/get-cloudwatch-data';
import {
convertSecondsToMS,
getFinalGroupedItems,
getInstanceTypes,
} from '../utils/utils';
import { PluginParams } from '../interfaces';
import { mockGlobalConfig, mockInstances } from '../__mocks__';
import { AWSCredentials } from '../types';
import { METRICS_CONFIG } from '../constants/constants';
import {PluginParams} from '../interfaces';
import {mockGlobalConfig, mockInstances} from '../__mocks__';
import {AWSCredentials} from '../types';
import {METRICS_CONFIG} from '../constants/constants';

// Mock the dependencies
jest.mock('@aws-sdk/client-cloudwatch', () => {
Expand All @@ -20,7 +18,10 @@ jest.mock('@aws-sdk/client-cloudwatch', () => {
CloudWatchClient: jest.fn().mockImplementation(() => ({
send: jest.fn().mockResolvedValue({
Datapoints: [
{ Average: 50, Timestamp: new Date().toISOString() },
{
Average: 50,
Timestamp: new Date().toISOString(),
},
],
}),
})),
Expand All @@ -41,7 +42,12 @@ jest.mock('../api/get-cloudwatch-metrics', () => ({
Metrics: [
{
MetricName: 'MemoryUsage',
Dimensions: [{ Name: 'InstanceId', Value: instance.InstanceId }],
Dimensions: [
{
Name: 'InstanceId',
Value: instance.InstanceId,
},
],
Namespace: 'AWS/EC2',
},
],
Expand All @@ -65,7 +71,10 @@ describe('getCloudWatchData', () => {
};

const expectedGroupedItems = [
{ [METRICS_CONFIG.MEM_UTILIZATION]: 50, [METRICS_CONFIG.CPU_UTILIZATION]: 50 },
{
[METRICS_CONFIG.MEM_UTILIZATION]: 50,
[METRICS_CONFIG.CPU_UTILIZATION]: 50,
},
];

const expectedInstanceTypes = ['t2.micro', 't2.medium'];
Expand Down
218 changes: 112 additions & 106 deletions src/__tests__/get-ec2-machines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,139 +7,145 @@ jest.mock('@aws-sdk/client-ec2', () => {
return {
...originalModule,
EC2Client: jest.fn().mockImplementation(() => ({
send: jest.fn().mockImplementation((command: { input: DescribeInstancesCommandInput }) => {
const tagFilter = command.input.Filters?.find((filter) => filter.Name === 'tag:Project');
const tagValue = tagFilter?.Values?.[0] ?? '';
send: jest
.fn()
.mockImplementation(
(command: {input: DescribeInstancesCommandInput}) => {
const tagFilter = command.input.Filters?.find(
filter => filter.Name === 'tag:Project'
);
const tagValue = tagFilter?.Values?.[0] ?? '';

if (tagValue === 'no-reservations') {
return Promise.resolve({
Reservations: undefined,
});
} else if (tagValue === 'no-instances') {
return Promise.resolve({
Reservations: [
{
Instances: undefined,
},
],
});
} else if (tagValue === 'no-block-devices') {
return Promise.resolve({
Reservations: [
{
Instances: [
if (tagValue === 'no-reservations') {
return Promise.resolve({
Reservations: undefined,
});
} else if (tagValue === 'no-instances') {
return Promise.resolve({
Reservations: [
{
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: undefined,
Instances: undefined,
},
],
},
],
});
} else if (tagValue === 'empty-block-devices') {
return Promise.resolve({
Reservations: [
{
Instances: [
});
} else if (tagValue === 'no-block-devices') {
return Promise.resolve({
Reservations: [
{
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: [],
Instances: [
{
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: undefined,
},
],
},
],
},
],
});
} else if (tagValue === 'mixed-block-devices') {
return Promise.resolve({
Reservations: [
{
Instances: [
});
} else if (tagValue === 'empty-block-devices') {
return Promise.resolve({
Reservations: [
{
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: [
Instances: [
{
DeviceName: '/dev/sda1',
Ebs: {
VolumeId: 'vol-01c4a06424ff37c90',
},
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: [],
},
],
},
{
InstanceId: 'i-0987654321fedcba0',
ImageId: 'ami-1234567890abcdef0',
InstanceType: 't2.medium',
RootDeviceName: '/dev/sda2',
BlockDeviceMappings: undefined,
},
],
},
],
});
} else if (tagValue === 'various-properties') {
return Promise.resolve({
Reservations: [
{
Instances: [
});
} else if (tagValue === 'mixed-block-devices') {
return Promise.resolve({
Reservations: [
{
InstanceId: 'i-11111111111111111',
ImageId: 'ami-11111111111111111',
InstanceType: 't2.large',
RootDeviceName: '/dev/sdb1',
BlockDeviceMappings: [
Instances: [
{
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: [
{
DeviceName: '/dev/sda1',
Ebs: {
VolumeId: 'vol-01c4a06424ff37c90',
},
},
],
},
{
DeviceName: '/dev/sdb1',
Ebs: {
VolumeId: 'vol-11111111111111111',
},
InstanceId: 'i-0987654321fedcba0',
ImageId: 'ami-1234567890abcdef0',
InstanceType: 't2.medium',
RootDeviceName: '/dev/sda2',
BlockDeviceMappings: undefined,
},
],
},
],
});
} else if (tagValue === 'various-properties') {
return Promise.resolve({
Reservations: [
{
InstanceId: undefined,
ImageId: undefined,
InstanceType: undefined,
RootDeviceName: undefined,
BlockDeviceMappings: undefined,
Instances: [
{
InstanceId: 'i-11111111111111111',
ImageId: 'ami-11111111111111111',
InstanceType: 't2.large',
RootDeviceName: '/dev/sdb1',
BlockDeviceMappings: [
{
DeviceName: '/dev/sdb1',
Ebs: {
VolumeId: 'vol-11111111111111111',
},
},
],
},
{
InstanceId: undefined,
ImageId: undefined,
InstanceType: undefined,
RootDeviceName: undefined,
BlockDeviceMappings: undefined,
},
],
},
],
},
],
});
} else {
return Promise.resolve({
Reservations: [
{
Instances: [
});
} else {
return Promise.resolve({
Reservations: [
{
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: [
Instances: [
{
DeviceName: '/dev/sda1',
Ebs: {
VolumeId: 'vol-01c4a06424ff37c90',
},
InstanceId: 'i-1234567890abcdef0',
ImageId: 'ami-05d72852800cbf29e',
InstanceType: 't2.micro',
RootDeviceName: '/dev/sda1',
BlockDeviceMappings: [
{
DeviceName: '/dev/sda1',
Ebs: {
VolumeId: 'vol-01c4a06424ff37c90',
},
},
],
},
],
},
],
},
],
});
}
}),
});
}
}
),
})),
};
});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/get-ec2-volumes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock('@aws-sdk/client-ec2', () => {
return {
...originalModule,
EC2Client: jest.fn().mockImplementation(() => ({
send: jest.fn().mockImplementation((command) => {
send: jest.fn().mockImplementation(command => {
const volumeIds = command.input.VolumeIds;

if (volumeIds && volumeIds.includes('no-volumes')) {
Expand Down
Loading

0 comments on commit 4ed4541

Please sign in to comment.