step3_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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/main/admin/security/add/step3');
  20. var stringUtils = require('utils/string_utils');
  21. var modelSetup = require('test/init_model_test');
  22. describe('App.MainAdminSecurityAddStep3Controller', function () {
  23. var controller = App.MainAdminSecurityAddStep3Controller.create({
  24. content: {}
  25. });
  26. describe('#openInfoInNewTab()', function() {
  27. it('Correct data', function() {
  28. var mock = {
  29. document: {
  30. write: function(){}
  31. },
  32. focus: function(){}
  33. };
  34. sinon.stub(window, 'open', function () {
  35. return mock;
  36. });
  37. sinon.stub(stringUtils, 'arrayToCSV', function () {
  38. return 'CSV_CONTENT';
  39. });
  40. sinon.spy(mock.document, 'write');
  41. sinon.spy(mock, 'focus');
  42. controller.set('hostComponents', ['comp1']);
  43. controller.openInfoInNewTab();
  44. expect(window.open.calledWith('')).to.be.true;
  45. expect(stringUtils.arrayToCSV.calledWith(['comp1'])).to.be.true;
  46. expect(mock.document.write.calledWith('CSV_CONTENT')).to.be.true;
  47. expect(mock.focus.calledOnce).to.be.true;
  48. window.open.restore();
  49. stringUtils.arrayToCSV.restore();
  50. });
  51. });
  52. describe('#loadStep()', function() {
  53. beforeEach(function(){
  54. sinon.stub(controller, 'getSecurityUsers', function () {
  55. return [{
  56. name: 'user_group',
  57. value: 'value1'
  58. }];
  59. });
  60. });
  61. afterEach(function(){
  62. controller.getSecurityUsers.restore();
  63. });
  64. it('No hosts installed', function() {
  65. controller.set('hosts', []);
  66. controller.loadStep();
  67. expect(controller.get('hostComponents')).to.be.empty;
  68. });
  69. it('One host installed', function () {
  70. controller.set('hosts', [Em.Object.create({hostName: 'host1'})]);
  71. sinon.stub(controller, 'setMandatoryConfigs', function (result) {
  72. return result.push('setMandatoryConfigs');
  73. });
  74. sinon.stub(controller, 'setComponentsConfig', function (result) {
  75. return result.push('setComponentsConfig');
  76. });
  77. sinon.stub(controller, 'setHostComponentsSecureValue', function (result) {
  78. return result.push('setHostComponentsSecureValue');
  79. });
  80. controller.loadStep();
  81. expect(controller.setMandatoryConfigs.calledOnce).to.be.true;
  82. expect(controller.setComponentsConfig.calledOnce).to.be.true;
  83. expect(controller.setHostComponentsSecureValue.calledOnce).to.be.true;
  84. expect(controller.get('hostComponents')).to.eql(["setMandatoryConfigs", "setComponentsConfig", "setHostComponentsSecureValue"]);
  85. controller.setMandatoryConfigs.restore();
  86. controller.setComponentsConfig.restore();
  87. controller.setHostComponentsSecureValue.restore();
  88. });
  89. });
  90. describe('#buildComponentToOwnerMap()', function() {
  91. it('componentToUserMap is empty', function() {
  92. controller.set('componentToUserMap', {});
  93. expect(controller.buildComponentToOwnerMap([])).to.eql({});
  94. });
  95. it('componentToUserMap has properties', function() {
  96. controller.set('componentToUserMap', {'COMP1': 'config1'});
  97. var securityUsers = [{
  98. name: 'config1',
  99. value: 'value1'
  100. }];
  101. expect(controller.buildComponentToOwnerMap(securityUsers)).to.eql({'COMP1': 'value1'});
  102. });
  103. });
  104. describe('#setComponentsConfig()', function() {
  105. beforeEach(function(){
  106. modelSetup.setupStackServiceComponent();
  107. controller.set('content.serviceConfigProperties', [
  108. {
  109. serviceName: 'HDFS',
  110. name: 'principal1',
  111. value: '_HOST'
  112. },
  113. {
  114. serviceName: 'HDFS',
  115. name: 'keytab1',
  116. value: 'value1'
  117. }
  118. ]);
  119. });
  120. afterEach(function() {
  121. modelSetup.cleanStackServiceComponent();
  122. });
  123. it('componentToConfigMap is empty', function() {
  124. controller.reopen({
  125. componentToConfigMap: []
  126. });
  127. var result = [];
  128. controller.setComponentsConfig(result, Em.Object.create({hostName: 'c6401',hostComponents: []}), 'hadoopGroupId');
  129. expect(result).to.be.empty;
  130. });
  131. it('isHadoop2Stack = false, when component from stack2', function() {
  132. sinon.stub(App, 'get', function () {
  133. return false;
  134. });
  135. controller.reopen({
  136. componentToConfigMap: [{
  137. componentName: 'DATANODE',
  138. principal: 'principal1',
  139. keytab: 'keytab1',
  140. displayName: 'displayName1',
  141. isHadoop2Stack: true
  142. }]
  143. });
  144. var host = Em.Object.create({
  145. hostComponents: [{componentName: 'DATANODE'}],
  146. hostName: 'host1'
  147. });
  148. var result = [];
  149. controller.setComponentsConfig(result, host, 'hadoopGroupId');
  150. expect(result).to.be.empty;
  151. App.get.restore();
  152. });
  153. it('isHadoop2Stack = true, when component from stack2', function() {
  154. sinon.stub(App, 'get', function () {
  155. return true;
  156. });
  157. controller.reopen({
  158. componentToConfigMap: [{
  159. componentName: 'DATANODE',
  160. principal: 'principal1',
  161. keytab: 'keytab1',
  162. displayName: 'displayName1',
  163. isHadoop2Stack: true
  164. }]
  165. });
  166. var host = Em.Object.create({
  167. hostComponents: [{componentName: 'DATANODE'}],
  168. hostName: 'host1'
  169. });
  170. var result = [];
  171. controller.setComponentsConfig(result, host, 'hadoopGroupId');
  172. expect(result.length).to.equal(1);
  173. App.get.restore();
  174. });
  175. it('Component does not match host-component', function() {
  176. controller.reopen({
  177. componentToConfigMap: [{
  178. componentName: 'DATANODE',
  179. principal: 'principal1',
  180. keytab: 'keytab1',
  181. displayName: 'displayName1'
  182. }]
  183. });
  184. var host = Em.Object.create({
  185. hostComponents: [{componentName: 'DATANODE1'}],
  186. hostName: 'host1'
  187. });
  188. var result = [];
  189. controller.setComponentsConfig(result, host, 'hadoopGroupId');
  190. expect(result).to.be.empty;
  191. });
  192. it('Component matches host-component', function() {
  193. controller.reopen({
  194. componentToConfigMap: [{
  195. componentName: 'DATANODE',
  196. principal: 'principal1',
  197. keytab: 'keytab1',
  198. displayName: 'displayName1'
  199. }]
  200. });
  201. var host = Em.Object.create({
  202. hostComponents: [{componentName: 'DATANODE'}],
  203. hostName: 'host1'
  204. });
  205. var result = [];
  206. controller.setComponentsConfig(result, host, 'hadoopGroupId');
  207. expect(result.length).to.equal(1);
  208. });
  209. });
  210. describe('#setMandatoryConfigs()', function() {
  211. beforeEach(function () {
  212. sinon.stub(App.Service, 'find', function () {
  213. return [
  214. {serviceName: 'SERVICE1'}
  215. ];
  216. });
  217. controller.set('content.serviceConfigProperties', [
  218. {
  219. serviceName: 'GENERAL',
  220. name: 'kerberos_domain',
  221. value: 'realm1'
  222. }
  223. ]);
  224. });
  225. afterEach(function () {
  226. App.Service.find.restore();
  227. });
  228. it('mandatoryConfigs is empty', function() {
  229. var result = [];
  230. controller.set('mandatoryConfigs', []);
  231. controller.setMandatoryConfigs(result, [], '', '');
  232. expect(result).to.be.empty;
  233. });
  234. it('config has unknown service to check', function() {
  235. var result = [];
  236. controller.set('mandatoryConfigs', [{
  237. userConfig: 'kerberos_domain',
  238. keytab: 'kerberos_domain',
  239. displayName: '',
  240. checkService: 'HBASE'
  241. }]);
  242. controller.setMandatoryConfigs(result, [], '', '');
  243. expect(result).to.be.empty;
  244. });
  245. it('config should be added', function() {
  246. var result = [];
  247. controller.set('mandatoryConfigs', [{
  248. userConfig: 'userConfig1',
  249. keytab: 'kerberos_domain',
  250. displayName: ''
  251. }]);
  252. var securityUsers = [{
  253. name: 'userConfig1',
  254. value: 'value1'
  255. }];
  256. controller.setMandatoryConfigs(result, securityUsers, '', '');
  257. expect(result.length).to.equal(1);
  258. });
  259. });
  260. describe('#setHostComponentsSecureValue()', function() {
  261. beforeEach(function () {
  262. sinon.stub(controller, 'buildComponentToOwnerMap', Em.K);
  263. sinon.stub(controller, 'changeDisplayName', Em.K);
  264. sinon.stub(controller, 'getSecureProperties', function(){
  265. return {principal: '', keytab: ''};
  266. });
  267. });
  268. afterEach(function () {
  269. controller.buildComponentToOwnerMap.restore();
  270. controller.changeDisplayName.restore();
  271. controller.getSecureProperties.restore();
  272. });
  273. it('host.hostComponents is empty', function() {
  274. var result = [];
  275. var host = Em.Object.create({
  276. hostComponents: []
  277. });
  278. controller.setHostComponentsSecureValue(result, host);
  279. expect(result).to.be.empty;
  280. });
  281. it('host-component does not match component to display', function() {
  282. var result = [];
  283. var host = Em.Object.create({
  284. hostComponents: [Em.Object.create({
  285. componentName: 'UNKNOWN'
  286. })]
  287. });
  288. controller.setHostComponentsSecureValue(result, host);
  289. expect(result).to.be.empty;
  290. });
  291. it('host-component matches component to display', function() {
  292. var result = [];
  293. var host = Em.Object.create({
  294. hostComponents: [Em.Object.create({
  295. componentName: 'DATANODE'
  296. })]
  297. });
  298. controller.setHostComponentsSecureValue(result, host, {}, [], '');
  299. expect(result.length).to.equal(1);
  300. });
  301. it('addedPrincipalsHost already contain such config', function() {
  302. var result = [];
  303. var host = Em.Object.create({
  304. hostName: 'host1',
  305. hostComponents: [Em.Object.create({
  306. componentName: 'DATANODE'
  307. })]
  308. });
  309. controller.setHostComponentsSecureValue(result, host, {'host1--': true}, [], '');
  310. expect(result.length).to.be.empty;
  311. });
  312. });
  313. describe('#getSecureProperties()', function () {
  314. beforeEach(function () {
  315. sinon.stub(controller, 'getPrincipal', function () {
  316. return 'principal';
  317. });
  318. });
  319. afterEach(function () {
  320. controller.getPrincipal.restore();
  321. });
  322. var testCases = [
  323. {
  324. title: 'serviceConfigs is empty',
  325. content: {
  326. serviceConfigs: [],
  327. componentName: ''
  328. },
  329. result: {}
  330. },
  331. {
  332. title: 'Config has component that does not match component name',
  333. content: {
  334. serviceConfigs: [{
  335. component: 'comp1'
  336. }],
  337. componentName: 'comp2'
  338. },
  339. result: {}
  340. },
  341. {
  342. title: 'Config has components that does not match component name',
  343. content: {
  344. serviceConfigs: [{
  345. components: ['comp1']
  346. }],
  347. componentName: 'comp2'
  348. },
  349. result: {}
  350. },
  351. {
  352. title: 'Config has component that matches component name',
  353. content: {
  354. serviceConfigs: [{
  355. name: 'C_principal_name',
  356. component: 'comp1',
  357. value: 'value1'
  358. }],
  359. componentName: 'comp1'
  360. },
  361. result: {
  362. principal: 'principal'
  363. }
  364. },
  365. {
  366. title: 'Config has components that matches component name',
  367. content: {
  368. serviceConfigs: [{
  369. name: 'C_principal_name',
  370. components: ['comp1'],
  371. value: 'value1'
  372. }],
  373. componentName: 'comp1'
  374. },
  375. result: {
  376. principal: 'principal'
  377. }
  378. },
  379. {
  380. title: 'Config name without correct postfix',
  381. content: {
  382. serviceConfigs: [{
  383. name: 'config1',
  384. component: 'comp1',
  385. value: 'value1'
  386. }],
  387. componentName: 'comp1'
  388. },
  389. result: {}
  390. },
  391. {
  392. title: 'Config name with "_keytab" postfix',
  393. content: {
  394. serviceConfigs: [{
  395. name: 'c_keytab',
  396. component: 'comp1',
  397. value: 'value1'
  398. }],
  399. componentName: 'comp1'
  400. },
  401. result: {
  402. keytab: 'value1'
  403. }
  404. },
  405. {
  406. title: 'Config name with "_keytab_path" postfix',
  407. content: {
  408. serviceConfigs: [{
  409. name: 'c_keytab_path',
  410. component: 'comp1',
  411. value: 'value1'
  412. }],
  413. componentName: 'comp1'
  414. },
  415. result: {
  416. keytab: 'value1'
  417. }
  418. }
  419. ];
  420. testCases.forEach(function (test) {
  421. it(test.title, function () {
  422. expect(controller.getSecureProperties(test.content.serviceConfigs, test.content.componentName, '')).to.eql(test.result);
  423. });
  424. });
  425. });
  426. describe('#getPrincipal()', function () {
  427. var testCases = [
  428. {
  429. title: 'Config value missing "_HOST" string, unit is empty',
  430. content: {
  431. config: {
  432. value: 'value1',
  433. unit: ''
  434. },
  435. hostName: ''
  436. },
  437. result: 'value1'
  438. },
  439. {
  440. title: 'Config value missing "_HOST" string, unit is correct',
  441. content: {
  442. config: {
  443. value: 'value1',
  444. unit: 'unit1'
  445. },
  446. hostName: ''
  447. },
  448. result: 'value1unit1'
  449. },
  450. {
  451. title: 'Config value contains "_HOST" string, host name in lowercase',
  452. content: {
  453. config: {
  454. value: '_HOST',
  455. unit: 'unit1'
  456. },
  457. hostName: 'host1'
  458. },
  459. result: 'host1unit1'
  460. },
  461. {
  462. title: 'Config value contains "_HOST" string, host name in uppercase',
  463. content: {
  464. config: {
  465. value: '_HOST',
  466. unit: 'unit1'
  467. },
  468. hostName: 'HOST1'
  469. },
  470. result: 'host1unit1'
  471. }
  472. ];
  473. testCases.forEach(function (test) {
  474. it(test.title, function () {
  475. expect(controller.getPrincipal(test.content.config, test.content.hostName)).to.equal(test.result);
  476. });
  477. });
  478. });
  479. describe('#changeDisplayName()', function() {
  480. it('name is HiveServer2', function() {
  481. expect(controller.changeDisplayName('HiveServer2')).to.equal('Hive Metastore and HiveServer2');
  482. });
  483. it('name is not HiveServer2', function() {
  484. expect(controller.changeDisplayName('something')).to.equal('something');
  485. });
  486. });
  487. describe('#getSecurityUsers()', function () {
  488. it('testMode is true, testModeUsers is empty', function () {
  489. controller.set('testModeUsers', []);
  490. App.testMode = true;
  491. expect(controller.getSecurityUsers()).to.eql([]);
  492. });
  493. it('testMode is true, testModeUsers is correct', function () {
  494. controller.set('testModeUsers', [
  495. {
  496. name: 'user1',
  497. value: 'value1'
  498. }
  499. ]);
  500. App.testMode = true;
  501. expect(controller.getSecurityUsers()).to.eql([
  502. {
  503. id: 'puppet var',
  504. name: 'user1',
  505. value: 'value1'
  506. }
  507. ]);
  508. });
  509. it('testMode is false', function () {
  510. sinon.stub(App.db, 'getSecureUserInfo', function () {
  511. return [{}];
  512. });
  513. App.testMode = false;
  514. expect(controller.getSecurityUsers()).to.eql([{}]);
  515. expect(App.db.getSecureUserInfo.calledOnce).to.be.true;
  516. App.db.getSecureUserInfo.restore();
  517. });
  518. });
  519. });