definitions_configs_controller_test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. var testHelpers = require('test/helpers');
  20. var controller;
  21. function getController() {
  22. return App.MainAlertDefinitionConfigsController.create({
  23. allServices: ['service1', 'service2', 'service3'],
  24. allComponents: ['component1', 'component2', 'component3'],
  25. aggregateAlertNames: ['alertDefinitionName', 'alertDefinitionName2', 'alertDefinitionName3']
  26. });
  27. }
  28. function getEmptyArray() {
  29. return [];
  30. }
  31. describe('App.MainAlertDefinitionConfigsController', function () {
  32. beforeEach(function () {
  33. controller = getController();
  34. });
  35. App.TestAliases.testAsComputedOr(getController(), 'hasErrors', ['someConfigIsInvalid', 'hasThresholdsError']);
  36. describe('#renderConfigs()', function () {
  37. beforeEach(function () {
  38. controller.set('content', Em.Object.create({}));
  39. sinon.stub(controller, 'renderPortConfigs', getEmptyArray);
  40. sinon.stub(controller, 'renderMetricConfigs', getEmptyArray);
  41. sinon.stub(controller, 'renderWebConfigs', getEmptyArray);
  42. sinon.stub(controller, 'renderScriptConfigs', getEmptyArray);
  43. sinon.stub(controller, 'renderAggregateConfigs', getEmptyArray);
  44. });
  45. afterEach(function () {
  46. controller.renderPortConfigs.restore();
  47. controller.renderMetricConfigs.restore();
  48. controller.renderWebConfigs.restore();
  49. controller.renderScriptConfigs.restore();
  50. controller.renderAggregateConfigs.restore();
  51. });
  52. it('should call renderPortConfigs method', function () {
  53. controller.set('alertDefinitionType', 'PORT');
  54. controller.renderConfigs();
  55. expect(controller.renderPortConfigs.calledOnce).to.be.true;
  56. });
  57. it('should call renderMetricConfigs method', function () {
  58. controller.set('alertDefinitionType', 'METRIC');
  59. controller.renderConfigs();
  60. expect(controller.renderMetricConfigs.calledOnce).to.be.true;
  61. });
  62. it('should call renderWebConfigs method', function () {
  63. controller.set('alertDefinitionType', 'WEB');
  64. controller.renderConfigs();
  65. expect(controller.renderWebConfigs.calledOnce).to.be.true;
  66. });
  67. it('should call renderScriptConfigs method', function () {
  68. controller.set('alertDefinitionType', 'SCRIPT');
  69. controller.renderConfigs();
  70. expect(controller.renderScriptConfigs.calledOnce).to.be.true;
  71. });
  72. it('should call renderAggregateConfigs method', function () {
  73. controller.set('alertDefinitionType', 'AGGREGATE');
  74. controller.renderConfigs();
  75. expect(controller.renderAggregateConfigs.calledOnce).to.be.true;
  76. });
  77. });
  78. describe('#renderPortConfigs()', function () {
  79. beforeEach(function () {
  80. controller.set('content', Em.Object.create({
  81. name: 'alertDefinitionName',
  82. service: {displayName: 'alertDefinitionService'},
  83. componentName: 'component1',
  84. scope: 'HOST',
  85. description: 'alertDefinitionDescription',
  86. interval: 60,
  87. reporting: [
  88. Em.Object.create({
  89. type: 'warning',
  90. value: 10
  91. }),
  92. Em.Object.create({
  93. type: 'critical',
  94. value: 20
  95. }),
  96. Em.Object.create({
  97. type: 'ok',
  98. value: 30
  99. })
  100. ],
  101. uri: 'alertDefinitionUri',
  102. defaultPort: '777'
  103. }));
  104. });
  105. it('isWizard = true', function () {
  106. controller.set('isWizard', true);
  107. var result = controller.renderPortConfigs();
  108. expect(result.length).to.equal(11);
  109. });
  110. it('isWizard = false', function () {
  111. controller.set('isWizard', false);
  112. var result = controller.renderPortConfigs();
  113. expect(result.length).to.equal(5);
  114. });
  115. });
  116. describe('#renderMetricConfigs()', function () {
  117. beforeEach(function () {
  118. controller.set('content', Em.Object.create({
  119. name: 'alertDefinitionName',
  120. service: {displayName: 'alertDefinitionService'},
  121. componentName: 'component1',
  122. scope: 'HOST',
  123. description: 'alertDefinitionDescription',
  124. interval: 60,
  125. reporting: [
  126. Em.Object.create({
  127. type: 'warning',
  128. value: 10
  129. }),
  130. Em.Object.create({
  131. type: 'critical',
  132. value: 20
  133. }),
  134. Em.Object.create({
  135. type: 'ok',
  136. value: 30
  137. })
  138. ],
  139. uri: {
  140. "http": "{{mapred-site/mapreduce.jobhistory.webapp.address}}",
  141. "https": "{{mapred-site/mapreduce.jobhistory.webapp.https.address}}",
  142. "https_property": "{{mapred-site/mapreduce.jobhistory.http.policy}}",
  143. "https_property_value": "HTTPS_ONLY",
  144. "default_port": 0.0
  145. },
  146. jmx: {
  147. propertyList: ['property1', 'property2'],
  148. value: 'jmxValue'
  149. },
  150. ganglia: {
  151. propertyList: null,
  152. value: null
  153. }
  154. }));
  155. });
  156. it('isWizard = true', function () {
  157. controller.set('isWizard', true);
  158. var result = controller.renderMetricConfigs();
  159. expect(result.length).to.equal(11);
  160. });
  161. it('isWizard = false', function () {
  162. controller.set('isWizard', false);
  163. var result = controller.renderMetricConfigs();
  164. expect(result.length).to.equal(5);
  165. });
  166. });
  167. describe('#renderWebConfigs()', function () {
  168. beforeEach(function () {
  169. controller.set('content', Em.Object.create({
  170. name: 'alertDefinitionName',
  171. service: {displayName: 'alertDefinitionService'},
  172. componentName: 'component1',
  173. scope: 'HOST',
  174. description: 'alertDefinitionDescription',
  175. interval: 60,
  176. reporting: [
  177. Em.Object.create({
  178. type: 'warning',
  179. value: 10
  180. }),
  181. Em.Object.create({
  182. type: 'critical',
  183. value: 20
  184. }),
  185. Em.Object.create({
  186. type: 'ok',
  187. value: 30
  188. })
  189. ],
  190. uri: {
  191. "http": "{{mapred-site/mapreduce.jobhistory.webapp.address}}",
  192. "https": "{{mapred-site/mapreduce.jobhistory.webapp.https.address}}",
  193. "https_property": "{{mapred-site/mapreduce.jobhistory.http.policy}}",
  194. "https_property_value": "HTTPS_ONLY",
  195. "default_port": 0.0,
  196. "connection_timeout": 123
  197. }
  198. }));
  199. });
  200. it('isWizard = true', function () {
  201. controller.set('isWizard', true);
  202. var result = controller.renderWebConfigs();
  203. expect(result.length).to.equal(12);
  204. });
  205. it('isWizard = false', function () {
  206. controller.set('isWizard', false);
  207. var result = controller.renderWebConfigs();
  208. expect(result.length).to.equal(6);
  209. });
  210. });
  211. describe('#renderScriptConfigs()', function () {
  212. beforeEach(function () {
  213. controller.set('content', Em.Object.create({
  214. name: 'alertDefinitionName',
  215. service: {displayName: 'alertDefinitionService'},
  216. componentName: 'component1',
  217. scope: 'HOST',
  218. description: 'alertDefinitionDescription',
  219. interval: 60,
  220. parameters: [
  221. Em.Object.create({}),
  222. Em.Object.create({}),
  223. ],
  224. reporting: [
  225. Em.Object.create({
  226. type: 'warning',
  227. value: 10
  228. }),
  229. Em.Object.create({
  230. type: 'critical',
  231. value: 20
  232. }),
  233. Em.Object.create({
  234. type: 'ok',
  235. value: 30
  236. })
  237. ],
  238. location: 'path to script'
  239. }));
  240. });
  241. it('isWizard = true', function () {
  242. controller.set('isWizard', true);
  243. var result = controller.renderScriptConfigs();
  244. expect(result.length).to.equal(10);
  245. });
  246. it('isWizard = false', function () {
  247. controller.set('isWizard', false);
  248. var result = controller.renderScriptConfigs();
  249. expect(result.length).to.equal(4);
  250. });
  251. });
  252. describe('#renderAggregateConfigs()', function () {
  253. it('should render array of configs with correct values', function () {
  254. controller.set('content', Em.Object.create({
  255. name: 'alertDefinitionName',
  256. description: 'alertDefinitionDescription',
  257. interval: 60,
  258. reporting: [
  259. Em.Object.create({
  260. type: 'warning',
  261. value: 10
  262. }),
  263. Em.Object.create({
  264. type: 'critical',
  265. value: 20
  266. }),
  267. Em.Object.create({
  268. type: 'ok',
  269. value: 30
  270. })
  271. ]
  272. }));
  273. var result = controller.renderAggregateConfigs();
  274. expect(result.length).to.equal(5);
  275. });
  276. });
  277. describe('#editConfigs()', function () {
  278. beforeEach(function () {
  279. controller.set('configs', [
  280. Em.Object.create({value: 'value1', previousValue: '', isDisabled: true}),
  281. Em.Object.create({value: 'value2', previousValue: '', isDisabled: true}),
  282. Em.Object.create({value: 'value3', previousValue: '', isDisabled: true})
  283. ]);
  284. controller.set('canEdit', false);
  285. controller.editConfigs();
  286. });
  287. it('should set previousValue', function () {
  288. expect(controller.get('configs').mapProperty('previousValue')).to.eql(['value1', 'value2', 'value3']);
  289. });
  290. it('should set isDisabled for each config', function () {
  291. expect(controller.get('configs').someProperty('isDisabled', true)).to.be.false;
  292. });
  293. it('should change canEdit flag', function () {
  294. expect(controller.get('canEdit')).to.be.true;
  295. });
  296. });
  297. describe('#cancelEditConfigs()', function () {
  298. beforeEach(function () {
  299. controller.set('configs', [
  300. Em.Object.create({value: '', previousValue: 'value1', isDisabled: false}),
  301. Em.Object.create({value: '', previousValue: 'value2', isDisabled: false}),
  302. Em.Object.create({value: '', previousValue: 'value3', isDisabled: false})
  303. ]);
  304. controller.set('canEdit', true);
  305. controller.cancelEditConfigs();
  306. });
  307. it('should set previousValue', function () {
  308. expect(controller.get('configs').mapProperty('value')).to.eql(['value1', 'value2', 'value3']);
  309. });
  310. it('should set isDisabled for each config', function () {
  311. expect(controller.get('configs').someProperty('isDisabled', false)).to.be.false;
  312. });
  313. it('should change canEdit flag', function () {
  314. expect(controller.get('canEdit')).to.be.false;
  315. });
  316. });
  317. describe('#saveConfigs()', function () {
  318. beforeEach(function () {
  319. controller.set('configs', [
  320. Em.Object.create({isDisabled: true}),
  321. Em.Object.create({isDisabled: true}),
  322. Em.Object.create({isDisabled: true})
  323. ]);
  324. controller.set('canEdit', true);
  325. controller.saveConfigs();
  326. });
  327. it('should set isDisabled for each config', function () {
  328. expect(controller.get('configs').someProperty('isDisabled', false)).to.be.false;
  329. });
  330. it('should change canEdit flag', function () {
  331. expect(controller.get('canEdit')).to.be.false;
  332. });
  333. it('should sent 1 request', function () {
  334. var args = testHelpers.findAjaxRequest('name', 'alerts.update_alert_definition');
  335. expect(args[0]).to.exists;
  336. });
  337. });
  338. describe('#getPropertiesToUpdate()', function () {
  339. beforeEach(function () {
  340. controller.set('content', {
  341. rawSourceData: {
  342. path1: 'value',
  343. path2: {
  344. path3: 'value'
  345. }
  346. }
  347. });
  348. });
  349. var testCases = [
  350. {
  351. m: 'should ignore configs with wasChanged false',
  352. configs: [
  353. Em.Object.create({
  354. wasChanged: false,
  355. apiProperty: 'name1',
  356. apiFormattedValue: 'test1'
  357. }),
  358. Em.Object.create({
  359. wasChanged: true,
  360. apiProperty: 'name2',
  361. apiFormattedValue: 'test2'
  362. }),
  363. Em.Object.create({
  364. wasChanged: false,
  365. apiProperty: 'name3',
  366. apiFormattedValue: 'test3'
  367. })
  368. ],
  369. result: {
  370. 'AlertDefinition/name2': 'test2'
  371. }
  372. },
  373. {
  374. m: 'should correctly map deep source properties',
  375. configs: [
  376. Em.Object.create({
  377. wasChanged: true,
  378. apiProperty: 'name1',
  379. apiFormattedValue: 'test1'
  380. }),
  381. Em.Object.create({
  382. wasChanged: true,
  383. apiProperty: 'source.path1',
  384. apiFormattedValue: 'value1'
  385. }),
  386. Em.Object.create({
  387. wasChanged: true,
  388. apiProperty: 'source.path2.path3',
  389. apiFormattedValue: 'value2'
  390. })
  391. ],
  392. result: {
  393. 'AlertDefinition/name1': 'test1',
  394. 'AlertDefinition/source': {
  395. path1: 'value1',
  396. path2: {
  397. path3: 'value2'
  398. }
  399. }
  400. }
  401. },
  402. {
  403. m: 'should correctly multiple apiProperties',
  404. configs: [
  405. Em.Object.create({
  406. wasChanged: true,
  407. apiProperty: ['name1', 'name2'],
  408. apiFormattedValue: ['value1', 'value2']
  409. })
  410. ],
  411. result: {
  412. 'AlertDefinition/name1': 'value1',
  413. 'AlertDefinition/name2': 'value2'
  414. }
  415. }
  416. ];
  417. testCases.forEach(function (testCase) {
  418. it(testCase.m, function () {
  419. controller.set('configs', testCase.configs);
  420. var result = controller.getPropertiesToUpdate(true);
  421. expect(result).to.eql(testCase.result);
  422. });
  423. });
  424. describe('`source/parameters` for SCRIPT configs', function () {
  425. beforeEach(function () {
  426. controller.set('content', Em.Object.create({
  427. parameters: [
  428. Em.Object.create({name: 'p1', value: 'v1'}),
  429. Em.Object.create({name: 'p2', value: 'v2'}),
  430. Em.Object.create({name: 'p3', value: 'v3'}),
  431. Em.Object.create({name: 'p4', value: 'v4'})
  432. ],
  433. rawSourceData: {
  434. parameters: [
  435. {name: 'p1', value: 'v1'},
  436. {name: 'p2', value: 'v2'},
  437. {name: 'p3', value: 'v3'},
  438. {name: 'p4', value: 'v4'}
  439. ]
  440. }
  441. }));
  442. controller.set('configs', [
  443. Em.Object.create({apiProperty:'p1', apiFormattedValue: 'v11', wasChanged: true, name: 'parameter'}),
  444. Em.Object.create({apiProperty:'p2', apiFormattedValue: 'v21', wasChanged: true, name: 'parameter'}),
  445. Em.Object.create({apiProperty:'p3', apiFormattedValue: 'v31', wasChanged: true, name: 'parameter'}),
  446. Em.Object.create({apiProperty:'p4', apiFormattedValue: 'v41', wasChanged: true, name: 'parameter'})
  447. ]);
  448. this.result = controller.getPropertiesToUpdate();
  449. });
  450. it('should update parameters', function () {
  451. expect(this.result['AlertDefinition/source/parameters']).to.have.property('length').equal(4);
  452. expect(this.result['AlertDefinition/source/parameters'].mapProperty('value')).to.be.eql(['v11', 'v21', 'v31', 'v41']);
  453. });
  454. });
  455. });
  456. describe('#changeType()', function () {
  457. beforeEach(function () {
  458. controller.set('allServices', ['service1', 'service2']);
  459. controller.set('allScopes', ['scope1', 'scope2']);
  460. controller.set('configs', [
  461. Em.Object.create({name: 'service', isDisabled: false}),
  462. Em.Object.create({name: 'component', isDisabled: false}),
  463. Em.Object.create({name: 'scope', isDisabled: false})
  464. ]);
  465. });
  466. describe('Host Alert Definition', function () {
  467. beforeEach(function () {
  468. controller.changeType('Host Alert Definition');
  469. });
  470. it('all configs are disabled', function () {
  471. expect(controller.get('configs').everyProperty('isDisabled', true)).to.be.true;
  472. });
  473. it('service.options = ["Ambari"]', function () {
  474. expect(controller.get('configs').findProperty('name', 'service').get('options')).to.eql(['Ambari']);
  475. });
  476. it('service.value = "Ambari"', function () {
  477. expect(controller.get('configs').findProperty('name', 'service').get('value')).to.equal('Ambari');
  478. });
  479. it('component.value = "Ambari Agent"', function () {
  480. expect(controller.get('configs').findProperty('name', 'component').get('value')).to.equal('Ambari Agent');
  481. });
  482. it('scope.options = ["Host"]', function () {
  483. expect(controller.get('configs').findProperty('name', 'scope').get('options')).to.eql(['Host']);
  484. });
  485. it('isDisabled.value = "Host"', function () {
  486. expect(controller.get('configs').findProperty('name', 'scope').get('value')).to.equal('Host');
  487. });
  488. });
  489. describe('alert_type_service', function () {
  490. beforeEach(function () {
  491. controller.changeType('alert_type_service');
  492. });
  493. it('all configs are not disabled', function () {
  494. expect(controller.get('configs').everyProperty('isDisabled', false)).to.be.true;
  495. });
  496. it('service.options = ["service1", "service2"]', function () {
  497. expect(controller.get('configs').findProperty('name', 'service').get('options')).to.eql(['service1', 'service2']);
  498. });
  499. it('service.value = "service1"', function () {
  500. expect(controller.get('configs').findProperty('name', 'service').get('value')).to.equal('service1');
  501. });
  502. it('component.value = "No component"', function () {
  503. expect(controller.get('configs').findProperty('name', 'component').get('value')).to.equal('No component');
  504. });
  505. it('scope.options = ["scope1", "scope2"]', function () {
  506. expect(controller.get('configs').findProperty('name', 'scope').get('options')).to.eql(['scope1', 'scope2']);
  507. });
  508. it('scope.value = "scope1"', function () {
  509. expect(controller.get('configs').findProperty('name', 'scope').get('value')).to.equal('scope1');
  510. });
  511. });
  512. });
  513. describe('#renderCommonWizardConfigs()', function () {
  514. it('should return correct number of configs', function () {
  515. var result = controller.renderCommonWizardConfigs();
  516. expect(result.length).to.equal(6);
  517. });
  518. });
  519. describe('#getConfigsValues()', function () {
  520. it('should create key-value map from configs', function () {
  521. controller.set('configs', [
  522. Em.Object.create({name: 'name1', value: 'value1'}),
  523. Em.Object.create({name: 'name2', value: 'value2'}),
  524. Em.Object.create({name: 'name3', value: 'value3'})
  525. ]);
  526. var result = controller.getConfigsValues();
  527. expect(result).to.eql([
  528. {name: 'name1', value: 'value1'},
  529. {name: 'name2', value: 'value2'},
  530. {name: 'name3', value: 'value3'}
  531. ]);
  532. });
  533. });
  534. });