service_test.js 13 KB

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