step2_controller_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/main/service/reassign/step2_controller');
  20. require('models/host_component');
  21. describe('App.ReassignMasterWizardStep2Controller', function () {
  22. var controller = App.ReassignMasterWizardStep2Controller.create({
  23. content: Em.Object.create({
  24. reassign: Em.Object.create({}),
  25. services: []
  26. }),
  27. renderComponents: Em.K,
  28. multipleComponents: []
  29. });
  30. controller.set('_super', Em.K);
  31. describe('#loadStep', function () {
  32. beforeEach(function () {
  33. sinon.stub(App.router, 'send', Em.K);
  34. sinon.stub(controller, 'clearStep', Em.K);
  35. sinon.stub(controller, 'loadComponents', Em.K);
  36. sinon.stub(controller, 'loadStepCallback', Em.K);
  37. sinon.stub(controller, 'rebalanceSingleComponentHosts', Em.K);
  38. });
  39. afterEach(function () {
  40. App.router.send.restore();
  41. controller.clearStep.restore();
  42. controller.loadStepCallback.restore();
  43. controller.loadComponents.restore();
  44. controller.rebalanceSingleComponentHosts.restore();
  45. });
  46. it('SECONDARY_NAMENODE is absent, reassign component is NAMENODE', function () {
  47. sinon.stub(App, 'get', function (k) {
  48. if (k === 'isHaEnabled') return true;
  49. return Em.get(App, k);
  50. });
  51. controller.set('content.reassign.component_name', 'NAMENODE');
  52. controller.set('content.masterComponentHosts', []);
  53. controller.loadStep();
  54. expect(controller.get('showCurrentHost')).to.be.false;
  55. expect(controller.get('componentToRebalance')).to.equal('NAMENODE');
  56. expect(controller.get('rebalanceComponentHostsCounter')).to.equal(1);
  57. App.get.restore();
  58. });
  59. it('SECONDARY_NAMENODE is present, reassign component is NAMENODE', function () {
  60. sinon.stub(App, 'get', function (k) {
  61. if (k === 'isHaEnabled') return false;
  62. return Em.get(App, k);
  63. });
  64. controller.set('content.reassign.component_name', 'NAMENODE');
  65. controller.set('content.masterComponentHosts', [
  66. {
  67. component: 'SECONDARY_NAMENODE'
  68. }
  69. ]);
  70. controller.loadStep();
  71. expect(controller.get('showCurrentHost')).to.be.true;
  72. expect(controller.rebalanceSingleComponentHosts.calledWith('NAMENODE'));
  73. App.get.restore();
  74. });
  75. it('SECONDARY_NAMENODE is absent, reassign component is not NAMENODE', function () {
  76. controller.set('content.reassign.component_name', 'COMP');
  77. controller.set('content.masterComponentHosts', []);
  78. controller.loadStep();
  79. expect(controller.get('showCurrentHost')).to.be.true;
  80. expect(controller.rebalanceSingleComponentHosts.calledWith('COMP'));
  81. });
  82. it('if HA is enabled then multipleComponents should contain NAMENODE', function () {
  83. controller.get('multipleComponents').clear();
  84. sinon.stub(App, 'get', function (k) {
  85. if (k === 'isHaEnabled') return true;
  86. return Em.get(App, k);
  87. });
  88. controller.loadStep();
  89. expect(controller.get('multipleComponents')).to.contain('NAMENODE');
  90. expect(controller.get('multipleComponents')).to.have.length(1);
  91. App.get.restore();
  92. });
  93. });
  94. describe('#loadComponents', function () {
  95. it('masterComponentHosts is empty', function () {
  96. controller.set('content.masterComponentHosts', []);
  97. controller.set('content.reassign.host_id', 1);
  98. expect(controller.loadComponents()).to.be.empty;
  99. expect(controller.get('currentHostId')).to.equal(1);
  100. });
  101. it('masterComponentHosts does not contain reassign component', function () {
  102. sinon.stub(App.HostComponent, 'find', function () {
  103. return [Em.Object.create({
  104. componentName: 'COMP1',
  105. serviceName: 'SERVICE'
  106. })];
  107. });
  108. controller.set('content.masterComponentHosts', [{
  109. component: 'COMP1',
  110. hostName: 'host1'
  111. }]);
  112. controller.set('content.reassign.host_id', 1);
  113. controller.set('content.reassign.component_name', 'COMP2');
  114. expect(controller.loadComponents()).to.eql([
  115. {
  116. "component_name": "COMP1",
  117. "display_name": "Comp1",
  118. "selectedHost": "host1",
  119. "isInstalled": true,
  120. "serviceId": "SERVICE",
  121. "isServiceCoHost": false,
  122. "color": "grey"
  123. }
  124. ]);
  125. expect(controller.get('currentHostId')).to.equal(1);
  126. App.HostComponent.find.restore();
  127. });
  128. it('masterComponentHosts contains reassign component', function () {
  129. sinon.stub(App.HostComponent, 'find', function () {
  130. return [Em.Object.create({
  131. componentName: 'COMP1',
  132. serviceName: 'SERVICE'
  133. })];
  134. });
  135. controller.set('content.masterComponentHosts', [{
  136. component: 'COMP1',
  137. hostName: 'host1'
  138. }]);
  139. controller.set('content.reassign.host_id', 1);
  140. controller.set('content.reassign.component_name', 'COMP1');
  141. expect(controller.loadComponents()).to.eql([
  142. {
  143. "component_name": "COMP1",
  144. "display_name": "Comp1",
  145. "selectedHost": "host1",
  146. "isInstalled": true,
  147. "serviceId": "SERVICE",
  148. "isServiceCoHost": false,
  149. "color": "green"
  150. }
  151. ]);
  152. expect(controller.get('currentHostId')).to.equal(1);
  153. App.HostComponent.find.restore();
  154. });
  155. });
  156. describe('#rebalanceSingleComponentHosts', function () {
  157. it('hosts is empty', function () {
  158. controller.set('hosts', []);
  159. expect(controller.rebalanceSingleComponentHosts()).to.be.false;
  160. });
  161. it('currentHostId matches one available host', function () {
  162. controller.set('hosts', [Em.Object.create({
  163. host_name: 'host1'
  164. })]);
  165. controller.set('currentHostId', 'host1');
  166. expect(controller.rebalanceSingleComponentHosts()).to.be.false;
  167. });
  168. var testCases = [
  169. {
  170. title: 'selectedHost = currentHostId and component_name = content.reassign.component_name',
  171. arguments: {
  172. selectedHost: 'host1',
  173. reassignComponentName: 'COMP1'
  174. },
  175. result: 'host3'
  176. },
  177. {
  178. title: 'selectedHost not equal to currentHostId and component_name = content.reassign.component_name',
  179. arguments: {
  180. selectedHost: 'host2',
  181. reassignComponentName: 'COMP1'
  182. },
  183. result: 'host2'
  184. },
  185. {
  186. title: 'selectedHost = currentHostId and component_name not equal to content.reassign.component_name',
  187. arguments: {
  188. selectedHost: 'host1',
  189. reassignComponentName: 'COMP2'
  190. },
  191. result: 'host1'
  192. }
  193. ];
  194. testCases.forEach(function (test) {
  195. it(test.title, function () {
  196. controller.set('hosts', [
  197. Em.Object.create({
  198. host_name: 'host3'
  199. }),
  200. Em.Object.create({
  201. host_name: 'host2'
  202. })
  203. ]);
  204. controller.set('currentHostId', 'host1');
  205. controller.set('content.reassign.component_name', test.arguments.reassignComponentName);
  206. controller.set('selectedServicesMasters', [Em.Object.create({
  207. component_name: 'COMP1',
  208. selectedHost: test.arguments.selectedHost
  209. })]);
  210. expect(controller.rebalanceSingleComponentHosts('COMP1')).to.be.true;
  211. expect(controller.get('selectedServicesMasters')[0].get('selectedHost')).to.equal(test.result);
  212. expect(controller.get('selectedServicesMasters')[0].get('availableHosts').mapProperty('host_name')).to.eql(['host2', 'host3']);
  213. });
  214. });
  215. });
  216. describe('#getIsSubmitDisabled', function () {
  217. var hostComponents = [];
  218. var isSubmitDisabled = false;
  219. beforeEach(function () {
  220. sinon.stub(App.HostComponent, 'find', function () {
  221. return hostComponents;
  222. });
  223. sinon.stub(controller, '_super', function() {
  224. return isSubmitDisabled;
  225. });
  226. });
  227. afterEach(function () {
  228. App.HostComponent.find.restore();
  229. controller._super.restore();
  230. });
  231. it('No host-components, reassigned equal 0', function () {
  232. expect(controller.getIsSubmitDisabled()).to.be.true;
  233. expect(controller.get('submitDisabled')).to.be.true;
  234. });
  235. it('Reassign component match existed components, reassigned equal 0', function () {
  236. controller.set('content.reassign.component_name', 'COMP1');
  237. hostComponents = [Em.Object.create({
  238. componentName: 'COMP1',
  239. hostName: 'host1'
  240. })];
  241. controller.set('servicesMasters', [{
  242. selectedHost: 'host1'
  243. }]);
  244. expect(controller.getIsSubmitDisabled()).to.be.true;
  245. expect(controller.get('submitDisabled')).to.be.true;
  246. });
  247. it('Reassign component do not match existed components, reassigned equal 1', function () {
  248. controller.set('content.reassign.component_name', 'COMP1');
  249. hostComponents = [Em.Object.create({
  250. componentName: 'COMP1',
  251. hostName: 'host1'
  252. })];
  253. controller.set('servicesMasters', []);
  254. expect(controller.getIsSubmitDisabled()).to.be.false;
  255. expect(controller.get('submitDisabled')).to.be.false;
  256. });
  257. it('Reassign component do not match existed components, reassigned equal 2', function () {
  258. controller.set('content.reassign.component_name', 'COMP1');
  259. hostComponents = [
  260. Em.Object.create({
  261. componentName: 'COMP1',
  262. hostName: 'host1'
  263. }),
  264. Em.Object.create({
  265. componentName: 'COMP1',
  266. hostName: 'host2'
  267. })
  268. ];
  269. controller.set('servicesMasters', []);
  270. expect(controller.getIsSubmitDisabled()).to.be.true;
  271. expect(controller.get('submitDisabled')).to.be.true;
  272. });
  273. it('submitDisabled is already true', function () {
  274. isSubmitDisabled = true;
  275. expect(controller.getIsSubmitDisabled()).to.be.true;
  276. expect(controller.get('submitDisabled')).to.be.true;
  277. });
  278. });
  279. });