add_controller_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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/host/add_controller');
  21. require('models/host_component');
  22. require('models/service');
  23. require('mappers/server_data_mapper');
  24. describe('App.AddHostController', function () {
  25. var controller = App.AddHostController.create({
  26. testDBHosts: null,
  27. getDBProperty: function () {
  28. return this.get('testDBHosts');
  29. },
  30. setDBProperty: function () {
  31. },
  32. loadClients: function () {
  33. }
  34. });
  35. beforeEach(function () {
  36. sinon.spy(controller, "setDBProperty");
  37. });
  38. afterEach(function () {
  39. controller.setDBProperty.restore();
  40. });
  41. describe('#removeHosts()', function () {
  42. var testCases = [
  43. {
  44. title: 'No hosts, db is empty',
  45. content: {
  46. hosts: [],
  47. dbHosts: {}
  48. },
  49. result: {}
  50. },
  51. {
  52. title: 'Host is passed, db is empty',
  53. content: {
  54. hosts: [
  55. {hostName: 'host1'}
  56. ],
  57. dbHosts: {}
  58. },
  59. result: {}
  60. },
  61. {
  62. title: 'Passed host different from hosts in db',
  63. content: {
  64. hosts: [
  65. {hostName: 'host1'}
  66. ],
  67. dbHosts: {
  68. 'host2': {}
  69. }
  70. },
  71. result: {
  72. 'host2': {}
  73. }
  74. },
  75. {
  76. title: 'Passed host match host in db',
  77. content: {
  78. hosts: [
  79. {hostName: 'host1'}
  80. ],
  81. dbHosts: {
  82. 'host1': {}
  83. }
  84. },
  85. result: {}
  86. }
  87. ];
  88. testCases.forEach(function (test) {
  89. it(test.title, function () {
  90. controller.set('testDBHosts', test.content.dbHosts);
  91. controller.removeHosts(test.content.hosts);
  92. expect(controller.setDBProperty.calledWith('hosts', test.result)).to.be.true;
  93. });
  94. });
  95. });
  96. describe('#saveClients()', function () {
  97. var modelSetup = require('test/init_model_test');
  98. var testCases = [
  99. {
  100. title: 'No services',
  101. services: [],
  102. result: []
  103. },
  104. {
  105. title: 'No selected services',
  106. services: [
  107. {isSelected: false}
  108. ],
  109. result: []
  110. },
  111. {
  112. title: 'Service is not in stack',
  113. services: [
  114. {
  115. serviceName: 'TEST',
  116. isSelected: true
  117. }
  118. ],
  119. result: []
  120. },
  121. {
  122. title: 'Service does not have any clients',
  123. services: [
  124. {
  125. serviceName: 'GANGLIA',
  126. isSelected: true
  127. }
  128. ],
  129. result: []
  130. },
  131. {
  132. title: 'StackServiceComponent is empty',
  133. services: [
  134. {
  135. serviceName: 'HDFS',
  136. isSelected: true
  137. }
  138. ],
  139. result: []
  140. }
  141. ];
  142. testCases.forEach(function (test) {
  143. it(test.title, function () {
  144. controller.set('content.services', test.services);
  145. controller.saveClients();
  146. expect(controller.setDBProperty.calledWith('clientInfo', test.result)).to.be.true;
  147. expect(controller.get('content.clients')).to.be.empty;
  148. });
  149. });
  150. it('HDFS has uninstalled client', function () {
  151. modelSetup.setupStackServiceComponent();
  152. var services = [
  153. {
  154. serviceName: 'HDFS',
  155. isSelected: true
  156. }
  157. ];
  158. controller.set('content.services', services);
  159. controller.saveClients();
  160. expect(controller.get('content.clients')).to.eql([
  161. {
  162. component_name: 'HDFS_CLIENT',
  163. display_name: 'HDFS Client',
  164. isInstalled: false
  165. }
  166. ]);
  167. expect(controller.setDBProperty.calledWith('clientInfo', [
  168. {
  169. component_name: 'HDFS_CLIENT',
  170. display_name: 'HDFS Client',
  171. isInstalled: false
  172. }
  173. ])).to.be.true;
  174. modelSetup.cleanStackServiceComponent();
  175. });
  176. it('HDFS has installed client', function () {
  177. modelSetup.setupStackServiceComponent();
  178. var services = [
  179. {
  180. serviceName: 'HDFS',
  181. isSelected: true
  182. }
  183. ];
  184. App.store.load(App.HostComponent, {
  185. id: 'HDFS_CLIENT_host1',
  186. component_name: "HDFS_CLIENT"
  187. });
  188. controller.set('content.services', services);
  189. controller.saveClients();
  190. expect(controller.get('content.clients')).to.eql([
  191. {
  192. component_name: 'HDFS_CLIENT',
  193. display_name: 'HDFS Client',
  194. isInstalled: true
  195. }
  196. ]);
  197. expect(controller.setDBProperty.calledWith('clientInfo', [
  198. {
  199. component_name: 'HDFS_CLIENT',
  200. display_name: 'HDFS Client',
  201. isInstalled: true
  202. }
  203. ])).to.be.true;
  204. modelSetup.cleanStackServiceComponent();
  205. });
  206. });
  207. describe('#applyConfigGroup()', function () {
  208. beforeEach(function () {
  209. sinon.spy(App.ajax, "send");
  210. });
  211. afterEach(function () {
  212. App.ajax.send.restore();
  213. });
  214. it('No config groups', function () {
  215. controller.set('content.serviceConfigGroups', []);
  216. controller.applyConfigGroup();
  217. expect(App.ajax.send.called).to.be.false;
  218. });
  219. it('Selected group has no groups', function () {
  220. var serviceConfigGroups = [
  221. {
  222. configGroups: [],
  223. selectedConfigGroup: ''
  224. }
  225. ];
  226. controller.set('content.serviceConfigGroups', serviceConfigGroups);
  227. controller.applyConfigGroup();
  228. expect(App.ajax.send.called).to.be.false;
  229. });
  230. it('Selected group does not match groups', function () {
  231. var serviceConfigGroups = [
  232. {
  233. configGroups: [
  234. {
  235. ConfigGroup: {
  236. group_name: 'group1'
  237. }
  238. }
  239. ],
  240. selectedConfigGroup: 'group2'
  241. }
  242. ];
  243. controller.set('content.serviceConfigGroups', serviceConfigGroups);
  244. controller.applyConfigGroup();
  245. expect(App.ajax.send.called).to.be.false;
  246. });
  247. it('Selected group has zero hosts', function () {
  248. var serviceConfigGroups = [
  249. {
  250. configGroups: [
  251. {
  252. ConfigGroup: {
  253. group_name: 'group1',
  254. hosts: []
  255. },
  256. href: 'href'
  257. }
  258. ],
  259. hosts: [],
  260. selectedConfigGroup: 'group1'
  261. }
  262. ];
  263. controller.set('content.serviceConfigGroups', serviceConfigGroups);
  264. controller.applyConfigGroup();
  265. expect(serviceConfigGroups[0].configGroups[0].ConfigGroup.hosts).to.be.empty;
  266. expect(serviceConfigGroups[0].configGroups[0].href).to.be.undefined;
  267. expect(App.ajax.send.calledOnce).to.be.true;
  268. });
  269. it('Selected group has host', function () {
  270. var serviceConfigGroups = [
  271. {
  272. configGroups: [
  273. {
  274. ConfigGroup: {
  275. group_name: 'group1',
  276. hosts: []
  277. },
  278. href: 'href'
  279. }
  280. ],
  281. hosts: ['host1'],
  282. selectedConfigGroup: 'group1'
  283. }
  284. ];
  285. controller.set('content.serviceConfigGroups', serviceConfigGroups);
  286. controller.applyConfigGroup();
  287. expect(serviceConfigGroups[0].configGroups[0].ConfigGroup.hosts).to.eql([
  288. {host_name: 'host1'}
  289. ]);
  290. expect(serviceConfigGroups[0].configGroups[0].href).to.be.undefined;
  291. expect(App.ajax.send.calledOnce).to.be.true;
  292. });
  293. });
  294. describe('#sortServiceConfigGroups()', function () {
  295. var testCases = [
  296. {
  297. title: 'No selected services',
  298. selectedServices: [
  299. {configGroups: []}
  300. ],
  301. result: [
  302. {configGroups: []}
  303. ]
  304. },
  305. {
  306. title: 'Only one group is present',
  307. selectedServices: [
  308. {configGroups: [
  309. {configGroups: {group_name: 'b'}}
  310. ]}
  311. ],
  312. result: [
  313. {configGroups: [
  314. {configGroups: {group_name: 'b'}}
  315. ]}
  316. ]
  317. },
  318. {
  319. title: 'Reverse order of groups',
  320. selectedServices: [
  321. {configGroups: [
  322. {ConfigGroup: {group_name: 'b2'}},
  323. {ConfigGroup: {group_name: 'a1'}}
  324. ]}
  325. ],
  326. result: [
  327. {configGroups: [
  328. {ConfigGroup: {group_name: 'a1'}},
  329. {ConfigGroup: {group_name: 'b2'}}
  330. ]}
  331. ]
  332. },
  333. {
  334. title: 'Correct order of groups',
  335. selectedServices: [
  336. {configGroups: [
  337. {ConfigGroup: {group_name: 'a1'}},
  338. {ConfigGroup: {group_name: 'b2'}}
  339. ]}
  340. ],
  341. result: [
  342. {configGroups: [
  343. {ConfigGroup: {group_name: 'a1'}},
  344. {ConfigGroup: {group_name: 'b2'}}
  345. ]}
  346. ]
  347. }
  348. ];
  349. testCases.forEach(function (test) {
  350. it(test.title, function () {
  351. controller.sortServiceConfigGroups(test.selectedServices);
  352. expect(test.selectedServices).to.eql(test.result);
  353. });
  354. });
  355. });
  356. describe('#loadServiceConfigGroupsBySlaves()', function () {
  357. var testCases = [
  358. {
  359. title: 'slaveComponentHosts is null',
  360. slaveComponentHosts: null,
  361. result: {
  362. output: false,
  363. selectedServices: []
  364. }
  365. },
  366. {
  367. title: 'slaveComponentHosts is empty',
  368. slaveComponentHosts: [],
  369. result: {
  370. output: false,
  371. selectedServices: []
  372. }
  373. },
  374. {
  375. title: 'Component does not have hosts',
  376. slaveComponentHosts: [
  377. {hosts: []}
  378. ],
  379. result: {
  380. output: true,
  381. selectedServices: []
  382. }
  383. },
  384. {
  385. title: 'Only client component is present',
  386. slaveComponentHosts: [
  387. {
  388. hosts: [
  389. {hostName: 'host1'}
  390. ],
  391. componentName: 'CLIENT'
  392. }
  393. ],
  394. result: {
  395. output: true,
  396. selectedServices: []
  397. }
  398. },
  399. {
  400. title: 'Slave component is present',
  401. slaveComponentHosts: [
  402. {
  403. hosts: [
  404. {hostName: 'host1'}
  405. ],
  406. componentName: 'DATANODE'
  407. }
  408. ],
  409. result: {
  410. output: true,
  411. selectedServices: [
  412. {
  413. serviceId: 'HDFS',
  414. displayName: 'HDFS',
  415. hosts: ['host1'],
  416. configGroupsNames: ['HDFS Default', 'HDFS test'],
  417. configGroups: [
  418. {
  419. ConfigGroup: {
  420. tag: 'HDFS',
  421. group_name: 'HDFS test'
  422. }
  423. }
  424. ],
  425. selectedConfigGroup: 'HDFS Default'
  426. }
  427. ]
  428. }
  429. }
  430. ];
  431. controller.set('content.configGroups', [
  432. {
  433. ConfigGroup: {
  434. tag: 'HDFS',
  435. group_name: 'HDFS test'
  436. }
  437. }
  438. ]);
  439. testCases.forEach(function (test) {
  440. it(test.title, function () {
  441. var selectedServices = [];
  442. controller.set('content.slaveComponentHosts', test.slaveComponentHosts);
  443. expect(controller.loadServiceConfigGroupsBySlaves(selectedServices)).to.equal(test.result.output);
  444. expect(selectedServices).to.eql(test.result.selectedServices);
  445. });
  446. });
  447. });
  448. describe('#loadServiceConfigGroupsByClients()', function () {
  449. var testCases = [
  450. {
  451. title: 'slaveComponentHosts is null',
  452. content: {
  453. slaveComponentHosts: null,
  454. clients: [],
  455. selectedServices: []
  456. },
  457. result: {
  458. output: false,
  459. selectedServices: []
  460. }
  461. },
  462. {
  463. title: 'slaveComponentHosts is empty',
  464. content: {
  465. slaveComponentHosts: [],
  466. clients: [],
  467. selectedServices: []
  468. },
  469. result: {
  470. output: false,
  471. selectedServices: []
  472. }
  473. },
  474. {
  475. title: 'Client does not have hosts',
  476. content: {
  477. slaveComponentHosts: [
  478. {
  479. componentName: 'CLIENT',
  480. hosts: []
  481. }
  482. ],
  483. clients: [],
  484. selectedServices: []
  485. },
  486. result: {
  487. output: false,
  488. selectedServices: []
  489. }
  490. },
  491. {
  492. title: 'Client has hosts, but clients is empty',
  493. content: {
  494. slaveComponentHosts: [
  495. {
  496. componentName: 'CLIENT',
  497. hosts: [
  498. {hostName: 'host1'}
  499. ]
  500. }
  501. ],
  502. clients: [],
  503. selectedServices: []
  504. },
  505. result: {
  506. output: false,
  507. selectedServices: []
  508. }
  509. },
  510. {
  511. title: 'Client is present',
  512. content: {
  513. slaveComponentHosts: [
  514. {
  515. componentName: 'CLIENT',
  516. hosts: [
  517. {hostName: 'host1'}
  518. ]
  519. }
  520. ],
  521. clients: [
  522. {
  523. component_name: 'HDFS_CLIENT'
  524. }
  525. ],
  526. selectedServices: []
  527. },
  528. result: {
  529. output: true,
  530. selectedServices: [
  531. {
  532. serviceId: 'HDFS',
  533. displayName: 'HDFS',
  534. hosts: ['host1'],
  535. configGroupsNames: ['HDFS Default', 'HDFS test'],
  536. configGroups: [
  537. {
  538. ConfigGroup: {
  539. tag: 'HDFS',
  540. group_name: 'HDFS test'
  541. }
  542. }
  543. ],
  544. selectedConfigGroup: 'HDFS Default'
  545. }
  546. ]
  547. }
  548. }
  549. ];
  550. testCases.forEach(function (test) {
  551. it(test.title, function () {
  552. controller.set('content.slaveComponentHosts', test.content.slaveComponentHosts);
  553. controller.set('content.clients', test.content.clients);
  554. expect(controller.loadServiceConfigGroupsByClients(test.content.selectedServices)).to.equal(test.result.output);
  555. expect(test.content.selectedServices).to.eql(test.result.selectedServices);
  556. });
  557. });
  558. });
  559. describe('#installServices()', function () {
  560. beforeEach(function () {
  561. sinon.spy(App.ajax, "send");
  562. });
  563. afterEach(function () {
  564. App.ajax.send.restore();
  565. });
  566. it('No hosts', function () {
  567. controller.set('content.cluster', {name: 'cl'});
  568. controller.set('testDBHosts', {});
  569. expect(controller.installServices()).to.be.false;
  570. expect(App.ajax.send.called).to.be.false;
  571. });
  572. it('Cluster name is empty', function () {
  573. controller.set('content.cluster', {name: ''});
  574. controller.set('testDBHosts', {'host1': {}});
  575. expect(controller.installServices()).to.be.false;
  576. expect(App.ajax.send.called).to.be.false;
  577. });
  578. it('Cluster name is correct and hosts are present', function () {
  579. controller.set('content.cluster', {name: 'cl'});
  580. controller.set('testDBHosts', {'host1': {}});
  581. expect(controller.installServices()).to.be.true;
  582. expect(App.ajax.send.called).to.be.true;
  583. });
  584. it('Cluster name is correct and hosts are present, isRetry = true', function () {
  585. controller.set('content.cluster', {name: 'cl'});
  586. controller.set('testDBHosts', {'host1': {}});
  587. expect(controller.installServices(true)).to.be.true;
  588. expect(App.ajax.send.called).to.be.true;
  589. });
  590. });
  591. });