open-nomad/ui/tests/unit/utils/node-stats-tracker-test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

277 lines
7.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2018-08-31 21:37:13 +00:00
import EmberObject from '@ember/object';
import { assign } from '@ember/polyfills';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
2018-08-31 21:37:13 +00:00
import sinon from 'sinon';
import Pretender from 'pretender';
2021-12-28 16:08:12 +00:00
import NodeStatsTracker, {
stats,
} from 'nomad-ui/utils/classes/node-stats-tracker';
2018-08-31 21:37:13 +00:00
import fetch from 'nomad-ui/utils/fetch';
2018-11-02 05:12:44 +00:00
import statsTrackerFrameMissingBehavior from './behaviors/stats-tracker-frame-missing';
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
import { settled } from '@ember/test-helpers';
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
module('Unit | Util | NodeStatsTracker', function () {
2019-03-13 00:04:16 +00:00
const refDate = Date.now() * 1000000;
2021-12-28 14:45:20 +00:00
const makeDate = (ts) => new Date(ts / 1000000);
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
const MockNode = (overrides) =>
2019-03-13 00:04:16 +00:00
assign(
{
id: 'some-identifier',
resources: {
cpu: 2000,
memory: 4096,
},
2018-08-31 21:37:13 +00:00
},
2019-03-13 00:04:16 +00:00
overrides
);
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
const mockFrame = (step) => ({
2019-03-13 00:04:16 +00:00
CPUTicksConsumed: step + 1000,
Memory: {
Used: (step + 2048) * 1024 * 1024,
2018-08-31 21:37:13 +00:00
},
2019-03-13 00:04:16 +00:00
Timestamp: refDate + step,
});
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
test('the NodeStatsTracker constructor expects a fetch definition and a node', async function (assert) {
2019-03-13 00:04:16 +00:00
const tracker = NodeStatsTracker.create();
assert.throws(
() => {
tracker.fetch();
},
/StatsTrackers need a fetch method/,
'Polling does not work without a fetch method provided'
);
});
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
test('the url property is computed based off the node id', async function (assert) {
2019-03-13 00:04:16 +00:00
const node = MockNode();
const tracker = NodeStatsTracker.create({ fetch, node });
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
assert.equal(
tracker.get('url'),
`/v1/client/stats?node_id=${node.id}`,
'Url is derived from the node id'
);
});
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
test('reservedCPU and reservedMemory properties come from the node', async function (assert) {
2019-03-13 00:04:16 +00:00
const node = MockNode();
const tracker = NodeStatsTracker.create({ fetch, node });
2018-08-31 21:37:13 +00:00
2021-12-28 16:08:12 +00:00
assert.equal(
tracker.get('reservedCPU'),
node.resources.cpu,
'reservedCPU comes from the node'
);
2019-03-13 00:04:16 +00:00
assert.equal(
tracker.get('reservedMemory'),
node.resources.memory,
'reservedMemory comes from the node'
);
2018-08-31 21:37:13 +00:00
});
2021-12-28 14:45:20 +00:00
test('poll results in requesting the url and calling append with the resulting JSON', async function (assert) {
2019-03-13 00:04:16 +00:00
const node = MockNode();
2021-12-28 16:08:12 +00:00
const tracker = NodeStatsTracker.create({
fetch,
node,
append: sinon.spy(),
});
2019-03-13 00:04:16 +00:00
const mockFrame = {
Some: {
data: ['goes', 'here'],
twelve: 12,
},
};
2018-08-31 21:37:13 +00:00
2021-12-28 14:45:20 +00:00
const server = new Pretender(function () {
2019-03-13 00:04:16 +00:00
this.get('/v1/client/stats', () => [200, {}, JSON.stringify(mockFrame)]);
});
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
tracker.get('poll').perform();
assert.equal(server.handledRequests.length, 1, 'Only one request was made');
assert.equal(
server.handledRequests[0].url,
`/v1/client/stats?node_id=${node.id}`,
'The correct URL was requested'
2018-08-31 21:37:13 +00:00
);
await settled();
assert.ok(
tracker.append.calledWith(mockFrame),
'The JSON response was passed into append as a POJO'
);
2019-03-13 00:04:16 +00:00
server.shutdown();
2018-08-31 21:37:13 +00:00
});
2021-12-28 14:45:20 +00:00
test('append appropriately maps a data frame to the tracked stats for cpu and memory for the node', async function (assert) {
2019-03-13 00:04:16 +00:00
const node = MockNode();
const tracker = NodeStatsTracker.create({ fetch, node });
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
assert.deepEqual(tracker.get('cpu'), [], 'No tracked cpu yet');
assert.deepEqual(tracker.get('memory'), [], 'No tracked memory yet');
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
tracker.append(mockFrame(1));
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
assert.deepEqual(
tracker.get('cpu'),
[{ timestamp: makeDate(refDate + 1), used: 1001, percent: 1001 / 2000 }],
'One frame of cpu'
);
assert.deepEqual(
tracker.get('memory'),
2021-12-28 16:08:12 +00:00
[
{
timestamp: makeDate(refDate + 1),
used: 2049 * 1024 * 1024,
percent: 2049 / 4096,
},
],
2019-03-13 00:04:16 +00:00
'One frame of memory'
);
tracker.append(mockFrame(2));
assert.deepEqual(
tracker.get('cpu'),
[
{ timestamp: makeDate(refDate + 1), used: 1001, percent: 1001 / 2000 },
{ timestamp: makeDate(refDate + 2), used: 1002, percent: 1002 / 2000 },
],
'Two frames of cpu'
);
assert.deepEqual(
tracker.get('memory'),
[
2021-12-28 16:08:12 +00:00
{
timestamp: makeDate(refDate + 1),
used: 2049 * 1024 * 1024,
percent: 2049 / 4096,
},
{
timestamp: makeDate(refDate + 2),
used: 2050 * 1024 * 1024,
percent: 2050 / 4096,
},
2019-03-13 00:04:16 +00:00
],
'Two frames of memory'
);
2018-08-31 21:37:13 +00:00
});
2021-12-28 14:45:20 +00:00
test('each stat list has maxLength equal to bufferSize', async function (assert) {
2019-03-13 00:04:16 +00:00
const node = MockNode();
const bufferSize = 10;
const tracker = NodeStatsTracker.create({ fetch, node, bufferSize });
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
for (let i = 1; i <= 20; i++) {
tracker.append(mockFrame(i));
}
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
assert.equal(
tracker.get('cpu.length'),
bufferSize,
`20 calls to append, only ${bufferSize} frames in the stats array`
);
assert.equal(
tracker.get('memory.length'),
bufferSize,
`20 calls to append, only ${bufferSize} frames in the stats array`
);
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
assert.equal(
+tracker.get('cpu')[0].timestamp,
+makeDate(refDate + 11),
'Old frames are removed in favor of newer ones'
);
assert.equal(
+tracker.get('memory')[0].timestamp,
+makeDate(refDate + 11),
'Old frames are removed in favor of newer ones'
);
2018-08-31 21:37:13 +00:00
});
2021-12-28 14:45:20 +00:00
test('the stats computed property macro constructs a NodeStatsTracker based on a nodeProp and a fetch definition', async function (assert) {
2019-03-13 00:04:16 +00:00
const node = MockNode();
const fetchSpy = sinon.spy();
const SomeClass = EmberObject.extend({
2021-12-28 14:45:20 +00:00
stats: stats('theNode', function () {
2019-03-13 00:04:16 +00:00
return () => fetchSpy(this);
}),
});
const someObject = SomeClass.create({
theNode: node,
});
assert.equal(
someObject.get('stats.url'),
`/v1/client/stats?node_id=${node.id}`,
'stats computed property macro creates a NodeStatsTracker'
);
someObject.get('stats').fetch();
assert.ok(
fetchSpy.calledWith(someObject),
'the fetch factory passed into the macro gets called to assign a bound version of fetch to the NodeStatsTracker instance'
);
2018-08-31 21:37:13 +00:00
});
2021-12-28 14:45:20 +00:00
test('changing the value of the nodeProp constructs a new NodeStatsTracker', async function (assert) {
2019-03-13 00:04:16 +00:00
const node1 = MockNode();
const node2 = MockNode();
const SomeClass = EmberObject.extend({
stats: stats('theNode', () => fetch),
});
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
const someObject = SomeClass.create({
theNode: node1,
});
2018-08-31 21:37:13 +00:00
2019-03-13 00:04:16 +00:00
const stats1 = someObject.get('stats');
2018-11-02 05:12:44 +00:00
2019-03-13 00:04:16 +00:00
someObject.set('theNode', node2);
const stats2 = someObject.get('stats');
2021-12-28 16:08:12 +00:00
assert.notStrictEqual(
stats1,
stats2,
2019-03-13 00:04:16 +00:00
'Changing the value of the node results in creating a new NodeStatsTracker instance'
);
});
statsTrackerFrameMissingBehavior({
resourceName: 'node',
ResourceConstructor: MockNode,
TrackerConstructor: NodeStatsTracker,
mockFrame,
compileResources(frame) {
const timestamp = makeDate(frame.Timestamp);
return [
{
timestamp,
used: frame.CPUTicksConsumed,
percent: frame.CPUTicksConsumed / 2000,
},
{
timestamp,
used: frame.Memory.Used,
percent: frame.Memory.Used / 1024 / 1024 / 4096,
},
];
},
});
2018-11-02 05:12:44 +00:00
});