progress_popup_controller_test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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/admin/highAvailability/progress_popup_controller');
  20. var testHelpers = require('test/helpers');
  21. describe('App.HighAvailabilityProgressPopupController', function () {
  22. var controller;
  23. beforeEach(function () {
  24. controller = App.HighAvailabilityProgressPopupController.create();
  25. });
  26. after(function () {
  27. controller.destroy();
  28. });
  29. describe('#startTaskPolling', function () {
  30. describe('should start task polling', function () {
  31. beforeEach(function () {
  32. controller.startTaskPolling(1, 2);
  33. });
  34. it('isTaskPolling = true', function () {
  35. expect(controller.get('isTaskPolling')).to.be.true;
  36. });
  37. it('taskInfo.id = 2', function () {
  38. expect(controller.get('taskInfo.id')).to.be.equal(2);
  39. });
  40. it('taskInfo.requestId = 1', function () {
  41. expect(controller.get('taskInfo.requestId')).to.be.equal(1);
  42. });
  43. it('App.updater.run is called once', function () {
  44. expect(App.updater.run.calledOnce).to.be.true;
  45. });
  46. it('App.updater.immediateRun is called once', function () {
  47. expect(App.updater.immediateRun.calledOnce).to.be.true;
  48. });
  49. });
  50. });
  51. describe('#stopTaskPolling', function () {
  52. it('should stop task polling', function () {
  53. controller.stopTaskPolling();
  54. expect(controller.get('isTaskPolling')).to.be.false;
  55. });
  56. });
  57. describe('#updateTask', function () {
  58. it('should send polling request', function () {
  59. controller.updateTask();
  60. var args = testHelpers.findAjaxRequest('name', 'background_operations.get_by_task');
  61. expect(args).to.exists;
  62. });
  63. });
  64. describe('#updateTaskSuccessCallback', function () {
  65. beforeEach(function () {
  66. controller.reopen({
  67. taskInfo: {}
  68. });
  69. });
  70. var cases = [
  71. {
  72. status: 'FAILED',
  73. isTaskPolling: false
  74. },
  75. {
  76. status: 'COMPLETED',
  77. isTaskPolling: false
  78. },
  79. {
  80. status: 'TIMEDOUT',
  81. isTaskPolling: false
  82. },
  83. {
  84. status: 'ABORTED',
  85. isTaskPolling: false
  86. },
  87. {
  88. status: 'QUEUED',
  89. isTaskPolling: true
  90. },
  91. {
  92. status: 'IN_PROGRESS',
  93. isTaskPolling: true
  94. }
  95. ],
  96. tasks = {
  97. stderr: 'error',
  98. stdout: 'output',
  99. output_log: 'output-log.txt',
  100. error_log: 'error-log.txt'
  101. },
  102. title = '{0}polling task if it\'s status is {1}';
  103. cases.forEach(function (item) {
  104. var message = title.format(item.isTaskPolling ? '' : 'not ', item.status);
  105. describe(message, function () {
  106. beforeEach(function () {
  107. controller.updateTaskSuccessCallback({
  108. Tasks: $.extend(tasks, {
  109. status: item.status
  110. })
  111. });
  112. });
  113. it('stderr is valid', function () {
  114. expect(controller.get('taskInfo.stderr')).to.equal('error');
  115. });
  116. it('stdout is valid', function () {
  117. expect(controller.get('taskInfo.stdout')).to.equal('output');
  118. });
  119. it('outputLog is valid', function () {
  120. expect(controller.get('taskInfo.outputLog')).to.equal('output-log.txt');
  121. });
  122. it('errorLog is valid', function () {
  123. expect(controller.get('taskInfo.errorLog')).to.equal('error-log.txt');
  124. });
  125. it('isTaskPolling is valid', function () {
  126. expect(controller.get('isTaskPolling')).to.equal(item.isTaskPolling);
  127. });
  128. });
  129. });
  130. });
  131. describe('#getHosts', function () {
  132. var cases = [
  133. {
  134. name: 'background_operations.get_by_request',
  135. title: 'default background operation polling'
  136. },
  137. {
  138. stageId: 0,
  139. name: 'common.request.polling',
  140. stageIdPassed: '0',
  141. title: 'polling by stage, stageId = 0'
  142. },
  143. {
  144. stageId: 1,
  145. name: 'common.request.polling',
  146. stageIdPassed: 1,
  147. title: 'polling by stage'
  148. }
  149. ];
  150. cases.forEach(function (item) {
  151. describe(item.title, function () {
  152. beforeEach(function () {
  153. controller.setProperties({
  154. requestIds: [1, 2],
  155. stageId: item.stageId
  156. });
  157. controller.getHosts();
  158. this.bgArgs = testHelpers.filterAjaxRequests('name', 'background_operations.get_by_request');
  159. this.pollingArgs = testHelpers.filterAjaxRequests('name', 'common.request.polling');
  160. this.args = item.name === 'background_operations.get_by_request' ? this.bgArgs : this.pollingArgs;
  161. });
  162. it('two requests are sent', function () {
  163. expect(this.args.length).to.be.equal(2);
  164. });
  165. it('1st call name is valid', function () {
  166. expect(this.args[0][0].name).to.equal(item.name);
  167. });
  168. it('2nd call name is valid', function () {
  169. expect(this.args[1][0].name).to.equal(item.name);
  170. });
  171. it('1st stageId is valid', function () {
  172. expect(this.args[0][0].data.stageId).to.eql(item.stageIdPassed);
  173. });
  174. it('2nd stageId is valid', function () {
  175. expect(this.args[1][0].data.stageId).to.eql(item.stageIdPassed);
  176. });
  177. });
  178. });
  179. });
  180. describe("#initPopup()", function () {
  181. beforeEach(function() {
  182. sinon.stub(App.ModalPopup, 'show');
  183. sinon.stub(controller, 'getHosts');
  184. sinon.stub(controller, 'setProperties');
  185. });
  186. afterEach(function() {
  187. App.ModalPopup.show.restore();
  188. controller.getHosts.restore();
  189. controller.setProperties.restore();
  190. });
  191. it("App.ModalPopup.show should be called", function() {
  192. controller.initPopup(null, null, null, true, null);
  193. expect(App.ModalPopup.show.calledOnce).to.be.true;
  194. });
  195. it("setProperties should be called", function() {
  196. controller.initPopup('popupTitle', [], {}, false, 1);
  197. expect(controller.setProperties.calledWith({
  198. progressController: {},
  199. popupTitle: 'popupTitle',
  200. requestIds: [],
  201. hostsData: [],
  202. stageId: 1
  203. })).to.be.true;
  204. });
  205. it("getHosts should be called", function() {
  206. controller.initPopup(null, null, null, false, null);
  207. expect(controller.getHosts.calledOnce).to.be.true;
  208. });
  209. });
  210. describe("#onGetHostsSuccess()", function () {
  211. var spinner = Em.Object.create({
  212. hide: Em.K
  213. });
  214. beforeEach(function() {
  215. sinon.stub(controller, 'calculateHostsData');
  216. sinon.stub(App.HostPopup, 'initPopup');
  217. sinon.stub(controller, 'isRequestRunning').returns(true);
  218. sinon.stub(controller, 'addObserver');
  219. sinon.stub(spinner, 'hide');
  220. controller.setProperties({
  221. requestIds: [1],
  222. hostsData: [],
  223. popupTitle: 'popupTitle',
  224. spinnerPopup: spinner
  225. });
  226. controller.onGetHostsSuccess({});
  227. });
  228. afterEach(function() {
  229. controller.calculateHostsData.restore();
  230. App.HostPopup.initPopup.restore();
  231. controller.isRequestRunning.restore();
  232. controller.addObserver.restore();
  233. spinner.hide.restore();
  234. });
  235. it("calculateHostsData should be called", function() {
  236. expect(controller.calculateHostsData.calledWith([{}])).to.be.true;
  237. });
  238. it("App.HostPopup.initPopup should be called", function() {
  239. expect(App.HostPopup.initPopup.calledWith('popupTitle', controller)).to.be.true;
  240. });
  241. it("addObserver should be called", function() {
  242. expect(controller.addObserver.calledWith('progressController.logs.length', controller, 'getDataFromProgressController')).to.be.true;
  243. });
  244. it("spinnerPopup.hide should be called", function() {
  245. expect(spinner.hide.calledOnce).to.be.true;
  246. });
  247. });
  248. describe("#calculateHostsData()", function () {
  249. beforeEach(function() {
  250. sinon.stub(App, 'dateTime').returns('dateTime');
  251. sinon.stub(controller, 'isRequestRunning').returns(false);
  252. sinon.stub(controller, 'removeObserver');
  253. });
  254. afterEach(function() {
  255. App.dateTime.restore();
  256. controller.isRequestRunning.restore();
  257. controller.removeObserver.restore();
  258. });
  259. it("calculate data", function() {
  260. var data = [
  261. {
  262. tasks: [
  263. {
  264. Tasks: {
  265. name: 't1',
  266. host_name: 'host1'
  267. }
  268. },
  269. {
  270. Tasks: {
  271. name: 't2',
  272. host_name: 'host1'
  273. }
  274. }
  275. ]
  276. }
  277. ];
  278. controller.setProperties({
  279. popupTitle: 'popupTitle'
  280. });
  281. controller.calculateHostsData(data);
  282. expect(controller.get('services')).to.be.eql([
  283. {
  284. "name": "popupTitle",
  285. "hosts": [
  286. {
  287. "name": "host1",
  288. "publicName": "host1",
  289. "logTasks": [
  290. {
  291. "Tasks": {
  292. "name": "t1",
  293. "host_name": "host1"
  294. }
  295. },
  296. {
  297. "Tasks": {
  298. "name": "t2",
  299. "host_name": "host1"
  300. }
  301. }
  302. ]
  303. }
  304. ]
  305. }
  306. ]);
  307. expect(controller.get('serviceTimestamp')).to.be.equal('dateTime');
  308. expect(controller.removeObserver.calledWith('progressController.logs.length', controller, 'getDataFromProgressController')).to.be.true;
  309. });
  310. });
  311. describe("#getDataFromProgressController()", function () {
  312. beforeEach(function() {
  313. sinon.stub(controller, 'calculateHostsData');
  314. });
  315. afterEach(function() {
  316. controller.calculateHostsData.restore();
  317. });
  318. it("empty logs", function() {
  319. controller.set('progressController', Em.Object.create({
  320. logs: []
  321. }));
  322. controller.getDataFromProgressController();
  323. expect(controller.calculateHostsData.calledOnce).to.be.false;
  324. });
  325. it("filtered logs", function() {
  326. controller.setProperties({
  327. progressController: Em.Object.create({
  328. logs: [
  329. {
  330. Tasks: {
  331. stage_id: 1,
  332. request_id: 1
  333. }
  334. }
  335. ]
  336. }),
  337. stageId: 1,
  338. hostsData: [
  339. {
  340. Requests: {
  341. id: 1
  342. }
  343. }
  344. ]
  345. });
  346. controller.getDataFromProgressController();
  347. expect(controller.calculateHostsData.calledWith([
  348. {
  349. Requests: {
  350. id: 1
  351. },
  352. tasks: [
  353. {
  354. Tasks: {
  355. stage_id: 1,
  356. request_id: 1
  357. }
  358. }
  359. ]
  360. }
  361. ])).to.be.true;
  362. });
  363. });
  364. describe("#isRequestRunning()", function () {
  365. var testCases = [
  366. {
  367. data: [
  368. {
  369. Requests: {
  370. task_count: 1,
  371. aborted_task_count: 1,
  372. completed_task_count: 0,
  373. failed_task_count: 0,
  374. timed_out_task_count: 0,
  375. queued_task_count: 0
  376. }
  377. }
  378. ],
  379. expected: false
  380. },
  381. {
  382. data: [
  383. {
  384. Requests: {
  385. task_count: 1,
  386. aborted_task_count: 0,
  387. completed_task_count: 1,
  388. failed_task_count: 0,
  389. timed_out_task_count: 0,
  390. queued_task_count: 0
  391. }
  392. }
  393. ],
  394. expected: false
  395. },
  396. {
  397. data: [
  398. {
  399. Requests: {
  400. task_count: 1,
  401. aborted_task_count: 0,
  402. completed_task_count: 0,
  403. failed_task_count: 1,
  404. timed_out_task_count: 0,
  405. queued_task_count: 0
  406. }
  407. }
  408. ],
  409. expected: false
  410. },
  411. {
  412. data: [
  413. {
  414. Requests: {
  415. task_count: 1,
  416. aborted_task_count: 0,
  417. completed_task_count: 0,
  418. failed_task_count: 0,
  419. timed_out_task_count: 1,
  420. queued_task_count: 0
  421. }
  422. }
  423. ],
  424. expected: false
  425. },
  426. {
  427. data: [
  428. {
  429. Requests: {
  430. task_count: 1,
  431. aborted_task_count: 0,
  432. completed_task_count: 0,
  433. failed_task_count: 0,
  434. timed_out_task_count: 0,
  435. queued_task_count: 1
  436. }
  437. }
  438. ],
  439. expected: true
  440. },
  441. {
  442. data: [
  443. {
  444. Requests: {
  445. task_count: 1,
  446. aborted_task_count: 0,
  447. completed_task_count: 0,
  448. failed_task_count: 0,
  449. timed_out_task_count: 0,
  450. queued_task_count: 0
  451. }
  452. }
  453. ],
  454. expected: true
  455. }
  456. ];
  457. testCases.forEach(function(test) {
  458. it("request: " + JSON.stringify(test.data), function() {
  459. expect(controller.isRequestRunning(test.data)).to.be.equal(test.expected);
  460. });
  461. });
  462. });
  463. });