slave_component_groups_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 Ember = require('ember');
  19. var App = require('app');
  20. require('utils/helper');
  21. require('controllers/wizard/slave_component_groups_controller');
  22. var controller;
  23. describe('App.SlaveComponentGroupsController', function () {
  24. beforeEach(function () {
  25. controller = App.SlaveComponentGroupsController.create({
  26. content: {},
  27. selectedSlaveComponent: null
  28. });
  29. });
  30. describe('#getHostsByGroup', function () {
  31. it('should return empty array', function () {
  32. controller.set('hosts', undefined);
  33. expect(controller.getHostsByGroup()).to.eql([]);
  34. });
  35. it('should return g1 hosts', function () {
  36. var hosts = Em.A([
  37. Em.Object.create({
  38. group: 'g1',
  39. name: 'h1'
  40. }),
  41. Em.Object.create({
  42. group: 'g2',
  43. name: 'h2'
  44. }),
  45. Em.Object.create({
  46. group: 'g1',
  47. name: 'h3'
  48. })
  49. ]);
  50. var group = {
  51. name: 'g1'
  52. };
  53. var selectedSlaveComponent = Em.Object.create({
  54. hosts: hosts
  55. });
  56. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  57. var expected = [
  58. {
  59. "group": "g1",
  60. "name": "h1"
  61. },
  62. {
  63. "group": "g1",
  64. "name": "h3"
  65. }
  66. ];
  67. expect(JSON.parse(JSON.stringify(controller.getHostsByGroup(group)))).to.eql(expected);
  68. });
  69. });
  70. describe('#changeSlaveGroupName', function () {
  71. it('should return true if group is exist', function () {
  72. var selectedSlaveComponent = Em.Object.create({
  73. groups: Em.A([
  74. Em.Object.create({
  75. name: 'g1'
  76. })
  77. ])
  78. });
  79. var group = {};
  80. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  81. expect(controller.changeSlaveGroupName(group, 'g1')).to.be.true;
  82. });
  83. it('should return false if group is not exist', function () {
  84. var hosts = Em.A([
  85. Em.Object.create({
  86. group: 'g1',
  87. name: 'h1'
  88. }),
  89. Em.Object.create({
  90. group: 'g2',
  91. name: 'h2'
  92. }),
  93. Em.Object.create({
  94. group: 'g1',
  95. name: 'h3'
  96. })
  97. ]);
  98. var selectedSlaveComponent = Em.Object.create({
  99. hosts: hosts,
  100. groups: Em.A([
  101. Em.Object.create({
  102. name: 'g1'
  103. })
  104. ])
  105. });
  106. var group = {
  107. name: 'g2'
  108. };
  109. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  110. expect(controller.changeSlaveGroupName(group, 'g2')).to.be.false;
  111. });
  112. });
  113. describe('#loadStep', function () {
  114. beforeEach(function () {
  115. sinon.stub(App.SlaveConfigs, 'create').returns(Em.Object.create({
  116. groups: false,
  117. componentName: '',
  118. displayName: ''
  119. }));
  120. sinon.stub(App.config, 'get').returns(Em.A([
  121. Em.Object.create({
  122. category: 'HBASE',
  123. serviceName: 'HBASE',
  124. configs: Em.A([Em.Object.create({})])
  125. })
  126. ]));
  127. sinon.stub(App.ServiceConfigProperty, 'create').returns(Em.Object.create({
  128. name: 'dfs_data_dir',
  129. initialValue: Em.K
  130. })
  131. );
  132. });
  133. afterEach(function () {
  134. App.SlaveConfigs.create.restore();
  135. App.ServiceConfigProperty.create.restore();
  136. App.config.get.restore();
  137. });
  138. it('should add groups to stepConfgig', function () {
  139. var stepConfigs = Em.A([
  140. Em.Object.create({
  141. serviceName: 'HBASE',
  142. configCategories: Em.A([
  143. Em.Object.create({
  144. isForSlaveComponent: true,
  145. primaryName: 'sl1',
  146. slaveConfigs: Em.A([])
  147. })
  148. ])
  149. })
  150. ]);
  151. var content = Em.A([
  152. Em.Object.create({
  153. componentName: 'sl1',
  154. displayName: 'd1'
  155. })
  156. ]);
  157. controller.set('content',content);
  158. controller.set('stepConfigs',stepConfigs);
  159. controller.loadStep();
  160. var expected = [
  161. {
  162. "serviceName": "HBASE",
  163. "configCategories": [
  164. {
  165. "isForSlaveComponent": true,
  166. "primaryName": "sl1",
  167. "slaveConfigs": {
  168. "groups": [
  169. {
  170. "name": "Default",
  171. "index":
  172. "default",
  173. "type": "default",
  174. "active": true,
  175. "properties": []
  176. }
  177. ],
  178. "componentName": "sl1",
  179. "displayName": "d1"
  180. }
  181. }
  182. ]
  183. }
  184. ];
  185. var result = JSON.parse(JSON.stringify(controller.get('stepConfigs')));
  186. expect(result).to.be.eql(expected);
  187. });
  188. });
  189. describe('#componentProperties', function () {
  190. beforeEach(function () {
  191. sinon.stub(App.config, 'get').returns(Em.A([
  192. Em.Object.create({
  193. category: 'RegionServer',
  194. serviceName: 'HBASE',
  195. configs: Em.A([
  196. Em.Object.create({
  197. category: 'RegionServer'
  198. }),
  199. Em.Object.create({
  200. next: true,
  201. category: 'RegionServer'
  202. })
  203. ])
  204. })
  205. ]));
  206. sinon.stub(App.ServiceConfigProperty, 'create', function(value) {
  207. if (value && value.next) {
  208. return Em.Object.create({
  209. name: 'dfs_data_dir',
  210. initialValue: Em.K,
  211. validate: Em.K
  212. });
  213. } else {
  214. return Em.Object.create({
  215. name: 'mapred_local_dir',
  216. initialValue: Em.K,
  217. validate: Em.K
  218. });
  219. }
  220. });
  221. sinon.stub(App.ConfigInitializer, 'initialValue', Em.K);
  222. });
  223. afterEach(function () {
  224. App.ServiceConfigProperty.create.restore();
  225. App.config.get.restore();
  226. App.ConfigInitializer.initialValue.restore();
  227. });
  228. it('should return created config', function () {
  229. var res = JSON.parse(JSON.stringify(controller.componentProperties('HBASE')));
  230. var expected = [
  231. {
  232. "name": "mapred_local_dir"
  233. },
  234. {
  235. "name": "dfs_data_dir"
  236. }
  237. ];
  238. expect(res).to.be.eql(expected);
  239. });
  240. });
  241. describe('#selectedComponentName', function () {
  242. afterEach(function () {
  243. App.router.get.restore();
  244. });
  245. it('should return selected component name HDFS', function () {
  246. sinon.stub(App.router, 'get').returns('HDFS');
  247. expect(controller.get('selectedComponentName')).to.be.eql({
  248. name: 'DATANODE',
  249. displayName: 'DataNode'
  250. });
  251. });
  252. it('should return selected component name HBASE', function () {
  253. sinon.stub(App.router, 'get').returns('HBASE');
  254. expect(controller.get('selectedComponentName')).to.be.eql({
  255. name: 'HBASE_REGIONSERVER',
  256. displayName: 'RegionServer'
  257. });
  258. });
  259. it('should return null', function () {
  260. sinon.stub(App.router, 'get').returns('undefined');
  261. expect(controller.get('selectedComponentName')).to.be.null;
  262. });
  263. });
  264. describe('#removeSlaveComponentGroup', function () {
  265. beforeEach(function() {
  266. sinon.stub(controller, 'componentProperties').returns(Em.A([]));
  267. });
  268. afterEach(function() {
  269. controller.componentProperties.restore();
  270. });
  271. it('should return empty selected component', function () {
  272. var selectedSlaveComponent = Em.Object.create({
  273. newGroupIndex: 0,
  274. hosts: Em.A([]),
  275. groups: Em.A([
  276. Em.Object.create({
  277. active: true,
  278. name: 'New Group',
  279. type: 'new'
  280. })
  281. ])
  282. });
  283. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  284. var ev = Em.Object.create({
  285. context: selectedSlaveComponent.groups[0]
  286. });
  287. controller.removeSlaveComponentGroup(ev);
  288. var expected = {
  289. "newGroupIndex": 0,
  290. "hosts": [],
  291. "groups": []
  292. };
  293. var res = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent')));
  294. expect(res).to.be.eql(expected);
  295. });
  296. });
  297. describe('#showSlaveComponentGroup', function () {
  298. it('should make all groups active', function () {
  299. var selectedSlaveComponent = Em.Object.create({
  300. newGroupIndex: 0,
  301. hosts: Em.A([]),
  302. groups: Em.A([
  303. Em.Object.create({
  304. active: false,
  305. name: 'New Group',
  306. type: 'new'
  307. })
  308. ])
  309. });
  310. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  311. var ev = Em.Object.create({
  312. context: selectedSlaveComponent.groups[0]
  313. });
  314. controller.showSlaveComponentGroup(ev);
  315. var expected = {
  316. "newGroupIndex": 0,
  317. "hosts": [],
  318. "groups": [
  319. {
  320. "active": true,
  321. "name": "New Group",
  322. "type": "new"
  323. }
  324. ]
  325. };
  326. var res = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent')));
  327. expect(res).to.be.eql(expected);
  328. });
  329. });
  330. describe('#selectedComponentDisplayName', function () {
  331. beforeEach(function () {
  332. sinon.stub(App.format, 'role').returns('name')
  333. });
  334. afterEach(function () {
  335. App.format.role.restore();
  336. });
  337. it('should return selected component name', function () {
  338. expect(controller.get('selectedComponentDisplayName')).to.be.equal('name');
  339. });
  340. });
  341. describe('#hosts', function () {
  342. it('should return hosts', function () {
  343. var selectedSlaveComponent = Em.Object.create({
  344. newGroupIndex: 0,
  345. hosts: Em.A([{name: 'h1'}]),
  346. groups: Em.A([
  347. Em.Object.create({
  348. active: false,
  349. name: 'New Group',
  350. type: 'new'
  351. })
  352. ])
  353. });
  354. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  355. expect(controller.get('hosts')).to.be.eql(Em.A([{name: 'h1'}]));
  356. });
  357. });
  358. describe('#componentGroups', function () {
  359. it('should return groups', function () {
  360. var selectedSlaveComponent = Em.Object.create({
  361. newGroupIndex: 0,
  362. hosts: Em.A([{name: 'h1', group: 'one'}, {name: 'h2', group: 'one'}]),
  363. groups: Em.A([
  364. Em.Object.create({
  365. active: false,
  366. name: 'New Group',
  367. type: 'new'
  368. })
  369. ])
  370. });
  371. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  372. expect(controller.get('componentGroups')).to.be.eql(Em.A([
  373. Em.Object.create({
  374. active: false,
  375. name: 'New Group',
  376. type: 'new'
  377. })
  378. ]));
  379. });
  380. });
  381. describe('#activeGroup', function () {
  382. it('should return active group', function () {
  383. var selectedSlaveComponent = Em.Object.create({
  384. newGroupIndex: 0,
  385. hosts: Em.A([{name: 'h1', group: 'one'}, {name: 'h2', group: 'one'}]),
  386. groups: Em.A([
  387. Em.Object.create({
  388. active: false,
  389. name: 'New Group',
  390. type: 'new'
  391. }),
  392. Em.Object.create({
  393. active: true,
  394. name: 'New Group',
  395. type: 'new'
  396. })
  397. ])
  398. });
  399. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  400. expect(controller.get('activeGroup')).to.be.eql(Em.Object.create({
  401. active: true,
  402. name: 'New Group',
  403. type: 'new'
  404. }));
  405. });
  406. });
  407. describe('#groups', function () {
  408. it('should return uniq groups names', function () {
  409. var selectedSlaveComponent = Em.Object.create({
  410. newGroupIndex: 0,
  411. hosts: Em.A([{name: 'h1', group: 'one'}, {name: 'h2', group: 'one'}]),
  412. groups: Em.A([
  413. Em.Object.create({
  414. active: false,
  415. name: 'New Group',
  416. type: 'new'
  417. })
  418. ])
  419. });
  420. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  421. expect(controller.get('groups')).to.be.eql(['one']);
  422. });
  423. });
  424. describe('#addSlaveComponentGroup', function () {
  425. beforeEach(function() {
  426. sinon.stub(controller, 'componentProperties').returns(Em.A([]));
  427. });
  428. afterEach(function() {
  429. controller.componentProperties.restore();
  430. });
  431. it('should return selected component name', function () {
  432. var selectedSlaveComponent = Em.Object.create({
  433. newGroupIndex: 0,
  434. groups: Em.A([
  435. Em.Object.create({
  436. active: true,
  437. name: 'New Group'
  438. })
  439. ])
  440. });
  441. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  442. controller.addSlaveComponentGroup();
  443. var expected = {
  444. "newGroupIndex": 1,
  445. "groups": [
  446. {
  447. "active": false,
  448. "name": "New Group"
  449. },
  450. {
  451. "name": "New Group 1",
  452. "index": 1,
  453. "type": "new",
  454. "active": true,
  455. "properties": []
  456. }
  457. ]
  458. };
  459. var res = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent')));
  460. expect(res).to.be.eql(expected);
  461. });
  462. });
  463. describe('#checkGroupName', function () {
  464. it('should make equal to 2', function () {
  465. var selectedSlaveComponent = Em.Object.create({
  466. groups: Em.A([
  467. Em.Object.create({
  468. name: 'New Group 1'
  469. })
  470. ]),
  471. newGroupIndex: 0
  472. });
  473. var group = {};
  474. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  475. controller.checkGroupName();
  476. expect(controller.get('selectedSlaveComponent').newGroupIndex).to.be.equal(2);
  477. });
  478. });
  479. describe('#changeHostGroup', function () {
  480. it('should push 1 host group', function () {
  481. var selectedSlaveComponent = Em.Object.create({
  482. tempSelectedGroups: undefined
  483. });
  484. var host = Em.Object.create({
  485. hostName: 'h1'
  486. });
  487. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  488. controller.changeHostGroup(host, 'g1');
  489. var expected = [
  490. {
  491. "hostName": "h1",
  492. "groupName": "g1"
  493. }
  494. ];
  495. var result = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent').tempSelectedGroups));
  496. expect(result).to.be.eql(expected);
  497. });
  498. it('should push change host group name', function () {
  499. var selectedSlaveComponent = Em.Object.create({
  500. tempSelectedGroups: [
  501. Em.Object.create({
  502. hostName: 'h1',
  503. groupName: ''
  504. })
  505. ]
  506. });
  507. var host = Em.Object.create({
  508. hostName: 'h1'
  509. });
  510. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  511. controller.changeHostGroup(host, 'g1');
  512. var expected = [
  513. Em.Object.create({
  514. "hostName": "h1",
  515. "groupName": "g1"
  516. })
  517. ]
  518. expect(controller.get('selectedSlaveComponent').tempSelectedGroups).to.be.eql(expected);
  519. });
  520. });
  521. describe('#loadGroups', function () {
  522. beforeEach(function () {
  523. sinon.stub(App.SlaveConfigs, 'create').returns(Em.Object.create({
  524. groups: false,
  525. componentName: '',
  526. displayName: ''
  527. }));
  528. sinon.stub(App.config, 'get').returns(Em.A([
  529. Em.Object.create({
  530. category: 'HDFS',
  531. serviceName: 'HDFS',
  532. configs: Em.A([])
  533. })
  534. ]));
  535. });
  536. afterEach(function () {
  537. App.SlaveConfigs.create.restore();
  538. App.config.get.restore();
  539. });
  540. it('should modefie step confgigs', function () {
  541. var stepConfigs = Em.A([
  542. Em.Object.create({
  543. serviceName: 'HDFS',
  544. configCategories: Em.A([
  545. Em.Object.create({
  546. isForSlaveComponent: true,
  547. primaryName: 'sl1',
  548. slaveConfigs: Em.A([])
  549. })
  550. ])
  551. })
  552. ]);
  553. var content = Em.A([
  554. Em.Object.create({
  555. componentName: 'sl1',
  556. displayName: 'd1',
  557. groups: Em.A([
  558. Em.Object.create({
  559. name: 'g1'
  560. })
  561. ])
  562. })
  563. ]);
  564. controller.set('content',content);
  565. controller.set('stepConfigs',stepConfigs);
  566. controller.loadGroups();
  567. var expected = [
  568. {
  569. "serviceName": "HDFS",
  570. "configCategories": [
  571. {
  572. "isForSlaveComponent": true,
  573. "primaryName": "sl1",
  574. "slaveConfigs": {
  575. "groups": [
  576. {
  577. "name": "g1"
  578. }
  579. ],
  580. "componentName": "sl1",
  581. "displayName": "d1"
  582. }
  583. }
  584. ]
  585. }
  586. ];
  587. var result = JSON.parse(JSON.stringify(controller.get('stepConfigs')));
  588. expect(result).to.be.eql(expected);
  589. });
  590. it('should add groups to stepConfgig', function () {
  591. var stepConfigs = Em.A([
  592. Em.Object.create({
  593. serviceName: 'HDFS',
  594. configCategories: Em.A([
  595. Em.Object.create({
  596. isForSlaveComponent: true,
  597. primaryName: 'sl1',
  598. slaveConfigs: Em.A([])
  599. })
  600. ])
  601. })
  602. ]);
  603. var content = Em.A([
  604. Em.Object.create({
  605. componentName: 'sl1',
  606. displayName: 'd1'
  607. })
  608. ]);
  609. controller.set('content',content);
  610. controller.set('stepConfigs',stepConfigs);
  611. controller.loadGroups();
  612. var expected = [
  613. {
  614. "serviceName": "HDFS",
  615. "configCategories": [
  616. {
  617. "isForSlaveComponent": true,
  618. "primaryName": "sl1",
  619. "slaveConfigs": {
  620. "groups": [
  621. {
  622. "name": "Default",
  623. "index":
  624. "default",
  625. "type": "default",
  626. "active": true,
  627. "properties": []
  628. }
  629. ],
  630. "componentName": "sl1",
  631. "displayName": "d1"
  632. }
  633. }
  634. ]
  635. }
  636. ];
  637. var result = JSON.parse(JSON.stringify(controller.get('stepConfigs')))
  638. expect(result).to.be.eql(expected);
  639. });
  640. });
  641. });