add_controller_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('controllers/wizard');
  20. require('controllers/main/service/add_controller');
  21. var addServiceController = null;
  22. describe('App.AddServiceController', function() {
  23. beforeEach(function () {
  24. addServiceController = App.AddServiceController.create({});
  25. });
  26. describe('#installAdditionalClients', function() {
  27. var t = {
  28. additionalClients: {
  29. componentName: "TEZ_CLIENT",
  30. hostNames: ["hostName1", "hostName2"]
  31. },
  32. additionalClientsWithoutHosts: {
  33. componentName: "TEZ_CLIENT",
  34. hostNames: []
  35. },
  36. RequestInfo: {
  37. "context": Em.I18n.t('requestInfo.installHostComponent') + ' ' + App.format.role("TEZ_CLIENT"),
  38. "query": "HostRoles/component_name=TEZ_CLIENT&HostRoles/host_name.in(hostName1,hostName2)"
  39. },
  40. Body: {
  41. HostRoles: {
  42. state: 'INSTALLED'
  43. }
  44. }
  45. };
  46. beforeEach(function () {
  47. sinon.spy($, 'ajax');
  48. sinon.stub(App, 'get', function(k) {
  49. if ('clusterName' === k) return 'tdk';
  50. return Em.get(App, k);
  51. });
  52. });
  53. afterEach(function () {
  54. $.ajax.restore();
  55. App.get.restore();
  56. });
  57. it('send request to install client', function () {
  58. addServiceController.set("content.additionalClients", [t.additionalClients]);
  59. addServiceController.installAdditionalClients();
  60. expect($.ajax.calledOnce).to.equal(true);
  61. expect(JSON.parse($.ajax.args[0][0].data).Body).to.deep.eql(t.Body);
  62. expect(JSON.parse($.ajax.args[0][0].data).RequestInfo).to.eql(t.RequestInfo);
  63. });
  64. it('should not send request to install client', function () {
  65. addServiceController.set("content.additionalClients", [t.additionalClientsWithoutHosts]);
  66. expect($.ajax.called).to.be.false;
  67. });
  68. });
  69. describe('#generateDataForInstallServices', function() {
  70. var tests = [{
  71. selected: ["YARN","HBASE"],
  72. res: {
  73. "context": Em.I18n.t('requestInfo.installServices'),
  74. "ServiceInfo": {"state": "INSTALLED"},
  75. "urlParams": "ServiceInfo/service_name.in(YARN,HBASE)"
  76. }
  77. },
  78. {
  79. selected: ['OOZIE'],
  80. res: {
  81. "context": Em.I18n.t('requestInfo.installServices'),
  82. "ServiceInfo": {"state": "INSTALLED"},
  83. "urlParams": "ServiceInfo/service_name.in(OOZIE,HDFS,YARN,MAPREDUCE2)"
  84. }
  85. }];
  86. tests.forEach(function(t){
  87. it('should generate data with ' + t.selected.join(","), function () {
  88. expect(addServiceController.generateDataForInstallServices(t.selected)).to.be.eql(t.res);
  89. });
  90. });
  91. });
  92. describe('#saveServices', function() {
  93. beforeEach(function() {
  94. sinon.stub(addServiceController, 'setDBProperty', Em.K);
  95. });
  96. afterEach(function() {
  97. addServiceController.setDBProperty.restore();
  98. });
  99. var tests = [
  100. {
  101. appService: [
  102. Em.Object.create({ serviceName: 'HDFS' }),
  103. Em.Object.create({ serviceName: 'KERBEROS' })
  104. ],
  105. stepCtrlContent: Em.Object.create({
  106. content: Em.A([
  107. Em.Object.create({ serviceName: 'HDFS', isInstalled: true, isSelected: true }),
  108. Em.Object.create({ serviceName: 'YARN', isInstalled: false, isSelected: true })
  109. ])
  110. }),
  111. e: {
  112. selected: ['YARN'],
  113. installed: ['HDFS', 'KERBEROS']
  114. }
  115. },
  116. {
  117. appService: [
  118. Em.Object.create({ serviceName: 'HDFS' }),
  119. Em.Object.create({ serviceName: 'STORM' })
  120. ],
  121. stepCtrlContent: Em.Object.create({
  122. content: Em.A([
  123. Em.Object.create({ serviceName: 'HDFS', isInstalled: true, isSelected: true }),
  124. Em.Object.create({ serviceName: 'YARN', isInstalled: false, isSelected: true }),
  125. Em.Object.create({ serviceName: 'MAPREDUCE2', isInstalled: false, isSelected: true })
  126. ])
  127. }),
  128. e: {
  129. selected: ['YARN', 'MAPREDUCE2'],
  130. installed: ['HDFS', 'STORM']
  131. }
  132. }
  133. ];
  134. var message = '{0} installed, {1} selected. Installed list should be {2} and selected - {3}';
  135. tests.forEach(function(test) {
  136. var installed = test.appService.mapProperty('serviceName');
  137. var selected = test.stepCtrlContent.get('content').filterProperty('isSelected', true)
  138. .filterProperty('isInstalled', false).mapProperty('serviceName');
  139. it(message.format(installed, selected, test.e.installed, test.e.selected), function() {
  140. sinon.stub(App.Service, 'find').returns(test.appService);
  141. addServiceController.saveServices(test.stepCtrlContent);
  142. App.Service.find.restore();
  143. var savedServices = addServiceController.setDBProperty.withArgs('services').args[0][1];
  144. expect(savedServices.selectedServices).to.have.members(test.e.selected);
  145. expect(savedServices.installedServices).to.have.members(test.e.installed);
  146. });
  147. });
  148. });
  149. describe('#loadHosts', function () {
  150. var cases = [
  151. {
  152. hosts: {},
  153. isAjaxRequestSent: false,
  154. title: 'hosts are already loaded'
  155. },
  156. {
  157. areHostsLoaded: false,
  158. isAjaxRequestSent: true,
  159. title: 'hosts aren\'t yet loaded'
  160. }
  161. ];
  162. beforeEach(function () {
  163. sinon.stub(App.ajax, 'send', function () {
  164. return {
  165. promise: Em.K
  166. };
  167. });
  168. });
  169. afterEach(function () {
  170. addServiceController.getDBProperty.restore();
  171. App.ajax.send.restore();
  172. });
  173. cases.forEach(function (item) {
  174. it(item.title, function () {
  175. sinon.stub(addServiceController, 'getDBProperty').withArgs('hosts').returns(item.hosts);
  176. addServiceController.loadHosts();
  177. expect(App.ajax.send.calledOnce).to.equal(item.isAjaxRequestSent);
  178. });
  179. });
  180. });
  181. describe('#loadHostsSuccessCallback', function () {
  182. it('should load hosts to local db and model', function () {
  183. var diskInfo = [
  184. {
  185. available: '600000',
  186. used: '400000',
  187. percent: '40%',
  188. size: '10000000',
  189. type: 'ext4',
  190. mountpoint: '/'
  191. },
  192. {
  193. available: '500000',
  194. used: '300000',
  195. percent: '50%',
  196. size: '6000000',
  197. type: 'ext4',
  198. mountpoint: '/'
  199. }
  200. ],
  201. hostComponents = [
  202. [
  203. {
  204. HostRoles: {
  205. component_name: 'c0',
  206. state: 'STARTED'
  207. }
  208. },
  209. {
  210. HostRoles: {
  211. component_name: 'c1',
  212. state: 'INSTALLED'
  213. }
  214. }
  215. ],
  216. [
  217. {
  218. HostRoles: {
  219. component_name: 'c2',
  220. state: 'STARTED'
  221. }
  222. },
  223. {
  224. HostRoles: {
  225. component_name: 'c3',
  226. state: 'INSTALLED'
  227. }
  228. }
  229. ]
  230. ],
  231. response = {
  232. items: [
  233. {
  234. Hosts: {
  235. cpu_count: 1,
  236. disk_info: [
  237. diskInfo[0]
  238. ],
  239. host_name: 'h0',
  240. ip: '10.1.1.0',
  241. os_arch: 'x86_64',
  242. os_type: 'centos6',
  243. total_mem: 4194304
  244. },
  245. host_components: hostComponents[0]
  246. },
  247. {
  248. Hosts: {
  249. cpu_count: 2,
  250. disk_info: [
  251. diskInfo[1]
  252. ],
  253. host_name: 'h1',
  254. ip: '10.1.1.1',
  255. os_arch: 'x86',
  256. os_type: 'centos5',
  257. total_mem: 3145728
  258. },
  259. host_components: hostComponents[1]
  260. }
  261. ]
  262. },
  263. expected = {
  264. h0: {
  265. name: 'h0',
  266. cpu: 1,
  267. memory: 4194304,
  268. disk_info: [diskInfo[0]],
  269. osType: 'centos6',
  270. osArch: 'x86_64',
  271. ip: '10.1.1.0',
  272. bootStatus: 'REGISTERED',
  273. isInstalled: true,
  274. hostComponents: hostComponents[0],
  275. id: 0
  276. },
  277. h1: {
  278. name: 'h1',
  279. cpu: 2,
  280. memory: 3145728,
  281. disk_info: [diskInfo[1]],
  282. osType: 'centos5',
  283. osArch: 'x86',
  284. ip: '10.1.1.1',
  285. bootStatus: 'REGISTERED',
  286. isInstalled: true,
  287. hostComponents: hostComponents[1],
  288. id: 1
  289. }
  290. };
  291. addServiceController.loadHostsSuccessCallback(response);
  292. var hostsInDb = addServiceController.getDBProperty('hosts');
  293. var hostsInModel = addServiceController.get('content.hosts');
  294. expect(hostsInDb).to.eql(expected);
  295. expect(hostsInModel).to.eql(expected);
  296. });
  297. });
  298. describe('#loadHostsErrorCallback', function () {
  299. beforeEach(function () {
  300. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  301. });
  302. afterEach(function () {
  303. App.ajax.defaultErrorHandler.restore();
  304. });
  305. it('should execute default error handler', function () {
  306. addServiceController.loadHostsErrorCallback({status: '500'}, 'textStatus', 'errorThrown', {url: 'url', method: 'GET'});
  307. expect(App.ajax.defaultErrorHandler.calledOnce).to.be.true;
  308. expect(App.ajax.defaultErrorHandler.calledWith({status: '500'}, 'url', 'GET', '500')).to.be.true;
  309. });
  310. });
  311. });