add_controller_test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. addServiceController.set('installClietsQueue', App.ajaxQueue.create())
  53. });
  54. afterEach(function () {
  55. $.ajax.restore();
  56. App.get.restore();
  57. });
  58. describe('send request to install client', function () {
  59. beforeEach(function () {
  60. addServiceController.set("content.additionalClients", [t.additionalClients]);
  61. addServiceController.installAdditionalClients();
  62. });
  63. it('1 request is sent', function () {
  64. expect($.ajax.calledOnce).to.equal(true);
  65. });
  66. it('data.Body is valid', function () {
  67. expect(JSON.parse($.ajax.args[0][0].data).Body).to.deep.eql(t.Body);
  68. });
  69. it('data.RequestInfo is valid', function () {
  70. expect(JSON.parse($.ajax.args[0][0].data).RequestInfo).to.eql(t.RequestInfo);
  71. });
  72. });
  73. it('should not send request to install client', function () {
  74. addServiceController.set("content.additionalClients", [t.additionalClientsWithoutHosts]);
  75. expect($.ajax.called).to.be.false;
  76. });
  77. });
  78. describe('#generateDataForInstallServices', function() {
  79. var tests = [{
  80. selected: ["YARN","HBASE"],
  81. res: {
  82. "context": Em.I18n.t('requestInfo.installServices'),
  83. "ServiceInfo": {"state": "INSTALLED"},
  84. "urlParams": "ServiceInfo/service_name.in(YARN,HBASE)"
  85. }
  86. },
  87. {
  88. selected: ['OOZIE'],
  89. res: {
  90. "context": Em.I18n.t('requestInfo.installServices'),
  91. "ServiceInfo": {"state": "INSTALLED"},
  92. "urlParams": "ServiceInfo/service_name.in(OOZIE,HDFS,YARN,MAPREDUCE2)"
  93. }
  94. }];
  95. tests.forEach(function(t){
  96. it('should generate data with ' + t.selected.join(","), function () {
  97. expect(addServiceController.generateDataForInstallServices(t.selected)).to.be.eql(t.res);
  98. });
  99. });
  100. });
  101. describe('#saveServices', function() {
  102. beforeEach(function() {
  103. sinon.stub(addServiceController, 'setDBProperty', Em.K);
  104. });
  105. afterEach(function() {
  106. addServiceController.setDBProperty.restore();
  107. });
  108. var tests = [
  109. {
  110. appService: [
  111. Em.Object.create({ serviceName: 'HDFS' }),
  112. Em.Object.create({ serviceName: 'KERBEROS' })
  113. ],
  114. stepCtrlContent: Em.Object.create({
  115. content: Em.A([
  116. Em.Object.create({ serviceName: 'HDFS', isInstalled: true, isSelected: true }),
  117. Em.Object.create({ serviceName: 'YARN', isInstalled: false, isSelected: true })
  118. ])
  119. }),
  120. e: {
  121. selected: ['YARN'],
  122. installed: ['HDFS', 'KERBEROS']
  123. }
  124. },
  125. {
  126. appService: [
  127. Em.Object.create({ serviceName: 'HDFS' }),
  128. Em.Object.create({ serviceName: 'STORM' })
  129. ],
  130. stepCtrlContent: Em.Object.create({
  131. content: Em.A([
  132. Em.Object.create({ serviceName: 'HDFS', isInstalled: true, isSelected: true }),
  133. Em.Object.create({ serviceName: 'YARN', isInstalled: false, isSelected: true }),
  134. Em.Object.create({ serviceName: 'MAPREDUCE2', isInstalled: false, isSelected: true })
  135. ])
  136. }),
  137. e: {
  138. selected: ['YARN', 'MAPREDUCE2'],
  139. installed: ['HDFS', 'STORM']
  140. }
  141. }
  142. ];
  143. var message = '{0} installed, {1} selected. Installed list should be {2} and selected - {3}';
  144. tests.forEach(function(test) {
  145. var installed = test.appService.mapProperty('serviceName');
  146. var selected = test.stepCtrlContent.get('content').filterProperty('isSelected', true)
  147. .filterProperty('isInstalled', false).mapProperty('serviceName');
  148. describe(message.format(installed, selected, test.e.installed, test.e.selected), function() {
  149. beforeEach(function () {
  150. sinon.stub(App.Service, 'find').returns(test.appService);
  151. addServiceController.saveServices(test.stepCtrlContent);
  152. this.savedServices = addServiceController.setDBProperty.withArgs('services').args[0][1];
  153. });
  154. afterEach(function () {
  155. App.Service.find.restore();
  156. });
  157. it(JSON.stringify(test.e.selected) + ' are in the selectedServices', function () {
  158. expect(this.savedServices.selectedServices).to.have.members(test.e.selected);
  159. });
  160. it(JSON.stringify(test.e.installed) + ' are in the installedServices', function () {
  161. expect(this.savedServices.installedServices).to.have.members(test.e.installed);
  162. });
  163. });
  164. });
  165. });
  166. describe('#loadHosts', function () {
  167. var cases = [
  168. {
  169. hosts: {},
  170. isAjaxRequestSent: false,
  171. title: 'hosts are already loaded'
  172. },
  173. {
  174. areHostsLoaded: false,
  175. isAjaxRequestSent: true,
  176. title: 'hosts aren\'t yet loaded'
  177. }
  178. ];
  179. beforeEach(function () {
  180. sinon.stub(App.ajax, 'send', function () {
  181. return {
  182. promise: Em.K
  183. };
  184. });
  185. });
  186. afterEach(function () {
  187. addServiceController.getDBProperty.restore();
  188. App.ajax.send.restore();
  189. });
  190. cases.forEach(function (item) {
  191. describe(item.title, function () {
  192. beforeEach(function () {
  193. sinon.stub(addServiceController, 'getDBProperty').withArgs('hosts').returns(item.hosts);
  194. addServiceController.loadHosts();
  195. });
  196. it('request is ' + (item.isAjaxRequestSent ? '' : 'not') + ' sent', function () {
  197. expect(App.ajax.send.calledOnce).to.equal(item.isAjaxRequestSent);
  198. });
  199. });
  200. });
  201. });
  202. describe('#loadHostsSuccessCallback', function () {
  203. it('should load hosts to local db and model', function () {
  204. var diskInfo = [
  205. {
  206. available: '600000',
  207. used: '400000',
  208. percent: '40%',
  209. size: '10000000',
  210. type: 'ext4',
  211. mountpoint: '/'
  212. },
  213. {
  214. available: '500000',
  215. used: '300000',
  216. percent: '50%',
  217. size: '6000000',
  218. type: 'ext4',
  219. mountpoint: '/'
  220. }
  221. ],
  222. hostComponents = [
  223. [
  224. {
  225. HostRoles: {
  226. component_name: 'c0',
  227. state: 'STARTED'
  228. }
  229. },
  230. {
  231. HostRoles: {
  232. component_name: 'c1',
  233. state: 'INSTALLED'
  234. }
  235. }
  236. ],
  237. [
  238. {
  239. HostRoles: {
  240. component_name: 'c2',
  241. state: 'STARTED'
  242. }
  243. },
  244. {
  245. HostRoles: {
  246. component_name: 'c3',
  247. state: 'INSTALLED'
  248. }
  249. }
  250. ]
  251. ],
  252. response = {
  253. items: [
  254. {
  255. Hosts: {
  256. cpu_count: 1,
  257. disk_info: [
  258. diskInfo[0]
  259. ],
  260. host_name: 'h0',
  261. ip: '10.1.1.0',
  262. os_arch: 'x86_64',
  263. os_type: 'centos6',
  264. total_mem: 4194304
  265. },
  266. host_components: hostComponents[0]
  267. },
  268. {
  269. Hosts: {
  270. cpu_count: 2,
  271. disk_info: [
  272. diskInfo[1]
  273. ],
  274. host_name: 'h1',
  275. ip: '10.1.1.1',
  276. os_arch: 'x86',
  277. os_type: 'centos5',
  278. total_mem: 3145728
  279. },
  280. host_components: hostComponents[1]
  281. }
  282. ]
  283. },
  284. expected = {
  285. h0: {
  286. name: 'h0',
  287. cpu: 1,
  288. memory: 4194304,
  289. disk_info: [diskInfo[0]],
  290. osType: 'centos6',
  291. osArch: 'x86_64',
  292. ip: '10.1.1.0',
  293. bootStatus: 'REGISTERED',
  294. isInstalled: true,
  295. hostComponents: hostComponents[0],
  296. id: 0
  297. },
  298. h1: {
  299. name: 'h1',
  300. cpu: 2,
  301. memory: 3145728,
  302. disk_info: [diskInfo[1]],
  303. osType: 'centos5',
  304. osArch: 'x86',
  305. ip: '10.1.1.1',
  306. bootStatus: 'REGISTERED',
  307. isInstalled: true,
  308. hostComponents: hostComponents[1],
  309. id: 1
  310. }
  311. };
  312. addServiceController.loadHostsSuccessCallback(response);
  313. var hostsInDb = addServiceController.getDBProperty('hosts');
  314. var hostsInModel = addServiceController.get('content.hosts');
  315. expect(hostsInDb).to.eql(expected);
  316. expect(hostsInModel).to.eql(expected);
  317. });
  318. });
  319. describe('#loadHostsErrorCallback', function () {
  320. beforeEach(function () {
  321. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  322. });
  323. afterEach(function () {
  324. App.ajax.defaultErrorHandler.restore();
  325. });
  326. it('should execute default error handler', function () {
  327. addServiceController.loadHostsErrorCallback({status: '500'}, 'textStatus', 'errorThrown', {url: 'url', method: 'GET'});
  328. expect(App.ajax.defaultErrorHandler.calledOnce).to.be.true;
  329. expect(App.ajax.defaultErrorHandler.calledWith({status: '500'}, 'url', 'GET', '500')).to.be.true;
  330. });
  331. });
  332. describe('#loadServices', function() {
  333. var mock = {
  334. db: {}
  335. };
  336. beforeEach(function() {
  337. this.controller = App.AddServiceController.create({});
  338. this.mockGetDBProperty = sinon.stub(this.controller, 'getDBProperty');
  339. sinon.stub(this.controller, 'setDBProperty', function(key, value) {
  340. mock.db = value;
  341. });
  342. this.mockStackService = sinon.stub(App.StackService, 'find');
  343. this.mockService = sinon.stub(App.Service, 'find');
  344. });
  345. afterEach(function() {
  346. this.mockGetDBProperty.restore();
  347. this.controller.setDBProperty.restore();
  348. this.mockStackService.restore();
  349. this.mockService.restore();
  350. });
  351. var tests = [
  352. {
  353. appStackService: [
  354. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS', coSelectedServices: []}),
  355. Em.Object.create({ id: 'YARN', serviceName: 'YARN', coSelectedServices: ['MAPREDUCE2']}),
  356. Em.Object.create({ id: 'MAPREDUCE2', serviceName: 'MAPREDUCE2', coSelectedServices: []}),
  357. Em.Object.create({ id: 'FALCON', serviceName: 'FALCON', coSelectedServices: []}),
  358. Em.Object.create({ id: 'STORM', serviceName: 'STORM', coSelectedServices: []})
  359. ],
  360. appService: [
  361. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS'}),
  362. Em.Object.create({ id: 'STORM', serviceName: 'STORM'})
  363. ],
  364. servicesFromDB: false,
  365. serviceToInstall: 'MAPREDUCE2',
  366. e: {
  367. selectedServices: ['HDFS', 'YARN', 'MAPREDUCE2', 'STORM'],
  368. installedServices: ['HDFS', 'STORM']
  369. },
  370. m: 'MapReduce selected on Admin -> Stack Versions Page, Yarn service should be selected because it coselected'
  371. },
  372. {
  373. appStackService: [
  374. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS', coSelectedServices: []}),
  375. Em.Object.create({ id: 'YARN', serviceName: 'YARN', coSelectedServices: ['MAPREDUCE2']}),
  376. Em.Object.create({ id: 'HBASE', serviceName: 'HBASE', coSelectedServices: []}),
  377. Em.Object.create({ id: 'STORM', serviceName: 'STORM', coSelectedServices: []})
  378. ],
  379. appService: [
  380. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS'}),
  381. Em.Object.create({ id: 'STORM', serviceName: 'STORM'})
  382. ],
  383. servicesFromDB: {
  384. selectedServices: ['HBASE'],
  385. installedServices: ['HDFS', 'STORM']
  386. },
  387. serviceToInstall: null,
  388. e: {
  389. selectedServices: ['HDFS', 'HBASE', 'STORM'],
  390. installedServices: ['HDFS', 'STORM']
  391. },
  392. m: 'HDFS and STORM are installed. Select HBASE'
  393. }
  394. ];
  395. tests.forEach(function(test) {
  396. describe(test.m, function() {
  397. beforeEach(function () {
  398. this.mockStackService.returns(test.appStackService);
  399. this.mockService.returns(test.appService);
  400. this.mockGetDBProperty.withArgs('services').returns(test.servicesFromDB);
  401. this.controller.set('serviceToInstall', test.serviceToInstall);
  402. this.controller.loadServices();
  403. });
  404. if (test.servicesFromDB) {
  405. // verify values for App.StackService
  406. it(JSON.stringify(test.e.selectedServices) + ' are selected', function () {
  407. expect(test.appStackService.filterProperty('isSelected', true).mapProperty('serviceName')).to.be.eql(test.e.selectedServices);
  408. });
  409. it(JSON.stringify(test.e.installedServices) + ' are installed', function () {
  410. expect(test.appStackService.filterProperty('isInstalled', true).mapProperty('serviceName')).to.be.eql(test.e.installedServices);
  411. });
  412. }
  413. else {
  414. // verify saving to local db on first enter to the wizard
  415. it('selectedServices are saced', function () {
  416. expect(mock.db.selectedServices).to.be.eql(test.e.selectedServices);
  417. });
  418. it('installedServices are saved', function () {
  419. expect(mock.db.installedServices).to.be.eql(test.e.installedServices);
  420. });
  421. }
  422. it('serviceToInstall is null', function () {
  423. expect(this.controller.get('serviceToInstall')).to.be.null;
  424. });
  425. });
  426. }, this);
  427. });
  428. describe('#checkSecurityStatus', function () {
  429. var cases = [
  430. {
  431. securityEnabled: true,
  432. skipConfigureIdentitiesStep: false,
  433. isStep5Disabled: false,
  434. title: 'security enabled'
  435. },
  436. {
  437. securityEnabled: false,
  438. skipConfigureIdentitiesStep: true,
  439. isStep5Disabled: true,
  440. title: 'security disabled'
  441. }
  442. ];
  443. beforeEach(function () {
  444. addServiceController.setProperties({
  445. skipConfigureIdentitiesStep: false,
  446. isStepDisabled: [
  447. Em.Object.create({
  448. step: 5,
  449. value: false
  450. })
  451. ]
  452. });
  453. });
  454. afterEach(function () {
  455. App.get.restore();
  456. });
  457. cases.forEach(function (item) {
  458. describe(item.title, function () {
  459. beforeEach(function () {
  460. sinon.stub(App, 'get').withArgs('isKerberosEnabled').returns(item.securityEnabled);
  461. addServiceController.checkSecurityStatus();
  462. });
  463. it('skipConfigureIdentitiesStep is ' + item.skipConfigureIdentitiesStep, function () {
  464. expect(addServiceController.get('skipConfigureIdentitiesStep')).to.equal(item.skipConfigureIdentitiesStep);
  465. });
  466. it('step 5 is ' + (item.isStep5Disabled ? 'disabved' : 'enabled'), function () {
  467. expect(addServiceController.get('isStepDisabled').findProperty('step', 5).get('value')).to.equal(item.isStep5Disabled);
  468. });
  469. });
  470. });
  471. });
  472. describe('#loadServiceConfigGroups', function () {
  473. var dbMock,
  474. dbMock2,
  475. cases = [
  476. {
  477. serviceConfigGroups: null,
  478. areInstalledConfigGroupsLoaded: false,
  479. title: 'config groups not yet loaded'
  480. },
  481. {
  482. serviceConfigGroups: [],
  483. areInstalledConfigGroupsLoaded: true,
  484. title: 'config groups already loaded'
  485. }
  486. ];
  487. beforeEach(function () {
  488. dbMock = sinon.stub(addServiceController, 'getDBProperties');
  489. dbMock2 = sinon.stub(addServiceController, 'getDBProperty');
  490. });
  491. afterEach(function () {
  492. dbMock.restore();
  493. dbMock2.restore();
  494. });
  495. cases.forEach(function (item) {
  496. it(item.title, function () {
  497. dbMock.withArgs(['serviceConfigGroups', 'hosts']).returns({
  498. hosts: {},
  499. serviceConfigGroups: item.serviceConfigGroups
  500. });
  501. dbMock2.withArgs('hosts').returns({}).
  502. withArgs('serviceConfigGroups').returns(item.serviceConfigGroups);
  503. addServiceController.loadServiceConfigGroups();
  504. expect(addServiceController.get('areInstalledConfigGroupsLoaded')).to.equal(item.areInstalledConfigGroupsLoaded);
  505. });
  506. });
  507. });
  508. describe('#clearStorageData', function () {
  509. it('areInstalledConfigGroupsLoaded should be false', function () {
  510. addServiceController.set('areInstalledConfigGroupsLoaded', true);
  511. addServiceController.clearStorageData();
  512. expect(addServiceController.get('areInstalledConfigGroupsLoaded')).to.be.false;
  513. });
  514. });
  515. });