step3_test.js 16 KB

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