cluster_controller_test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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: 'NAGIOS'}
  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('#setNagiosUrl()', function () {
  226. beforeEach(function () {
  227. controller.set('nagiosUrl', null);
  228. });
  229. it('testMode = true', function () {
  230. App.testMode = true;
  231. controller.setNagiosUrl();
  232. expect(controller.get('nagiosUrl')).to.equal('http://nagiosserver/nagios');
  233. expect(controller.get('isNagiosUrlLoaded')).to.be.true;
  234. });
  235. it('Cluster is not loaded', function () {
  236. App.testMode = false;
  237. controller.set('isLoaded', false);
  238. controller.setNagiosUrl();
  239. expect(controller.get('nagiosUrl')).to.equal(null);
  240. });
  241. it('GANGLIA_SERVER component is absent', function () {
  242. controller.set('isLoaded', true);
  243. App.testMode = false;
  244. sinon.stub(App.HostComponent, 'find', function(){
  245. return [];
  246. });
  247. controller.setNagiosUrl();
  248. expect(controller.get('nagiosUrl')).to.equal(null);
  249. App.HostComponent.find.restore();
  250. });
  251. it('Ganglia Server host is "NAGIOS_host"', function () {
  252. controller.set('isLoaded', true);
  253. App.testMode = false;
  254. sinon.stub(App.HostComponent, 'find', function(){
  255. return [Em.Object.create({
  256. componentName: 'NAGIOS_SERVER',
  257. hostName: 'NAGIOS_host'
  258. })];
  259. });
  260. sinon.spy(App.ajax, 'send');
  261. controller.setNagiosUrl();
  262. expect(App.ajax.send.calledOnce).to.be.true;
  263. expect(controller.get('isNagiosUrlLoaded')).to.be.false;
  264. App.ajax.send.restore();
  265. App.HostComponent.find.restore();
  266. });
  267. });
  268. describe('#nagiosWebProtocol', function () {
  269. var testCases = [
  270. {
  271. title: 'if ambariProperties is null then nagiosWebProtocol should be "http"',
  272. data: null,
  273. result: 'http'
  274. },
  275. {
  276. title: 'if ambariProperties is empty object then nagiosWebProtocol should be "http"',
  277. data: {},
  278. result: 'http'
  279. },
  280. {
  281. title: 'if nagios.https is false then nagiosWebProtocol should be "http"',
  282. data: {'nagios.https': false},
  283. result: 'http'
  284. },
  285. {
  286. title: 'if nagios.https is true then nagiosWebProtocol should be "http"',
  287. data: {'nagios.https': true},
  288. result: 'https'
  289. }
  290. ];
  291. testCases.forEach(function (test) {
  292. it(test.title, function () {
  293. controller.set('ambariProperties', test.data);
  294. expect(controller.get('nagiosWebProtocol')).to.equal(test.result);
  295. });
  296. });
  297. });
  298. describe('#gangliaWebProtocol', function () {
  299. var testCases = [
  300. {
  301. title: 'if ambariProperties is null then nagiosWebProtocol should be "http"',
  302. data: null,
  303. result: 'http'
  304. },
  305. {
  306. title: 'if ambariProperties is empty object then nagiosWebProtocol should be "http"',
  307. data: {},
  308. result: 'http'
  309. },
  310. {
  311. title: 'if nagios.https is false then nagiosWebProtocol should be "http"',
  312. data: {'ganglia.https': false},
  313. result: 'http'
  314. },
  315. {
  316. title: 'if nagios.https is true then nagiosWebProtocol should be "http"',
  317. data: {'ganglia.https': true},
  318. result: 'https'
  319. }
  320. ];
  321. testCases.forEach(function (test) {
  322. it(test.title, function () {
  323. controller.set('ambariProperties', test.data);
  324. expect(controller.get('gangliaWebProtocol')).to.equal(test.result);
  325. });
  326. });
  327. });
  328. describe('#setGangliaUrlSuccessCallback()', function () {
  329. it('Query return no hosts', function () {
  330. controller.setGangliaUrlSuccessCallback({items: []});
  331. expect(controller.get('gangliaUrl')).to.equal(null);
  332. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  333. });
  334. it('App.singleNodeInstall is true', function () {
  335. controller.reopen({
  336. gangliaWebProtocol: 'http'
  337. });
  338. App.set('singleNodeInstall', true);
  339. App.set('singleNodeAlias', 'localhost');
  340. controller.setGangliaUrlSuccessCallback({items: [{
  341. Hosts: {
  342. public_host_name: 'host1'
  343. }
  344. }]});
  345. expect(controller.get('gangliaUrl')).to.equal('http://localhost:42080/ganglia');
  346. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  347. });
  348. it('App.singleNodeInstall is false', function () {
  349. controller.reopen({
  350. gangliaWebProtocol: 'http'
  351. });
  352. App.set('singleNodeInstall', false);
  353. App.set('singleNodeAlias', 'localhost');
  354. controller.setGangliaUrlSuccessCallback({items: [{
  355. Hosts: {
  356. public_host_name: 'host1'
  357. }
  358. }]});
  359. expect(controller.get('gangliaUrl')).to.equal('http://host1/ganglia');
  360. expect(controller.get('isGangliaUrlLoaded')).to.be.true;
  361. });
  362. });
  363. describe('#setNagiosUrlSuccessCallback()', function () {
  364. it('Query return no hosts', function () {
  365. controller.setNagiosUrlSuccessCallback({items: []});
  366. expect(controller.get('nagiosUrl')).to.equal(null);
  367. expect(controller.get('isNagiosUrlLoaded')).to.be.true;
  368. });
  369. it('App.singleNodeInstall is true', function () {
  370. controller.reopen({
  371. nagiosWebProtocol: 'http'
  372. });
  373. App.set('singleNodeInstall', true);
  374. App.set('singleNodeAlias', 'localhost');
  375. controller.setNagiosUrlSuccessCallback({items: [{
  376. Hosts: {
  377. public_host_name: 'host1'
  378. }
  379. }]});
  380. expect(controller.get('nagiosUrl')).to.equal('http://localhost:42080/nagios');
  381. expect(controller.get('isNagiosUrlLoaded')).to.be.true;
  382. });
  383. it('App.singleNodeInstall is false', function () {
  384. controller.reopen({
  385. nagiosWebProtocol: 'http'
  386. });
  387. App.set('singleNodeInstall', false);
  388. App.set('singleNodeAlias', 'localhost');
  389. controller.setNagiosUrlSuccessCallback({items: [{
  390. Hosts: {
  391. public_host_name: 'host1'
  392. }
  393. }]});
  394. expect(controller.get('nagiosUrl')).to.equal('http://host1/nagios');
  395. expect(controller.get('isNagiosUrlLoaded')).to.be.true;
  396. });
  397. });
  398. });