From ea395e003fbdf3d08f26c1f904ca0db35700a02e Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Wed, 12 Dec 2018 18:40:10 -0800 Subject: [PATCH] Don't use Ember.get in conjunction with dynamic strings in the job-plan serializer --- ui/app/serializers/job-plan.js | 6 +- ui/tests/unit/serializers/job-plan-test.js | 95 ++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 ui/tests/unit/serializers/job-plan-test.js diff --git a/ui/app/serializers/job-plan.js b/ui/app/serializers/job-plan.js index 0280734383df..19f9556d304b 100644 --- a/ui/app/serializers/job-plan.js +++ b/ui/app/serializers/job-plan.js @@ -1,11 +1,11 @@ -import { get } from '@ember/object'; import { assign } from '@ember/polyfills'; import ApplicationSerializer from './application'; export default ApplicationSerializer.extend({ normalize(typeHash, hash) { - hash.FailedTGAllocs = Object.keys(hash.FailedTGAllocs || {}).map(key => { - return assign({ Name: key }, get(hash, `FailedTGAllocs.${key}`) || {}); + const failures = hash.FailedTGAllocs || {}; + hash.FailedTGAllocs = Object.keys(failures).map(key => { + return assign({ Name: key }, failures[key] || {}); }); return this._super(...arguments); }, diff --git a/ui/tests/unit/serializers/job-plan-test.js b/ui/tests/unit/serializers/job-plan-test.js new file mode 100644 index 000000000000..429ae9a18444 --- /dev/null +++ b/ui/tests/unit/serializers/job-plan-test.js @@ -0,0 +1,95 @@ +import { test } from 'ember-qunit'; +import JobPlanModel from 'nomad-ui/models/job-plan'; +import moduleForSerializer from '../../helpers/module-for-serializer'; + +moduleForSerializer('job-plan', 'Unit | Serializer | JobPlan', { + needs: [ + 'service:token', + 'service:system', + 'serializer:job-plan', + 'transform:fragment-array', + 'model:placement-failure', + ], +}); + +const sampleDate = new Date('2018-12-12T00:00:00'); +const normalizationTestCases = [ + { + name: 'Normal', + in: { + ID: 'test-plan', + Diff: { + Arbitrary: 'Value', + }, + FailedTGAllocs: { + taskGroup: { + NodesAvailable: 10, + }, + }, + }, + out: { + data: { + id: 'test-plan', + type: 'job-plan', + attributes: { + diff: { + Arbitrary: 'Value', + }, + failedTGAllocs: [ + { + name: 'taskGroup', + nodesAvailable: 10, + }, + ], + }, + relationships: {}, + }, + }, + }, + + { + name: 'Dots in task names', + in: { + ID: 'test-plan', + Diff: { + Arbitrary: 'Value', + }, + FailedTGAllocs: { + 'one.two': { + NodesAvailable: 10, + }, + 'three.four': { + NodesAvailable: 25, + }, + }, + }, + out: { + data: { + id: 'test-plan', + type: 'job-plan', + attributes: { + diff: { + Arbitrary: 'Value', + }, + failedTGAllocs: [ + { + name: 'one.two', + nodesAvailable: 10, + }, + { + name: 'three.four', + nodesAvailable: 25, + }, + ], + }, + relationships: {}, + }, + }, + }, +]; + +normalizationTestCases.forEach(testCase => { + test(`normalization: ${testCase.name}`, function(assert) { + assert.deepEqual(this.subject().normalize(JobPlanModel, testCase.in), testCase.out); + }); +});