add_controller_test.js 18 KB

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