cluster_controller_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. describe('App.clusterController', function () {
  26. var controller = App.ClusterController.create();
  27. App.Service.FIXTURES = [
  28. {service_name: 'NAGIOS'}
  29. ];
  30. describe('#updateLoadStatus()', function () {
  31. controller.set('dataLoadList', Em.Object.create({
  32. 'item1': false,
  33. 'item2': false
  34. }));
  35. it('when none item is loaded then width should be "width:0"', function () {
  36. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:0');
  37. });
  38. it('when first item is loaded then isLoaded should be false', function () {
  39. controller.updateLoadStatus.call(controller, 'item1');
  40. expect(controller.get('isLoaded')).to.equal(false);
  41. });
  42. it('when first item is loaded then width should be "width:50%"', function () {
  43. controller.updateLoadStatus.call(controller, 'item1');
  44. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:50%');
  45. });
  46. it('when all items are loaded then isLoaded should be true', function () {
  47. controller.updateLoadStatus.call(controller, 'item2');
  48. expect(controller.get('isLoaded')).to.equal(true);
  49. });
  50. it('when all items are loaded then width should be "width:100%"', function () {
  51. controller.updateLoadStatus.call(controller, 'item2');
  52. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:100%');
  53. });
  54. });
  55. describe('#loadClusterName()', function () {
  56. beforeEach(function () {
  57. sinon.spy(App.ajax, 'send');
  58. });
  59. afterEach(function () {
  60. App.ajax.send.restore();
  61. });
  62. it('if clusterName is "mycluster" and reload is false then loadClusterName should return false', function () {
  63. controller.set('cluster', {Clusters: {cluster_name: 'mycluster'}});
  64. expect(controller.loadClusterName(false)).to.equal(false);
  65. });
  66. it('reload is true and clusterName is not empty', function () {
  67. controller.loadClusterName(true);
  68. expect(App.ajax.send.calledOnce).to.equal(true);
  69. expect(App.get('currentStackVersion')).to.equal('HDP-2.0.5');
  70. });
  71. it('reload is false and clusterName is empty', function () {
  72. controller.set('cluster', {Clusters: {cluster_name: ''}});
  73. controller.loadClusterName(false);
  74. expect(App.ajax.send.calledOnce).to.equal(true);
  75. expect(App.get('currentStackVersion')).to.equal('HDP-2.0.5');
  76. });
  77. });
  78. describe('#loadClusterNameSuccessCallback', function () {
  79. var test_data = {
  80. "items": [
  81. {
  82. "Clusters": {
  83. "cluster_name": "tdk",
  84. "version": "HDP-1.3.0"
  85. }
  86. }
  87. ]
  88. };
  89. it('Check cluster', function () {
  90. controller.loadClusterNameSuccessCallback(test_data);
  91. expect(controller.get('cluster.Clusters.cluster_name')).to.equal('tdk');
  92. expect(controller.get('cluster.Clusters.version')).to.equal('HDP-1.3.0');
  93. expect(App.get('clusterName')).to.equal('tdk');
  94. });
  95. });
  96. describe('#loadClusterNameErrorCallback', function () {
  97. controller.loadClusterNameErrorCallback();
  98. it('', function () {
  99. expect(controller.get('isLoaded')).to.equal(true);
  100. });
  101. });
  102. describe('#getServerClockSuccessCallback()', function () {
  103. var testCases = [
  104. {
  105. title: 'if server clock is 1 then currentServerTime should be 1000',
  106. data: {
  107. RootServiceComponents: {
  108. server_clock: 1
  109. }
  110. },
  111. result: 1000
  112. },
  113. {
  114. title: 'if server clock is 0 then currentServerTime should be 0',
  115. data: {
  116. RootServiceComponents: {
  117. server_clock: 0
  118. }
  119. },
  120. result: 0
  121. },
  122. {
  123. title: 'if server clock is 111111111111 then currentServerTime should be 111111111111000',
  124. data: {
  125. RootServiceComponents: {
  126. server_clock: 111111111111
  127. }
  128. },
  129. result: 111111111111000
  130. },
  131. {
  132. title: 'if server clock is 1111111111113 then currentServerTime should be 1111111111113',
  133. data: {
  134. RootServiceComponents: {
  135. server_clock: 1111111111113
  136. }
  137. },
  138. result: 1111111111113
  139. }
  140. ];
  141. var currentServerTime = App.get('currentServerTime');
  142. var clockDistance = App.get('clockDistance');
  143. testCases.forEach(function (test) {
  144. it(test.title, function () {
  145. controller.getServerClockSuccessCallback(test.data);
  146. expect(App.get('currentServerTime')).to.equal(test.result);
  147. App.set('clockDistance', clockDistance);
  148. App.set('currentServerTime', currentServerTime);
  149. });
  150. });
  151. });
  152. describe('#getUrl', function () {
  153. controller.set('clusterName', 'tdk');
  154. var tests = ['test1', 'test2', 'test3'];
  155. it('testMode = true', function () {
  156. App.testMode = true;
  157. tests.forEach(function (test) {
  158. expect(controller.getUrl(test, test)).to.equal(test);
  159. });
  160. });
  161. it('testMode = false', function () {
  162. App.testMode = false;
  163. tests.forEach(function (test) {
  164. expect(controller.getUrl(test, test)).to.equal(App.apiPrefix + '/clusters/' + controller.get('clusterName') + test);
  165. });
  166. });
  167. });
  168. describe('#gangliaUrl', function () {
  169. it('testMode = true', function () {
  170. App.testMode = true;
  171. expect(controller.get('gangliaUrl')).to.equal('http://gangliaserver/ganglia/?t=yes');
  172. });
  173. it('Ganglia service is absent', function () {
  174. App.testMode = false;
  175. controller.set('gangliaWebProtocol', '');
  176. expect(controller.get('gangliaUrl')).to.equal(null);
  177. });
  178. it('Ganglia doesn\'t have any components', function () {
  179. App.store.load(App.Service, {
  180. id: 'GANGLIA',
  181. service_name: 'GANGLIA'
  182. });
  183. controller.set('gangliaWebProtocol', '');
  184. expect(controller.get('gangliaUrl')).to.equal(null);
  185. expect(controller.get('isGangliaInstalled')).to.equal(true);
  186. });
  187. it('Ganglia Server doesn\'t have host', function () {
  188. App.store.load(App.HostComponent, {
  189. id: 'GANGLIA_SERVER_GANGLIA_host',
  190. component_name: 'GANGLIA_SERVER',
  191. service_id: 'GANGLIA',
  192. host_id: 'GANGLIA_host'
  193. });
  194. App.store.load(App.Service, {
  195. id: 'GANGLIA',
  196. service_name: 'GANGLIA',
  197. host_components: ['GANGLIA_SERVER_GANGLIA_host']
  198. });
  199. controller.set('gangliaWebProtocol', '');
  200. expect(controller.get('gangliaUrl')).to.equal(null);
  201. });
  202. it('Ganglia Server host is "GANGLIA_host"', function () {
  203. App.store.load(App.Host, {
  204. id: 'GANGLIA_host',
  205. host_name: 'GANGLIA_host',
  206. host_components: ['GANGLIA_SERVER_GANGLIA_host'],
  207. public_host_name: 'GANGLIA_host'
  208. });
  209. controller.set('gangliaWebProtocol', '');
  210. expect(controller.get('gangliaUrl')).to.equal("http://GANGLIA_host/ganglia");
  211. });
  212. it('singleNodeInstall = true', function () {
  213. App.set('singleNodeInstall', true);
  214. controller.set('gangliaWebProtocol', '');
  215. expect(controller.get('gangliaUrl')).to.equal("http://" + location.hostname + ":42080/ganglia");
  216. });
  217. it('singleNodeAlias is "alias"', function () {
  218. App.set('singleNodeAlias', 'alias');
  219. controller.set('gangliaWebProtocol', '');
  220. expect(controller.get('gangliaUrl')).to.equal("http://alias:42080/ganglia");
  221. App.set('singleNodeInstall', false);
  222. App.set('singleNodeAlias', '');
  223. });
  224. });
  225. describe('#nagiosUrl', function () {
  226. it('testMode = true', function () {
  227. App.testMode = true;
  228. controller.set('nagiosWebProtocol', '');
  229. expect(controller.get('nagiosUrl')).to.equal('http://nagiosserver/nagios');
  230. });
  231. it('Nagios service is absent', function () {
  232. App.testMode = false;
  233. controller.set('nagiosWebProtocol', '');
  234. expect(controller.get('nagiosUrl')).to.equal(null);
  235. });
  236. it('Nagios doesn\'t have any components', function () {
  237. App.store.load(App.Service, {
  238. id: 'NAGIOS',
  239. service_name: 'NAGIOS'
  240. });
  241. controller.set('nagiosWebProtocol', '');
  242. expect(controller.get('nagiosUrl')).to.equal(null);
  243. expect(controller.get('isNagiosInstalled')).to.equal(true);
  244. });
  245. it('NAGIOS Server doesn\'t have host', function () {
  246. App.store.load(App.HostComponent, {
  247. id: 'NAGIOS_SERVER_NAGIOS_host',
  248. component_name: 'NAGIOS_SERVER',
  249. service_id: 'NAGIOS',
  250. host_id: 'NAGIOS_host'
  251. });
  252. App.store.load(App.Service, {
  253. id: 'NAGIOS',
  254. service_name: 'NAGIOS',
  255. host_components: ['NAGIOS_SERVER_NAGIOS_host']
  256. });
  257. controller.set('nagiosWebProtocol', '');
  258. expect(controller.get('nagiosUrl')).to.equal(null);
  259. });
  260. it('NAGIOS Server host is "NAGIOS_host"', function () {
  261. App.store.load(App.Host, {
  262. id: 'NAGIOS_host',
  263. host_name: 'NAGIOS_host',
  264. host_components: ['NAGIOS_SERVER_NAGIOS_host'],
  265. public_host_name: 'NAGIOS_host'
  266. });
  267. controller.set('nagiosWebProtocol', '');
  268. expect(controller.get('nagiosUrl')).to.equal("http://NAGIOS_host/nagios");
  269. });
  270. it('singleNodeInstall = true', function () {
  271. App.set('singleNodeInstall', true);
  272. controller.set('nagiosWebProtocol', '');
  273. expect(controller.get('nagiosUrl')).to.equal("http://:42080/nagios");
  274. });
  275. it('singleNodeAlias is "alias"', function () {
  276. App.set('singleNodeAlias', 'alias');
  277. controller.set('nagiosWebProtocol', '');
  278. expect(controller.get('nagiosUrl')).to.equal("http://alias:42080/nagios");
  279. });
  280. });
  281. describe('#nagiosWebProtocol', function () {
  282. var testCases = [
  283. {
  284. title: 'if ambariProperties is null then nagiosWebProtocol should be "http"',
  285. data: null,
  286. result: 'http'
  287. },
  288. {
  289. title: 'if ambariProperties is empty object then nagiosWebProtocol should be "http"',
  290. data: {},
  291. result: 'http'
  292. },
  293. {
  294. title: 'if nagios.https is false then nagiosWebProtocol should be "http"',
  295. data: {'nagios.https': false},
  296. result: 'http'
  297. },
  298. {
  299. title: 'if nagios.https is true then nagiosWebProtocol should be "http"',
  300. data: {'nagios.https': true},
  301. result: 'https'
  302. }
  303. ];
  304. testCases.forEach(function (test) {
  305. it(test.title, function () {
  306. controller.set('ambariProperties', test.data);
  307. expect(controller.get('nagiosWebProtocol')).to.equal(test.result);
  308. });
  309. });
  310. });
  311. describe('#gangliaWebProtocol', function () {
  312. var testCases = [
  313. {
  314. title: 'if ambariProperties is null then nagiosWebProtocol should be "http"',
  315. data: null,
  316. result: 'http'
  317. },
  318. {
  319. title: 'if ambariProperties is empty object then nagiosWebProtocol should be "http"',
  320. data: {},
  321. result: 'http'
  322. },
  323. {
  324. title: 'if nagios.https is false then nagiosWebProtocol should be "http"',
  325. data: {'ganglia.https': false},
  326. result: 'http'
  327. },
  328. {
  329. title: 'if nagios.https is true then nagiosWebProtocol should be "http"',
  330. data: {'ganglia.https': true},
  331. result: 'https'
  332. }
  333. ];
  334. testCases.forEach(function (test) {
  335. it(test.title, function () {
  336. controller.set('ambariProperties', test.data);
  337. expect(controller.get('gangliaWebProtocol')).to.equal(test.result);
  338. });
  339. });
  340. });
  341. describe('#startPolling()', function () {
  342. beforeEach(function () {
  343. sinon.spy(App.updater, 'run');
  344. });
  345. afterEach(function () {
  346. App.updater.run.restore();
  347. });
  348. it('isWorking = false', function () {
  349. controller.set('isWorking', false);
  350. expect(App.updater.run.calledOnce).to.equal(false);
  351. expect(controller.startPolling()).to.equal(false);
  352. });
  353. it('isWorking = true', function () {
  354. controller.set('isWorking', true);
  355. expect(App.updater.run.calledOnce).to.equal(true);
  356. expect(controller.startPolling()).to.equal(true);
  357. });
  358. });
  359. describe('#deferServiceMetricsLoad()', function () {
  360. beforeEach(function () {
  361. sinon.spy(App.serviceMetricsMapper, 'map');
  362. });
  363. afterEach(function () {
  364. App.serviceMetricsMapper.map.restore();
  365. });
  366. it('json is null', function () {
  367. controller.set('serviceMetricsJson', {});
  368. controller.set('dataLoadList.serviceMetrics', false);
  369. controller.deferServiceMetricsLoad(null);
  370. expect(controller.get('serviceMetricsJson')).to.equal(null);
  371. expect(controller.get('dataLoadList.serviceMetrics')).to.equal(true);
  372. expect(App.serviceMetricsMapper.map.calledOnce).to.equal(true);
  373. });
  374. it('json is correct', function () {
  375. controller.deferServiceMetricsLoad({data: ''});
  376. expect(controller.get('serviceMetricsJson')).to.eql({data: ''});
  377. });
  378. it('json is correct and dataLoadList.hosts is true', function () {
  379. controller.set('serviceMetricsJson', {});
  380. controller.set('dataLoadList.serviceMetrics', false);
  381. controller.set('dataLoadList.hosts', true);
  382. controller.deferServiceMetricsLoad(null);
  383. expect(controller.get('dataLoadList.serviceMetrics')).to.equal(true);
  384. expect(App.serviceMetricsMapper.map.calledOnce).to.equal(true);
  385. });
  386. });
  387. describe('#clusterName', function () {
  388. var testCases = [
  389. {
  390. title: 'if cluster is null then clusterName should be null',
  391. data: null,
  392. result: null
  393. },
  394. {
  395. title: 'if cluster.Clusters.cluster_name is null then clusterName should be null',
  396. data: {
  397. Clusters: {
  398. cluster_name: null
  399. }
  400. },
  401. result: null
  402. },
  403. {
  404. title: 'if cluster.Clusters.cluster_name is null then clusterName should be null',
  405. data: {
  406. Clusters: {
  407. cluster_name: 'mycluster'
  408. }
  409. },
  410. result: 'mycluster'
  411. }
  412. ];
  413. testCases.forEach(function (test) {
  414. it(test.title, function () {
  415. controller.set('cluster', test.data);
  416. expect(controller.get('clusterName')).to.equal(test.result);
  417. });
  418. });
  419. });
  420. });