2019-03-14 02:17:12 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
2018-02-20 20:04:36 +00:00
|
|
|
import { run } from '@ember/runloop';
|
|
|
|
import NodeModel from 'nomad-ui/models/node';
|
|
|
|
import pushPayloadToStore from '../../utils/push-payload-to-store';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { settled } from '@ember/test-helpers';
|
2018-02-20 20:04:36 +00:00
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
module('Unit | Serializer | Node', function(hooks) {
|
|
|
|
setupTest(hooks);
|
|
|
|
hooks.beforeEach(function() {
|
|
|
|
this.store = this.owner.lookup('service:store');
|
|
|
|
this.subject = () => this.store.serializerFor('node');
|
|
|
|
});
|
2018-02-20 20:04:36 +00:00
|
|
|
|
2019-03-14 18:35:46 +00:00
|
|
|
test('local store is culled to reflect the state of findAll requests', async function(assert) {
|
2019-03-14 02:17:12 +00:00
|
|
|
const findAllResponse = [
|
|
|
|
makeNode('1', 'One', '127.0.0.1:4646'),
|
|
|
|
makeNode('2', 'Two', '127.0.0.2:4646'),
|
|
|
|
makeNode('3', 'Three', '127.0.0.3:4646'),
|
|
|
|
];
|
2018-02-20 20:04:36 +00:00
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
const payload = this.subject().normalizeFindAllResponse(this.store, NodeModel, findAllResponse);
|
|
|
|
pushPayloadToStore(this.store, payload, NodeModel.modelName);
|
2018-02-20 20:04:36 +00:00
|
|
|
|
|
|
|
assert.equal(
|
2019-03-14 02:17:12 +00:00
|
|
|
payload.data.length,
|
|
|
|
findAllResponse.length,
|
|
|
|
'Each original record is returned in the response'
|
2018-02-20 20:04:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
this.store
|
|
|
|
.peekAll('node')
|
|
|
|
.filterBy('id')
|
|
|
|
.get('length'),
|
2019-03-14 02:17:12 +00:00
|
|
|
findAllResponse.length,
|
|
|
|
'Each original record is now in the store'
|
2018-02-20 20:04:36 +00:00
|
|
|
);
|
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
const newFindAllResponse = [
|
|
|
|
makeNode('2', 'Two', '127.0.0.2:4646'),
|
|
|
|
makeNode('3', 'Three', '127.0.0.3:4646'),
|
|
|
|
makeNode('4', 'Four', '127.0.0.4:4646'),
|
|
|
|
];
|
|
|
|
|
|
|
|
let newPayload;
|
|
|
|
run(() => {
|
|
|
|
newPayload = this.subject().normalizeFindAllResponse(
|
|
|
|
this.store,
|
|
|
|
NodeModel,
|
|
|
|
newFindAllResponse
|
|
|
|
);
|
|
|
|
});
|
|
|
|
pushPayloadToStore(this.store, newPayload, NodeModel.modelName);
|
|
|
|
|
2019-03-14 18:35:46 +00:00
|
|
|
await settled();
|
|
|
|
assert.equal(
|
|
|
|
newPayload.data.length,
|
|
|
|
newFindAllResponse.length,
|
|
|
|
'Each new record is returned in the response'
|
|
|
|
);
|
2019-03-14 02:17:12 +00:00
|
|
|
|
2019-03-14 18:35:46 +00:00
|
|
|
assert.equal(
|
|
|
|
this.store
|
|
|
|
.peekAll('node')
|
|
|
|
.filterBy('id')
|
|
|
|
.get('length'),
|
|
|
|
newFindAllResponse.length,
|
|
|
|
'The node length in the store reflects the new response'
|
|
|
|
);
|
2019-03-14 02:17:12 +00:00
|
|
|
|
2019-03-14 18:35:46 +00:00
|
|
|
assert.notOk(this.store.peekAll('node').findBy('id', '1'), 'Record One is no longer found');
|
2018-02-20 20:04:36 +00:00
|
|
|
});
|
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
function makeNode(id, name, ip) {
|
|
|
|
return { ID: id, Name: name, HTTPAddr: ip };
|
|
|
|
}
|
2018-12-13 01:23:09 +00:00
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
const normalizationTestCases = [
|
|
|
|
{
|
|
|
|
name: 'Normal',
|
|
|
|
in: {
|
|
|
|
ID: 'test-node',
|
|
|
|
HTTPAddr: '867.53.0.9:4646',
|
|
|
|
Drain: false,
|
|
|
|
Drivers: {
|
|
|
|
docker: {
|
|
|
|
Detected: true,
|
|
|
|
Healthy: false,
|
|
|
|
},
|
2018-12-13 01:23:09 +00:00
|
|
|
},
|
|
|
|
},
|
2019-03-14 02:17:12 +00:00
|
|
|
out: {
|
|
|
|
data: {
|
|
|
|
id: 'test-node',
|
|
|
|
type: 'node',
|
|
|
|
attributes: {
|
|
|
|
isDraining: false,
|
|
|
|
httpAddr: '867.53.0.9:4646',
|
|
|
|
drivers: [
|
|
|
|
{
|
|
|
|
name: 'docker',
|
|
|
|
detected: true,
|
|
|
|
healthy: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
relationships: {
|
|
|
|
allocations: {
|
|
|
|
links: {
|
|
|
|
related: '/v1/node/test-node/allocations',
|
|
|
|
},
|
2018-12-13 01:23:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
{
|
|
|
|
name: 'Dots in driver names',
|
|
|
|
in: {
|
|
|
|
ID: 'test-node',
|
|
|
|
HTTPAddr: '867.53.0.9:4646',
|
|
|
|
Drain: false,
|
|
|
|
Drivers: {
|
|
|
|
'my.driver': {
|
|
|
|
Detected: true,
|
|
|
|
Healthy: false,
|
|
|
|
},
|
|
|
|
'my.other.driver': {
|
|
|
|
Detected: false,
|
|
|
|
Healthy: false,
|
|
|
|
},
|
2018-12-13 01:23:09 +00:00
|
|
|
},
|
|
|
|
},
|
2019-03-14 02:17:12 +00:00
|
|
|
out: {
|
|
|
|
data: {
|
|
|
|
id: 'test-node',
|
|
|
|
type: 'node',
|
|
|
|
attributes: {
|
|
|
|
isDraining: false,
|
|
|
|
httpAddr: '867.53.0.9:4646',
|
|
|
|
drivers: [
|
|
|
|
{
|
|
|
|
name: 'my.driver',
|
|
|
|
detected: true,
|
|
|
|
healthy: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'my.other.driver',
|
|
|
|
detected: false,
|
|
|
|
healthy: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
relationships: {
|
|
|
|
allocations: {
|
|
|
|
links: {
|
|
|
|
related: '/v1/node/test-node/allocations',
|
|
|
|
},
|
2018-12-13 01:23:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-03-14 02:17:12 +00:00
|
|
|
];
|
2018-12-13 01:23:09 +00:00
|
|
|
|
2019-03-14 02:17:12 +00:00
|
|
|
normalizationTestCases.forEach(testCase => {
|
2019-03-14 18:35:46 +00:00
|
|
|
test(`normalization: ${testCase.name}`, async function(assert) {
|
2019-03-14 02:17:12 +00:00
|
|
|
assert.deepEqual(this.subject().normalize(NodeModel, testCase.in), testCase.out);
|
|
|
|
});
|
2018-12-13 01:23:09 +00:00
|
|
|
});
|
|
|
|
});
|