forked from kamranahmedse/aws-cost-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcost.test.ts
159 lines (135 loc) · 4.52 KB
/
cost.test.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import AWS from 'aws-sdk';
import { AWSConfig } from './config';
import { getRawCostByService, getTotalCosts } from './cost';
import { generateMockedCostByService } from './testUtils';
import AWSMock from 'aws-sdk-mock';
import dayjs from 'dayjs';
// Use Apr 2024 (30 days) as the 'last month'
// Thus 'today' is someday in May 2024
const costDataLength = 65;
const fixedToday = '2024-05-11'; // cost of 'this month' will be sum of 10 days from May 1 to May 10 ('today' is omitted because its cost is incomplete)
const fixedFirstDay = dayjs(fixedToday).subtract(costDataLength, 'day');
describe('Cost Functions', () => {
beforeAll(() => {
AWSMock.setSDKInstance(AWS);
});
afterAll(() => {
AWSMock.restore();
});
beforeEach(() => {
jest.useFakeTimers('modern');
jest.setSystemTime(new Date(fixedToday).getTime());
});
afterEach(() => {
jest.useRealTimers();
});
describe('getRawCostByService', () => {
it('should return raw cost by service', async () => {
const awsConfig: AWSConfig = {
credentials: {
accessKeyId: 'testAccessKeyId',
secretAccessKey: 'testSecretAccessKey',
sessionToken: 'testSessionToken',
},
region: 'us-east-1',
};
const mockedPricingData = generateMockedCostByService(
fixedToday,
costDataLength,
);
AWSMock.mock('CostExplorer', 'getCostAndUsage', (params, callback) => {
callback(null, mockedPricingData);
});
const rawCostByService = await getRawCostByService(awsConfig);
const expectedRawCostByService = {
service1: {},
service2: {},
};
for (let i = 0; i < costDataLength; i++) {
const date = dayjs(fixedFirstDay).add(i, 'day').format('YYYY-MM-DD');
const month = dayjs(date).month();
let service1Cost;
switch (month) {
case 2: // March
service1Cost = 0.9;
break;
case 3: // April
service1Cost = 1.0; // Total cost of service1 in April will be 30.00
break;
case 4: // May
service1Cost = 1.1;
break;
default:
service1Cost = 0.0; // Default cost if none of the above
}
expectedRawCostByService.service1[date] = service1Cost;
expectedRawCostByService.service2[date] = service1Cost * 100;
}
expect(rawCostByService).toEqual(expectedRawCostByService);
AWSMock.restore('CostExplorer');
});
});
describe('getTotalCosts', () => {
it('should return total costs', async () => {
const awsConfig: AWSConfig = {
credentials: {
accessKeyId: 'testAccessKeyId',
secretAccessKey: 'testSecretAccessKey',
sessionToken: 'testSessionToken',
},
region: 'us-east-1',
};
const mockedPricingData = generateMockedCostByService(
fixedToday,
costDataLength,
);
AWSMock.mock('CostExplorer', 'getCostAndUsage', (params, callback) => {
callback(null, mockedPricingData);
});
const totalCosts = await getTotalCosts(awsConfig);
const expectedTotalCosts = {
totals: {
lastMonth: 30 * (1 + 100), // Apr
thisMonth: 10 * (1.1 + 110), // sum of May 1..May 10
last7Days: 7 * 1.1 + 7 * 110, // sum of May 4..May 10
yesterday: 1.1 + 110, // on May 10
},
totalsByService: {
lastMonth: {
// Apr
service1: 30.0,
service2: 3000.0,
},
thisMonth: {
service1: 11.0, // 10 days of May
service2: 1100.0,
},
last7Days: {
service1: 7.7,
service2: 770.0,
},
yesterday: {
service1: 1.1,
service2: 110.0,
},
},
};
console.log(expectedTotalCosts);
const roundToTwoDecimals = (num: number) => Math.round(num * 100) / 100;
Object.keys(totalCosts.totals).forEach((key) => {
expect(roundToTwoDecimals(totalCosts.totals[key])).toBeCloseTo(
expectedTotalCosts.totals[key],
1,
);
});
Object.keys(totalCosts.totalsByService).forEach((period) => {
Object.keys(totalCosts.totalsByService[period]).forEach((service) => {
expect(
roundToTwoDecimals(totalCosts.totalsByService[period][service]),
).toBeCloseTo(expectedTotalCosts.totalsByService[period][service], 1);
});
});
AWSMock.restore('CostExplorer');
});
});
});