Skip to content

Commit

Permalink
Update stat tracker unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DingoEatingFuzz committed Sep 20, 2018
1 parent 866f650 commit 47ec74e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 62 deletions.
16 changes: 10 additions & 6 deletions ui/app/utils/classes/abstract-stats-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ export default Mixin.create({
// Interrupt any pause attempt
this.get('signalPause').cancelAll();

const url = this.get('url');
assert('Url must be defined', url);

yield this.get('fetch')(url)
.then(res => res.json())
.then(frame => this.append(frame));
try {
const url = this.get('url');
assert('Url must be defined', url);

yield this.get('fetch')(url)
.then(res => res.json())
.then(frame => this.append(frame));
} catch (error) {
throw new Error(error);
}

yield timeout(2000);
}).drop(),
Expand Down
111 changes: 68 additions & 43 deletions ui/tests/unit/utils/allocation-stats-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import fetch from 'nomad-ui/utils/fetch';

module('Unit | Util | AllocationStatsTracker');

const refDate = Date.now();
const refDate = Date.now() * 1000000;
const makeDate = ts => new Date(ts / 1000000);

const MockAllocation = overrides =>
assign(
Expand Down Expand Up @@ -91,7 +92,7 @@ test('the AllocationStatsTracker constructor expects a fetch definition and an a
const tracker = AllocationStatsTracker.create();
assert.throws(
() => {
tracker.poll();
tracker.fetch();
},
/StatsTrackers need a fetch method/,
'Polling does not work without a fetch method provided'
Expand Down Expand Up @@ -159,7 +160,7 @@ test('poll results in requesting the url and calling append with the resulting J
this.get('/v1/client/allocation/:id/stats', () => [200, {}, JSON.stringify(mockFrame)]);
});

tracker.poll();
tracker.get('poll').perform();

assert.equal(server.handledRequests.length, 1, 'Only one request was made');
assert.equal(
Expand Down Expand Up @@ -199,12 +200,18 @@ test('append appropriately maps a data frame to the tracked stats for cpu and me

assert.deepEqual(
tracker.get('cpu'),
[{ timestamp: refDate + 1000, used: 101, percent: 101 / 200 }],
[{ timestamp: makeDate(refDate + 1000), used: 101, percent: 101 / 200 }],
'One frame of cpu'
);
assert.deepEqual(
tracker.get('memory'),
[{ timestamp: refDate + 1000, used: 401 * 1024 * 1024, percent: 401 / 512 }],
[
{
timestamp: makeDate(refDate + 1000),
used: 401 * 1024 * 1024,
percent: 401 / 512,
},
],
'One frame of memory'
);

Expand All @@ -215,22 +222,40 @@ test('append appropriately maps a data frame to the tracked stats for cpu and me
task: 'service',
reservedCPU: 100,
reservedMemory: 256,
cpu: [{ timestamp: refDate + 1, used: 51, percent: 51 / 100 }],
memory: [{ timestamp: refDate + 1, used: 101 * 1024 * 1024, percent: 101 / 256 }],
cpu: [{ timestamp: makeDate(refDate + 1), used: 51, percent: 51 / 100 }],
memory: [
{
timestamp: makeDate(refDate + 1),
used: 101 * 1024 * 1024,
percent: 101 / 256,
},
],
},
{
task: 'log-shipper',
reservedCPU: 50,
reservedMemory: 128,
cpu: [{ timestamp: refDate + 10, used: 26, percent: 26 / 50 }],
memory: [{ timestamp: refDate + 10, used: 51 * 1024 * 1024, percent: 51 / 128 }],
cpu: [{ timestamp: makeDate(refDate + 10), used: 26, percent: 26 / 50 }],
memory: [
{
timestamp: makeDate(refDate + 10),
used: 51 * 1024 * 1024,
percent: 51 / 128,
},
],
},
{
task: 'sidecar',
reservedCPU: 50,
reservedMemory: 128,
cpu: [{ timestamp: refDate + 100, used: 27, percent: 27 / 50 }],
memory: [{ timestamp: refDate + 100, used: 52 * 1024 * 1024, percent: 52 / 128 }],
cpu: [{ timestamp: makeDate(refDate + 100), used: 27, percent: 27 / 50 }],
memory: [
{
timestamp: makeDate(refDate + 100),
used: 52 * 1024 * 1024,
percent: 52 / 128,
},
],
},
],
'tasks represents the tasks for the allocation, each with one frame of stats'
Expand All @@ -241,16 +266,16 @@ test('append appropriately maps a data frame to the tracked stats for cpu and me
assert.deepEqual(
tracker.get('cpu'),
[
{ timestamp: refDate + 1000, used: 101, percent: 101 / 200 },
{ timestamp: refDate + 2000, used: 102, percent: 102 / 200 },
{ timestamp: makeDate(refDate + 1000), used: 101, percent: 101 / 200 },
{ timestamp: makeDate(refDate + 2000), used: 102, percent: 102 / 200 },
],
'Two frames of cpu'
);
assert.deepEqual(
tracker.get('memory'),
[
{ timestamp: refDate + 1000, used: 401 * 1024 * 1024, percent: 401 / 512 },
{ timestamp: refDate + 2000, used: 402 * 1024 * 1024, percent: 402 / 512 },
{ timestamp: makeDate(refDate + 1000), used: 401 * 1024 * 1024, percent: 401 / 512 },
{ timestamp: makeDate(refDate + 2000), used: 402 * 1024 * 1024, percent: 402 / 512 },
],
'Two frames of memory'
);
Expand All @@ -263,38 +288,38 @@ test('append appropriately maps a data frame to the tracked stats for cpu and me
reservedCPU: 100,
reservedMemory: 256,
cpu: [
{ timestamp: refDate + 1, used: 51, percent: 51 / 100 },
{ timestamp: refDate + 2, used: 52, percent: 52 / 100 },
{ timestamp: makeDate(refDate + 1), used: 51, percent: 51 / 100 },
{ timestamp: makeDate(refDate + 2), used: 52, percent: 52 / 100 },
],
memory: [
{ timestamp: refDate + 1, used: 101 * 1024 * 1024, percent: 101 / 256 },
{ timestamp: refDate + 2, used: 102 * 1024 * 1024, percent: 102 / 256 },
{ timestamp: makeDate(refDate + 1), used: 101 * 1024 * 1024, percent: 101 / 256 },
{ timestamp: makeDate(refDate + 2), used: 102 * 1024 * 1024, percent: 102 / 256 },
],
},
{
task: 'log-shipper',
reservedCPU: 50,
reservedMemory: 128,
cpu: [
{ timestamp: refDate + 10, used: 26, percent: 26 / 50 },
{ timestamp: refDate + 20, used: 27, percent: 27 / 50 },
{ timestamp: makeDate(refDate + 10), used: 26, percent: 26 / 50 },
{ timestamp: makeDate(refDate + 20), used: 27, percent: 27 / 50 },
],
memory: [
{ timestamp: refDate + 10, used: 51 * 1024 * 1024, percent: 51 / 128 },
{ timestamp: refDate + 20, used: 52 * 1024 * 1024, percent: 52 / 128 },
{ timestamp: makeDate(refDate + 10), used: 51 * 1024 * 1024, percent: 51 / 128 },
{ timestamp: makeDate(refDate + 20), used: 52 * 1024 * 1024, percent: 52 / 128 },
],
},
{
task: 'sidecar',
reservedCPU: 50,
reservedMemory: 128,
cpu: [
{ timestamp: refDate + 100, used: 27, percent: 27 / 50 },
{ timestamp: refDate + 200, used: 28, percent: 28 / 50 },
{ timestamp: makeDate(refDate + 100), used: 27, percent: 27 / 50 },
{ timestamp: makeDate(refDate + 200), used: 28, percent: 28 / 50 },
],
memory: [
{ timestamp: refDate + 100, used: 52 * 1024 * 1024, percent: 52 / 128 },
{ timestamp: refDate + 200, used: 53 * 1024 * 1024, percent: 53 / 128 },
{ timestamp: makeDate(refDate + 100), used: 52 * 1024 * 1024, percent: 52 / 128 },
{ timestamp: makeDate(refDate + 200), used: 53 * 1024 * 1024, percent: 53 / 128 },
],
},
],
Expand Down Expand Up @@ -323,13 +348,13 @@ test('each stat list has maxLength equal to bufferSize', function(assert) {
);

assert.equal(
tracker.get('cpu')[0].timestamp,
refDate + 11000,
+tracker.get('cpu')[0].timestamp,
+makeDate(refDate + 11000),
'Old frames are removed in favor of newer ones'
);
assert.equal(
tracker.get('memory')[0].timestamp,
refDate + 11000,
+tracker.get('memory')[0].timestamp,
+makeDate(refDate + 11000),
'Old frames are removed in favor of newer ones'
);

Expand All @@ -347,35 +372,35 @@ test('each stat list has maxLength equal to bufferSize', function(assert) {
});

assert.equal(
tracker.get('tasks').findBy('task', 'service').cpu[0].timestamp,
refDate + 11,
+tracker.get('tasks').findBy('task', 'service').cpu[0].timestamp,
+makeDate(refDate + 11),
'Old frames are removed in favor of newer ones'
);
assert.equal(
tracker.get('tasks').findBy('task', 'service').memory[0].timestamp,
refDate + 11,
+tracker.get('tasks').findBy('task', 'service').memory[0].timestamp,
+makeDate(refDate + 11),
'Old frames are removed in favor of newer ones'
);

assert.equal(
tracker.get('tasks').findBy('task', 'log-shipper').cpu[0].timestamp,
refDate + 110,
+tracker.get('tasks').findBy('task', 'log-shipper').cpu[0].timestamp,
+makeDate(refDate + 110),
'Old frames are removed in favor of newer ones'
);
assert.equal(
tracker.get('tasks').findBy('task', 'log-shipper').memory[0].timestamp,
refDate + 110,
+tracker.get('tasks').findBy('task', 'log-shipper').memory[0].timestamp,
+makeDate(refDate + 110),
'Old frames are removed in favor of newer ones'
);

assert.equal(
tracker.get('tasks').findBy('task', 'sidecar').cpu[0].timestamp,
refDate + 1100,
+tracker.get('tasks').findBy('task', 'sidecar').cpu[0].timestamp,
+makeDate(refDate + 1100),
'Old frames are removed in favor of newer ones'
);
assert.equal(
tracker.get('tasks').findBy('task', 'sidecar').memory[0].timestamp,
refDate + 1100,
+tracker.get('tasks').findBy('task', 'sidecar').memory[0].timestamp,
+makeDate(refDate + 1100),
'Old frames are removed in favor of newer ones'
);
});
Expand Down
27 changes: 14 additions & 13 deletions ui/tests/unit/utils/node-stats-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import fetch from 'nomad-ui/utils/fetch';

module('Unit | Util | NodeStatsTracker');

const refDate = Date.now();
const refDate = Date.now() * 1000000;
const makeDate = ts => new Date(ts / 1000000);

const MockNode = overrides =>
assign(
Expand All @@ -35,7 +36,7 @@ test('the NodeStatsTracker constructor expects a fetch definition and a node', f
const tracker = NodeStatsTracker.create();
assert.throws(
() => {
tracker.poll();
tracker.fetch();
},
/StatsTrackers need a fetch method/,
'Polling does not work without a fetch method provided'
Expand Down Expand Up @@ -79,7 +80,7 @@ test('poll results in requesting the url and calling append with the resulting J
this.get('/v1/client/stats', () => [200, {}, JSON.stringify(mockFrame)]);
});

tracker.poll();
tracker.get('poll').perform();

assert.equal(server.handledRequests.length, 1, 'Only one request was made');
assert.equal(
Expand Down Expand Up @@ -109,13 +110,13 @@ test('append appropriately maps a data frame to the tracked stats for cpu and me

assert.deepEqual(
tracker.get('cpu'),
[{ timestamp: refDate + 1, used: 1001, percent: 1001 / 2000 }],
[{ timestamp: makeDate(refDate + 1), used: 1001, percent: 1001 / 2000 }],
'One frame of cpu'
);

assert.deepEqual(
tracker.get('memory'),
[{ timestamp: refDate + 1, used: 2049 * 1024 * 1024, percent: 2049 / 4096 }],
[{ timestamp: makeDate(refDate + 1), used: 2049 * 1024 * 1024, percent: 2049 / 4096 }],
'One frame of memory'
);

Expand All @@ -124,17 +125,17 @@ test('append appropriately maps a data frame to the tracked stats for cpu and me
assert.deepEqual(
tracker.get('cpu'),
[
{ timestamp: refDate + 1, used: 1001, percent: 1001 / 2000 },
{ timestamp: refDate + 2, used: 1002, percent: 1002 / 2000 },
{ 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'),
[
{ timestamp: refDate + 1, used: 2049 * 1024 * 1024, percent: 2049 / 4096 },
{ timestamp: refDate + 2, used: 2050 * 1024 * 1024, percent: 2050 / 4096 },
{ timestamp: makeDate(refDate + 1), used: 2049 * 1024 * 1024, percent: 2049 / 4096 },
{ timestamp: makeDate(refDate + 2), used: 2050 * 1024 * 1024, percent: 2050 / 4096 },
],
'Two frames of memory'
);
Expand All @@ -161,13 +162,13 @@ test('each stat list has maxLength equal to bufferSize', function(assert) {
);

assert.equal(
tracker.get('cpu')[0].timestamp,
refDate + 11,
+tracker.get('cpu')[0].timestamp,
+makeDate(refDate + 11),
'Old frames are removed in favor of newer ones'
);
assert.equal(
tracker.get('memory')[0].timestamp,
refDate + 11,
+tracker.get('memory')[0].timestamp,
+makeDate(refDate + 11),
'Old frames are removed in favor of newer ones'
);
});
Expand Down

0 comments on commit 47ec74e

Please sign in to comment.