add_controller_test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. it(message.format(installed, selected, test.e.installed, test.e.selected), function() {
  141. sinon.stub(App.Service, 'find').returns(test.appService);
  142. addServiceController.saveServices(test.stepCtrlContent);
  143. App.Service.find.restore();
  144. var savedServices = addServiceController.setDBProperty.withArgs('services').args[0][1];
  145. expect(savedServices.selectedServices).to.have.members(test.e.selected);
  146. expect(savedServices.installedServices).to.have.members(test.e.installed);
  147. });
  148. });
  149. });
  150. describe('#loadHosts', function () {
  151. var cases = [
  152. {
  153. hosts: {},
  154. isAjaxRequestSent: false,
  155. title: 'hosts are already loaded'
  156. },
  157. {
  158. areHostsLoaded: false,
  159. isAjaxRequestSent: true,
  160. title: 'hosts aren\'t yet loaded'
  161. }
  162. ];
  163. beforeEach(function () {
  164. sinon.stub(App.ajax, 'send', function () {
  165. return {
  166. promise: Em.K
  167. };
  168. });
  169. });
  170. afterEach(function () {
  171. addServiceController.getDBProperty.restore();
  172. App.ajax.send.restore();
  173. });
  174. cases.forEach(function (item) {
  175. it(item.title, function () {
  176. sinon.stub(addServiceController, 'getDBProperty').withArgs('hosts').returns(item.hosts);
  177. addServiceController.loadHosts();
  178. expect(App.ajax.send.calledOnce).to.equal(item.isAjaxRequestSent);
  179. });
  180. });
  181. });
  182. describe('#loadHostsSuccessCallback', function () {
  183. it('should load hosts to local db and model', function () {
  184. var diskInfo = [
  185. {
  186. available: '600000',
  187. used: '400000',
  188. percent: '40%',
  189. size: '10000000',
  190. type: 'ext4',
  191. mountpoint: '/'
  192. },
  193. {
  194. available: '500000',
  195. used: '300000',
  196. percent: '50%',
  197. size: '6000000',
  198. type: 'ext4',
  199. mountpoint: '/'
  200. }
  201. ],
  202. hostComponents = [
  203. [
  204. {
  205. HostRoles: {
  206. component_name: 'c0',
  207. state: 'STARTED'
  208. }
  209. },
  210. {
  211. HostRoles: {
  212. component_name: 'c1',
  213. state: 'INSTALLED'
  214. }
  215. }
  216. ],
  217. [
  218. {
  219. HostRoles: {
  220. component_name: 'c2',
  221. state: 'STARTED'
  222. }
  223. },
  224. {
  225. HostRoles: {
  226. component_name: 'c3',
  227. state: 'INSTALLED'
  228. }
  229. }
  230. ]
  231. ],
  232. response = {
  233. items: [
  234. {
  235. Hosts: {
  236. cpu_count: 1,
  237. disk_info: [
  238. diskInfo[0]
  239. ],
  240. host_name: 'h0',
  241. ip: '10.1.1.0',
  242. os_arch: 'x86_64',
  243. os_type: 'centos6',
  244. total_mem: 4194304
  245. },
  246. host_components: hostComponents[0]
  247. },
  248. {
  249. Hosts: {
  250. cpu_count: 2,
  251. disk_info: [
  252. diskInfo[1]
  253. ],
  254. host_name: 'h1',
  255. ip: '10.1.1.1',
  256. os_arch: 'x86',
  257. os_type: 'centos5',
  258. total_mem: 3145728
  259. },
  260. host_components: hostComponents[1]
  261. }
  262. ]
  263. },
  264. expected = {
  265. h0: {
  266. name: 'h0',
  267. cpu: 1,
  268. memory: 4194304,
  269. disk_info: [diskInfo[0]],
  270. osType: 'centos6',
  271. osArch: 'x86_64',
  272. ip: '10.1.1.0',
  273. bootStatus: 'REGISTERED',
  274. isInstalled: true,
  275. hostComponents: hostComponents[0],
  276. id: 0
  277. },
  278. h1: {
  279. name: 'h1',
  280. cpu: 2,
  281. memory: 3145728,
  282. disk_info: [diskInfo[1]],
  283. osType: 'centos5',
  284. osArch: 'x86',
  285. ip: '10.1.1.1',
  286. bootStatus: 'REGISTERED',
  287. isInstalled: true,
  288. hostComponents: hostComponents[1],
  289. id: 1
  290. }
  291. };
  292. addServiceController.loadHostsSuccessCallback(response);
  293. var hostsInDb = addServiceController.getDBProperty('hosts');
  294. var hostsInModel = addServiceController.get('content.hosts');
  295. expect(hostsInDb).to.eql(expected);
  296. expect(hostsInModel).to.eql(expected);
  297. });
  298. });
  299. describe('#loadHostsErrorCallback', function () {
  300. beforeEach(function () {
  301. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  302. });
  303. afterEach(function () {
  304. App.ajax.defaultErrorHandler.restore();
  305. });
  306. it('should execute default error handler', function () {
  307. addServiceController.loadHostsErrorCallback({status: '500'}, 'textStatus', 'errorThrown', {url: 'url', method: 'GET'});
  308. expect(App.ajax.defaultErrorHandler.calledOnce).to.be.true;
  309. expect(App.ajax.defaultErrorHandler.calledWith({status: '500'}, 'url', 'GET', '500')).to.be.true;
  310. });
  311. });
  312. describe('#loadServices', function() {
  313. var mock = {
  314. db: {}
  315. };
  316. beforeEach(function() {
  317. this.controller = App.AddServiceController.create({});
  318. this.mockGetDBProperty = sinon.stub(this.controller, 'getDBProperty');
  319. sinon.stub(this.controller, 'setDBProperty', function(key, value) {
  320. mock.db = value;
  321. });
  322. this.mockStackService = sinon.stub(App.StackService, 'find');
  323. this.mockService = sinon.stub(App.Service, 'find');
  324. });
  325. afterEach(function() {
  326. this.mockGetDBProperty.restore();
  327. this.controller.setDBProperty.restore();
  328. this.mockStackService.restore();
  329. this.mockService.restore();
  330. });
  331. var tests = [
  332. {
  333. appStackService: [
  334. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS', coSelectedServices: []}),
  335. Em.Object.create({ id: 'YARN', serviceName: 'YARN', coSelectedServices: ['MAPREDUCE2']}),
  336. Em.Object.create({ id: 'MAPREDUCE2', serviceName: 'MAPREDUCE2', coSelectedServices: []}),
  337. Em.Object.create({ id: 'FALCON', serviceName: 'FALCON', coSelectedServices: []}),
  338. Em.Object.create({ id: 'STORM', serviceName: 'STORM', coSelectedServices: []})
  339. ],
  340. appService: [
  341. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS'}),
  342. Em.Object.create({ id: 'STORM', serviceName: 'STORM'})
  343. ],
  344. servicesFromDB: false,
  345. serviceToInstall: 'MAPREDUCE2',
  346. e: {
  347. selectedServices: ['HDFS', 'YARN', 'MAPREDUCE2', 'STORM'],
  348. installedServices: ['HDFS', 'STORM']
  349. },
  350. m: 'MapReduce selected on Admin -> Stack Versions Page, Yarn service should be selected because it coselected'
  351. },
  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: 'HBASE', serviceName: 'HBASE', coSelectedServices: []}),
  357. Em.Object.create({ id: 'STORM', serviceName: 'STORM', coSelectedServices: []})
  358. ],
  359. appService: [
  360. Em.Object.create({ id: 'HDFS', serviceName: 'HDFS'}),
  361. Em.Object.create({ id: 'STORM', serviceName: 'STORM'})
  362. ],
  363. servicesFromDB: {
  364. selectedServices: ['HBASE'],
  365. installedServices: ['HDFS', 'STORM']
  366. },
  367. serviceToInstall: null,
  368. e: {
  369. selectedServices: ['HDFS', 'HBASE', 'STORM'],
  370. installedServices: ['HDFS', 'STORM']
  371. },
  372. m: 'HDFS and STORM are installed. Select HBASE'
  373. }
  374. ];
  375. tests.forEach(function(test) {
  376. it(test.m, function() {
  377. this.mockStackService.returns(test.appStackService);
  378. this.mockService.returns(test.appService);
  379. this.mockGetDBProperty.withArgs('services').returns(test.servicesFromDB);
  380. this.controller.set('serviceToInstall', test.serviceToInstall);
  381. this.controller.loadServices();
  382. if (!test.servicesFromDB) {
  383. // verify saving to local db on first enter to the wizard
  384. expect(mock.db.selectedServices).to.be.eql(test.e.selectedServices);
  385. expect(mock.db.installedServices).to.be.eql(test.e.installedServices);
  386. } else {
  387. // verify values for App.StackService
  388. expect(test.appStackService.filterProperty('isSelected', true).mapProperty('serviceName')).to.be.eql(test.e.selectedServices);
  389. expect(test.appStackService.filterProperty('isInstalled', true).mapProperty('serviceName')).to.be.eql(test.e.installedServices);
  390. }
  391. expect(this.controller.get('serviceToInstall')).to.be.null;
  392. });
  393. }, this);
  394. });
  395. describe('#checkSecurityStatus', function () {
  396. var cases = [
  397. {
  398. securityEnabled: true,
  399. skipConfigureIdentitiesStep: false,
  400. isStep5Disabled: false,
  401. title: 'security enabled'
  402. },
  403. {
  404. securityEnabled: false,
  405. skipConfigureIdentitiesStep: true,
  406. isStep5Disabled: true,
  407. title: 'security disabled'
  408. }
  409. ];
  410. beforeEach(function () {
  411. addServiceController.setProperties({
  412. skipConfigureIdentitiesStep: false,
  413. isStepDisabled: [
  414. Em.Object.create({
  415. step: 5,
  416. value: false
  417. })
  418. ]
  419. });
  420. });
  421. afterEach(function () {
  422. App.get.restore();
  423. });
  424. cases.forEach(function (item) {
  425. it(item.title, function () {
  426. sinon.stub(App, 'get').withArgs('isKerberosEnabled').returns(item.securityEnabled);
  427. addServiceController.checkSecurityStatus();
  428. expect(addServiceController.get('skipConfigureIdentitiesStep')).to.equal(item.skipConfigureIdentitiesStep);
  429. expect(addServiceController.get('isStepDisabled').findProperty('step', 5).get('value')).to.equal(item.isStep5Disabled);
  430. });
  431. });
  432. });
  433. describe('#loadServiceConfigGroups', function () {
  434. var dbMock,
  435. dbMock2,
  436. cases = [
  437. {
  438. serviceConfigGroups: null,
  439. areInstalledConfigGroupsLoaded: false,
  440. title: 'config groups not yet loaded'
  441. },
  442. {
  443. serviceConfigGroups: [],
  444. areInstalledConfigGroupsLoaded: true,
  445. title: 'config groups already loaded'
  446. }
  447. ];
  448. beforeEach(function () {
  449. dbMock = sinon.stub(addServiceController, 'getDBProperties');
  450. dbMock2 = sinon.stub(addServiceController, 'getDBProperty');
  451. });
  452. afterEach(function () {
  453. dbMock.restore();
  454. dbMock2.restore();
  455. });
  456. cases.forEach(function (item) {
  457. it(item.title, function () {
  458. dbMock.withArgs(['serviceConfigGroups', 'hosts']).returns({
  459. hosts: {},
  460. serviceConfigGroups: item.serviceConfigGroups
  461. });
  462. dbMock2.withArgs('hosts').returns({}).
  463. withArgs('serviceConfigGroups').returns(item.serviceConfigGroups);
  464. addServiceController.loadServiceConfigGroups();
  465. expect(addServiceController.get('areInstalledConfigGroupsLoaded')).to.equal(item.areInstalledConfigGroupsLoaded);
  466. });
  467. });
  468. });
  469. describe('#clearStorageData', function () {
  470. it('areInstalledConfigGroupsLoaded should be false', function () {
  471. addServiceController.set('areInstalledConfigGroupsLoaded', true);
  472. addServiceController.clearStorageData();
  473. expect(addServiceController.get('areInstalledConfigGroupsLoaded')).to.be.false;
  474. });
  475. });
  476. });