service_test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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('#cluster', function() {
  113. var tests = Em.A([
  114. {
  115. isLoaded: true,
  116. cluster: [],
  117. m: 'cluster is loaded',
  118. e: {name: 'c1'}
  119. },
  120. {
  121. isLoaded: false,
  122. cluster: [],
  123. m: 'cluster is not loaded',
  124. e: null
  125. }
  126. ]).forEach(function(test) {
  127. it(test.m, function() {
  128. sinon.stub(App.router, 'get', function(k) {
  129. if ('clusterController.isLoaded' === k) return test.isLoaded;
  130. return Em.get(App.router, k);
  131. });
  132. sinon.stub(App.Cluster, 'find', function() {
  133. return [test.e];
  134. });
  135. var c = mainServiceController.get('cluster');
  136. App.router.get.restore();
  137. App.Cluster.find.restore();
  138. expect(c).to.eql(test.e);
  139. });
  140. });
  141. });
  142. describe('#startAllService', function() {
  143. beforeEach(function() {
  144. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  145. });
  146. afterEach(function() {
  147. mainServiceController.allServicesCall.restore();
  148. });
  149. it('target is disabled', function() {
  150. var event = {target: {className: 'disabled', nodeType: 1}};
  151. var r = mainServiceController.startAllService(event);
  152. expect(r).to.be.null;
  153. });
  154. it('parent is disabled', function() {
  155. var event = {target: {parentElement: {className: 'disabled', nodeType: 1}}};
  156. var r = mainServiceController.startAllService(event);
  157. expect(r).to.be.null;
  158. });
  159. it('nothing disabled', function() {
  160. var event = {target: {}}, query = 'query';
  161. mainServiceController.startAllService(event).onPrimary(query);
  162. expect(mainServiceController.allServicesCall.calledWith('STARTED', query));
  163. });
  164. });
  165. describe('#stopAllService', function() {
  166. beforeEach(function() {
  167. sinon.stub(mainServiceController, 'allServicesCall', Em.K);
  168. });
  169. afterEach(function() {
  170. mainServiceController.allServicesCall.restore();
  171. });
  172. it('target is disabled', function() {
  173. var event = {target: {className: 'disabled', nodeType: 1}};
  174. var r = mainServiceController.stopAllService(event);
  175. expect(r).to.be.null;
  176. });
  177. it('parent is disabled', function() {
  178. var event = {target: {parentElement: {className: 'disabled', nodeType: 1}}};
  179. var r = mainServiceController.stopAllService(event);
  180. expect(r).to.be.null;
  181. });
  182. it('nothing disabled', function() {
  183. var event = {target: {}}, query = 'query';
  184. mainServiceController.stopAllService(event).onPrimary(query);
  185. expect(mainServiceController.allServicesCall.calledWith('STARTED', query));
  186. });
  187. });
  188. describe('#allServicesCall', function() {
  189. beforeEach(function() {
  190. sinon.stub($, 'ajax', Em.K);
  191. sinon.stub(App, 'get', function(k) {
  192. if ('testMode' === k) return false;
  193. if ('clusterName' === k) return 'tdk';
  194. return Em.get(App, k);
  195. });
  196. });
  197. afterEach(function() {
  198. $.ajax.restore();
  199. App.get.restore();
  200. });
  201. it('should do ajax-request', function() {
  202. var state = 'STARTED',
  203. query = 'some query';
  204. mainServiceController.allServicesCall(state, query);
  205. var params = $.ajax.args[0][0];
  206. expect(params.type).to.equal('PUT');
  207. expect(params.url.contains('/clusters/tdk/services?')).to.be.true;
  208. var data = JSON.parse(params.data);
  209. expect(data.Body.ServiceInfo.state).to.equal(state);
  210. expect(data.RequestInfo.context).to.equal(App.BackgroundOperationsController.CommandContexts.START_ALL_SERVICES);
  211. });
  212. });
  213. describe('#allServicesCallSuccessCallback', function() {
  214. it('should set status to FAIL', function() {
  215. var params = {query: Em.Object.create({status: ''})};
  216. mainServiceController.allServicesCallSuccessCallback({Requests: {id: 1}}, {}, params);
  217. expect(params.query.get('status')).to.equal('SUCCESS');
  218. });
  219. });
  220. describe('#allServicesCallErrorCallback', function() {
  221. it('should set status to FAIL', function() {
  222. var params = {query: Em.Object.create({status: ''})};
  223. mainServiceController.allServicesCallErrorCallback({}, {}, '', {}, params);
  224. expect(params.query.get('status')).to.equal('FAIL');
  225. });
  226. });
  227. describe('#gotoAddService', function() {
  228. beforeEach(function() {
  229. sinon.stub(App.router, 'transitionTo', Em.K);
  230. });
  231. afterEach(function() {
  232. App.router.transitionTo.restore();
  233. });
  234. it('should not go to wizard', function() {
  235. mainServiceController.reopen({isAllServicesInstalled: true});
  236. mainServiceController.gotoAddService();
  237. expect(App.router.transitionTo.called).to.be.false;
  238. });
  239. it('should go to wizard', function() {
  240. mainServiceController.reopen({isAllServicesInstalled: false});
  241. mainServiceController.gotoAddService();
  242. expect(App.router.transitionTo.calledWith('main.serviceAdd')).to.be.true;
  243. });
  244. });
  245. });