step2_controller_test.js 10 KB

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