cluster_controller_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/global/cluster_controller');
  20. require('models/host_component');
  21. require('utils/http_client');
  22. require('models/service');
  23. require('models/host');
  24. require('utils/ajax/ajax');
  25. var modelSetup = require('test/init_model_test');
  26. describe('App.clusterController', function () {
  27. var controller = App.ClusterController.create();
  28. App.Service.FIXTURES = [
  29. {service_name: 'GANGLIA'}
  30. ];
  31. describe('#updateLoadStatus()', function () {
  32. controller.set('dataLoadList', Em.Object.create({
  33. 'item1': false,
  34. 'item2': false
  35. }));
  36. it('when none item is loaded then width should be "width:0"', function () {
  37. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:0');
  38. });
  39. it('when first item is loaded then isLoaded should be false', function () {
  40. controller.updateLoadStatus.call(controller, 'item1');
  41. expect(controller.get('isLoaded')).to.equal(false);
  42. });
  43. it('when first item is loaded then width should be "width:50%"', function () {
  44. controller.updateLoadStatus.call(controller, 'item1');
  45. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:50%');
  46. });
  47. it('when all items are loaded then isLoaded should be true', function () {
  48. controller.updateLoadStatus.call(controller, 'item2');
  49. expect(controller.get('isLoaded')).to.equal(true);
  50. });
  51. it('when all items are loaded then width should be "width:100%"', function () {
  52. controller.updateLoadStatus.call(controller, 'item2');
  53. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:100%');
  54. });
  55. });
  56. describe('#loadClusterName()', function () {
  57. beforeEach(function () {
  58. modelSetup.setupStackVersion(this, 'HDP-2.0.5');
  59. sinon.stub(App.ajax, 'send', function () {
  60. return {
  61. complete: function (callback) {
  62. App.set('clusterName', 'clusterNameFromServer');
  63. App.set('currentStackVersion', 'HDP-2.0.5');
  64. callback();
  65. }
  66. }
  67. });
  68. });
  69. afterEach(function () {
  70. modelSetup.restoreStackVersion(this);
  71. App.ajax.send.restore();
  72. });
  73. it('if clusterName is "mycluster" and reload is false then clusterName stays the same', function () {
  74. App.set('clusterName', 'mycluster');
  75. controller.loadClusterName(false);
  76. expect(App.ajax.send.called).to.be.false;
  77. expect(App.get('clusterName')).to.equal('mycluster');
  78. });
  79. it('reload is true and clusterName is not empty', function () {
  80. controller.loadClusterName(true);
  81. expect(App.ajax.send.calledOnce).to.be.true;
  82. expect(App.get('clusterName')).to.equal('clusterNameFromServer');
  83. expect(App.get('currentStackVersion')).to.equal('HDP-2.0.5');
  84. });
  85. it('reload is false and clusterName is empty', function () {
  86. App.set('clusterName', '');
  87. controller.loadClusterName(false);
  88. expect(App.ajax.send.calledOnce).to.be.true;
  89. expect(App.get('clusterName')).to.equal('clusterNameFromServer');
  90. expect(App.get('currentStackVersion')).to.equal('HDP-2.0.5');
  91. });
  92. });
  93. describe('#loadClusterNameSuccessCallback', function () {
  94. var test_data = {
  95. "items": [
  96. {
  97. "Clusters": {
  98. "cluster_name": "tdk",
  99. "version": "HDP-1.3.0"
  100. }
  101. }
  102. ]
  103. };
  104. it('Check cluster', function () {
  105. controller.loadClusterNameSuccessCallback(test_data);
  106. expect(App.get('clusterName')).to.equal('tdk');
  107. expect(App.get('currentStackVersion')).to.equal('HDP-1.3.0');
  108. });
  109. });
  110. describe('#loadClusterNameErrorCallback', function () {
  111. controller.loadClusterNameErrorCallback();
  112. it('', function () {
  113. expect(controller.get('isLoaded')).to.equal(true);
  114. });
  115. });
  116. describe('#getServerClockSuccessCallback()', function () {
  117. var testCases = [
  118. {
  119. title: 'if server clock is 1 then currentServerTime should be 1000',
  120. data: {
  121. RootServiceComponents: {
  122. server_clock: 1
  123. }
  124. },
  125. result: 1000
  126. },
  127. {
  128. title: 'if server clock is 0 then currentServerTime should be 0',
  129. data: {
  130. RootServiceComponents: {
  131. server_clock: 0
  132. }
  133. },
  134. result: 0
  135. },
  136. {
  137. title: 'if server clock is 111111111111 then currentServerTime should be 111111111111000',
  138. data: {
  139. RootServiceComponents: {
  140. server_clock: 111111111111
  141. }
  142. },
  143. result: 111111111111000
  144. },
  145. {
  146. title: 'if server clock is 1111111111113 then currentServerTime should be 1111111111113',
  147. data: {
  148. RootServiceComponents: {
  149. server_clock: 1111111111113
  150. }
  151. },
  152. result: 1111111111113
  153. }
  154. ];
  155. var currentServerTime = App.get('currentServerTime');
  156. var clockDistance = App.get('clockDistance');
  157. testCases.forEach(function (test) {
  158. it(test.title, function () {
  159. controller.getServerClockSuccessCallback(test.data);
  160. expect(App.get('currentServerTime')).to.equal(test.result);
  161. App.set('clockDistance', clockDistance);
  162. App.set('currentServerTime', currentServerTime);
  163. });
  164. });
  165. });
  166. describe('#getUrl', function () {
  167. controller.set('clusterName', 'tdk');
  168. var tests = ['test1', 'test2', 'test3'];
  169. it('testMode = true', function () {
  170. App.testMode = true;
  171. tests.forEach(function (test) {
  172. expect(controller.getUrl(test, test)).to.equal(test);
  173. });
  174. });
  175. it('testMode = false', function () {
  176. App.testMode = false;
  177. tests.forEach(function (test) {
  178. expect(controller.getUrl(test, test)).to.equal(App.apiPrefix + '/clusters/' + controller.get('clusterName') + test);
  179. });
  180. });
  181. });
  182. describe('#setGangliaUrl()', function () {
  183. beforeEach(function () {
  184. controller.set('gangliaUrl', null);
  185. });
  186. it('testMode = true', function () {
  187. App.testMode = true;
  188. controller.setGangliaUrl();
  189. expect(controller.get('gangliaUrl')).to.equal('http://gangliaserver/ganglia/?t=yes');
  190. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  191. });
  192. it('Cluster is not loaded', function () {
  193. App.testMode = false;
  194. controller.set('isLoaded', false);
  195. controller.setGangliaUrl();
  196. expect(controller.get('gangliaUrl')).to.equal(null);
  197. });
  198. it('GANGLIA_SERVER component is absent', function () {
  199. controller.set('isLoaded', true);
  200. App.testMode = false;
  201. sinon.stub(App.HostComponent, 'find', function(){
  202. return [];
  203. });
  204. controller.setGangliaUrl();
  205. expect(controller.get('gangliaUrl')).to.equal(null);
  206. App.HostComponent.find.restore();
  207. });
  208. it('Ganglia Server host is "GANGLIA_host"', function () {
  209. controller.set('isLoaded', true);
  210. App.testMode = false;
  211. sinon.stub(App.HostComponent, 'find', function(){
  212. return [Em.Object.create({
  213. componentName: 'GANGLIA_SERVER',
  214. hostName: 'GANGLIA_host'
  215. })];
  216. });
  217. sinon.spy(App.ajax, 'send');
  218. controller.setGangliaUrl();
  219. expect(App.ajax.send.calledOnce).to.be.true;
  220. expect(controller.get('isGangliaUrlLoaded')).to.be.false;
  221. App.HostComponent.find.restore();
  222. App.ajax.send.restore();
  223. });
  224. });
  225. describe('#gangliaWebProtocol', function () {
  226. var testCases = [
  227. {
  228. title: 'if ambariProperties is null then gangliaWebProtocol should be "http"',
  229. data: null,
  230. result: 'http'
  231. },
  232. {
  233. title: 'if ambariProperties is empty object then gangliaWebProtocol should be "http"',
  234. data: {},
  235. result: 'http'
  236. },
  237. {
  238. title: 'if ganglia.https is false then gangliaWebProtocol should be "http"',
  239. data: {'ganglia.https': false},
  240. result: 'http'
  241. },
  242. {
  243. title: 'if ganglia.https is true then gangliaWebProtocol should be "http"',
  244. data: {'ganglia.https': true},
  245. result: 'https'
  246. }
  247. ];
  248. testCases.forEach(function (test) {
  249. it(test.title, function () {
  250. controller.set('ambariProperties', test.data);
  251. expect(controller.get('gangliaWebProtocol')).to.equal(test.result);
  252. });
  253. });
  254. });
  255. describe('#setGangliaUrlSuccessCallback()', function () {
  256. it('Query return no hosts', function () {
  257. controller.setGangliaUrlSuccessCallback({items: []});
  258. expect(controller.get('gangliaUrl')).to.equal(null);
  259. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  260. });
  261. it('App.singleNodeInstall is true', function () {
  262. controller.reopen({
  263. gangliaWebProtocol: 'http'
  264. });
  265. App.set('singleNodeInstall', true);
  266. App.set('singleNodeAlias', 'localhost');
  267. controller.setGangliaUrlSuccessCallback({items: [{
  268. Hosts: {
  269. public_host_name: 'host1'
  270. }
  271. }]});
  272. expect(controller.get('gangliaUrl')).to.equal('http://localhost:42080/ganglia');
  273. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  274. });
  275. it('App.singleNodeInstall is false', function () {
  276. controller.reopen({
  277. gangliaWebProtocol: 'http'
  278. });
  279. App.set('singleNodeInstall', false);
  280. App.set('singleNodeAlias', 'localhost');
  281. controller.setGangliaUrlSuccessCallback({items: [{
  282. Hosts: {
  283. public_host_name: 'host1'
  284. }
  285. }]});
  286. expect(controller.get('gangliaUrl')).to.equal('http://host1/ganglia');
  287. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  288. });
  289. });
  290. describe("#createKerberosAdminSession()", function() {
  291. before(function () {
  292. sinon.stub(App.ajax, 'send', function() {
  293. return {success: Em.K}
  294. });
  295. });
  296. after(function () {
  297. App.ajax.send.restore();
  298. });
  299. it("make ajax call", function() {
  300. controller.createKerberosAdminSession("admin", "pass", {});
  301. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  302. name: 'common.cluster.update',
  303. sender: controller,
  304. data: {
  305. clusterName: App.get('clusterName'),
  306. data: [{
  307. session_attributes: {
  308. kerberos_admin: {principal: "admin", password: "pass"}
  309. }
  310. }]
  311. }
  312. });
  313. });
  314. });
  315. });