slave_component_groups_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. });
  222. afterEach(function () {
  223. App.ServiceConfigProperty.create.restore();
  224. App.config.get.restore();
  225. });
  226. it('should return created config', function () {
  227. var res = JSON.parse(JSON.stringify(controller.componentProperties('HBASE')));
  228. var expected = [
  229. {
  230. "name": "mapred_local_dir"
  231. },
  232. {
  233. "name": "dfs_data_dir"
  234. }
  235. ];
  236. expect(res).to.be.eql(expected);
  237. });
  238. });
  239. describe('#selectedComponentName', function () {
  240. afterEach(function () {
  241. App.router.get.restore();
  242. });
  243. it('should return selected component name HDFS', function () {
  244. sinon.stub(App.router, 'get').returns('HDFS');
  245. expect(controller.get('selectedComponentName')).to.be.eql({
  246. name: 'DATANODE',
  247. displayName: 'DataNode'
  248. });
  249. });
  250. it('should return selected component name HBASE', function () {
  251. sinon.stub(App.router, 'get').returns('HBASE');
  252. expect(controller.get('selectedComponentName')).to.be.eql({
  253. name: 'HBASE_REGIONSERVER',
  254. displayName: 'RegionServer'
  255. });
  256. });
  257. it('should return null', function () {
  258. sinon.stub(App.router, 'get').returns('undefined');
  259. expect(controller.get('selectedComponentName')).to.be.null;
  260. });
  261. });
  262. describe('#selectedSlaveComponent', function () {
  263. beforeEach(function () {
  264. sinon.stub(App.router, 'get').returns(Em.A([
  265. Em.Object.create({
  266. serviceName: 'HDFS',
  267. configCategories: Em.A([
  268. Em.Object.create({
  269. isForSlaveComponent: true,
  270. primaryName: 'sl1',
  271. slaveConfigs: Em.A([]),
  272. name: 'name'
  273. })
  274. ])
  275. })
  276. ]));
  277. });
  278. afterEach(function () {
  279. App.router.get.restore();
  280. });
  281. it('should return selected component name', function () {
  282. var controller = App.SlaveComponentGroupsController.create({
  283. content: {}
  284. });
  285. controller.set('selectedComponentName', Em.Object.create({
  286. displayName: 'name'
  287. }));
  288. controller.set('selectedComponentName', 'value')
  289. expect(controller.get('selectedSlaveComponent')).to.be.null;
  290. });
  291. });
  292. describe('#removeSlaveComponentGroup', function () {
  293. beforeEach(function() {
  294. sinon.stub(controller, 'componentProperties').returns(Em.A([]));
  295. });
  296. afterEach(function() {
  297. controller.componentProperties.restore();
  298. });
  299. it('should return empty selected component', function () {
  300. var selectedSlaveComponent = Em.Object.create({
  301. newGroupIndex: 0,
  302. hosts: Em.A([]),
  303. groups: Em.A([
  304. Em.Object.create({
  305. active: true,
  306. name: 'New Group',
  307. type: 'new'
  308. })
  309. ])
  310. });
  311. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  312. var ev = Em.Object.create({
  313. context: selectedSlaveComponent.groups[0]
  314. });
  315. controller.removeSlaveComponentGroup(ev);
  316. var expected = {
  317. "newGroupIndex": 0,
  318. "hosts": [],
  319. "groups": []
  320. };
  321. var res = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent')));
  322. expect(res).to.be.eql(expected);
  323. });
  324. });
  325. describe('#showSlaveComponentGroup', function () {
  326. it('should make all groups active', function () {
  327. var selectedSlaveComponent = Em.Object.create({
  328. newGroupIndex: 0,
  329. hosts: Em.A([]),
  330. groups: Em.A([
  331. Em.Object.create({
  332. active: false,
  333. name: 'New Group',
  334. type: 'new'
  335. })
  336. ])
  337. });
  338. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  339. var ev = Em.Object.create({
  340. context: selectedSlaveComponent.groups[0]
  341. });
  342. controller.showSlaveComponentGroup(ev);
  343. var expected = {
  344. "newGroupIndex": 0,
  345. "hosts": [],
  346. "groups": [
  347. {
  348. "active": true,
  349. "name": "New Group",
  350. "type": "new"
  351. }
  352. ]
  353. };
  354. var res = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent')));
  355. expect(res).to.be.eql(expected);
  356. });
  357. });
  358. describe('#selectedComponentDisplayName', function () {
  359. beforeEach(function () {
  360. sinon.stub(App.format, 'role').returns('name')
  361. });
  362. afterEach(function () {
  363. App.format.role.restore();
  364. });
  365. it('should return selected component name', function () {
  366. expect(controller.get('selectedComponentDisplayName')).to.be.equal('name');
  367. });
  368. });
  369. describe('#hosts', function () {
  370. it('should return hosts', function () {
  371. var selectedSlaveComponent = Em.Object.create({
  372. newGroupIndex: 0,
  373. hosts: Em.A([{name: 'h1'}]),
  374. groups: Em.A([
  375. Em.Object.create({
  376. active: false,
  377. name: 'New Group',
  378. type: 'new'
  379. })
  380. ])
  381. });
  382. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  383. expect(controller.get('hosts')).to.be.eql(Em.A([{name: 'h1'}]));
  384. });
  385. });
  386. describe('#componentGroups', function () {
  387. it('should return groups', function () {
  388. var selectedSlaveComponent = Em.Object.create({
  389. newGroupIndex: 0,
  390. hosts: Em.A([{name: 'h1', group: 'one'}, {name: 'h2', group: 'one'}]),
  391. groups: Em.A([
  392. Em.Object.create({
  393. active: false,
  394. name: 'New Group',
  395. type: 'new'
  396. })
  397. ])
  398. });
  399. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  400. expect(controller.get('componentGroups')).to.be.eql(Em.A([
  401. Em.Object.create({
  402. active: false,
  403. name: 'New Group',
  404. type: 'new'
  405. })
  406. ]));
  407. });
  408. });
  409. describe('#activeGroup', function () {
  410. it('should return active group', function () {
  411. var selectedSlaveComponent = Em.Object.create({
  412. newGroupIndex: 0,
  413. hosts: Em.A([{name: 'h1', group: 'one'}, {name: 'h2', group: 'one'}]),
  414. groups: Em.A([
  415. Em.Object.create({
  416. active: false,
  417. name: 'New Group',
  418. type: 'new'
  419. }),
  420. Em.Object.create({
  421. active: true,
  422. name: 'New Group',
  423. type: 'new'
  424. })
  425. ])
  426. });
  427. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  428. expect(controller.get('activeGroup')).to.be.eql(Em.Object.create({
  429. active: true,
  430. name: 'New Group',
  431. type: 'new'
  432. }));
  433. });
  434. });
  435. describe('#groups', function () {
  436. it('should return uniq groups names', function () {
  437. var selectedSlaveComponent = Em.Object.create({
  438. newGroupIndex: 0,
  439. hosts: Em.A([{name: 'h1', group: 'one'}, {name: 'h2', group: 'one'}]),
  440. groups: Em.A([
  441. Em.Object.create({
  442. active: false,
  443. name: 'New Group',
  444. type: 'new'
  445. })
  446. ])
  447. });
  448. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  449. expect(controller.get('groups')).to.be.eql(['one']);
  450. });
  451. });
  452. describe('#addSlaveComponentGroup', function () {
  453. beforeEach(function() {
  454. sinon.stub(controller, 'componentProperties').returns(Em.A([]));
  455. });
  456. afterEach(function() {
  457. controller.componentProperties.restore();
  458. });
  459. it('should return selected component name', function () {
  460. var selectedSlaveComponent = Em.Object.create({
  461. newGroupIndex: 0,
  462. groups: Em.A([
  463. Em.Object.create({
  464. active: true,
  465. name: 'New Group'
  466. })
  467. ])
  468. });
  469. controller.set('selectedSlaveComponent', selectedSlaveComponent);
  470. controller.addSlaveComponentGroup();
  471. var expected = {
  472. "newGroupIndex": 1,
  473. "groups": [
  474. {
  475. "active": false,
  476. "name": "New Group"
  477. },
  478. {
  479. "name": "New Group 1",
  480. "index": 1,
  481. "type": "new",
  482. "active": true,
  483. "properties": []
  484. }
  485. ]
  486. };
  487. var res = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent')));
  488. expect(res).to.be.eql(expected);
  489. });
  490. });
  491. describe('#checkGroupName', function () {
  492. it('should make equal to 2', function () {
  493. var selectedSlaveComponent = Em.Object.create({
  494. groups: Em.A([
  495. Em.Object.create({
  496. name: 'New Group 1'
  497. })
  498. ]),
  499. newGroupIndex: 0
  500. });
  501. var group = {};
  502. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  503. controller.checkGroupName();
  504. expect(controller.get('selectedSlaveComponent').newGroupIndex).to.be.equal(2);
  505. });
  506. });
  507. describe('#changeHostGroup', function () {
  508. it('should push 1 host group', function () {
  509. var selectedSlaveComponent = Em.Object.create({
  510. tempSelectedGroups: undefined
  511. });
  512. var host = Em.Object.create({
  513. hostName: 'h1'
  514. });
  515. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  516. controller.changeHostGroup(host, 'g1');
  517. var expected = [
  518. {
  519. "hostName": "h1",
  520. "groupName": "g1"
  521. }
  522. ];
  523. var result = JSON.parse(JSON.stringify(controller.get('selectedSlaveComponent').tempSelectedGroups));
  524. expect(result).to.be.eql(expected);
  525. });
  526. it('should push change host group name', function () {
  527. var selectedSlaveComponent = Em.Object.create({
  528. tempSelectedGroups: [
  529. Em.Object.create({
  530. hostName: 'h1',
  531. groupName: ''
  532. })
  533. ]
  534. });
  535. var host = Em.Object.create({
  536. hostName: 'h1'
  537. });
  538. controller.set('selectedSlaveComponent',selectedSlaveComponent);
  539. controller.changeHostGroup(host, 'g1');
  540. var expected = [
  541. Em.Object.create({
  542. "hostName": "h1",
  543. "groupName": "g1"
  544. })
  545. ]
  546. expect(controller.get('selectedSlaveComponent').tempSelectedGroups).to.be.eql(expected);
  547. });
  548. });
  549. describe('#loadGroups', function () {
  550. beforeEach(function () {
  551. sinon.stub(App.SlaveConfigs, 'create').returns(Em.Object.create({
  552. groups: false,
  553. componentName: '',
  554. displayName: ''
  555. }));
  556. sinon.stub(App.config, 'get').returns(Em.A([
  557. Em.Object.create({
  558. category: 'HDFS',
  559. serviceName: 'HDFS',
  560. configs: Em.A([])
  561. })
  562. ]));
  563. });
  564. afterEach(function () {
  565. App.SlaveConfigs.create.restore();
  566. App.config.get.restore();
  567. });
  568. it('should modefie step confgigs', function () {
  569. var stepConfigs = Em.A([
  570. Em.Object.create({
  571. serviceName: 'HDFS',
  572. configCategories: Em.A([
  573. Em.Object.create({
  574. isForSlaveComponent: true,
  575. primaryName: 'sl1',
  576. slaveConfigs: Em.A([])
  577. })
  578. ])
  579. })
  580. ]);
  581. var content = Em.A([
  582. Em.Object.create({
  583. componentName: 'sl1',
  584. displayName: 'd1',
  585. groups: Em.A([
  586. Em.Object.create({
  587. name: 'g1'
  588. })
  589. ])
  590. })
  591. ]);
  592. controller.set('content',content);
  593. controller.set('stepConfigs',stepConfigs);
  594. controller.loadGroups();
  595. var expected = [
  596. {
  597. "serviceName": "HDFS",
  598. "configCategories": [
  599. {
  600. "isForSlaveComponent": true,
  601. "primaryName": "sl1",
  602. "slaveConfigs": {
  603. "groups": [
  604. {
  605. "name": "g1"
  606. }
  607. ],
  608. "componentName": "sl1",
  609. "displayName": "d1"
  610. }
  611. }
  612. ]
  613. }
  614. ];
  615. var result = JSON.parse(JSON.stringify(controller.get('stepConfigs')));
  616. expect(result).to.be.eql(expected);
  617. });
  618. it('should add groups to stepConfgig', function () {
  619. var stepConfigs = Em.A([
  620. Em.Object.create({
  621. serviceName: 'HDFS',
  622. configCategories: Em.A([
  623. Em.Object.create({
  624. isForSlaveComponent: true,
  625. primaryName: 'sl1',
  626. slaveConfigs: Em.A([])
  627. })
  628. ])
  629. })
  630. ]);
  631. var content = Em.A([
  632. Em.Object.create({
  633. componentName: 'sl1',
  634. displayName: 'd1'
  635. })
  636. ]);
  637. controller.set('content',content);
  638. controller.set('stepConfigs',stepConfigs);
  639. controller.loadGroups();
  640. var expected = [
  641. {
  642. "serviceName": "HDFS",
  643. "configCategories": [
  644. {
  645. "isForSlaveComponent": true,
  646. "primaryName": "sl1",
  647. "slaveConfigs": {
  648. "groups": [
  649. {
  650. "name": "Default",
  651. "index":
  652. "default",
  653. "type": "default",
  654. "active": true,
  655. "properties": []
  656. }
  657. ],
  658. "componentName": "sl1",
  659. "displayName": "d1"
  660. }
  661. }
  662. ]
  663. }
  664. ];
  665. var result = JSON.parse(JSON.stringify(controller.get('stepConfigs')))
  666. expect(result).to.be.eql(expected);
  667. });
  668. });
  669. });