service_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/service');
  20. var mainServiceController;
  21. describe('App.MainServiceController', function () {
  22. var tests = Em.A([
  23. {
  24. isStartStopAllClicked: true,
  25. content: Em.A([
  26. Em.Object.create({
  27. healthStatus: 'red',
  28. serviceName: 'HIVE',
  29. isClientsOnly: false
  30. }),
  31. Em.Object.create({
  32. healthStatus: 'red',
  33. serviceName: 'HDFS',
  34. isClientsOnly: false
  35. }),
  36. Em.Object.create({
  37. healthStatus: 'red',
  38. serviceName: 'TEZ',
  39. isClientsOnly: true
  40. })
  41. ]),
  42. eStart: true,
  43. eStop: true,
  44. mStart: 'mainServiceController StartAll is Disabled 2',
  45. mStop: 'mainServiceController StopAll is Disabled 2'
  46. },
  47. {
  48. isStartStopAllClicked: false,
  49. content: Em.A([
  50. Em.Object.create({
  51. healthStatus: 'green',
  52. serviceName: 'HIVE',
  53. isClientsOnly: false
  54. }),
  55. Em.Object.create({
  56. healthStatus: 'red',
  57. serviceName: 'HDFS',
  58. isClientsOnly: false
  59. }),
  60. Em.Object.create({
  61. healthStatus: 'red',
  62. serviceName: 'TEZ',
  63. isClientsOnly: true
  64. })
  65. ]),
  66. eStart: false,
  67. eStop: false,
  68. mStart: 'mainServiceController StartAll is Enabled 3',
  69. mStop: 'mainServiceController StopAll is Enabled 3'
  70. }
  71. ]);
  72. beforeEach(function() {
  73. mainServiceController = App.MainServiceController.create();
  74. });
  75. describe('#isStartAllDisabled', function () {
  76. tests.forEach(function (test) {
  77. it(test.mStart, function () {
  78. mainServiceController = App.MainServiceController.create({
  79. content: test.content,
  80. isStartStopAllClicked: test.isStartStopAllClicked
  81. });
  82. expect(mainServiceController.get('isStartAllDisabled')).to.equals(test.eStart);
  83. });
  84. });
  85. });
  86. describe('#isStopAllDisabled', function () {
  87. tests.forEach(function (test) {
  88. it(test.mStop, function () {
  89. mainServiceController = App.MainServiceController.create({
  90. content: test.content,
  91. isStartStopAllClicked: test.isStartStopAllClicked
  92. });
  93. expect(mainServiceController.get('isStopAllDisabled')).to.equals(test.eStop);
  94. });
  95. });
  96. });
  97. describe('#isStartStopAllClicked', function () {
  98. beforeEach(function () {
  99. sinon.stub(App, 'get').withArgs('router.backgroundOperationsController.allOperationsCount').returns(1);
  100. });
  101. afterEach(function () {
  102. App.get.restore();
  103. });
  104. it('should be based on BG ops count', function () {
  105. expect(mainServiceController.get('isStartStopAllClicked')).to.be.true;
  106. });
  107. });
  108. describe('#cluster', function() {
  109. var tests = Em.A([
  110. {
  111. isLoaded: true,
  112. cluster: [],
  113. m: 'cluster is loaded',
  114. e: {name: 'c1'}
  115. },
  116. {
  117. isLoaded: false,
  118. cluster: [],
  119. m: 'cluster is not loaded',
  120. e: null
  121. }
  122. ]).forEach(function(test) {
  123. it(test.m, function() {
  124. sinon.stub(App.router, 'get', function(k) {
  125. if ('clusterController.isClusterDataLoaded' === k) return test.isLoaded;
  126. return Em.get(App.router, k);
  127. });
  128. sinon.stub(App.Cluster, 'find', function() {
  129. return [test.e];
  130. });
  131. var c = mainServiceController.get('cluster');
  132. App.router.get.restore();
  133. App.Cluster.find.restore();
  134. expect(c).to.eql(test.e);
  135. });
  136. });
  137. });
  138. describe('#startAllService', function() {
  139. beforeEach(function() {
  140. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  141. });
  142. afterEach(function() {
  143. mainServiceController.allServicesCall.restore();
  144. });
  145. it('target is disabled', function() {
  146. var event = {target: {className: 'disabled', nodeType: 1}};
  147. var r = mainServiceController.startAllService(event);
  148. expect(r).to.be.null;
  149. });
  150. it('parent is disabled', function() {
  151. var event = {target: {parentElement: {className: 'disabled', nodeType: 1}}};
  152. var r = mainServiceController.startAllService(event);
  153. expect(r).to.be.null;
  154. });
  155. it('nothing disabled', function() {
  156. var event = {target: {}}, query = 'query';
  157. mainServiceController.startAllService(event).onPrimary(query);
  158. expect(mainServiceController.allServicesCall.calledWith('STARTED', query));
  159. });
  160. });
  161. describe('#stopAllService', function() {
  162. beforeEach(function() {
  163. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  164. });
  165. afterEach(function() {
  166. mainServiceController.allServicesCall.restore();
  167. });
  168. it('target is disabled', function() {
  169. var event = {target: {className: 'disabled', nodeType: 1}};
  170. var r = mainServiceController.stopAllService(event);
  171. expect(r).to.be.null;
  172. });
  173. it('parent is disabled', function() {
  174. var event = {target: {parentElement: {className: 'disabled', nodeType: 1}}};
  175. var r = mainServiceController.stopAllService(event);
  176. expect(r).to.be.null;
  177. });
  178. it('nothing disabled', function() {
  179. var event = {target: {}}, query = 'query';
  180. mainServiceController.stopAllService(event).onPrimary(query);
  181. expect(mainServiceController.allServicesCall.calledWith('STARTED', query));
  182. });
  183. });
  184. describe('#startStopAllService', function() {
  185. var event = { target: document.createElement("BUTTON") };
  186. beforeEach(function() {
  187. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  188. sinon.spy(Em.I18n, "t");
  189. });
  190. afterEach(function() {
  191. mainServiceController.allServicesCall.restore();
  192. Em.I18n.t.restore();
  193. });
  194. it ("should confirm stop if state is INSTALLED", function() {
  195. mainServiceController.startStopAllService(event, "INSTALLED");
  196. expect(Em.I18n.t.calledWith('services.service.stopAll.confirmMsg')).to.be.ok;
  197. expect(Em.I18n.t.calledWith('services.service.stop.confirmButton')).to.be.ok;
  198. });
  199. it ("should check last checkpoint for NN before confirming stop", function() {
  200. var mainServiceItemController = App.MainServiceItemController.create({});
  201. sinon.stub(mainServiceItemController, 'checkNnLastCheckpointTime', function() {
  202. return true;
  203. });
  204. sinon.stub(App.router, 'get', function(k) {
  205. if ('mainServiceItemController' === k) {
  206. return mainServiceItemController;
  207. }
  208. return Em.get(App.router, k);
  209. });
  210. sinon.stub(App.Service, 'find', function() {
  211. return [{
  212. serviceName: "HDFS",
  213. workStatus: "STARTED"
  214. }];
  215. });
  216. mainServiceController.startStopAllService(event, "INSTALLED");
  217. expect(mainServiceItemController.checkNnLastCheckpointTime.calledOnce).to.equal(true);
  218. mainServiceItemController.checkNnLastCheckpointTime.restore();
  219. App.router.get.restore();
  220. App.Service.find.restore();
  221. });
  222. it ("should confirm start if state is not INSTALLED", function() {
  223. mainServiceController.startStopAllService(event, "STARTED");
  224. expect(Em.I18n.t.calledWith('services.service.startAll.confirmMsg')).to.be.ok;
  225. expect(Em.I18n.t.calledWith('services.service.start.confirmButton')).to.be.ok;
  226. });
  227. });
  228. describe('#allServicesCall', function() {
  229. beforeEach(function() {
  230. sinon.stub($, 'ajax', Em.K);
  231. sinon.stub(App, 'get', function(k) {
  232. if ('testMode' === k) return false;
  233. if ('clusterName' === k) return 'tdk';
  234. return Em.get(App, k);
  235. });
  236. });
  237. afterEach(function() {
  238. $.ajax.restore();
  239. App.get.restore();
  240. });
  241. it('should do ajax-request', function() {
  242. var state = 'STARTED',
  243. query = 'some query';
  244. mainServiceController.allServicesCall(state, query);
  245. var params = $.ajax.args[0][0];
  246. expect(params.type).to.equal('PUT');
  247. expect(params.url.contains('/clusters/tdk/services?')).to.be.true;
  248. var data = JSON.parse(params.data);
  249. expect(data.Body.ServiceInfo.state).to.equal(state);
  250. expect(data.RequestInfo.context).to.equal(App.BackgroundOperationsController.CommandContexts.START_ALL_SERVICES);
  251. });
  252. });
  253. describe('#allServicesCallErrorCallback', function() {
  254. it('should set status to FAIL', function() {
  255. var params = {query: Em.Object.create({status: ''})};
  256. mainServiceController.allServicesCallErrorCallback({}, {}, '', {}, params);
  257. expect(params.query.get('status')).to.equal('FAIL');
  258. });
  259. });
  260. describe('#gotoAddService', function() {
  261. beforeEach(function() {
  262. sinon.stub(App.router, 'transitionTo', Em.K);
  263. });
  264. afterEach(function() {
  265. App.router.transitionTo.restore();
  266. });
  267. it('should not go to wizard', function() {
  268. mainServiceController.reopen({isAllServicesInstalled: true});
  269. mainServiceController.gotoAddService();
  270. expect(App.router.transitionTo.called).to.be.false;
  271. });
  272. it('should go to wizard', function() {
  273. mainServiceController.reopen({isAllServicesInstalled: false});
  274. mainServiceController.gotoAddService();
  275. expect(App.router.transitionTo.calledWith('main.serviceAdd')).to.be.true;
  276. });
  277. });
  278. describe('#isRestartAllRequiredDisabled', function () {
  279. it('should be false if there is at least one service with isRestartRequired=true', function () {
  280. mainServiceController.reopen({
  281. content: [
  282. {isRestartRequired: false}, {isRestartRequired: false}, {isRestartRequired: true}
  283. ]
  284. });
  285. expect(mainServiceController.get('isRestartAllRequiredDisabled')).to.be.false;
  286. });
  287. it('should be true if there is no services with isRestartRequired=true', function () {
  288. mainServiceController.reopen({
  289. content: [
  290. {isRestartRequired: false}, {isRestartRequired: false}, {isRestartRequired: false}
  291. ]
  292. });
  293. expect(mainServiceController.get('isRestartAllRequiredDisabled')).to.be.true;
  294. });
  295. });
  296. describe('#restartAllRequired', function () {
  297. beforeEach(function () {
  298. sinon.spy(App, 'showConfirmationPopup');
  299. sinon.spy(mainServiceController, 'restartHostComponents');
  300. sinon.stub(App.HostComponent, 'find', function() {
  301. return [
  302. Em.Object.create({
  303. componentName: 'componentName1',
  304. hostName: 'hostName1',
  305. service: {
  306. serviceName: 'serviceName1',
  307. displayName: 'displayName1'
  308. },
  309. staleConfigs: true
  310. }),
  311. Em.Object.create({
  312. componentName: 'componentName2',
  313. hostName: 'hostName2',
  314. service: {
  315. serviceName: 'serviceName2',
  316. displayName: 'displayName2'
  317. },
  318. staleConfigs: true
  319. }),
  320. Em.Object.create({
  321. componentName: 'componentName3',
  322. hostName: 'hostName3',
  323. service: {
  324. serviceName: 'serviceName3',
  325. displayName: 'displayName3'
  326. },
  327. staleConfigs: false
  328. })
  329. ];
  330. });
  331. });
  332. afterEach(function () {
  333. App.HostComponent.find.restore();
  334. App.showConfirmationPopup.restore();
  335. mainServiceController.restartHostComponents.restore();
  336. });
  337. it('should show confirmation popup with list of services and call restartHostComponents after confirmation', function () {
  338. var popup = mainServiceController.restartAllRequired();
  339. popup.onPrimary();
  340. expect(App.showConfirmationPopup.args[0][1]).to.equal(Em.I18n.t('services.service.refreshAll.confirmMsg').format('displayName1, displayName2'));
  341. expect(mainServiceController.restartHostComponents.calledWith([
  342. {
  343. component_name: 'componentName1',
  344. service_name: 'serviceName1',
  345. hosts: 'hostName1'
  346. },
  347. {
  348. component_name: 'componentName2',
  349. service_name: 'serviceName2',
  350. hosts: 'hostName2'
  351. }
  352. ])).to.be.true;
  353. });
  354. });
  355. describe('#restartHostComponents', function () {
  356. beforeEach(function () {
  357. sinon.stub(App.ajax, 'send', Em.K);
  358. });
  359. afterEach(function () {
  360. App.ajax.send.restore();
  361. });
  362. it('should send ajax request', function () {
  363. mainServiceController.restartHostComponents();
  364. expect(App.ajax.send.calledOnce).to.be.true;
  365. });
  366. });
  367. });