step5_view_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. var lazyloading = require('utils/lazy_loading');
  20. require('views/wizard/step5_view');
  21. var view;
  22. describe('App.WizardStep5View', function() {
  23. beforeEach(function() {
  24. view = App.WizardStep5View.create({
  25. controller: App.WizardStep5Controller.create({})
  26. });
  27. });
  28. describe('#didInsertElement', function() {
  29. it('should call controller.loadStep', function() {
  30. sinon.stub(view.get('controller'), 'loadStep', Em.K);
  31. view.didInsertElement();
  32. expect(view.get('controller').loadStep.calledOnce).to.equal(true);
  33. view.get('controller').loadStep.restore();
  34. });
  35. });
  36. });
  37. describe('App.SelectHostView', function() {
  38. beforeEach(function() {
  39. view = App.SelectHostView.create({
  40. controller: App.WizardStep5Controller.create({})
  41. });
  42. });
  43. describe('#didInsertElement', function() {
  44. beforeEach(function() {
  45. sinon.stub(view, 'initContent', Em.K);
  46. });
  47. afterEach(function() {
  48. view.initContent.restore();
  49. });
  50. it('should call initContent', function() {
  51. view.didInsertElement();
  52. expect(view.initContent.calledOnce).to.equal(true);
  53. });
  54. it('should set selectedHost to value', function() {
  55. view.set('selectedHost', 'h1');
  56. view.set('value', '');
  57. view.didInsertElement();
  58. expect(view.get('value')).to.equal('h1');
  59. });
  60. });
  61. describe('#change', function() {
  62. beforeEach(function() {
  63. view.set('componentName', 'ZOOKEEPER_SERVER');
  64. view.set('value', 'h1');
  65. view.set('zId', 1);
  66. view.set('controller.rebalanceComponentHostsCounter', 0);
  67. view.set('controller.componentToRebalance', '');
  68. sinon.stub(view.get('controller'), 'assignHostToMaster', Em.K);
  69. });
  70. afterEach(function() {
  71. view.get('controller').assignHostToMaster.restore();
  72. });
  73. it('should call assignHostToMaster', function() {
  74. view.change();
  75. expect(view.get('controller').assignHostToMaster.calledWith('ZOOKEEPER_SERVER', 'h1', 1));
  76. });
  77. it('should increment rebalanceComponentHostsCounter', function() {
  78. view.change();
  79. expect(view.get('controller.rebalanceComponentHostsCounter')).to.equal(1);
  80. });
  81. it('should set componentToRebalance', function() {
  82. view.change();
  83. expect(view.get('controller.componentToRebalance')).to.equal('ZOOKEEPER_SERVER');
  84. });
  85. });
  86. describe('#getAvailableHosts', function() {
  87. var tests = Em.A([
  88. {
  89. hosts: Em.A([]),
  90. selectedHost: 'h2',
  91. componentName: 'ZOOKEEPER_SERVER',
  92. selectedServicesMasters: Em.A([
  93. Em.Object.create({component_name: 'ZOOKEEPER_SERVER', selectedHost: 'h1'})
  94. ]),
  95. m: 'Empty hosts',
  96. e: []
  97. },
  98. {
  99. hosts: Em.A([
  100. Em.Object.create({host_name: 'h1'}),
  101. Em.Object.create({host_name: 'h2'})
  102. ]),
  103. selectedHost: 'h2',
  104. componentName: 'c1',
  105. selectedServicesMasters: Em.A([
  106. Em.Object.create({component_name: 'c2', selectedHost: 'h1'})
  107. ]),
  108. m: 'Two hosts',
  109. e: ['h1', 'h2']
  110. },
  111. {
  112. hosts: Em.A([
  113. Em.Object.create({host_name: 'h1'}),
  114. Em.Object.create({host_name: 'h2'})
  115. ]),
  116. selectedHost: 'h2',
  117. componentName: 'ZOOKEEPER_SERVER',
  118. selectedServicesMasters: Em.A([
  119. Em.Object.create({component_name: 'ZOOKEEPER_SERVER', selectedHost: 'h1'})
  120. ]),
  121. m: 'Two hosts, ZOOKEEPER_SERVER',
  122. e: ['h2']
  123. },
  124. {
  125. hosts: Em.A([
  126. Em.Object.create({host_name: 'h1'}),
  127. Em.Object.create({host_name: 'h2'})
  128. ]),
  129. selectedHost: 'h2',
  130. componentName: 'HBASE_MASTER',
  131. selectedServicesMasters: Em.A([
  132. Em.Object.create({component_name: 'HBASE_MASTER', selectedHost: 'h1'})
  133. ]),
  134. m: 'Two hosts, HBASE_MASTER',
  135. e: ['h2']
  136. }
  137. ]);
  138. tests.forEach(function(test) {
  139. it(test.m, function() {
  140. view.set('controller.hosts', test.hosts);
  141. view.set('componentName', test.componentName);
  142. view.set('controller.selectedServicesMasters', test.selectedServicesMasters);
  143. var r = view.getAvailableHosts();
  144. expect(r.mapProperty('host_name')).to.eql(test.e);
  145. });
  146. });
  147. });
  148. describe('#rebalanceComponentHosts', function() {
  149. var tests = Em.A([
  150. {
  151. componentName: 'c1',
  152. componentToRebalance: 'c2',
  153. isLoaded: true,
  154. content: [{}],
  155. m: 'componentName not equal to componentToRebalance',
  156. e: {
  157. initContent: false,
  158. isLoaded: true,
  159. content: 1
  160. }
  161. },
  162. {
  163. componentName: 'c2',
  164. componentToRebalance: 'c2',
  165. isLoaded: true,
  166. content: [{}],
  167. m: 'componentName equal to componentToRebalance',
  168. e: {
  169. initContent: true,
  170. isLoaded: false,
  171. content: 0
  172. }
  173. }
  174. ]);
  175. tests.forEach(function(test) {
  176. it(test.m, function() {
  177. view.set('isLoaded', test.isLoaded);
  178. view.set('content', test.content);
  179. view.set('componentName', test.componentName);
  180. view.set('controller.componentToRebalance', test.componentToRebalance);
  181. sinon.stub(view, 'initContent', Em.K);
  182. view.rebalanceComponentHosts();
  183. expect(view.initContent.calledOnce).to.equal(test.e.initContent);
  184. expect(view.get('isLoaded')).to.equal(test.e.isLoaded);
  185. expect(view.get('content.length')).to.equal(test.e.content);
  186. view.initContent.restore();
  187. });
  188. });
  189. });
  190. describe('#initContent', function() {
  191. var tests = Em.A([
  192. {
  193. isLazyLoading: false,
  194. hosts: 25,
  195. m: 'not lazy loading, 25 hosts, no selected host',
  196. e: 25
  197. },
  198. {
  199. isLazyLoading: false,
  200. hosts: 25,
  201. h: 4,
  202. m: 'not lazy loading, 25 hosts, one selected host',
  203. e: 25
  204. },
  205. {
  206. isLazyLoading: true,
  207. hosts: 25,
  208. h: 4,
  209. m: 'lazy loading, 25 hosts, one selected host',
  210. e: 25
  211. },
  212. {
  213. isLazyLoading: true,
  214. hosts: 25,
  215. m: 'lazy loading, 25 hosts, no selected host',
  216. e: 26
  217. },
  218. {
  219. isLazyLoading: true,
  220. hosts: 100,
  221. h: 4,
  222. m: 'lazy loading, 100 hosts, one selected host',
  223. e: 30
  224. },
  225. {
  226. isLazyLoading: true,
  227. hosts: 100,
  228. m: 'lazy loading, 100 hosts, no selected host',
  229. e: 31
  230. }
  231. ]);
  232. tests.forEach(function(test) {
  233. it(test.m, function() {
  234. view.reopen({getAvailableHosts: function() {return d3.range(0, test.hosts).map(function(indx){return Em.Object.create({host_name: indx})});}});
  235. if (test.h) {
  236. view.set('selectedHost', test.h);
  237. }
  238. view.set('isLazyLoading', test.isLazyLoading);
  239. view.initContent();
  240. expect(view.get('content.length')).to.equal(test.e);
  241. });
  242. });
  243. });
  244. describe('#click', function() {
  245. beforeEach(function() {
  246. sinon.stub(lazyloading, 'run', Em.K);
  247. });
  248. afterEach(function() {
  249. lazyloading.run.restore();
  250. });
  251. Em.A([
  252. {
  253. isLoaded: true,
  254. isLazyLoading: true,
  255. e: false
  256. },
  257. {
  258. isLoaded: true,
  259. isLazyLoading: false,
  260. e: false
  261. },
  262. {
  263. isLoaded: false,
  264. isLazyLoading: true,
  265. e: true
  266. },
  267. {
  268. isLoaded: false,
  269. isLazyLoading: false,
  270. e: false
  271. }
  272. ]).forEach(function(test) {
  273. it('isLoaded = ' + test.isLoaded.toString() + ', isLazyLoading = ' + test.isLazyLoading.toString(), function() {
  274. view.reopen({
  275. isLazyLoading: test.isLazyLoading,
  276. isLoaded: test.isLoaded
  277. });
  278. view.click();
  279. if(test.e) {
  280. expect(lazyloading.run.calledOnce).to.equal(true);
  281. }
  282. else {
  283. expect(lazyloading.run.called).to.equal(false);
  284. }
  285. });
  286. });
  287. it('check lazyLoading parameters', function() {
  288. view.reopen({
  289. isLoaded: false,
  290. isLazyLoading: true,
  291. content: [{host_name: 'host1'}, {host_name: 'host2'}]
  292. });
  293. var availableHosts = d3.range(1, 100).map(function(i) {return {host_name: 'host' + i.toString()};});
  294. sinon.stub(view, 'getAvailableHosts', function() {return availableHosts;});
  295. view.click();
  296. expect(lazyloading.run.args[0][0].source.length).to.equal(97); // 99-2
  297. view.getAvailableHosts.restore();
  298. });
  299. });
  300. });
  301. describe('App.RemoveControlView', function() {
  302. beforeEach(function() {
  303. view = App.RemoveControlView.create({
  304. controller: App.WizardStep5Controller.create({})
  305. });
  306. });
  307. describe('#click', function() {
  308. beforeEach(function() {
  309. sinon.stub(view.get('controller'), 'removeComponent', Em.K);
  310. });
  311. afterEach(function() {
  312. view.get('controller').removeComponent.restore();
  313. });
  314. it('should call removeComponent', function() {
  315. view.set('zId', 1);
  316. view.set('componentName', 'c1');
  317. view.click();
  318. expect(view.get('controller').removeComponent.calledWith('c1', 1)).to.equal(true);
  319. });
  320. });
  321. });
  322. describe('App.AddControlView', function() {
  323. beforeEach(function() {
  324. view = App.AddControlView.create({
  325. controller: App.WizardStep5Controller.create({})
  326. });
  327. });
  328. describe('#click', function() {
  329. beforeEach(function() {
  330. sinon.stub(view.get('controller'), 'addComponent', Em.K);
  331. });
  332. afterEach(function() {
  333. view.get('controller').addComponent.restore();
  334. });
  335. it('should call addComponent', function() {
  336. view.set('componentName', 'c1');
  337. view.click();
  338. expect(view.get('controller').addComponent.calledWith('c1')).to.equal(true);
  339. });
  340. });
  341. });