step3_test.js 16 KB

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