cluster_controller_test.js 17 KB

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