security_progress_controller_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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: Em.K
  143. });
  144. beforeEach(function () {
  145. sinon.spy(command, "start");
  146. sinon.spy(controller, "loadClusterConfigs");
  147. sinon.spy(controller, "deleteComponents");
  148. sinon.stub(controller, "saveCommands", Em.K);
  149. });
  150. afterEach(function () {
  151. command.start.restore();
  152. controller.loadClusterConfigs.restore();
  153. controller.deleteComponents.restore();
  154. controller.saveCommands.restore();
  155. });
  156. it('number of commands doesn\'t match totalSteps', function () {
  157. controller.set('commands', []);
  158. controller.set('totalSteps', 1);
  159. expect(controller.startCommand()).to.be.false;
  160. });
  161. it('commands is empty', function () {
  162. controller.set('commands', []);
  163. controller.set('totalSteps', 0);
  164. expect(controller.startCommand()).to.be.false;
  165. });
  166. it('command is started and completed', function () {
  167. controller.set('commands', [Em.Object.create({
  168. isStarted: true,
  169. isCompleted: true
  170. })]);
  171. controller.set('totalSteps', 1);
  172. expect(controller.startCommand()).to.be.false;
  173. });
  174. it('command is started and incompleted', function () {
  175. controller.set('commands', [Em.Object.create({
  176. isStarted: true,
  177. isCompleted: false
  178. })]);
  179. controller.set('totalSteps', 1);
  180. expect(controller.startCommand()).to.be.true;
  181. });
  182. it('command parameter passed, isPolling is true', function () {
  183. controller.set('commands', []);
  184. controller.set('totalSteps', 0);
  185. command.set('isPolling', true);
  186. expect(controller.startCommand(command)).to.be.true;
  187. expect(command.get('isStarted')).to.be.true;
  188. expect(command.start.calledOnce).to.be.true;
  189. command.set('isPolling', false);
  190. });
  191. it('command parameter passed, name is "APPLY_CONFIGURATIONS"', function () {
  192. command.set('name', 'APPLY_CONFIGURATIONS');
  193. expect(controller.startCommand(command)).to.be.true;
  194. expect(command.get('isStarted')).to.be.true;
  195. expect(controller.loadClusterConfigs.calledOnce).to.be.true;
  196. });
  197. it('command parameter passed, name is "DELETE_ATS"', function () {
  198. command.set('name', 'DELETE_ATS');
  199. sinon.stub(App.HostComponent, 'find', function() {
  200. return [Em.Object.create({
  201. id: 'APP_TIMELINE_SERVER_ats_host',
  202. componentName: 'APP_TIMELINE_SERVER',
  203. hostName: 'ats_host'
  204. })];
  205. });
  206. expect(controller.startCommand(command)).to.be.true;
  207. expect(command.get('isStarted')).to.be.true;
  208. expect(controller.deleteComponents.calledWith('APP_TIMELINE_SERVER', 'ats_host')).to.be.true;
  209. App.HostComponent.find.restore();
  210. });
  211. });
  212. describe('#onCompleteCommand()', function () {
  213. beforeEach(function () {
  214. sinon.spy(controller, "moveToNextCommand");
  215. sinon.stub(controller, "saveCommands", Em.K);
  216. });
  217. afterEach(function () {
  218. controller.moveToNextCommand.restore();
  219. controller.saveCommands.restore();
  220. });
  221. it('number of commands doesn\'t match totalSteps', function () {
  222. controller.set('commands', []);
  223. controller.set('totalSteps', 1);
  224. expect(controller.onCompleteCommand()).to.be.false;
  225. });
  226. it('No successful commands', function () {
  227. controller.set('commands', [Em.Object.create({
  228. isSuccess: false
  229. })]);
  230. controller.set('totalSteps', 1);
  231. expect(controller.onCompleteCommand()).to.be.false;
  232. });
  233. it('No successful commands', function () {
  234. controller.set('commands', [Em.Object.create({
  235. isSuccess: false
  236. })]);
  237. controller.set('totalSteps', 1);
  238. expect(controller.onCompleteCommand()).to.be.false;
  239. });
  240. it('Last command is successful', function () {
  241. controller.set('commands', [
  242. Em.Object.create({
  243. isSuccess: false
  244. }),
  245. Em.Object.create({
  246. isSuccess: true
  247. })
  248. ]);
  249. controller.set('totalSteps', 2);
  250. expect(controller.onCompleteCommand()).to.be.false;
  251. });
  252. it('all commands are successful', function () {
  253. controller.set('commands', [
  254. Em.Object.create({
  255. isSuccess: true,
  256. name: 'command1'
  257. }),
  258. Em.Object.create({
  259. isSuccess: false,
  260. name: 'command2'
  261. })
  262. ]);
  263. controller.set('totalSteps', 2);
  264. expect(controller.onCompleteCommand()).to.be.true;
  265. expect(controller.moveToNextCommand.calledWith(Em.Object.create({
  266. isSuccess: false,
  267. name: 'command2'
  268. }))).to.be.true;
  269. });
  270. });
  271. describe('#moveToNextCommand()', function () {
  272. beforeEach(function () {
  273. sinon.spy(controller, "startCommand");
  274. });
  275. afterEach(function () {
  276. controller.startCommand.restore();
  277. });
  278. it('No commands present', function () {
  279. controller.set('commands', []);
  280. expect(controller.moveToNextCommand()).to.be.false;
  281. });
  282. it('Only started command present', function () {
  283. controller.set('commands', [
  284. Em.Object.create({
  285. isStarted: true
  286. })
  287. ]);
  288. expect(controller.moveToNextCommand()).to.be.false;
  289. });
  290. it('Command is not started', function () {
  291. controller.set('commands', [
  292. Em.Object.create({
  293. isStarted: false,
  294. name: 'command1'
  295. })
  296. ]);
  297. expect(controller.moveToNextCommand()).to.be.true;
  298. expect(controller.startCommand.calledWith(Em.Object.create({
  299. isStarted: false,
  300. name: 'command1'
  301. }))).to.be.true;
  302. });
  303. it('Next command provide as argument', function () {
  304. var nextCommand = Em.Object.create({
  305. isStarted: false,
  306. name: 'command2'
  307. });
  308. expect(controller.moveToNextCommand(nextCommand)).to.be.true;
  309. expect(controller.startCommand.calledWith(Em.Object.create({
  310. isStarted: false,
  311. name: 'command2'
  312. }))).to.be.true;
  313. });
  314. });
  315. describe('#setServiceTagNames()', function () {
  316. var testCases = [
  317. {
  318. title: 'configs is empty object',
  319. content: {
  320. secureService: {},
  321. configs: {}
  322. },
  323. result: undefined
  324. },
  325. {
  326. title: 'secureService.sites is null',
  327. content: {
  328. secureService: {
  329. sites: null
  330. },
  331. configs: {
  332. site1: {}
  333. }
  334. },
  335. result: undefined
  336. },
  337. {
  338. title: 'secureService.sites doesn\'t contain required config tag',
  339. content: {
  340. secureService: {
  341. sites: []
  342. },
  343. configs: {
  344. site1: {}
  345. }
  346. },
  347. result: undefined
  348. },
  349. {
  350. title: 'secureService.sites contains required config tag',
  351. content: {
  352. secureService: {
  353. sites: ['site1']
  354. },
  355. configs: {
  356. site1: {
  357. tag: 'tag1'
  358. }
  359. }
  360. },
  361. result: {
  362. siteName: 'site1',
  363. tagName: 'tag1',
  364. newTagName: null,
  365. configs: {}
  366. }
  367. }
  368. ];
  369. testCases.forEach(function (test) {
  370. it(test.title, function () {
  371. expect(controller.setServiceTagNames(test.content.secureService, test.content.configs)).to.eql(test.result);
  372. });
  373. });
  374. });
  375. });