add_controller_test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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('#sortServiceConfigGroups()', function () {
  97. var testCases = [
  98. {
  99. title: 'No selected services',
  100. selectedServices: [
  101. {configGroups: []}
  102. ],
  103. result: [
  104. {configGroups: []}
  105. ]
  106. },
  107. {
  108. title: 'Only one group is present',
  109. selectedServices: [
  110. {configGroups: [
  111. {configGroups: {group_name: 'b'}}
  112. ]}
  113. ],
  114. result: [
  115. {configGroups: [
  116. {configGroups: {group_name: 'b'}}
  117. ]}
  118. ]
  119. },
  120. {
  121. title: 'Reverse order of groups',
  122. selectedServices: [
  123. {configGroups: [
  124. {ConfigGroup: {group_name: 'b2'}},
  125. {ConfigGroup: {group_name: 'a1'}}
  126. ]}
  127. ],
  128. result: [
  129. {configGroups: [
  130. {ConfigGroup: {group_name: 'a1'}},
  131. {ConfigGroup: {group_name: 'b2'}}
  132. ]}
  133. ]
  134. },
  135. {
  136. title: 'Correct order of groups',
  137. selectedServices: [
  138. {configGroups: [
  139. {ConfigGroup: {group_name: 'a1'}},
  140. {ConfigGroup: {group_name: 'b2'}}
  141. ]}
  142. ],
  143. result: [
  144. {configGroups: [
  145. {ConfigGroup: {group_name: 'a1'}},
  146. {ConfigGroup: {group_name: 'b2'}}
  147. ]}
  148. ]
  149. }
  150. ];
  151. testCases.forEach(function (test) {
  152. it(test.title, function () {
  153. controller.sortServiceConfigGroups(test.selectedServices);
  154. expect(test.selectedServices).to.eql(test.result);
  155. });
  156. });
  157. });
  158. describe('#loadServiceConfigGroupsBySlaves()', function () {
  159. var testCases = [
  160. {
  161. title: 'slaveComponentHosts is null',
  162. slaveComponentHosts: null,
  163. result: {
  164. output: false,
  165. selectedServices: []
  166. }
  167. },
  168. {
  169. title: 'slaveComponentHosts is empty',
  170. slaveComponentHosts: [],
  171. result: {
  172. output: false,
  173. selectedServices: []
  174. }
  175. },
  176. {
  177. title: 'Component does not have hosts',
  178. slaveComponentHosts: [
  179. {hosts: []}
  180. ],
  181. result: {
  182. output: true,
  183. selectedServices: []
  184. }
  185. },
  186. {
  187. title: 'Only client component is present',
  188. slaveComponentHosts: [
  189. {
  190. hosts: [
  191. {hostName: 'host1'}
  192. ],
  193. componentName: 'CLIENT'
  194. }
  195. ],
  196. result: {
  197. output: true,
  198. selectedServices: []
  199. }
  200. }
  201. ];
  202. controller.set('content.configGroups', [
  203. {
  204. ConfigGroup: {
  205. tag: 'HDFS',
  206. group_name: 'HDFS test'
  207. }
  208. }
  209. ]);
  210. testCases.forEach(function (test) {
  211. it(test.title, function () {
  212. var selectedServices = [];
  213. controller.set('content.slaveComponentHosts', test.slaveComponentHosts);
  214. expect(controller.loadServiceConfigGroupsBySlaves(selectedServices)).to.equal(test.result.output);
  215. expect(selectedServices).to.eql(test.result.selectedServices);
  216. });
  217. });
  218. });
  219. describe('#loadServiceConfigGroupsByClients()', function () {
  220. var testCases = [
  221. {
  222. title: 'slaveComponentHosts is null',
  223. content: {
  224. slaveComponentHosts: null,
  225. clients: [],
  226. selectedServices: []
  227. },
  228. result: {
  229. output: false,
  230. selectedServices: []
  231. }
  232. },
  233. {
  234. title: 'slaveComponentHosts is empty',
  235. content: {
  236. slaveComponentHosts: [],
  237. clients: [],
  238. selectedServices: []
  239. },
  240. result: {
  241. output: false,
  242. selectedServices: []
  243. }
  244. },
  245. {
  246. title: 'Client does not have hosts',
  247. content: {
  248. slaveComponentHosts: [
  249. {
  250. componentName: 'CLIENT',
  251. hosts: []
  252. }
  253. ],
  254. clients: [],
  255. selectedServices: []
  256. },
  257. result: {
  258. output: false,
  259. selectedServices: []
  260. }
  261. },
  262. {
  263. title: 'Client has hosts, but clients is empty',
  264. content: {
  265. slaveComponentHosts: [
  266. {
  267. componentName: 'CLIENT',
  268. hosts: [
  269. {hostName: 'host1'}
  270. ]
  271. }
  272. ],
  273. clients: [],
  274. selectedServices: []
  275. },
  276. result: {
  277. output: false,
  278. selectedServices: []
  279. }
  280. }
  281. ];
  282. testCases.forEach(function (test) {
  283. it(test.title, function () {
  284. controller.set('content.slaveComponentHosts', test.content.slaveComponentHosts);
  285. controller.set('content.clients', test.content.clients);
  286. expect(controller.loadServiceConfigGroupsByClients(test.content.selectedServices)).to.equal(test.result.output);
  287. expect(test.content.selectedServices).to.eql(test.result.selectedServices);
  288. });
  289. });
  290. });
  291. describe('#installServices()', function () {
  292. beforeEach(function () {
  293. sinon.spy(App.ajax, "send");
  294. });
  295. afterEach(function () {
  296. App.ajax.send.restore();
  297. });
  298. it('No hosts', function () {
  299. controller.set('content.cluster', {name: 'cl'});
  300. controller.set('testDBHosts', {});
  301. expect(controller.installServices()).to.be.false;
  302. expect(App.ajax.send.called).to.be.false;
  303. });
  304. it('Cluster name is empty', function () {
  305. controller.set('content.cluster', {name: ''});
  306. controller.set('testDBHosts', {'host1': {}});
  307. expect(controller.installServices()).to.be.false;
  308. expect(App.ajax.send.called).to.be.false;
  309. });
  310. it('Cluster name is correct and hosts are present', function () {
  311. controller.set('content.cluster', {name: 'cl'});
  312. controller.set('testDBHosts', {'host1': {isInstalled: false}});
  313. expect(controller.installServices()).to.be.true;
  314. expect(App.ajax.send.called).to.be.true;
  315. });
  316. });
  317. });