host_progress_popup_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 Ember = require('ember');
  19. var App = require('app');
  20. require('controllers/global/background_operations_controller');
  21. require('views/common/modal_popup');
  22. require('utils/helper');
  23. require('utils/host_progress_popup');
  24. describe('App.HostPopup', function () {
  25. var testTasks = [
  26. {
  27. t: [
  28. {
  29. Tasks: {
  30. status: 'COMPLETED',
  31. id: 2
  32. }
  33. },
  34. {
  35. Tasks: {
  36. status: 'COMPLETED',
  37. id: 3
  38. }
  39. },
  40. {
  41. Tasks: {
  42. status: 'COMPLETED',
  43. id: 1
  44. }
  45. }
  46. ],
  47. m: 'All COMPLETED',
  48. r: 'SUCCESS',
  49. p: 100,
  50. ids: [1,2,3]
  51. },
  52. {
  53. t: [
  54. {
  55. Tasks: {
  56. status: 'FAILED',
  57. id: 2
  58. }
  59. },
  60. {
  61. Tasks: {
  62. status: 'COMPLETED',
  63. id: 1
  64. }
  65. }
  66. ,
  67. {
  68. Tasks: {
  69. status: 'COMPLETED',
  70. id: 3
  71. }
  72. }
  73. ],
  74. m: 'One FAILED',
  75. r: 'FAILED',
  76. p: 100,
  77. ids: [1,2,3]
  78. },
  79. {
  80. t: [
  81. {
  82. Tasks: {
  83. status: 'ABORTED',
  84. id: 1
  85. }
  86. },
  87. {
  88. Tasks: {
  89. status: 'COMPLETED',
  90. id: 2
  91. }
  92. }
  93. ],
  94. m: 'One ABORTED',
  95. r: 'ABORTED',
  96. p: 100,
  97. ids: [1,2]
  98. },
  99. {
  100. t: [
  101. {
  102. Tasks: {
  103. status: 'TIMEDOUT',
  104. id: 3
  105. }
  106. },
  107. {
  108. Tasks: {
  109. status: 'COMPLETED',
  110. id: 1
  111. }
  112. }
  113. ],
  114. m: 'One TIMEDOUT',
  115. r: 'TIMEDOUT',
  116. p: 100,
  117. ids: [1,3]
  118. },
  119. {
  120. t: [
  121. {
  122. Tasks: {
  123. status: 'IN_PROGRESS',
  124. id: 1
  125. }
  126. },
  127. {
  128. Tasks: {
  129. status: 'COMPLETED',
  130. id: 2
  131. }
  132. }
  133. ],
  134. m: 'One IN_PROGRESS',
  135. r: 'IN_PROGRESS',
  136. p: 68,
  137. ids: [1,2]
  138. },
  139. {
  140. t: [
  141. {
  142. Tasks: {
  143. status: 'QUEUED',
  144. id: 2
  145. }
  146. },
  147. {
  148. Tasks: {
  149. status: 'COMPLETED',
  150. id: 3
  151. }
  152. }
  153. ],
  154. m: 'Something else',
  155. r: 'PENDING',
  156. p: 55,
  157. ids: [2,3]
  158. }
  159. ];
  160. var statusCases = [
  161. {
  162. status: 'FAILED',
  163. result: false
  164. },
  165. {
  166. status: 'ABORTED',
  167. result: false
  168. },
  169. {
  170. status: 'TIMEDOUT',
  171. result: false
  172. },
  173. {
  174. status: 'IN_PROGRESS',
  175. result: true
  176. },
  177. {
  178. status: 'COMPLETED',
  179. result: false
  180. },
  181. {
  182. status: 'PENDING',
  183. result: true
  184. }
  185. ];
  186. describe('#setSelectCount', function () {
  187. var itemsForStatusTest = [
  188. {
  189. title: 'Empty',
  190. data: [],
  191. result: [0, 0, 0, 0, 0, 0, 0]
  192. },
  193. {
  194. title: 'All Pending',
  195. data: [
  196. {status: 'pending'},
  197. {status: 'queued'}
  198. ],
  199. result: [2, 2, 0, 0, 0, 0, 0]
  200. },
  201. {
  202. title: 'All Completed',
  203. data: [
  204. {status: 'success'},
  205. {status: 'completed'}
  206. ],
  207. result: [2, 0, 0, 0, 2, 0, 0]
  208. },
  209. {
  210. title: 'All Failed',
  211. data: [
  212. {status: 'failed'},
  213. {status: 'failed'}
  214. ],
  215. result: [2, 0, 0, 2, 0, 0, 0]
  216. },
  217. {
  218. title: 'All InProgress',
  219. data: [
  220. {status: 'in_progress'},
  221. {status: 'in_progress'}
  222. ],
  223. result: [2, 0, 2, 0, 0, 0, 0]
  224. },
  225. {
  226. title: 'All Aborted',
  227. data: [
  228. {status: 'aborted'},
  229. {status: 'aborted'}
  230. ],
  231. result: [2, 0, 0, 0, 0, 2, 0]
  232. },
  233. {
  234. title: 'All Timedout',
  235. data: [
  236. {status: 'timedout'},
  237. {status: 'timedout'}
  238. ],
  239. result: [2, 0, 0, 0, 0, 0, 2]
  240. },
  241. {
  242. title: 'Every Category',
  243. data: [
  244. {status: 'pending'},
  245. {status: 'queued'},
  246. {status: 'success'},
  247. {status: 'completed'},
  248. {status: 'failed'},
  249. {status: 'in_progress'},
  250. {status: 'aborted'},
  251. {status: 'timedout'}
  252. ],
  253. result: [8, 2, 1, 1, 2, 1, 1]
  254. }
  255. ];
  256. var categories = [
  257. Ember.Object.create({value: 'all'}),
  258. Ember.Object.create({value: 'pending'}),
  259. Ember.Object.create({value: 'in_progress'}),
  260. Ember.Object.create({value: 'failed'}),
  261. Ember.Object.create({value: 'completed'}),
  262. Ember.Object.create({value: 'aborted'}),
  263. Ember.Object.create({value: 'timedout'})
  264. ];
  265. itemsForStatusTest.forEach(function(statusTest) {
  266. it(statusTest.title, function() {
  267. App.HostPopup.setSelectCount(statusTest.data, categories);
  268. expect(categories.mapProperty('count')).to.deep.equal(statusTest.result);
  269. });
  270. });
  271. });
  272. describe('#getStatus', function() {
  273. testTasks.forEach(function(testTask) {
  274. it(testTask.m, function() {
  275. expect(App.HostPopup.getStatus(testTask.t)[0]).to.equal(testTask.r);
  276. });
  277. });
  278. });
  279. describe('#getProgress', function() {
  280. testTasks.forEach(function(testTask) {
  281. it(testTask.m, function() {
  282. expect(App.HostPopup.getProgress(testTask.t)).to.equal(testTask.p);
  283. });
  284. });
  285. });
  286. describe('#isAbortableByStatus', function () {
  287. statusCases.forEach(function (item) {
  288. it('should return ' + item.result + ' for ' + item.status, function () {
  289. expect(App.HostPopup.isAbortableByStatus(item.status)).to.equal(item.result);
  290. });
  291. });
  292. });
  293. describe('#abortRequest', function () {
  294. beforeEach(function () {
  295. sinon.spy(App, 'showConfirmationPopup');
  296. });
  297. afterEach(function () {
  298. App.showConfirmationPopup.restore();
  299. });
  300. it('should show confirmation popup', function () {
  301. App.HostPopup.abortRequest(Em.Object.create({
  302. name: 'name'
  303. }));
  304. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  305. });
  306. });
  307. describe('#abortRequestSuccessCallback', function () {
  308. beforeEach(function () {
  309. sinon.spy(App.ModalPopup, 'show');
  310. });
  311. afterEach(function () {
  312. App.ModalPopup.show.restore();
  313. });
  314. it('should open popup', function () {
  315. App.HostPopup.abortRequestSuccessCallback(null, null, {
  316. requestName: 'name',
  317. serviceInfo: Em.Object.create()
  318. });
  319. expect(App.ModalPopup.show.calledOnce).to.be.true;
  320. });
  321. });
  322. describe('#abortRequestErrorCallback', function () {
  323. var popup = App.HostPopup;
  324. beforeEach(function () {
  325. sinon.stub(App.ajax, 'get', function(k) {
  326. if (k === 'modalPopup') return null;
  327. return Em.get(App, k);
  328. });
  329. sinon.spy(App.ModalPopup, 'show');
  330. });
  331. afterEach(function () {
  332. App.ModalPopup.show.restore();
  333. App.ajax.get.restore();
  334. });
  335. it('should open popup', function () {
  336. popup.abortRequestErrorCallback({
  337. responseText: {
  338. message: 'message'
  339. },
  340. status: 404
  341. }, 'status', 'error', {
  342. url: 'url'
  343. }, {
  344. requestId: 0,
  345. serviceInfo: Em.Object.create()
  346. });
  347. expect(App.ModalPopup.show.calledOnce).to.be.true;
  348. });
  349. statusCases.forEach(function (item) {
  350. it('should set serviceInfo.isAbortable to' + item.result + ' if status is ' + item.status, function () {
  351. popup.abortRequestErrorCallback({
  352. responseText: {
  353. message: 'message'
  354. },
  355. status: 404
  356. }, 'status', 'error', {
  357. url: 'url'
  358. }, {
  359. requestId: 0,
  360. serviceInfo: Em.Object.create({
  361. status: item.status
  362. })
  363. });
  364. expect(App.HostPopup.isAbortableByStatus(item.status)).to.equal(item.result);
  365. });
  366. });
  367. });
  368. describe('#setBackgroundOperationHeader', function(){
  369. beforeEach(function (){
  370. sinon.stub(App.HostPopup, "get").returns(true);
  371. sinon.spy(App.HostPopup, "set");
  372. this.stub = sinon.stub(App.router, "get");
  373. });
  374. afterEach(function (){
  375. App.HostPopup.get.restore();
  376. App.HostPopup.set.restore();
  377. App.router.get.restore();
  378. });
  379. it("should display '2 Background Operations Running' when there are 2 background operations running", function(){
  380. this.stub.returns(2);
  381. App.HostPopup.setBackgroundOperationHeader(false);
  382. expect(App.HostPopup.set.calledWith("popupHeaderName", "2 Background Operations Running")).to.be.true;
  383. });
  384. it("should display '1 Background Operation Running' when there is 1 background operation running", function(){
  385. this.stub.returns(1);
  386. App.HostPopup.setBackgroundOperationHeader(false);
  387. expect(App.HostPopup.set.calledWith("popupHeaderName", "1 Background Operation Running")).to.be.true;
  388. });
  389. });
  390. describe('#_getHostsMap', function () {
  391. Em.A([
  392. {
  393. inputData: [
  394. {name: 's1', hostsMap: {h1: {}, h2: {}}},
  395. {name: 's2'}
  396. ],
  397. isBackgroundOperations: true,
  398. currentServiceId: null,
  399. serviceName: 's1',
  400. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` true, `hostsMap` exists',
  401. e: {h1: {}, h2: {}}
  402. },
  403. {
  404. inputData: [
  405. {name: 's1', hosts: [
  406. {name: 'h1'},
  407. {name: 'h2'}
  408. ]},
  409. {name: 's2'}
  410. ],
  411. isBackgroundOperations: true,
  412. currentServiceId: null,
  413. serviceName: 's1',
  414. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` true, `hosts` exists',
  415. e: {h1: {name: 'h1'}, h2: {name: 'h2'}}
  416. },
  417. {
  418. inputData: [
  419. {id: 1, hostsMap: {h1: {}, h2: {}}},
  420. {id: 2}
  421. ],
  422. isBackgroundOperations: true,
  423. currentServiceId: 1,
  424. serviceName: 's1',
  425. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` true, `hostsMap` exists',
  426. e: {h1: {}, h2: {}}
  427. },
  428. {
  429. inputData: [
  430. {id: 1, hosts: [
  431. {name: 'h1'},
  432. {name: 'h2'}
  433. ]},
  434. {id: 2}
  435. ],
  436. isBackgroundOperations: true,
  437. currentServiceId: 1,
  438. serviceName: 's1',
  439. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` true, `hosts` exists',
  440. e: {h1: {name: 'h1'}, h2: {name: 'h2'}}
  441. },
  442. {
  443. inputData: [
  444. {name: 's1', hostsMap: {h1: {}, h2: {}}},
  445. {name: 's2'}
  446. ],
  447. isBackgroundOperations: false,
  448. currentServiceId: null,
  449. serviceName: 's1',
  450. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` false, `hostsMap` exists',
  451. e: {h1: {}, h2: {}}
  452. },
  453. {
  454. inputData: [
  455. {name: 's1', hosts: [
  456. {name: 'h1'},
  457. {name: 'h2'}
  458. ]},
  459. {name: 's2'}
  460. ],
  461. isBackgroundOperations: false,
  462. currentServiceId: null,
  463. serviceName: 's1',
  464. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` false, `hosts` exists',
  465. e: {h1: {name: 'h1'}, h2: {name: 'h2'}}
  466. },
  467. {
  468. inputData: [
  469. {name: 's1', hostsMap: {h1: {}, h2: {}}}
  470. ],
  471. isBackgroundOperations: false,
  472. currentServiceId: 1,
  473. serviceName: 's1',
  474. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` false, `hostsMap` exists',
  475. e: {h1: {}, h2: {}}
  476. },
  477. {
  478. inputData: [
  479. {name: 's1', hostsMap: {h1: {}, h2: {}}}
  480. ],
  481. isBackgroundOperations: false,
  482. currentServiceId: 1,
  483. serviceName: 's1',
  484. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` false, `hosts` exists',
  485. e: {h1: {}, h2: {}}
  486. }
  487. ]).forEach(function (test) {
  488. it(test.m, function () {
  489. App.HostPopup.setProperties(test);
  490. expect(App.HostPopup._getHostsMap()).to.eql(test.e);
  491. });
  492. });
  493. });
  494. });