add_controller_test.js 18 KB

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