host_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 validator = require('utils/validator');
  20. require('utils/component');
  21. require('utils/batch_scheduled_requests');
  22. require('controllers/main/host');
  23. describe('MainHostController', function () {
  24. var hostController;
  25. describe('#bulkOperation', function() {
  26. beforeEach(function() {
  27. hostController = App.MainHostController.create({
  28. bulkOperationForHostsRestart: function(){},
  29. bulkOperationForHosts: function(){},
  30. bulkOperationForHostComponentsRestart: function(){},
  31. bulkOperationForHostComponentsDecommission: function(){},
  32. bulkOperationForHostComponents: function(){},
  33. bulkOperationForHostComponentsPassiveState: function(){},
  34. bulkOperationForHostsPassiveState: function(){}
  35. });
  36. sinon.spy(hostController, 'bulkOperationForHostsRestart');
  37. sinon.spy(hostController, 'bulkOperationForHosts');
  38. sinon.spy(hostController, 'bulkOperationForHostComponentsRestart');
  39. sinon.spy(hostController, 'bulkOperationForHostComponentsDecommission');
  40. sinon.spy(hostController, 'bulkOperationForHostComponents');
  41. sinon.spy(hostController, 'bulkOperationForHostComponentsPassiveState');
  42. sinon.spy(hostController, 'bulkOperationForHostsPassiveState');
  43. });
  44. afterEach(function() {
  45. hostController.bulkOperationForHosts.restore();
  46. hostController.bulkOperationForHostsRestart.restore();
  47. hostController.bulkOperationForHostComponentsRestart.restore();
  48. hostController.bulkOperationForHostComponentsDecommission.restore();
  49. hostController.bulkOperationForHostComponents.restore();
  50. hostController.bulkOperationForHostComponentsPassiveState.restore();
  51. hostController.bulkOperationForHostsPassiveState.restore();
  52. });
  53. it('RESTART for hosts', function() {
  54. var operationData = {
  55. action: 'RESTART'
  56. };
  57. hostController.bulkOperation(operationData, []);
  58. expect(hostController.bulkOperationForHostsRestart.calledOnce).to.equal(true);
  59. });
  60. it('START for hosts', function() {
  61. var operationData = {
  62. action: 'STARTED'
  63. };
  64. hostController.bulkOperation(operationData, []);
  65. expect(hostController.bulkOperationForHosts.calledOnce).to.equal(true);
  66. });
  67. it('STOP for hosts', function() {
  68. var operationData = {
  69. action: 'INSTALLED'
  70. };
  71. hostController.bulkOperation(operationData, []);
  72. expect(hostController.bulkOperationForHosts.calledOnce).to.equal(true);
  73. });
  74. it('PASSIVE_STATE for hosts', function() {
  75. var operationData = {
  76. action: 'PASSIVE_STATE'
  77. };
  78. hostController.bulkOperation(operationData, []);
  79. expect(hostController.bulkOperationForHostsPassiveState.calledOnce).to.equal(true);
  80. });
  81. it('RESTART for hostComponents', function() {
  82. var operationData = {
  83. action: 'RESTART',
  84. componentNameFormatted: 'DataNodes'
  85. };
  86. hostController.bulkOperation(operationData, []);
  87. expect(hostController.bulkOperationForHostComponentsRestart.calledOnce).to.equal(true);
  88. });
  89. it('START for hostComponents', function() {
  90. var operationData = {
  91. action: 'STARTED',
  92. componentNameFormatted: 'DataNodes'
  93. };
  94. hostController.bulkOperation(operationData, []);
  95. expect(hostController.bulkOperationForHostComponents.calledOnce).to.equal(true);
  96. });
  97. it('STOP for hostComponents', function() {
  98. var operationData = {
  99. action: 'INSTALLED',
  100. componentNameFormatted: 'DataNodes'
  101. };
  102. hostController.bulkOperation(operationData, []);
  103. expect(hostController.bulkOperationForHostComponents.calledOnce).to.equal(true);
  104. });
  105. it('DECOMMISSION for hostComponents', function() {
  106. var operationData = {
  107. action: 'DECOMMISSION',
  108. componentNameFormatted: 'DataNodes'
  109. };
  110. hostController.bulkOperation(operationData, []);
  111. expect(hostController.bulkOperationForHostComponentsDecommission.calledOnce).to.equal(true);
  112. });
  113. it('RECOMMISSION for hostComponents', function() {
  114. var operationData = {
  115. action: 'DECOMMISSION_OFF',
  116. componentNameFormatted: 'DataNodes'
  117. };
  118. hostController.bulkOperation(operationData, []);
  119. expect(hostController.bulkOperationForHostComponentsDecommission.calledOnce).to.equal(true);
  120. });
  121. it('PASSIVE_STATE for hostComponents', function() {
  122. var operationData = {
  123. action: 'PASSIVE_STATE',
  124. componentNameFormatted: 'DataNodes'
  125. };
  126. hostController.bulkOperation(operationData, []);
  127. expect(hostController.bulkOperationForHostComponentsPassiveState.calledOnce).to.equal(true);
  128. });
  129. });
  130. describe('#bulkOperationForHosts', function() {
  131. beforeEach(function(){
  132. hostController = App.MainHostController.create({});
  133. sinon.spy($, 'ajax');
  134. });
  135. afterEach(function() {
  136. $.ajax.restore();
  137. });
  138. var tests = [
  139. {
  140. operationData: {},
  141. hosts: [],
  142. m: 'no hosts',
  143. e: false
  144. },
  145. {
  146. operationData: {
  147. actionToCheck: 'STARTED'
  148. },
  149. hosts: [
  150. Em.Object.create({
  151. hostComponents: Em.A([
  152. Em.Object.create({isMaster: true, isSlave: false, host: {hostName:'host1'}, workStatus: 'STARTED', componentName: 'NAMENODE'}),
  153. Em.Object.create({isMaster: false, isSlave: true, host: {hostName:'host1'}, workStatus: 'STARTED', componentName: 'DATANODE'})
  154. ])
  155. })
  156. ],
  157. m: '1 host. components are in proper state',
  158. e: true
  159. },
  160. {
  161. operationData: {
  162. actionToCheck: 'INSTALLED'
  163. },
  164. hosts: [
  165. Em.Object.create({
  166. hostComponents: Em.A([
  167. Em.Object.create({isMaster: true, isSlave: false, host: {hostName:'host1'}, workStatus: 'STARTED', componentName: 'NAMENODE'}),
  168. Em.Object.create({isMaster: false, isSlave: true, host: {hostName:'host1'}, workStatus: 'STARTED', componentName: 'DATANODE'})
  169. ])
  170. })
  171. ],
  172. m: '1 host. components are not in proper state',
  173. e: false
  174. },
  175. {
  176. operationData: {
  177. actionToCheck: 'INSTALLED'
  178. },
  179. hosts: [
  180. Em.Object.create({
  181. hostComponents: Em.A([
  182. Em.Object.create({isMaster: true, isSlave: false, host: {hostName:'host1'}, workStatus: 'INSTALLED', componentName: 'NAMENODE'}),
  183. Em.Object.create({isMaster: false, isSlave: true, host: {hostName:'host1'}, workStatus: 'STARTED', componentName: 'DATANODE'})
  184. ])
  185. })
  186. ],
  187. m: '1 host. some components are in proper state',
  188. e: true
  189. }
  190. ];
  191. tests.forEach(function(test) {
  192. it(test.m, function() {
  193. hostController.bulkOperationForHosts(test.operationData, test.hosts);
  194. expect($.ajax.called).to.equal(test.e);
  195. });
  196. });
  197. });
  198. describe('#bulkOperationForHostsRestart', function() {
  199. beforeEach(function(){
  200. hostController = App.MainHostController.create({});
  201. sinon.spy($, 'ajax');
  202. });
  203. afterEach(function() {
  204. $.ajax.restore();
  205. });
  206. var tests = [
  207. {
  208. hosts: Em.A([]),
  209. m: 'No hosts',
  210. e: false
  211. },
  212. {
  213. hosts: Em.A([
  214. Em.Object.create({
  215. hostComponents: Em.A([Em.Object.create({}), Em.Object.create({})])
  216. })
  217. ]),
  218. m: 'One host',
  219. e: true
  220. }
  221. ];
  222. tests.forEach(function(test) {
  223. it(test.m, function() {
  224. hostController.bulkOperationForHostsRestart({}, test.hosts);
  225. expect($.ajax.calledOnce).to.equal(test.e)
  226. });
  227. });
  228. });
  229. describe('#bulkOperationForHostsPassiveState', function() {
  230. beforeEach(function(){
  231. hostController = App.MainHostController.create({});
  232. sinon.spy($, 'ajax');
  233. });
  234. afterEach(function() {
  235. $.ajax.restore();
  236. });
  237. var tests = [
  238. {
  239. hosts: Em.A([]),
  240. operationData: {},
  241. m: 'No hosts',
  242. e: false
  243. },
  244. {
  245. hosts: Em.A([
  246. Em.Object.create({
  247. passiveState: 'ACTIVE'
  248. })
  249. ]),
  250. operationData: {
  251. state: 'ACTIVE'
  252. },
  253. m: 'One host, but in state that should get',
  254. e: false
  255. },
  256. {
  257. hosts: Em.A([
  258. Em.Object.create({
  259. passiveState: 'ACTIVE'
  260. })
  261. ]),
  262. operationData: {
  263. state: 'PASSIVE'
  264. },
  265. m: 'One host with proper state',
  266. e: true
  267. }
  268. ];
  269. tests.forEach(function(test) {
  270. it(test.m, function() {
  271. hostController.bulkOperationForHostsPassiveState(test.operationData, test.hosts);
  272. expect($.ajax.calledOnce).to.equal(test.e)
  273. });
  274. });
  275. });
  276. describe('#bulkOperationForHostComponentsPassiveState', function() {
  277. beforeEach(function(){
  278. hostController = App.MainHostController.create({});
  279. sinon.spy($, 'ajax');
  280. });
  281. afterEach(function() {
  282. $.ajax.restore();
  283. });
  284. var tests = [
  285. {
  286. hosts: Em.A([]),
  287. operationData: {},
  288. m: 'No hosts',
  289. e: false
  290. },
  291. {
  292. hosts: Em.A([
  293. Em.Object.create({
  294. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'ACTIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  295. }),
  296. Em.Object.create({
  297. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'ACTIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  298. })
  299. ]),
  300. operationData: {
  301. componentName: 'CN',
  302. state: 'ACTIVE'
  303. },
  304. m: 'Two hosts with components in state that they should get',
  305. e: false
  306. },
  307. {
  308. hosts: Em.A([
  309. Em.Object.create({
  310. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'ACTIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  311. }),
  312. Em.Object.create({
  313. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'PASSIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  314. })
  315. ]),
  316. operationData: {
  317. componentName: 'CN',
  318. state: 'PASSIVE'
  319. },
  320. m: 'One host with component in proper state (ACTIVE)',
  321. e: true
  322. },
  323. {
  324. hosts: Em.A([
  325. Em.Object.create({
  326. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'ACTIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  327. }),
  328. Em.Object.create({
  329. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'PASSIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  330. })
  331. ]),
  332. operationData: {
  333. componentName: 'CN',
  334. state: 'PASSIVE'
  335. },
  336. m: 'One host with component in proper state (PASSIVE)',
  337. e: true
  338. },
  339. {
  340. hosts: Em.A([
  341. Em.Object.create({
  342. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'ACTIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  343. }),
  344. Em.Object.create({
  345. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'IMPLIED'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  346. })
  347. ]),
  348. operationData: {
  349. componentName: 'CN',
  350. state: 'PASSIVE'
  351. },
  352. m: 'One host with component in proper state (ACTIVE)',
  353. e: true
  354. },
  355. {
  356. hosts: Em.A([
  357. Em.Object.create({
  358. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'ACTIVE'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  359. }),
  360. Em.Object.create({
  361. hostComponents: Em.A([Em.Object.create({componentName:'CN',passiveState:'IMPLIED'}), Em.Object.create({componentName:'ACN',passiveState:'ACTIVE'})])
  362. })
  363. ]),
  364. operationData: {
  365. componentName: 'CN',
  366. state: 'ACTIVE'
  367. },
  368. m: 'One host with component in proper state (PASSIVE)',
  369. e: true
  370. }
  371. ];
  372. tests.forEach(function(test) {
  373. it(test.m, function() {
  374. hostController.bulkOperationForHostComponentsPassiveState(test.operationData, test.hosts);
  375. expect($.ajax.calledOnce).to.equal(test.e)
  376. });
  377. });
  378. });
  379. });