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