service_test.js 12 KB

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