host_test.js 13 KB

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