cluster_controller_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. var credentialUtils = require('utils/credentials');
  20. var testHelpers = require('test/helpers');
  21. require('controllers/global/cluster_controller');
  22. require('models/host_component');
  23. require('utils/http_client');
  24. require('models/service');
  25. require('models/host');
  26. require('utils/ajax/ajax');
  27. require('utils/string_utils');
  28. var modelSetup = require('test/init_model_test');
  29. describe('App.clusterController', function () {
  30. var controller = App.ClusterController.create();
  31. App.Service.FIXTURES = [
  32. {service_name: 'GANGLIA'}
  33. ];
  34. App.TestAliases.testAsComputedAnd(controller, 'isHostContentLoaded', ['isHostsLoaded', 'isComponentsStateLoaded']);
  35. App.TestAliases.testAsComputedAnd(controller, 'isServiceContentFullyLoaded', ['isServiceMetricsLoaded', 'isComponentsStateLoaded', 'isComponentsConfigLoaded']);
  36. App.TestAliases.testAsComputedAlias(controller, 'clusterName', 'App.clusterName', 'string');
  37. describe('#updateLoadStatus()', function () {
  38. controller.set('dataLoadList', Em.Object.create({
  39. 'item1': false,
  40. 'item2': false
  41. }));
  42. it('when none item is loaded then width should be "width:0"', function () {
  43. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:0');
  44. });
  45. it('when first item is loaded then isLoaded should be false', function () {
  46. controller.updateLoadStatus('item1');
  47. expect(controller.get('isLoaded')).to.equal(false);
  48. });
  49. it('when first item is loaded then width should be "width:50%"', function () {
  50. controller.updateLoadStatus('item1');
  51. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:50%');
  52. });
  53. it('when all items are loaded then isLoaded should be true', function () {
  54. controller.updateLoadStatus('item2');
  55. expect(controller.get('isLoaded')).to.equal(true);
  56. });
  57. it('when all items are loaded then width should be "width:100%"', function () {
  58. controller.updateLoadStatus('item2');
  59. expect(controller.get('clusterDataLoadedPercent')).to.equal('width:100%');
  60. });
  61. });
  62. describe('#loadClusterName()', function () {
  63. beforeEach(function () {
  64. modelSetup.setupStackVersion(this, 'HDP-2.0.5');
  65. App.ajax.send.restore(); // default ajax-mock can't be used here
  66. sinon.stub(App.ajax, 'send', function () {
  67. return {
  68. then: function (successCallback) {
  69. App.set('clusterName', 'clusterNameFromServer');
  70. App.set('currentStackVersion', 'HDP-2.0.5');
  71. successCallback();
  72. }
  73. }
  74. });
  75. this.args = testHelpers.findAjaxRequest('name', 'cluster.load_cluster_name');
  76. });
  77. afterEach(function () {
  78. modelSetup.restoreStackVersion(this);
  79. });
  80. it('if clusterName is "mycluster" and reload is false then clusterName stays the same', function () {
  81. App.set('clusterName', 'mycluster');
  82. controller.loadClusterName(false);
  83. expect(this.args).to.not.exists;
  84. expect(App.get('clusterName')).to.equal('mycluster');
  85. });
  86. it('reload is true and clusterName is not empty', function () {
  87. controller.loadClusterName(true);
  88. expect(this.args).to.exists;
  89. expect(App.get('clusterName')).to.equal('clusterNameFromServer');
  90. expect(App.get('currentStackVersion')).to.equal('HDP-2.0.5');
  91. });
  92. it('reload is false and clusterName is empty', function () {
  93. App.set('clusterName', '');
  94. controller.loadClusterName(false);
  95. expect(this.args).to.exists;
  96. expect(App.get('clusterName')).to.equal('clusterNameFromServer');
  97. expect(App.get('currentStackVersion')).to.equal('HDP-2.0.5');
  98. });
  99. });
  100. describe('#reloadSuccessCallback', function () {
  101. var testData = {
  102. "items": [
  103. {
  104. "Clusters": {
  105. "cluster_name": "tdk",
  106. "version": "HDP-1.3.0"
  107. }
  108. }
  109. ]
  110. };
  111. it('Check cluster', function () {
  112. controller.reloadSuccessCallback(testData);
  113. expect(App.get('clusterName')).to.equal('tdk');
  114. expect(App.get('currentStackVersion')).to.equal('HDP-1.3.0');
  115. });
  116. });
  117. describe('#getServerClockSuccessCallback()', function () {
  118. var testCases = [
  119. {
  120. title: 'if server clock is 1 then currentServerTime should be 1000',
  121. data: {
  122. RootServiceComponents: {
  123. server_clock: 1
  124. }
  125. },
  126. result: 1000
  127. },
  128. {
  129. title: 'if server clock is 0 then currentServerTime should be 0',
  130. data: {
  131. RootServiceComponents: {
  132. server_clock: 0
  133. }
  134. },
  135. result: 0
  136. },
  137. {
  138. title: 'if server clock is 111111111111 then currentServerTime should be 111111111111000',
  139. data: {
  140. RootServiceComponents: {
  141. server_clock: 111111111111
  142. }
  143. },
  144. result: 111111111111000
  145. },
  146. {
  147. title: 'if server clock is 1111111111113 then currentServerTime should be 1111111111113',
  148. data: {
  149. RootServiceComponents: {
  150. server_clock: 1111111111113
  151. }
  152. },
  153. result: 1111111111113
  154. }
  155. ];
  156. var currentServerTime = App.get('currentServerTime');
  157. var clockDistance = App.get('clockDistance');
  158. testCases.forEach(function (test) {
  159. it(test.title, function () {
  160. controller.getServerClockSuccessCallback(test.data);
  161. expect(App.get('currentServerTime')).to.equal(test.result);
  162. App.set('clockDistance', clockDistance);
  163. App.set('currentServerTime', currentServerTime);
  164. });
  165. });
  166. });
  167. describe('#getUrl', function () {
  168. controller.set('clusterName', 'tdk');
  169. var tests = ['test1', 'test2', 'test3'];
  170. it('testMode = false', function () {
  171. tests.forEach(function (test) {
  172. expect(controller.getUrl(test, test)).to.equal(App.apiPrefix + '/clusters/' + controller.get('clusterName') + test);
  173. });
  174. });
  175. });
  176. describe("#createKerberosAdminSession()", function() {
  177. beforeEach(function () {
  178. sinon.stub(credentialUtils, 'createOrUpdateCredentials', function() {
  179. return $.Deferred().resolve().promise();
  180. });
  181. this.stub = sinon.stub(App, 'get');
  182. this.stub.withArgs('clusterName').returns('test');
  183. });
  184. afterEach(function () {
  185. credentialUtils.createOrUpdateCredentials.restore();
  186. App.get.restore();
  187. });
  188. it("KDC Store supports disabled, credentials updated via kdc session call", function() {
  189. this.stub.withArgs('supports.storeKDCCredentials').returns(false);
  190. controller.createKerberosAdminSession({
  191. principal: 'admin',
  192. key: 'pass',
  193. type: 'persistent'
  194. }, {});
  195. var args = testHelpers.findAjaxRequest('name', 'common.cluster.update');
  196. expect(args[0]).to.exists;
  197. expect(args[0].sender).to.be.eql(controller);
  198. expect(args[0].data).to.be.eql({
  199. clusterName: 'test',
  200. data: [{
  201. session_attributes: {
  202. kerberos_admin: {principal: "admin", password: "pass"}
  203. }
  204. }]
  205. });
  206. });
  207. it("KDC Store supports enabled, credentials updated via credentials storage call", function() {
  208. this.stub.withArgs('supports.storeKDCCredentials').returns(true);
  209. controller.createKerberosAdminSession({
  210. principal: 'admin',
  211. key: 'pass',
  212. type: 'persistent'
  213. }, {});
  214. var args = testHelpers.findAjaxRequest('name', 'common.cluster.update');
  215. expect(args).to.not.exists;
  216. expect(credentialUtils.createOrUpdateCredentials.getCall(0).args).to.eql([
  217. 'test', 'kdc.admin.credential', {
  218. principal: 'admin',
  219. key: 'pass',
  220. type: 'persistent'
  221. }
  222. ]);
  223. });
  224. });
  225. describe('#checkDetailedRepoVersion()', function () {
  226. var cases = [
  227. {
  228. currentStackName: 'HDP',
  229. currentStackVersionNumber: '2.1',
  230. isStormMetricsSupported: false,
  231. title: 'HDP < 2.2'
  232. },
  233. {
  234. currentStackName: 'HDP',
  235. currentStackVersionNumber: '2.3',
  236. isStormMetricsSupported: true,
  237. title: 'HDP > 2.2'
  238. },
  239. {
  240. currentStackName: 'BIGTOP',
  241. currentStackVersionNumber: '0.8',
  242. isStormMetricsSupported: true,
  243. title: 'not HDP'
  244. }
  245. ];
  246. afterEach(function () {
  247. App.get.restore();
  248. });
  249. describe('should check detailed repo version for HDP 2.2', function () {
  250. beforeEach(function () {
  251. sinon.stub(App, 'get').withArgs('currentStackName').returns('HDP').withArgs('currentStackVersionNumber').returns('2.2');
  252. });
  253. it('request is sent', function () {
  254. controller.checkDetailedRepoVersion();
  255. var args = testHelpers.findAjaxRequest('name', 'cluster.load_detailed_repo_version');
  256. expect(args).to.exists;
  257. });
  258. });
  259. cases.forEach(function (item) {
  260. describe(item.title, function () {
  261. beforeEach(function () {
  262. sinon.stub(App, 'get', function (key) {
  263. return item[key] || Em.get(App, key);
  264. });
  265. controller.checkDetailedRepoVersion();
  266. });
  267. it('request is not sent', function () {
  268. var args = testHelpers.findAjaxRequest('name', 'cluster.load_detailed_repo_version');
  269. expect(args).to.not.exists;
  270. });
  271. it('App.isStormMetricsSupported is ' + item.isStormMetricsSupported, function () {
  272. expect(App.get('isStormMetricsSupported')).to.equal(item.isStormMetricsSupported);
  273. });
  274. });
  275. });
  276. });
  277. describe('#checkDetailedRepoVersionSuccessCallback()', function () {
  278. var cases = [
  279. {
  280. items: [
  281. {
  282. repository_versions: [
  283. {
  284. RepositoryVersions: {
  285. repository_version: '2.1'
  286. }
  287. }
  288. ]
  289. }
  290. ],
  291. isStormMetricsSupported: false,
  292. title: 'HDP < 2.2.2'
  293. },
  294. {
  295. items: [
  296. {
  297. repository_versions: [
  298. {
  299. RepositoryVersions: {
  300. repository_version: '2.2.2'
  301. }
  302. }
  303. ]
  304. }
  305. ],
  306. isStormMetricsSupported: true,
  307. title: 'HDP 2.2.2'
  308. },
  309. {
  310. items: [
  311. {
  312. repository_versions: [
  313. {
  314. RepositoryVersions: {
  315. repository_version: '2.2.3'
  316. }
  317. }
  318. ]
  319. }
  320. ],
  321. isStormMetricsSupported: true,
  322. title: 'HDP > 2.2.2'
  323. },
  324. {
  325. items: null,
  326. isStormMetricsSupported: true,
  327. title: 'empty response'
  328. },
  329. {
  330. items: [],
  331. isStormMetricsSupported: true,
  332. title: 'no items'
  333. },
  334. {
  335. items: [{}],
  336. isStormMetricsSupported: true,
  337. title: 'empty item'
  338. },
  339. {
  340. items: [{
  341. repository_versions: []
  342. }],
  343. isStormMetricsSupported: true,
  344. title: 'no versions'
  345. },
  346. {
  347. items: [{
  348. repository_versions: [{}]
  349. }],
  350. isStormMetricsSupported: true,
  351. title: 'no version info'
  352. },
  353. {
  354. items: [{
  355. repository_versions: [
  356. {
  357. RepositoryVersions: {}
  358. }
  359. ]
  360. }],
  361. isStormMetricsSupported: true,
  362. title: 'empty version info'
  363. }
  364. ];
  365. cases.forEach(function (item) {
  366. it(item.title, function () {
  367. controller.checkDetailedRepoVersionSuccessCallback({
  368. items: item.items
  369. });
  370. expect(App.get('isStormMetricsSupported')).to.equal(item.isStormMetricsSupported);
  371. });
  372. });
  373. });
  374. describe('#checkDetailedRepoVersionErrorCallback()', function () {
  375. it('should set isStormMetricsSupported to default value', function () {
  376. controller.checkDetailedRepoVersionErrorCallback();
  377. expect(App.get('isStormMetricsSupported')).to.be.true;
  378. });
  379. });
  380. describe('#getAllUpgrades()', function () {
  381. it('should send request to get upgrades data', function () {
  382. controller.getAllUpgrades();
  383. var args = testHelpers.findAjaxRequest('name', 'cluster.load_last_upgrade');
  384. expect(args).to.exists;
  385. });
  386. });
  387. describe("#restoreUpgradeState()", function() {
  388. var data = {upgradeData: {}};
  389. var mock = {
  390. done: function (callback) {
  391. callback(data.upgradeData);
  392. }
  393. };
  394. var upgradeController = Em.Object.create({
  395. restoreLastUpgrade: Em.K,
  396. initDBProperties: Em.K,
  397. loadUpgradeData: Em.K,
  398. loadStackVersionsToModel: function () {
  399. return {done: Em.K};
  400. }
  401. });
  402. beforeEach(function () {
  403. sinon.stub(controller, 'getAllUpgrades').returns(mock);
  404. sinon.spy(mock, 'done');
  405. sinon.stub(App.router, 'get').returns(upgradeController);
  406. sinon.stub(App.db, 'get').returns('PENDING');
  407. sinon.spy(upgradeController, 'restoreLastUpgrade');
  408. sinon.spy(upgradeController, 'initDBProperties');
  409. sinon.spy(upgradeController, 'loadUpgradeData');
  410. sinon.spy(upgradeController, 'loadStackVersionsToModel');
  411. });
  412. afterEach(function () {
  413. mock.done.restore();
  414. controller.getAllUpgrades.restore();
  415. App.router.get.restore();
  416. App.db.get.restore();
  417. upgradeController.restoreLastUpgrade.restore();
  418. upgradeController.initDBProperties.restore();
  419. upgradeController.loadUpgradeData.restore();
  420. upgradeController.loadStackVersionsToModel.restore();
  421. });
  422. describe("has upgrade request", function() {
  423. beforeEach(function () {
  424. data.upgradeData = {items: [
  425. {
  426. Upgrade: {
  427. request_id: 1
  428. }
  429. }
  430. ]};
  431. controller.restoreUpgradeState();
  432. });
  433. it('getAllUpgrades is called once', function () {
  434. expect(controller.getAllUpgrades.calledOnce).to.be.true;
  435. });
  436. it('upgradeState is PENDING', function () {
  437. expect(App.get('upgradeState')).to.equal('PENDING');
  438. });
  439. it('restoreLastUpgrade is called with valid arguments', function () {
  440. expect(upgradeController.restoreLastUpgrade.calledWith(data.upgradeData.items[0])).to.be.true;
  441. });
  442. it('loadStackVersionsToModel is called with valid arguments', function () {
  443. expect(upgradeController.loadStackVersionsToModel.calledWith(true)).to.be.true;
  444. });
  445. it('initDBProperties is not called', function () {
  446. expect(upgradeController.initDBProperties.called).to.be.false;
  447. });
  448. it('loadUpgradeData is not called', function () {
  449. expect(upgradeController.loadUpgradeData.called).to.be.false;
  450. });
  451. });
  452. describe("does not have upgrade request", function() {
  453. beforeEach(function () {
  454. data.upgradeData = {items: []};
  455. controller.restoreUpgradeState();
  456. });
  457. it('getAllUpgrades is called once', function () {
  458. expect(controller.getAllUpgrades.calledOnce).to.be.true;
  459. });
  460. it('upgradeState is PENDING', function () {
  461. expect(App.get('upgradeState')).to.equal('PENDING');
  462. });
  463. it('restoreLastUpgrade is not called', function () {
  464. expect(upgradeController.restoreLastUpgrade.called).to.be.false;
  465. });
  466. it('loadStackVersionsToModel is called with valid arguments', function () {
  467. expect(upgradeController.loadStackVersionsToModel.calledWith(true)).to.be.true;
  468. });
  469. it('initDBProperties is called once', function () {
  470. expect(upgradeController.initDBProperties.calledOnce).to.be.true;
  471. });
  472. it('loadUpgradeData is called with valid arguments', function () {
  473. expect(upgradeController.loadUpgradeData.calledWith(true)).to.be.true;
  474. });
  475. });
  476. });
  477. });