host_progress_popup_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.stub(App.ajax, 'send', Em.K);
  296. sinon.spy(App, 'showConfirmationPopup');
  297. });
  298. afterEach(function () {
  299. App.ajax.send.restore();
  300. App.showConfirmationPopup.restore();
  301. });
  302. it('should show confirmation popup', function () {
  303. App.HostPopup.abortRequest(Em.Object.create({
  304. name: 'name'
  305. }));
  306. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  307. });
  308. });
  309. describe('#abortRequestSuccessCallback', function () {
  310. beforeEach(function () {
  311. sinon.spy(App.ModalPopup, 'show');
  312. });
  313. afterEach(function () {
  314. App.ModalPopup.show.restore();
  315. });
  316. it('should open popup', function () {
  317. App.HostPopup.abortRequestSuccessCallback(null, null, {
  318. requestName: 'name',
  319. serviceInfo: Em.Object.create()
  320. });
  321. expect(App.ModalPopup.show.calledOnce).to.be.true;
  322. });
  323. });
  324. describe('#abortRequestErrorCallback', function () {
  325. var popup = App.HostPopup;
  326. beforeEach(function () {
  327. sinon.stub(App.ajax, 'get', function(k) {
  328. if (k === 'modalPopup') return null;
  329. return Em.get(App, k);
  330. });
  331. sinon.spy(App.ModalPopup, 'show');
  332. });
  333. afterEach(function () {
  334. App.ModalPopup.show.restore();
  335. App.ajax.get.restore();
  336. });
  337. it('should open popup', function () {
  338. popup.abortRequestErrorCallback({
  339. responseText: {
  340. message: 'message'
  341. },
  342. status: 404
  343. }, 'status', 'error', {
  344. url: 'url'
  345. }, {
  346. requestId: 0,
  347. serviceInfo: Em.Object.create()
  348. });
  349. expect(App.ModalPopup.show.calledOnce).to.be.true;
  350. });
  351. statusCases.forEach(function (item) {
  352. it('should set serviceInfo.isAbortable to' + item.result + ' if status is ' + item.status, function () {
  353. popup.abortRequestErrorCallback({
  354. responseText: {
  355. message: 'message'
  356. },
  357. status: 404
  358. }, 'status', 'error', {
  359. url: 'url'
  360. }, {
  361. requestId: 0,
  362. serviceInfo: Em.Object.create({
  363. status: item.status
  364. })
  365. });
  366. expect(App.HostPopup.isAbortableByStatus(item.status)).to.equal(item.result);
  367. });
  368. });
  369. });
  370. describe('#setBackgroundOperationHeader', function(){
  371. beforeEach(function (){
  372. sinon.stub(App.HostPopup, "get").returns(true);
  373. sinon.spy(App.HostPopup, "set");
  374. this.stub = sinon.stub(App.router, "get");
  375. });
  376. afterEach(function (){
  377. App.HostPopup.get.restore();
  378. App.HostPopup.set.restore();
  379. App.router.get.restore();
  380. });
  381. it("should display '2 Background Operations Running' when there are 2 background operations running", function(){
  382. this.stub.returns(2);
  383. App.HostPopup.setBackgroundOperationHeader(false);
  384. expect(App.HostPopup.set.calledWith("popupHeaderName", "2 Background Operations Running")).to.be.true;
  385. });
  386. it("should display '1 Background Operation Running' when there is 1 background operation running", function(){
  387. this.stub.returns(1);
  388. App.HostPopup.setBackgroundOperationHeader(false);
  389. expect(App.HostPopup.set.calledWith("popupHeaderName", "1 Background Operation Running")).to.be.true;
  390. });
  391. });
  392. describe('#_getHostsMap', function () {
  393. Em.A([
  394. {
  395. inputData: [
  396. {name: 's1', hostsMap: {h1: {}, h2: {}}},
  397. {name: 's2'}
  398. ],
  399. isBackgroundOperations: true,
  400. currentServiceId: null,
  401. serviceName: 's1',
  402. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` true, `hostsMap` exists',
  403. e: {h1: {}, h2: {}}
  404. },
  405. {
  406. inputData: [
  407. {name: 's1', hosts: [
  408. {name: 'h1'},
  409. {name: 'h2'}
  410. ]},
  411. {name: 's2'}
  412. ],
  413. isBackgroundOperations: true,
  414. currentServiceId: null,
  415. serviceName: 's1',
  416. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` true, `hosts` exists',
  417. e: {h1: {name: 'h1'}, h2: {name: 'h2'}}
  418. },
  419. {
  420. inputData: [
  421. {id: 1, hostsMap: {h1: {}, h2: {}}},
  422. {id: 2}
  423. ],
  424. isBackgroundOperations: true,
  425. currentServiceId: 1,
  426. serviceName: 's1',
  427. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` true, `hostsMap` exists',
  428. e: {h1: {}, h2: {}}
  429. },
  430. {
  431. inputData: [
  432. {id: 1, hosts: [
  433. {name: 'h1'},
  434. {name: 'h2'}
  435. ]},
  436. {id: 2}
  437. ],
  438. isBackgroundOperations: true,
  439. currentServiceId: 1,
  440. serviceName: 's1',
  441. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` true, `hosts` exists',
  442. e: {h1: {name: 'h1'}, h2: {name: 'h2'}}
  443. },
  444. {
  445. inputData: [
  446. {name: 's1', hostsMap: {h1: {}, h2: {}}},
  447. {name: 's2'}
  448. ],
  449. isBackgroundOperations: false,
  450. currentServiceId: null,
  451. serviceName: 's1',
  452. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` false, `hostsMap` exists',
  453. e: {h1: {}, h2: {}}
  454. },
  455. {
  456. inputData: [
  457. {name: 's1', hosts: [
  458. {name: 'h1'},
  459. {name: 'h2'}
  460. ]},
  461. {name: 's2'}
  462. ],
  463. isBackgroundOperations: false,
  464. currentServiceId: null,
  465. serviceName: 's1',
  466. m: '`currentServiceId` is null, `serviceName` exists, `isBackgroundOperations` false, `hosts` exists',
  467. e: {h1: {name: 'h1'}, h2: {name: 'h2'}}
  468. },
  469. {
  470. inputData: [
  471. {name: 's1', hostsMap: {h1: {}, h2: {}}}
  472. ],
  473. isBackgroundOperations: false,
  474. currentServiceId: 1,
  475. serviceName: 's1',
  476. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` false, `hostsMap` exists',
  477. e: {h1: {}, h2: {}}
  478. },
  479. {
  480. inputData: [
  481. {name: 's1', hostsMap: {h1: {}, h2: {}}}
  482. ],
  483. isBackgroundOperations: false,
  484. currentServiceId: 1,
  485. serviceName: 's1',
  486. m: '`currentServiceId` is 1, `serviceName` exists, `isBackgroundOperations` false, `hosts` exists',
  487. e: {h1: {}, h2: {}}
  488. }
  489. ]).forEach(function (test) {
  490. it(test.m, function () {
  491. App.HostPopup.setProperties(test);
  492. expect(App.HostPopup._getHostsMap()).to.eql(test.e);
  493. });
  494. });
  495. });
  496. });