forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.ts
133 lines (119 loc) · 4.82 KB
/
util_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
import { expect, should } from 'chai';
import { CoreV1Api, V1Container, V1Pod } from './api';
import { findSuffix, podsForNode, quantityToScalar, totalCPU, totalMemory } from './util';
describe('Utils', () => {
it('should get zero pods for a node', async () => {
const podList = {
body: {
items: [],
},
};
const mockApi = {
listPodForAllNamespaces: (): Promise<any> => {
return new Promise<any>((resolve) => {
resolve(podList);
});
},
};
const pods = await podsForNode(mockApi as CoreV1Api, 'foo');
expect(pods.length).to.equal(0);
});
it('should only gets for pods named node', async () => {
const podList = {
body: {
items: [
{
spec: {
nodeName: 'foo',
},
},
{
spec: {
nodeName: 'bar',
},
},
],
},
};
const mockApi = {
listPodForAllNamespaces: (): Promise<any> => {
return new Promise<any>((resolve) => {
resolve(podList);
});
},
};
const pods = await podsForNode(mockApi as CoreV1Api, 'foo');
expect(pods.length).to.equal(1);
expect(pods[0].spec!.nodeName).to.equal('foo');
});
it('should parse quantities', () => {
expect(quantityToScalar('')).to.equal(0);
expect(quantityToScalar('2n')).to.equal(2 / 1_000_000_000);
expect(quantityToScalar('3u')).to.equal(3 / 1_000_000);
expect(quantityToScalar('100m')).to.equal(0.1);
expect(quantityToScalar('3k')).to.equal(BigInt(3000));
expect(quantityToScalar('3M')).to.equal(BigInt(3 * 1000 * 1000));
expect(quantityToScalar('3G')).to.equal(BigInt(3 * 1000 * 1000 * 1000));
expect(quantityToScalar('5T')).to.equal(BigInt(5 * 1000 * 1000 * 1000) * BigInt(1000));
expect(quantityToScalar('3P')).to.equal(BigInt(3 * 1000 * 1000 * 1000) * BigInt(1000 * 1000));
expect(quantityToScalar('14E')).to.equal(
BigInt(14 * 1000 * 1000 * 1000) * BigInt(1000 * 1000 * 1000),
);
expect(quantityToScalar('0.2')).to.equal(0.2);
expect(quantityToScalar('1976m')).to.equal(1.976);
expect(quantityToScalar('1024')).to.equal(1024);
expect(quantityToScalar('10e3')).to.equal(10000);
expect(quantityToScalar('10Ki')).to.equal(BigInt(10240));
expect(quantityToScalar('20Mi')).to.equal(BigInt(1024 * 1024 * 20));
expect(quantityToScalar('30Gi')).to.equal(BigInt(1024 * 1024 * 1024 * 30));
expect(quantityToScalar('40Ti')).to.equal(BigInt(1024 * 1024 * 1024 * 1024 * 40));
expect(quantityToScalar('50Pi')).to.equal(BigInt(1024 * 1024 * 1024 * 1024 * 1024 * 50));
expect(quantityToScalar('60Ei')).to.equal(
BigInt(1024 * 1024 * 1024) * BigInt(1024 * 1024 * 1024 * 60),
);
expect(() => quantityToScalar('foobar')).to.throw('Unknown quantity foobar');
expect(() => quantityToScalar('100foobar')).to.throw('Unknown suffix: foobar');
});
it('should get resources for pod', () => {
const pod = {
spec: {
containers: [
{
name: 'foo',
resources: {
requests: {
cpu: '100m',
memory: '10Ki',
},
limits: {
cpu: '200m',
},
},
} as V1Container,
{
name: 'bar',
resources: {
requests: {
cpu: '1.0',
},
limits: {
memory: '20Ki',
},
},
} as V1Container,
] as V1Container[],
},
} as V1Pod;
const cpuResult = totalCPU(pod);
expect(cpuResult.request).to.equal(1.1);
expect(cpuResult.limit).to.equal(0.2);
const memResult = totalMemory(pod);
expect(memResult.request).to.equal(BigInt(10240));
expect(memResult.limit).to.equal(BigInt(20480));
});
it('should find the suffix correctly', () => {
expect(findSuffix('1234567')).to.equal('');
expect(findSuffix('1234asdf')).to.equal('asdf');
expect(findSuffix('1.0')).to.equal('');
});
});