background_operations_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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('config');
  20. require('utils/updater');
  21. require('utils/ajax');
  22. require('controllers/global/background_operations_controller');
  23. require('views/common/modal_popup');
  24. /*window.console.log = function(text){
  25. console.log(text);
  26. }*/
  27. describe('App.BackgroundOperationsController', function () {
  28. /**
  29. * Predefined data
  30. *
  31. */
  32. App.set('clusterName', 'testName');
  33. App.set('testMode', 'true');
  34. App.bgOperationsUpdateInterval = 100;
  35. /**
  36. * Test object
  37. */
  38. var controller = App.BackgroundOperationsController.create();
  39. describe('#getOperationsForRequestId', function(){
  40. var obj = new window.Array({"exit_code":0,"stdout":"Output","status":"COMPLETED","stderr":"none","host_name":"dev.hortonworks.com","id":10,"cluster_name":"mycluster","attempt_cnt":1,"request_id":2,"command":"EXECUTE","role":"HDFS_SERVICE_CHECK","start_time":1352119106480,"stage_id":2,"display_exit_code":true},{"exit_code":0,"stdout":"Output","status":"COMPLETED","stderr":"none","host_name":"dev.hortonworks.com","id":14,"cluster_name":"mycluster","attempt_cnt":1,"request_id":2,"command":"EXECUTE","role":"MAPREDUCE_SERVICE_CHECK","start_time":1352119157294,"stage_id":3,"display_exit_code":true},{"exit_code":0,"stdout":"Output","status":"QUEUED","stderr":"none","host_name":"dev.hortonworks.com","id":16,"cluster_name":"mycluster","attempt_cnt":1,"request_id":3,"command":"STOP","role":"NAMENODE","start_time":1352125378300,"stage_id":1,"display_exit_code":true});
  41. var controller = App.BackgroundOperationsController.create();
  42. it('test 1 with 3 items ', function(){
  43. controller.set('allOperations', obj);
  44. expect(controller.getOperationsForRequestId(1).length).to.be.equal(0);
  45. expect(controller.getOperationsForRequestId(2).length).to.be.equal(2);
  46. expect(controller.getOperationsForRequestId(3).length).to.be.equal(1);
  47. });
  48. it('test 2 with 0 items ', function(){
  49. controller.set('allOperations', new window.Array());
  50. expect(controller.getOperationsForRequestId(1).length).to.be.equal(0);
  51. expect(controller.getOperationsForRequestId(2).length).to.be.equal(0);
  52. expect(controller.getOperationsForRequestId(3).length).to.be.equal(0);
  53. });
  54. it('test 3 with 9 items ', function(){
  55. controller.set('allOperations', obj.concat(obj, obj));
  56. expect(controller.getOperationsForRequestId(1).length).to.be.equal(0);
  57. expect(controller.getOperationsForRequestId(2).length).to.be.equal(6);
  58. expect(controller.getOperationsForRequestId(3).length).to.be.equal(3);
  59. });
  60. });
  61. describe('when set isWorking to true ', function () {
  62. it('startPolling executes App.updater.run ', function(done){
  63. sinon.stub(App.updater, 'run', function(){
  64. controller.set('isWorking', false);
  65. App.updater.run.restore();
  66. done();
  67. });
  68. controller.set('isWorking', true);
  69. });
  70. it('loadOperations should be called ', function(done){
  71. this.timeout(App.bgOperationsUpdateInterval + 500);
  72. sinon.stub(controller, 'loadOperations', function(){
  73. controller.set('isWorking', false);
  74. controller.loadOperations.restore();
  75. done();
  76. });
  77. controller.set('isWorking', true);
  78. });
  79. it('updateBackgroundOperations should be called ', function(done){
  80. this.timeout(App.bgOperationsUpdateInterval + 500);
  81. sinon.stub(controller, 'updateBackgroundOperations', function(){
  82. controller.set('isWorking', false);
  83. controller.updateBackgroundOperations.restore();
  84. done();
  85. });
  86. controller.set('isWorking', true);
  87. });
  88. it('allOperations should be set ', function(done){
  89. this.timeout(App.bgOperationsUpdateInterval + 500);
  90. sinon.stub(controller, 'updateBackgroundOperations', function(data){
  91. controller.set('isWorking', false);
  92. controller.updateBackgroundOperations.restore();
  93. controller.updateBackgroundOperations(data);
  94. expect(controller.get('executeTasks').length).to.be.equal(2);
  95. expect(controller.get('allOperationsCount')).to.be.equal(1);
  96. var bgOperation = controller.get('allOperations')[0];
  97. expect(bgOperation).to.have.property('id');
  98. expect(bgOperation).to.have.property('request_id');
  99. expect(bgOperation).to.have.property('role');
  100. expect(bgOperation).to.have.property('command');
  101. expect(bgOperation).to.have.property('status');
  102. done();
  103. });
  104. controller.set('isWorking', true);
  105. });
  106. })
  107. describe('#showPopup', function () {
  108. it('works without exceptions ', function(){
  109. var popup = controller.showPopup();
  110. popup.onPrimary();
  111. });
  112. });
  113. function generateTask(time, role, status, id, command){
  114. command = command || 'STOP';
  115. return {
  116. "Tasks" : {
  117. "exit_code" : 0,
  118. "stdout" : "Output",
  119. "status" : status,
  120. "stderr" : "none",
  121. "host_name" : "dev.hortonworks.com",
  122. "id" : id,
  123. "cluster_name" : "mycluster",
  124. "attempt_cnt" : 1,
  125. "request_id" : 1,
  126. "command" : command,
  127. "role" : role,
  128. "start_time" : time,
  129. "stage_id" : 1
  130. }
  131. };
  132. }
  133. function generate_test_json(){
  134. var time = new Date().getTime();
  135. return {
  136. items:[
  137. {
  138. "Requests" : {
  139. "id" : 3,
  140. "cluster_name" : "mycluster"
  141. },
  142. "tasks":[
  143. generateTask(time, 'NAMENODE', 'QUEUED', 16),
  144. generateTask(time, 'DATANODE', 'QUEUED', 15),
  145. generateTask(time, 'SECONDARY_NAMENODE', 'QUEUED', 14),
  146. generateTask(time, 'MAPREDUCE_SERVICE_CHECK', 'QUEUED', 13, 'EXECUTE')
  147. ]
  148. }
  149. ]
  150. };
  151. }
  152. function update_test_json(json, index, state){
  153. var item = json.items[0].tasks[index].Tasks;
  154. item.status = state;
  155. item.finishedTime = new Date().getTime();
  156. }
  157. function add_to_test_json(json){
  158. var tasks = json.items[0].tasks;
  159. var item = tasks[0].Tasks;
  160. tasks.push(generateTask(item.start_time, 'HBASE_MASTER', 'QUEUED', 12));
  161. tasks.push(generateTask(item.start_time, 'ZOOKEEPER_SERVER', 'QUEUED', 11));
  162. }
  163. describe('#updateBackgroundOperations', function () {
  164. describe('finished items are removed with delay ', function(){
  165. controller.set('allOperations', new window.Array());
  166. controller.set('executeTasks', new window.Array());
  167. controller.set('allOperationsCount', 0);
  168. var json = generate_test_json();
  169. it('on start we have 4 tasks ', function(){
  170. controller.updateBackgroundOperations(json);
  171. expect(controller.get('allOperationsCount')).to.be.equal(4);
  172. expect(controller.get('allOperations').length).to.be.equal(4);
  173. expect(controller.get('executeTasks').length).to.be.equal(1);
  174. })
  175. it('first task is in progress ', function(){
  176. update_test_json(json, 2, 'IN_PROGRESS');
  177. controller.updateBackgroundOperations(json);
  178. expect(controller.get('allOperationsCount')).to.be.equal(4);
  179. expect(controller.get('allOperations').length).to.be.equal(4);
  180. expect(controller.get('executeTasks').length).to.be.equal(1);
  181. });
  182. it('first task finished ', function(){
  183. update_test_json(json, 2, 'COMPLETED');
  184. controller.updateBackgroundOperations(json);
  185. expect(controller.get('allOperationsCount')).to.be.equal(3);
  186. expect(controller.get('allOperations').length).to.be.equal(3);
  187. expect(controller.get('executeTasks').length).to.be.equal(1);
  188. });
  189. it('second task is in progress ', function(){
  190. update_test_json(json, 3, 'IN_PROGRESS');
  191. controller.updateBackgroundOperations(json);
  192. expect(controller.get('allOperationsCount')).to.be.equal(3);
  193. expect(controller.get('allOperations').length).to.be.equal(3);
  194. expect(controller.get('executeTasks').length).to.be.equal(1);
  195. });
  196. it('second task finished ', function(){
  197. update_test_json(json, 3, 'COMPLETED');
  198. controller.updateBackgroundOperations(json);
  199. expect(controller.get('allOperationsCount')).to.be.equal(2);
  200. expect(controller.get('allOperations').length).to.be.equal(3);
  201. expect(controller.get('executeTasks').length).to.be.equal(1);
  202. });
  203. var oldLifeTime = controller.get('taskLifeTime');
  204. controller.set('taskLifeTime', 10);
  205. it('second task removed from list ', function(done){
  206. setTimeout(function(){
  207. controller.updateBackgroundOperations(json);
  208. expect(controller.get('allOperationsCount')).to.be.equal(2);
  209. expect(controller.get('allOperations').length).to.be.equal(2);
  210. expect(controller.get('executeTasks').length).to.be.equal(0);
  211. done();
  212. }, controller.get('taskLifeTime'));
  213. });
  214. it('add new items ', function(){
  215. add_to_test_json(json);
  216. controller.updateBackgroundOperations(json);
  217. expect(controller.get('allOperationsCount')).to.be.equal(4);
  218. expect(controller.get('allOperations').length).to.be.equal(4);
  219. expect(controller.get('executeTasks').length).to.be.equal(0);
  220. controller.set('taskLifeTime', oldLifeTime);
  221. });
  222. });
  223. });
  224. describe('#updateFinishedTask ', function(){
  225. var json = null;
  226. beforeEach(function(){
  227. controller.set('allOperations', new window.Array());
  228. controller.set('executeTasks', new window.Array());
  229. controller.set('allOperationsCount', 0);
  230. json = generate_test_json();
  231. controller.updateBackgroundOperations(json);
  232. });
  233. it('update task ', function(){
  234. update_test_json(json, 3, 'COMPLETED');
  235. controller.updateFinishedTask(json.items[0].tasks[3]);
  236. var item = controller.get('allOperations').findProperty('id', 13);
  237. expect(item).to.be.an('object');
  238. expect(item).to.have.property('status', 'COMPLETED');
  239. expect(item.finishedTime).to.be.above(0);
  240. })
  241. it("don't update task ", function(){
  242. controller.updateFinishedTask({ Tasks: { id : 1 } });
  243. var item = controller.get('allOperations').findProperty('id', 13);
  244. expect(item).to.be.an('object');
  245. expect(item).to.have.property('status', 'QUEUED');
  246. expect(item.finishedTime).to.be.equal(undefined);
  247. })
  248. });
  249. describe('#eventsArray ', function(){
  250. var json = null;
  251. beforeEach(function(){
  252. controller.set('allOperations', new window.Array());
  253. controller.set('executeTasks', new window.Array());
  254. controller.set('allOperationsCount', 0);
  255. json = generate_test_json();
  256. controller.updateBackgroundOperations(json);
  257. });
  258. it("it's working ", function(done){
  259. controller.get('eventsArray').push({
  260. "when" : function(controller){
  261. return controller.get('allOperationsCount') == 3;
  262. },
  263. "do" : done
  264. });
  265. controller.updateBackgroundOperations(json);
  266. controller.updateBackgroundOperations(json);
  267. controller.updateBackgroundOperations(json);
  268. controller.updateBackgroundOperations(json);
  269. update_test_json(json, 1, 'COMPLETED');
  270. controller.updateBackgroundOperations(json);
  271. })
  272. });
  273. })