slave_component_groups_controller.js 20 KB

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