service_test.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.router, 'get', function () {
  100. return Em.Object.create({
  101. allOperationsCount: 1
  102. });
  103. });
  104. });
  105. afterEach(function () {
  106. App.router.get.restore();
  107. });
  108. it('should be based on BG ops count', function () {
  109. expect(mainServiceController.get('isStartStopAllClicked')).to.be.true;
  110. });
  111. });
  112. describe('#isAllServicesInstalled', function() {
  113. beforeEach(function() {
  114. sinon.stub(App.StackService, 'find', function() {
  115. return [
  116. {serviceName: 's1'},
  117. {serviceName: 's2'},
  118. {serviceName: 'HUE'}
  119. ];
  120. });
  121. mainServiceController.set('content', {});
  122. });
  123. afterEach(function() {
  124. App.StackService.find.restore();
  125. });
  126. it('should be false if content is not loaded', function() {
  127. expect(mainServiceController.get('isAllServicesInstalled')).to.be.false;
  128. });
  129. var tests = Em.A([
  130. {
  131. hue: false,
  132. content: ['', ''],
  133. m: 'no hue',
  134. e: true
  135. },
  136. {
  137. hue: false,
  138. content: [''],
  139. m: 'no hue (2)',
  140. e: false
  141. },
  142. {
  143. hue: true,
  144. content: ['', '', ''],
  145. m: 'hue',
  146. e: true
  147. },
  148. {
  149. hue: false,
  150. content: ['', ''],
  151. m: 'hue (2)',
  152. e: true
  153. }
  154. ]).forEach(function(test) {
  155. it(test.m, function() {
  156. mainServiceController.reopen({content: {content: test.content}});
  157. sinon.stub(App, 'get', function(k) {
  158. if ('supports.hue' == k) return test.hue;
  159. return Em.get(App, k);
  160. });
  161. var r = mainServiceController.get('isAllServicesInstalled');
  162. App.get.restore();
  163. expect(r).to.equal(test.e);
  164. });
  165. });
  166. });
  167. describe('#cluster', function() {
  168. var tests = Em.A([
  169. {
  170. isLoaded: true,
  171. cluster: [],
  172. m: 'cluster is loaded',
  173. e: {name: 'c1'}
  174. },
  175. {
  176. isLoaded: false,
  177. cluster: [],
  178. m: 'cluster is not loaded',
  179. e: null
  180. }
  181. ]).forEach(function(test) {
  182. it(test.m, function() {
  183. sinon.stub(App.router, 'get', function(k) {
  184. if ('clusterController.isLoaded' === k) return test.isLoaded;
  185. return Em.get(App.router, k);
  186. });
  187. sinon.stub(App.Cluster, 'find', function() {
  188. return [test.e];
  189. });
  190. var c = mainServiceController.get('cluster');
  191. App.router.get.restore();
  192. App.Cluster.find.restore();
  193. expect(c).to.eql(test.e);
  194. });
  195. });
  196. });
  197. describe('#startAllService', function() {
  198. beforeEach(function() {
  199. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  200. });
  201. afterEach(function() {
  202. mainServiceController.allServicesCall.restore();
  203. });
  204. it('target is disabled', function() {
  205. var event = {target: {className: 'disabled', nodeType: 1}};
  206. var r = mainServiceController.startAllService(event);
  207. expect(r).to.be.null;
  208. });
  209. it('parent is disabled', function() {
  210. var event = {target: {parentElement: {className: 'disabled', nodeType: 1}}};
  211. var r = mainServiceController.startAllService(event);
  212. expect(r).to.be.null;
  213. });
  214. it('nothing disabled', function() {
  215. var event = {target: {}}, query = 'query';
  216. mainServiceController.startAllService(event).onPrimary(query);
  217. expect(mainServiceController.allServicesCall.calledWith('STARTED', query));
  218. });
  219. });
  220. describe('#stopAllService', function() {
  221. beforeEach(function() {
  222. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  223. });
  224. afterEach(function() {
  225. mainServiceController.allServicesCall.restore();
  226. });
  227. it('target is disabled', function() {
  228. var event = {target: {className: 'disabled', nodeType: 1}};
  229. var r = mainServiceController.stopAllService(event);
  230. expect(r).to.be.null;
  231. });
  232. it('parent is disabled', function() {
  233. var event = {target: {parentElement: {className: 'disabled', nodeType: 1}}};
  234. var r = mainServiceController.stopAllService(event);
  235. expect(r).to.be.null;
  236. });
  237. it('nothing disabled', function() {
  238. var event = {target: {}}, query = 'query';
  239. mainServiceController.stopAllService(event).onPrimary(query);
  240. expect(mainServiceController.allServicesCall.calledWith('STARTED', query));
  241. });
  242. });
  243. describe('#allServicesCall', function() {
  244. beforeEach(function() {
  245. sinon.stub($, 'ajax', Em.K);
  246. sinon.stub(App, 'get', function(k) {
  247. if ('testMode' === k) return false;
  248. if ('clusterName' === k) return 'tdk';
  249. return Em.get(App, k);
  250. });
  251. });
  252. afterEach(function() {
  253. $.ajax.restore();
  254. App.get.restore();
  255. });
  256. it('should do ajax-request', function() {
  257. var state = 'STARTED',
  258. query = 'some query';
  259. mainServiceController.allServicesCall(state, query);
  260. var params = $.ajax.args[0][0];
  261. expect(params.type).to.equal('PUT');
  262. expect(params.url.contains('/clusters/tdk/services?')).to.be.true;
  263. var data = JSON.parse(params.data);
  264. expect(data.Body.ServiceInfo.state).to.equal(state);
  265. expect(data.RequestInfo.context).to.equal(App.BackgroundOperationsController.CommandContexts.START_ALL_SERVICES);
  266. });
  267. });
  268. describe('#allServicesCallSuccessCallback', function() {
  269. it('should set status to FAIL', function() {
  270. var params = {query: Em.Object.create({status: ''})};
  271. mainServiceController.allServicesCallSuccessCallback({Requests: {id: 1}}, {}, params);
  272. expect(params.query.get('status')).to.equal('SUCCESS');
  273. });
  274. });
  275. describe('#allServicesCallErrorCallback', function() {
  276. it('should set status to FAIL', function() {
  277. var params = {query: Em.Object.create({status: ''})};
  278. mainServiceController.allServicesCallErrorCallback({}, {}, '', {}, params);
  279. expect(params.query.get('status')).to.equal('FAIL');
  280. });
  281. });
  282. describe('#gotoAddService', function() {
  283. beforeEach(function() {
  284. sinon.stub(App.router, 'transitionTo', Em.K);
  285. });
  286. afterEach(function() {
  287. App.router.transitionTo.restore();
  288. });
  289. it('should not go to wizard', function() {
  290. mainServiceController.reopen({isAllServicesInstalled: true});
  291. mainServiceController.gotoAddService();
  292. expect(App.router.transitionTo.called).to.be.false;
  293. });
  294. it('should go to wizard', function() {
  295. mainServiceController.reopen({isAllServicesInstalled: false});
  296. mainServiceController.gotoAddService();
  297. expect(App.router.transitionTo.calledWith('main.serviceAdd')).to.be.true;
  298. });
  299. });
  300. });