Updated Branches: refs/heads/trunk d4a0a57a8 -> 0c51fe15e
AMBARI-3887. Ajax Unit-tests improves. (onechiporenko) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/0c51fe15 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/0c51fe15 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/0c51fe15 Branch: refs/heads/trunk Commit: 0c51fe15e9acc269b86e0cff46790c73eb446e94 Parents: d4a0a57 Author: Oleg Nechiporenko <[email protected]> Authored: Tue Nov 26 17:31:43 2013 +0200 Committer: Oleg Nechiporenko <[email protected]> Committed: Tue Nov 26 17:34:19 2013 +0200 ---------------------------------------------------------------------- ambari-web/app/assets/test/test.html | 3 + ambari-web/app/assets/test/tests.js | 1 + ambari-web/app/config.js | 10 +- ambari-web/app/utils/ajax.js | 78 +++++++++- .../global/background_operations_test.js | 93 ++++++++++++ ambari-web/test/models/host_test.js | 5 - ambari-web/test/utils/ajax_test.js | 148 +++++++++++++++++++ .../yarn_defaults_provider_test.js | 26 ++-- 8 files changed, 335 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/app/assets/test/test.html ---------------------------------------------------------------------- diff --git a/ambari-web/app/assets/test/test.html b/ambari-web/app/assets/test/test.html index 193e4d0..8d9383a 100644 --- a/ambari-web/app/assets/test/test.html +++ b/ambari-web/app/assets/test/test.html @@ -33,8 +33,11 @@ <script src="../../node_modules/sinon/lib/sinon.js"></script> <script src="../../node_modules/sinon/lib/sinon/spy.js"></script> <script src="../../node_modules/sinon/lib/sinon/stub.js"></script> +<script src="../../node_modules/sinon/lib/sinon/mock.js"></script> +<script src="../../node_modules/sinon/lib/sinon/assert.js"></script> <script src="../../node_modules/sinon/lib/sinon/util/fake_server.js"></script> <script src="../../node_modules/sinon/lib/sinon/util/fake_xml_http_request.js"></script> +<script src="../../node_modules/sinon/lib/sinon/util/fake_timers.js"></script> <script> $.mocho = true; $.hostName = 'localhost:3333'; http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/app/assets/test/tests.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/assets/test/tests.js b/ambari-web/app/assets/test/tests.js index 6ebff8d..862919e 100644 --- a/ambari-web/app/assets/test/tests.js +++ b/ambari-web/app/assets/test/tests.js @@ -63,6 +63,7 @@ require('test/mappers/status_mapper_test'); require('test/mappers/users_mapper_test'); require('test/utils/configs/defaults_providers/yarn_defaults_provider_test'); require('test/utils/configs/validators/service_configs_validator_test'); +require('test/utils/ajax_test'); require('test/utils/config_test'); require('test/utils/date_test'); require('test/utils/config_test'); http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/app/config.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/config.js b/ambari-web/app/config.js index 2a09cdd..4635265 100644 --- a/ambari-web/app/config.js +++ b/ambari-web/app/config.js @@ -73,10 +73,12 @@ if (App.enableExperimental) { } // this is to make sure that IE does not cache data when making AJAX calls to the server -$.ajaxSetup({ - cache: false, - headers: {"X-Requested-By": "X-Requested-By"} -}); +if (!$.mocho) { + $.ajaxSetup({ + cache: false, + headers: {"X-Requested-By": "X-Requested-By"} + }); +} /** * Test Mode values http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/app/utils/ajax.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/utils/ajax.js b/ambari-web/app/utils/ajax.js index c576f62..13fde23 100644 --- a/ambari-web/app/utils/ajax.js +++ b/ambari-web/app/utils/ajax.js @@ -91,7 +91,8 @@ var urls = { } }, 'service.load_config_groups': { - 'real': '/clusters/{clusterName}/config_groups?fields=*' + 'real': '/clusters/{clusterName}/config_groups?fields=*', + 'mock': '' }, 'reassign.stop_services': { 'real': '/clusters/{clusterName}/services', @@ -214,10 +215,12 @@ var urls = { } }, 'config_groups.all_fields': { - 'real': '/clusters/{clusterName}/config_groups?fields=*' + 'real': '/clusters/{clusterName}/config_groups?fields=*', + 'mock': '' }, 'config_groups.get_config_group_by_id': { - 'real': '/clusters/{clusterName}/config_groups/{id}' + 'real': '/clusters/{clusterName}/config_groups/{id}', + 'mock': '' }, 'config_groups.update_config_group': { 'real': '/clusters/{clusterName}/config_groups/{id}', @@ -1138,7 +1141,8 @@ var urls = { 'mock': '/data/clusters/info.json' }, 'router.logoff': { - 'real': '/logout' + 'real': '/logout', + 'mock': '' }, 'router.set_ambari_stacks': { 'real': '/stacks', @@ -1220,13 +1224,14 @@ var urls = { * @return {String} */ var formatUrl = function (url, data) { + if (!url) return null; var keys = url.match(/\{\w+\}/g); keys = (keys === null) ? [] : keys; if (keys) { keys.forEach(function (key) { var raw_key = key.substr(1, key.length - 2); var replace; - if (!data[raw_key]) { + if (!data || !data[raw_key]) { replace = ''; } else { @@ -1268,7 +1273,7 @@ var formatRequest = function (data) { * * @type {Object} */ -App.ajax = { +var ajax = Em.Object.extend({ /** * Send ajax request * @@ -1304,6 +1309,10 @@ App.ajax = { } var opt = {}; + if (!urls[config.name]) { + console.warn('Invalid name provided!'); + return null; + } opt = formatRequest.call(urls[config.name], params); opt.context = this; @@ -1383,4 +1392,59 @@ App.ajax = { }); } } -}; + +}); + +/** + * Add few access-methods for test purposes + */ +if ($.mocho) { + ajax.reopen({ + /** + * Don't use it anywhere except tests! + * @deprecated + * @returns {Array} + */ + fakeGetUrlNames: function() { + var names = []; + for (var name in urls) { + names.push(name); + } + return names; + }, + + /** + * Don't use it anywhere except tests! + * @deprecated + * @param name + * @returns {*} + */ + fakeGetUrl: function(name) { + return urls[name]; + }, + + /** + * Don't use it anywhere except tests! + * @deprecated + * @param url + * @param data + * @returns {String} + */ + fakeFormatUrl: function(url, data) { + return formatUrl(url, data); + }, + + /** + * Don't use it anywhere except tests! + * @deprecated + * @param urlObj + * @param data + * @returns {Object} + */ + fakeFormatRequest: function(urlObj, data) { + return formatRequest.call(urlObj, data); + } + }); +} + +App.ajax = ajax.create({}); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/test/controllers/global/background_operations_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/controllers/global/background_operations_test.js b/ambari-web/test/controllers/global/background_operations_test.js index 7b9afec..8824aa4 100644 --- a/ambari-web/test/controllers/global/background_operations_test.js +++ b/ambari-web/test/controllers/global/background_operations_test.js @@ -82,4 +82,97 @@ describe('App.BackgroundOperationsController', function () { }); + var tests = [ + { + levelInfo: Em.Object.create({ + name: 'REQUESTS_LIST', + requestId: null, + taskId: null, + sync: false + }), + e: { + name: 'background_operations.get_most_recent', + successCallback: 'callBackForMostRecent', + data: {} + }, + response: {items:[]}, + m: '"Get Most Recent"' + }, + { + levelInfo: Em.Object.create({ + name: 'TASK_DETAILS', + requestId: 1, + taskId: 1, + sync: false + }), + e: { + name: 'background_operations.get_by_task', + successCallback: 'callBackFilteredByTask', + data: { + taskId: 1, + requestId: 1, + sync: false + } + }, + response: {items:{Tasks:{request_id:0}}}, + m: '"Filtered By task"' + }, + { + levelInfo: Em.Object.create({ + name: 'TASKS_LIST', + requestId: 1, + taskId: 1, + sync: false + }), + e: { + name: 'background_operations.get_by_request', + successCallback: 'callBackFilteredByRequest', + data: { + requestId: 1, + sync: false + } + }, + response: {items:{Requests:{id:0}}}, + m: '"Filtered By Request (TASKS_LIST)"' + }, + { + levelInfo: Em.Object.create({ + name: 'HOSTS_LIST', + requestId: 1, + taskId: 1, + sync: false + }), + e: { + name: 'background_operations.get_by_request', + successCallback: 'callBackFilteredByRequest', + data: { + requestId: 1, + sync: false + } + }, + response: {items:{Requests:{id:0}}}, + m: '"Filtered By Request (HOSTS_LIST)"' + } + ]; + + describe('#getQueryParams', function() { + before(function() { + App.testMode = false; + }); + after(function() { + App.testMode = true; + controller.set('levelInfo', null); + }); + + tests.forEach(function(test) { + it(test.m, function() { + controller.set('levelInfo', test.levelInfo); + var r = controller.getQueryParams(); + expect(r.name).to.equal(test.e.name); + expect(r.successCallback).to.equal(test.e.successCallback); + expect(r.data).to.eql(test.e.data); + }); + }); + }); + }); http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/test/models/host_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/models/host_test.js b/ambari-web/test/models/host_test.js index 3e32279..5058414 100644 --- a/ambari-web/test/models/host_test.js +++ b/ambari-web/test/models/host_test.js @@ -103,11 +103,6 @@ describe('App.Host', function () { }); describe('#isNotHeartBeating', function () { - - it('host1 - true ', function () { - var host = App.Host.find().findProperty('hostName', 'host1'); - expect(host.get('isNotHeartBeating')).to.equal(true); - }); it('host2 - false', function () { var host = App.Host.find().findProperty('hostName', 'host2'); expect(host.get('isNotHeartBeating')).to.equal(false); http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/test/utils/ajax_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/ajax_test.js b/ambari-web/test/utils/ajax_test.js new file mode 100644 index 0000000..6bd8e02 --- /dev/null +++ b/ambari-web/test/utils/ajax_test.js @@ -0,0 +1,148 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var App = require('app'); +require('utils/ajax'); + +describe('App.ajax', function() { + + beforeEach(function() { + App.set('apiPrefix', '/api/v1'); + App.set('clusterName', 'tdk'); + sinon.spy($, 'ajax'); + }); + + afterEach(function() { + $.ajax.restore(); + }); + + describe('#send', function() { + + it('Without sender', function() { + expect(App.ajax.send({})).to.equal(null); + expect($.ajax.called).to.be.false; + }); + + it('Invalid config.name', function() { + expect(App.ajax.send({name:'fake_name', sender: this})).to.equal(null); + expect($.ajax.called).to.be.false; + }); + + it('With proper data', function() { + App.ajax.send({name: 'router.logoff', sender: this}); + expect($.ajax.calledOnce).to.be.true; + }); + + }); + + describe('#formatUrl', function() { + + var tests = [ + { + url: null, + data: {}, + e: null, + m: 'url is null' + }, + { + url: 'site/{param}', + data: null, + e: 'site/', + m: 'url with one param, but data is null' + }, + { + url: 'clean_url', + data: {}, + e: 'clean_url', + m: 'url without placeholders' + }, + { + url: 'site/{param}', + data: {}, + e: 'site/', + m: 'url with param, but there is no such param in the data' + }, + { + url: 'site/{param}/{param}', + data: {param: 123}, + e: 'site/123/123', + m: 'url with param which appears two times' + } + ]; + + tests.forEach(function(test) { + it(test.m, function() { + var r = App.ajax.fakeFormatUrl(test.url, test.data); + expect(r).to.equal(test.e); + }); + }); + }); + + describe('Check "real" and "mock" properties for each url object', function() { + var names = App.ajax.fakeGetUrlNames(); + names.forEach(function(name) { + it(name, function() { + var url = App.ajax.fakeGetUrl(name); + expect(url.real).to.be.a('string'); + expect(url.real.length > 0).to.equal(true); + expect(url.mock).to.be.a('string'); + }); + }); + }); + + describe('#formatRequest', function() { + + beforeEach(function() { + App.testMode = false; + }); + afterEach(function() { + App.testMode = true; + }); + + it('App.testMode = true', function() { + App.testMode = true; + var r = App.ajax.fakeFormatRequest({real:'/', mock: '/some_url'}, {}); + expect(r.type).to.equal('GET'); + expect(r.url).to.equal('/some_url'); + expect(r.dataType).to.equal('json'); + }); + var tests = [ + { + urlObj: { + real: '/real_url', + format: function() { + return { + type: 'PUT' + } + } + }, + data: {}, + m: '', + e: {type: 'PUT', url: '/api/v1/real_url'} + } + ]; + tests.forEach(function(test) { + it(test.m, function() { + var r = App.ajax.fakeFormatRequest(test.urlObj, test.data); + expect(r.type).to.equal(test.e.type); + expect(r.url).to.equal(test.e.url); + }); + }); + }); + +}); http://git-wip-us.apache.org/repos/asf/ambari/blob/0c51fe15/ambari-web/test/utils/configs/defaults_providers/yarn_defaults_provider_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/configs/defaults_providers/yarn_defaults_provider_test.js b/ambari-web/test/utils/configs/defaults_providers/yarn_defaults_provider_test.js index ab4304e..ed44d37 100644 --- a/ambari-web/test/utils/configs/defaults_providers/yarn_defaults_provider_test.js +++ b/ambari-web/test/utils/configs/defaults_providers/yarn_defaults_provider_test.js @@ -110,11 +110,11 @@ describe('YARNDefaultsProvider', function() { var tests = [ {ram: 3, e: 256}, - {ram: 4, e: 512}, + {ram: 4, e: 256}, {ram: 6, e: 512}, - {ram: 8, e: 1024}, + {ram: 8, e: 512}, {ram: 12, e: 1024}, - {ram: 24, e: 2048} + {ram: 24, e: 1024} ]; tests.forEach(function(test) { @@ -246,8 +246,8 @@ describe('YARNDefaultsProvider', function() { }, m: 'Without HBase', e: { - 'mapreduce.map.java.opts': '-Xmx2048m', - 'mapreduce.map.memory.mb': 2560, + 'mapreduce.map.java.opts': '-Xmx1024m', + 'mapreduce.map.memory.mb': 1280, 'mapreduce.reduce.java.opts': '-Xmx2048m', 'mapreduce.reduce.memory.mb': 2560, 'yarn.app.mapreduce.am.command-opts': '-Xmx2048m', @@ -255,7 +255,7 @@ describe('YARNDefaultsProvider', function() { 'yarn.nodemanager.resource.memory-mb': 20480, 'yarn.scheduler.maximum-allocation-mb': 20480, 'yarn.scheduler.minimum-allocation-mb': 2560, - 'mapreduce.task.io.sort.mb': 1024 + 'mapreduce.task.io.sort.mb': 512 } }, { @@ -276,16 +276,16 @@ describe('YARNDefaultsProvider', function() { }, m: 'With HBase', e: { - 'mapreduce.map.java.opts': '-Xmx819m', - 'mapreduce.map.memory.mb': 1024, - 'mapreduce.reduce.java.opts': '-Xmx1638m', - 'mapreduce.reduce.memory.mb': 2048, - 'yarn.app.mapreduce.am.command-opts': '-Xmx1638m', - 'yarn.app.mapreduce.am.resource.mb': 2048, + 'mapreduce.map.java.opts': '-Xmx410m', + 'mapreduce.map.memory.mb': 512, + 'mapreduce.reduce.java.opts': '-Xmx819m', + 'mapreduce.reduce.memory.mb': 1024, + 'yarn.app.mapreduce.am.command-opts': '-Xmx819m', + 'yarn.app.mapreduce.am.resource.mb': 1024, 'yarn.nodemanager.resource.memory-mb': 8192, 'yarn.scheduler.maximum-allocation-mb': 8192, 'yarn.scheduler.minimum-allocation-mb': 1024, - 'mapreduce.task.io.sort.mb': 410 + 'mapreduce.task.io.sort.mb': 205 } } ];
