security_progress_controller_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/security_progress_controller');
  20. require('models/host_component');
  21. require('models/host');
  22. describe('App.MainAdminSecurityProgressController', function () {
  23. var controller = App.MainAdminSecurityProgressController.create({
  24. loadClusterConfigs: function () {},
  25. deleteComponents: function () {}
  26. });
  27. describe('#retry()', function () {
  28. beforeEach(function () {
  29. sinon.spy(controller, "startCommand");
  30. });
  31. afterEach(function () {
  32. controller.startCommand.restore();
  33. });
  34. it('commands are empty', function () {
  35. controller.set('commands', []);
  36. controller.retry();
  37. expect(controller.startCommand.called).to.be.false;
  38. });
  39. it('command is successful', function () {
  40. controller.set('commands', [
  41. Em.Object.create({
  42. name: 'test',
  43. isSuccess: true,
  44. isError: false,
  45. isStarted: true
  46. })
  47. ]);
  48. controller.retry();
  49. expect(controller.startCommand.calledOnce).to.be.false;
  50. expect(controller.get('commands').findProperty('name', 'test').get('isError')).to.be.false;
  51. expect(controller.get('commands').findProperty('name', 'test').get('isStarted')).to.be.true;
  52. });
  53. it('command is failed', function () {
  54. controller.set('commands', [
  55. Em.Object.create({
  56. name: 'test',
  57. isSuccess: true,
  58. isError: true,
  59. isStarted: true
  60. })
  61. ]);
  62. controller.retry();
  63. expect(controller.startCommand.calledOnce).to.be.true;
  64. expect(controller.get('commands').findProperty('name', 'test').get('isError')).to.be.false;
  65. expect(controller.get('commands').findProperty('name', 'test').get('isStarted')).to.be.false;
  66. });
  67. });
  68. describe('#updateServices()', function () {
  69. it('commands are empty', function () {
  70. controller.set('services', [
  71. {}
  72. ]);
  73. controller.set('commands', []);
  74. controller.updateServices();
  75. expect(controller.get('services')).to.be.empty;
  76. });
  77. it('command doesn\'t have polledData', function () {
  78. controller.set('services', [
  79. {}
  80. ]);
  81. controller.set('commands', [Em.Object.create({
  82. label: 'label'
  83. })]);
  84. controller.updateServices();
  85. expect(controller.get('services')).to.be.empty;
  86. });
  87. it('command has polledData', function () {
  88. controller.set('services', [
  89. {}
  90. ]);
  91. controller.set('commands', [Em.Object.create({
  92. label: 'service1',
  93. polledData: [
  94. {
  95. Tasks: {
  96. host_name: 'host1'
  97. }
  98. }
  99. ]
  100. })]);
  101. controller.updateServices();
  102. expect(controller.get('services').findProperty('name', 'service1').get('hosts')).to.eql([
  103. {
  104. name: 'host1',
  105. publicName: 'host1',
  106. logTasks: [
  107. {
  108. Tasks: {host_name: 'host1'}
  109. }
  110. ]
  111. }
  112. ]);
  113. });
  114. });
  115. describe('#setIndex()', function () {
  116. it('commandArray is empty', function () {
  117. var commandArray = [];
  118. controller.setIndex(commandArray);
  119. expect(commandArray).to.be.empty;
  120. expect(controller.get('totalSteps')).to.equal(0);
  121. });
  122. it('one command in commandArray', function () {
  123. var commandArray = [
  124. Em.Object.create({name: 'command1'})
  125. ];
  126. controller.setIndex(commandArray);
  127. expect(commandArray[0].get('index')).to.equal(1);
  128. expect(controller.get('totalSteps')).to.equal(1);
  129. });
  130. it('commands with random indexes', function () {
  131. var commandArray = [];
  132. commandArray[3] = Em.Object.create({name: 'command3'});
  133. commandArray[11] = Em.Object.create({name: 'command11'});
  134. controller.setIndex(commandArray);
  135. expect(commandArray[3].get('index')).to.equal(4);
  136. expect(commandArray[11].get('index')).to.equal(12);
  137. expect(controller.get('totalSteps')).to.equal(12);
  138. });
  139. });
  140. describe('#startCommand()', function () {
  141. var command = Em.Object.create({
  142. start: function () {
  143. }
  144. });
  145. beforeEach(function () {
  146. sinon.spy(command, "start");
  147. sinon.spy(controller, "loadClusterConfigs");
  148. sinon.spy(controller, "deleteComponents");
  149. });
  150. afterEach(function () {
  151. command.start.restore();
  152. controller.loadClusterConfigs.restore();
  153. controller.deleteComponents.restore();
  154. });
  155. it('number of commands doesn\'t match totalSteps', function () {
  156. controller.set('commands', []);
  157. controller.set('totalSteps', 1);
  158. expect(controller.startCommand()).to.be.false;
  159. });
  160. it('commands is empty', function () {
  161. controller.set('commands', []);
  162. controller.set('totalSteps', 0);
  163. expect(controller.startCommand()).to.be.false;
  164. });
  165. it('command is started and completed', function () {
  166. controller.set('commands', [Em.Object.create({
  167. isStarted: true,
  168. isCompleted: true
  169. })]);
  170. controller.set('totalSteps', 1);
  171. expect(controller.startCommand()).to.be.false;
  172. });
  173. it('command is started and incompleted', function () {
  174. controller.set('commands', [Em.Object.create({
  175. isStarted: true,
  176. isCompleted: false
  177. })]);
  178. controller.set('totalSteps', 1);
  179. expect(controller.startCommand()).to.be.true;
  180. });
  181. it('command parameter passed, isPolling is true', function () {
  182. controller.set('commands', []);
  183. controller.set('totalSteps', 0);
  184. command.set('isPolling', true);
  185. expect(controller.startCommand(command)).to.be.true;
  186. expect(command.get('isStarted')).to.be.true;
  187. expect(command.start.calledOnce).to.be.true;
  188. command.set('isPolling', false);
  189. });
  190. it('command parameter passed, name is "APPLY_CONFIGURATIONS"', function () {
  191. command.set('name', 'APPLY_CONFIGURATIONS');
  192. expect(controller.startCommand(command)).to.be.true;
  193. expect(command.get('isStarted')).to.be.true;
  194. expect(controller.loadClusterConfigs.calledOnce).to.be.true;
  195. });
  196. it('command parameter passed, name is "APPLY_CONFIGURATIONS", testMode = true', function () {
  197. App.testMode = true;
  198. expect(controller.startCommand(command)).to.be.true;
  199. expect(command.get('isStarted')).to.be.true;
  200. expect(command.get('isError')).to.be.false;
  201. expect(command.get('isSuccess')).to.be.true;
  202. expect(controller.loadClusterConfigs.called).to.be.false;
  203. App.testMode = false;
  204. });
  205. it('command parameter passed, name is "DELETE_ATS"', function () {
  206. command.set('name', 'DELETE_ATS');
  207. App.store.load(App.HostComponent, {
  208. id: 'APP_TIMELINE_SERVER_ats_host',
  209. component_name: 'APP_TIMELINE_SERVER',
  210. host_id: 'ats_host'
  211. });
  212. App.store.load(App.Host, {
  213. id: 'ats_host',
  214. host_name: 'ats_host',
  215. host_components: ['APP_TIMELINE_SERVER_ats_host']
  216. });
  217. expect(controller.startCommand(command)).to.be.true;
  218. expect(command.get('isStarted')).to.be.true;
  219. expect(controller.deleteComponents.calledWith('APP_TIMELINE_SERVER', 'ats_host')).to.be.true;
  220. });
  221. it('command parameter passed, name is "DELETE_ATS", testMode = true', function () {
  222. App.testMode = true;
  223. expect(controller.startCommand(command)).to.be.true;
  224. expect(command.get('isStarted')).to.be.true;
  225. expect(command.get('isError')).to.be.false;
  226. expect(command.get('isSuccess')).to.be.true;
  227. expect(controller.deleteComponents.called).to.be.false;
  228. App.testMode = false;
  229. });
  230. });
  231. describe('#onCompleteCommand()', function () {
  232. beforeEach(function () {
  233. sinon.spy(controller, "moveToNextCommand");
  234. });
  235. afterEach(function () {
  236. controller.moveToNextCommand.restore();
  237. });
  238. it('number of commands doesn\'t match totalSteps', function () {
  239. controller.set('commands', []);
  240. controller.set('totalSteps', 1);
  241. expect(controller.onCompleteCommand()).to.be.false;
  242. });
  243. it('No successful commands', function () {
  244. controller.set('commands', [Em.Object.create({
  245. isSuccess: false
  246. })]);
  247. controller.set('totalSteps', 1);
  248. expect(controller.onCompleteCommand()).to.be.false;
  249. });
  250. it('No successful commands', function () {
  251. controller.set('commands', [Em.Object.create({
  252. isSuccess: false
  253. })]);
  254. controller.set('totalSteps', 1);
  255. expect(controller.onCompleteCommand()).to.be.false;
  256. });
  257. it('Last command is successful', function () {
  258. controller.set('commands', [
  259. Em.Object.create({
  260. isSuccess: false
  261. }),
  262. Em.Object.create({
  263. isSuccess: true
  264. })
  265. ]);
  266. controller.set('totalSteps', 2);
  267. expect(controller.onCompleteCommand()).to.be.false;
  268. });
  269. it('all commands are successful', function () {
  270. controller.set('commands', [
  271. Em.Object.create({
  272. isSuccess: true,
  273. name: 'command1'
  274. }),
  275. Em.Object.create({
  276. isSuccess: false,
  277. name: 'command2'
  278. })
  279. ]);
  280. controller.set('totalSteps', 2);
  281. expect(controller.onCompleteCommand()).to.be.true;
  282. expect(controller.moveToNextCommand.calledWith(Em.Object.create({
  283. isSuccess: false,
  284. name: 'command2'
  285. }))).to.be.true;
  286. });
  287. });
  288. describe('#moveToNextCommand()', function () {
  289. beforeEach(function () {
  290. sinon.spy(controller, "startCommand");
  291. });
  292. afterEach(function () {
  293. controller.startCommand.restore();
  294. });
  295. it('No commands present', function () {
  296. controller.set('commands', []);
  297. expect(controller.moveToNextCommand()).to.be.false;
  298. });
  299. it('Only started command present', function () {
  300. controller.set('commands', [
  301. Em.Object.create({
  302. isStarted: true
  303. })
  304. ]);
  305. expect(controller.moveToNextCommand()).to.be.false;
  306. });
  307. it('Command is not started', function () {
  308. controller.set('commands', [
  309. Em.Object.create({
  310. isStarted: false,
  311. name: 'command1'
  312. })
  313. ]);
  314. expect(controller.moveToNextCommand()).to.be.true;
  315. expect(controller.startCommand.calledWith(Em.Object.create({
  316. isStarted: false,
  317. name: 'command1'
  318. }))).to.be.true;
  319. });
  320. it('Next command provide as argument', function () {
  321. var nextCommand = Em.Object.create({
  322. isStarted: false,
  323. name: 'command2'
  324. });
  325. expect(controller.moveToNextCommand(nextCommand)).to.be.true;
  326. expect(controller.startCommand.calledWith(Em.Object.create({
  327. isStarted: false,
  328. name: 'command2'
  329. }))).to.be.true;
  330. });
  331. });
  332. describe('#setServiceTagNames()', function () {
  333. var testCases = [
  334. {
  335. title: 'configs is empty object',
  336. content: {
  337. secureService: {},
  338. configs: {}
  339. },
  340. result: undefined
  341. },
  342. {
  343. title: 'secureService.sites is null',
  344. content: {
  345. secureService: {
  346. sites: null
  347. },
  348. configs: {
  349. site1: {}
  350. }
  351. },
  352. result: undefined
  353. },
  354. {
  355. title: 'secureService.sites doesn\'t contain required config tag',
  356. content: {
  357. secureService: {
  358. sites: []
  359. },
  360. configs: {
  361. site1: {}
  362. }
  363. },
  364. result: undefined
  365. },
  366. {
  367. title: 'secureService.sites contains required config tag',
  368. content: {
  369. secureService: {
  370. sites: ['site1']
  371. },
  372. configs: {
  373. site1: {
  374. tag: 'tag1'
  375. }
  376. }
  377. },
  378. result: {
  379. siteName: 'site1',
  380. tagName: 'tag1',
  381. newTagName: null,
  382. configs: {}
  383. }
  384. }
  385. ];
  386. testCases.forEach(function (test) {
  387. it(test.title, function () {
  388. expect(controller.setServiceTagNames(test.content.secureService, test.content.configs)).to.eql(test.result);
  389. });
  390. });
  391. });
  392. describe('#setServerConfigValue()', function () {
  393. beforeEach(function () {
  394. sinon.spy(App.config, "escapeXMLCharacters");
  395. });
  396. afterEach(function () {
  397. App.config.escapeXMLCharacters.restore();
  398. });
  399. it('Empty config', function () {
  400. expect(controller.setServerConfigValue('', '')).to.equal('');
  401. expect(App.config.escapeXMLCharacters.calledWith('')).to.be.true;
  402. });
  403. it('Config1, value = "value1"', function () {
  404. expect(controller.setServerConfigValue('config1', 'value1')).to.equal('value1');
  405. expect(App.config.escapeXMLCharacters.calledWith('value1')).to.be.true;
  406. });
  407. it('config = "storm.zookeeper.servers", value = "value1"', function () {
  408. expect(controller.setServerConfigValue('storm.zookeeper.servers', 'value1')).to.equal('value1');
  409. expect(App.config.escapeXMLCharacters.called).to.be.false;
  410. });
  411. });
  412. describe('#escapeXMLCharacters()', function () {
  413. beforeEach(function () {
  414. sinon.spy(controller, "setServerConfigValue");
  415. });
  416. afterEach(function () {
  417. controller.setServerConfigValue.restore();
  418. });
  419. it('serviceConfigTags is empty', function () {
  420. var serviceConfigTags = [];
  421. controller.escapeXMLCharacters(serviceConfigTags);
  422. expect(controller.setServerConfigValue.called).to.be.false;
  423. });
  424. it('configs is empty', function () {
  425. var serviceConfigTags = [
  426. {
  427. configs: {}
  428. }
  429. ];
  430. controller.escapeXMLCharacters(serviceConfigTags);
  431. expect(controller.setServerConfigValue.called).to.be.false;
  432. });
  433. it('serviceConfigTags has property', function () {
  434. var serviceConfigTags = [
  435. {
  436. configs: {
  437. 'p1': 'value1'
  438. }
  439. }
  440. ];
  441. controller.escapeXMLCharacters(serviceConfigTags);
  442. expect(controller.setServerConfigValue.withArgs('p1', 'value1').calledOnce).to.be.true;
  443. });
  444. it('serviceConfigTags has multiple properties', function () {
  445. var serviceConfigTags = [
  446. {
  447. configs: {
  448. 'p1': 'value1',
  449. 'p2': 'value2'
  450. }
  451. }
  452. ];
  453. controller.escapeXMLCharacters(serviceConfigTags);
  454. expect(controller.setServerConfigValue.callCount).to.equal(2);
  455. });
  456. });
  457. });