alert_definitions_mapper_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. require('mappers/alert_definitions_mapper');
  19. var testHelpers = require('test/helpers');
  20. describe('App.alertDefinitionsMapper', function () {
  21. describe('#map', function () {
  22. var json = {
  23. items: [
  24. {
  25. "AlertDefinition" : {
  26. "component_name" : "RESOURCEMANAGER",
  27. "enabled" : true,
  28. "id" : 1,
  29. "ignore_host" : false,
  30. "interval" : 5,
  31. "label" : "ResourceManager RPC Latency",
  32. "name" : "yarn_resourcemanager_rpc_latency",
  33. "description" : "some description",
  34. "scope" : "ANY",
  35. "service_name" : "YARN",
  36. "source" : {
  37. "jmx" : {
  38. "property_list" : [
  39. "Hadoop:service=ResourceManager,name=RpcActivityForPort*/RpcQueueTimeAvgTime",
  40. "Hadoop:service=ResourceManager,name=RpcActivityForPort*/RpcProcessingTimeAvgTime"
  41. ],
  42. "value" : "{0}"
  43. },
  44. "reporting" : {
  45. "ok" : {
  46. "text" : "Average Queue Time:[{0}], Average Processing Time:[{1}]"
  47. },
  48. "warning" : {
  49. "text" : "Average Queue Time:[{0}], Average Processing Time:[{1}]",
  50. "value" : 3000.0
  51. },
  52. "critical" : {
  53. "text" : "Average Queue Time:[{0}], Average Processing Time:[{1}]",
  54. "value" : 5000.0
  55. }
  56. },
  57. "type" : "METRIC",
  58. "uri" : {
  59. "http" : "{{yarn-site/yarn.resourcemanager.webapp.address}}",
  60. "https" : "{{yarn-site/yarn.resourcemanager.webapp.https.address}}",
  61. "https_property" : "{{yarn-site/yarn.http.policy}}",
  62. "https_property_value" : "HTTPS_ONLY",
  63. "default_port" : 0.0
  64. }
  65. }
  66. }
  67. },
  68. {
  69. "AlertDefinition" : {
  70. "component_name" : "RESOURCEMANAGER",
  71. "enabled" : true,
  72. "id" : 2,
  73. "ignore_host" : false,
  74. "interval" : 1,
  75. "label" : "ResourceManager Web UI",
  76. "name" : "yarn_resourcemanager_webui",
  77. "description" : "",
  78. "scope" : "ANY",
  79. "service_name" : "YARN",
  80. "source" : {
  81. "reporting" : {
  82. "ok" : {
  83. "text" : "HTTP {0} response in {2:.4f} seconds"
  84. },
  85. "warning" : {
  86. "text" : "HTTP {0} response in {2:.4f} seconds"
  87. },
  88. "critical" : {
  89. "text" : "Connection failed to {1}"
  90. }
  91. },
  92. "type" : "WEB",
  93. "uri" : {
  94. "http" : "{{yarn-site/yarn.resourcemanager.webapp.address}}",
  95. "https" : "{{yarn-site/yarn.resourcemanager.webapp.https.address}}",
  96. "https_property" : "{{yarn-site/yarn.http.policy}}",
  97. "https_property_value" : "HTTPS_ONLY",
  98. "default_port" : 0.0
  99. }
  100. }
  101. }
  102. },
  103. {
  104. "AlertDefinition" : {
  105. "component_name" : null,
  106. "enabled" : true,
  107. "id" : 3,
  108. "ignore_host" : false,
  109. "interval" : 1,
  110. "label" : "Percent NodeManagers Available",
  111. "name" : "yarn_nodemanager_webui_percent",
  112. "description" : null,
  113. "scope" : "SERVICE",
  114. "service_name" : "YARN",
  115. "source" : {
  116. "alert_name" : "yarn_nodemanager_webui",
  117. "reporting" : {
  118. "ok" : {
  119. "text" : "affected: [{1}], total: [{0}]"
  120. },
  121. "warning" : {
  122. "text" : "affected: [{1}], total: [{0}]",
  123. "value" : 0.1
  124. },
  125. "critical" : {
  126. "text" : "affected: [{1}], total: [{0}]",
  127. "value" : 0.3
  128. }
  129. },
  130. "type" : "AGGREGATE"
  131. }
  132. }
  133. },
  134. {
  135. "AlertDefinition" : {
  136. "component_name" : "NODEMANAGER",
  137. "enabled" : true,
  138. "id" : 4,
  139. "ignore_host" : false,
  140. "interval" : 1,
  141. "label" : "NodeManager Health",
  142. "name" : "yarn_nodemanager_health",
  143. "description" : "some description",
  144. "scope" : "HOST",
  145. "service_name" : "YARN",
  146. "source" : {
  147. "path" : "HDP/2.0.6/services/YARN/package/files/alert_nodemanager_health.py",
  148. "type" : "SCRIPT"
  149. }
  150. }
  151. },
  152. {
  153. "AlertDefinition" : {
  154. "component_name" : "ZOOKEEPER_SERVER",
  155. "enabled" : true,
  156. "id" : 5,
  157. "ignore_host" : false,
  158. "interval" : 1,
  159. "label" : "ZooKeeper Server Process",
  160. "name" : "zookeeper_server_process",
  161. "description" : "some description",
  162. "scope" : "ANY",
  163. "service_name" : "ZOOKEEPER",
  164. "source" : {
  165. "default_port" : 2181.0,
  166. "reporting" : {
  167. "ok" : {
  168. "text" : "TCP OK - {0:.4f} response on port {1}"
  169. },
  170. "critical" : {
  171. "text" : "Connection failed: {0} to {1}:{2}"
  172. }
  173. },
  174. "type" : "PORT",
  175. "uri" : "{{zookeeper-env/clientPort}}"
  176. }
  177. }
  178. }
  179. ]
  180. };
  181. beforeEach(function () {
  182. App.alertDefinitionsMapper.setProperties({
  183. 'model': {},
  184. 'portModel': {},
  185. 'metricsModel': {},
  186. 'webModel': {},
  187. 'aggregateModel': {},
  188. 'scriptModel': {},
  189. 'reportModel': {},
  190. 'metricsSourceModel': {},
  191. 'metricsUriModel': {}
  192. });
  193. sinon.stub(App.PortAlertDefinition, 'find', function() {return [];});
  194. sinon.stub(App.MetricsAlertDefinition, 'find', function() {return [];});
  195. sinon.stub(App.WebAlertDefinition, 'find', function() {return [];});
  196. sinon.stub(App.AggregateAlertDefinition, 'find', function() {return [];});
  197. sinon.stub(App.ScriptAlertDefinition, 'find', function() {return [];});
  198. sinon.stub(App.alertDefinitionsMapper, 'deleteRecord', Em.K);
  199. sinon.stub(App.store, 'commit', Em.K);
  200. sinon.stub(App.store, 'loadMany', function (type, content) {
  201. type.content = content;
  202. });
  203. sinon.stub(App.router, 'get', function() {return false;});
  204. App.cache['previousAlertGroupsMap'] = {};
  205. sinon.stub(App.alertDefinitionsMapper, 'setMetricsSourcePropertyLists', Em.K);
  206. sinon.stub(App.alertDefinitionsMapper, 'setAlertDefinitionsRawSourceData', Em.K);
  207. });
  208. afterEach(function () {
  209. App.store.commit.restore();
  210. App.store.loadMany.restore();
  211. App.alertDefinitionsMapper.setProperties({
  212. 'model': App.AlertDefinition,
  213. 'portModel': App.PortAlertDefinition,
  214. 'metricsModel': App.MetricsAlertDefinition,
  215. 'webModel': App.WebAlertDefinition,
  216. 'aggregateModel': App.AggregateAlertDefinition,
  217. 'scriptModel': App.ScriptAlertDefinition,
  218. 'reportModel': App.AlertReportDefinition,
  219. 'metricsSourceModel': App.AlertMetricsSourceDefinition,
  220. 'metricsUriModel': App.AlertMetricsUriDefinition
  221. });
  222. App.PortAlertDefinition.find.restore();
  223. App.MetricsAlertDefinition.find.restore();
  224. App.WebAlertDefinition.find.restore();
  225. App.AggregateAlertDefinition.find.restore();
  226. App.ScriptAlertDefinition.find.restore();
  227. App.alertDefinitionsMapper.deleteRecord.restore();
  228. App.router.get.restore();
  229. App.cache['previousAlertGroupsMap'] = {};
  230. App.alertDefinitionsMapper.setMetricsSourcePropertyLists.restore();
  231. App.alertDefinitionsMapper.setAlertDefinitionsRawSourceData.restore();
  232. });
  233. describe('should parse METRIC alertDefinitions', function () {
  234. var data = {items: [json.items[0]]},
  235. expected = [{
  236. id: 1,
  237. "name": "yarn_resourcemanager_rpc_latency",
  238. "label": "ResourceManager RPC Latency",
  239. "description" : "some description",
  240. "service_id": "YARN",
  241. "component_name": "RESOURCEMANAGER",
  242. "enabled": true,
  243. "scope": "ANY",
  244. "interval": 5,
  245. "type": "METRIC",
  246. "jmx_id": "1jmx",
  247. "uri_id": "1uri"
  248. }],
  249. expectedMetricsSource = [{
  250. "id":"1jmx",
  251. "value":"{0}",
  252. "property_list":[
  253. "Hadoop:service=ResourceManager,name=RpcActivityForPort*/RpcQueueTimeAvgTime",
  254. "Hadoop:service=ResourceManager,name=RpcActivityForPort*/RpcProcessingTimeAvgTime"
  255. ]
  256. }],
  257. expectedMetricsUri = [{
  258. "id":"1uri",
  259. "http":"{{yarn-site/yarn.resourcemanager.webapp.address}}",
  260. "https":"{{yarn-site/yarn.resourcemanager.webapp.https.address}}",
  261. "https_property":"{{yarn-site/yarn.http.policy}}",
  262. "https_property_value":"HTTPS_ONLY"
  263. }];
  264. beforeEach(function () {
  265. App.alertDefinitionsMapper.map(data);
  266. });
  267. it('parsing metrics model', function() {
  268. testHelpers.nestedExpect(expected, App.alertDefinitionsMapper.get('metricsModel.content'));
  269. });
  270. it('parse metrics source', function() {
  271. testHelpers.nestedExpect(expectedMetricsSource, App.alertDefinitionsMapper.get('metricsSourceModel.content'));
  272. });
  273. it('parse metrics uri', function() {
  274. testHelpers.nestedExpect(expectedMetricsUri, App.alertDefinitionsMapper.get('metricsUriModel.content'));
  275. });
  276. });
  277. describe('should parse WEB alertDefinitions', function () {
  278. var data = {items: [json.items[1]]},
  279. expected = [
  280. {
  281. "id": 2,
  282. "name": "yarn_resourcemanager_webui",
  283. "label": "ResourceManager Web UI",
  284. "description" : "",
  285. "service_id": "YARN",
  286. "component_name": "RESOURCEMANAGER",
  287. "enabled": true,
  288. "scope": "ANY",
  289. "interval": 1,
  290. "type": "WEB",
  291. "uri_id": "2uri"
  292. }
  293. ],
  294. expectedMetricsUri = [{
  295. "id":"2uri",
  296. "http":"{{yarn-site/yarn.resourcemanager.webapp.address}}",
  297. "https":"{{yarn-site/yarn.resourcemanager.webapp.https.address}}",
  298. "https_property":"{{yarn-site/yarn.http.policy}}",
  299. "https_property_value":"HTTPS_ONLY"
  300. }];
  301. beforeEach(function () {
  302. App.alertDefinitionsMapper.map(data);
  303. });
  304. it('parsing web model', function() {
  305. testHelpers.nestedExpect(expected, App.alertDefinitionsMapper.get('webModel.content'));
  306. });
  307. it('parse metrics uri', function() {
  308. testHelpers.nestedExpect(expectedMetricsUri, App.alertDefinitionsMapper.get('metricsUriModel.content'));
  309. });
  310. });
  311. it('should parse AGGREGATE alertDefinitions', function () {
  312. var data = {items: [json.items[2]]},
  313. expected = [
  314. {
  315. "id":3,
  316. "name":"yarn_nodemanager_webui_percent",
  317. "label":"Percent NodeManagers Available",
  318. "description" : "",
  319. "service_id":"YARN",
  320. "component_name":null,
  321. "enabled":true,
  322. "scope":"SERVICE",
  323. "interval":1,
  324. "type":"AGGREGATE",
  325. "alert_name":"yarn_nodemanager_webui"
  326. }
  327. ];
  328. App.alertDefinitionsMapper.map(data);
  329. testHelpers.nestedExpect(expected, App.alertDefinitionsMapper.get('aggregateModel.content'));
  330. });
  331. it('should parse SCRIPT alertDefinitions', function () {
  332. var data = {items: [json.items[3]]},
  333. expected = [
  334. {
  335. "id":4,
  336. "name":"yarn_nodemanager_health",
  337. "label":"NodeManager Health",
  338. "description" : "some description",
  339. "service_id":"YARN",
  340. "component_name":"NODEMANAGER",
  341. "enabled":true,
  342. "scope":"HOST",
  343. "interval":1,
  344. "type":"SCRIPT",
  345. "location":"HDP/2.0.6/services/YARN/package/files/alert_nodemanager_health.py"
  346. }
  347. ];
  348. App.alertDefinitionsMapper.map(data);
  349. testHelpers.nestedExpect(expected, App.alertDefinitionsMapper.get('scriptModel.content'));
  350. });
  351. it('should parse PORT alertDefinitions', function () {
  352. var data = {items: [json.items[4]]},
  353. expected = [
  354. {
  355. "id":5,
  356. "name":"zookeeper_server_process",
  357. "label":"ZooKeeper Server Process",
  358. "description" : "some description",
  359. "service_id":"ZOOKEEPER",
  360. "component_name":"ZOOKEEPER_SERVER",
  361. "enabled":true,
  362. "scope":"ANY",
  363. "interval":1,
  364. "type":"PORT",
  365. "default_port":2181,
  366. "uri":"{{zookeeper-env/clientPort}}"
  367. }
  368. ];
  369. App.alertDefinitionsMapper.map(data);
  370. testHelpers.nestedExpect(expected, App.alertDefinitionsMapper.get('portModel.content'));
  371. });
  372. it('should set groups from App.cache.previousAlertGroupsMap', function () {
  373. App.cache['previousAlertGroupsMap'] = {
  374. 1: [5,1],
  375. 2: [4,3],
  376. 3: [3,2],
  377. 4: [2,5],
  378. 5: [1,4]
  379. };
  380. App.alertDefinitionsMapper.map(json);
  381. expect(App.alertDefinitionsMapper.get('portModel.content')[0].groups).to.eql([1, 4]);
  382. expect(App.alertDefinitionsMapper.get('metricsModel.content')[0].groups).to.eql([5, 1]);
  383. expect(App.alertDefinitionsMapper.get('webModel.content')[0].groups).to.eql([4, 3]);
  384. expect(App.alertDefinitionsMapper.get('aggregateModel.content')[0].groups).to.eql([3, 2]);
  385. expect(App.alertDefinitionsMapper.get('scriptModel.content')[0].groups).to.eql([2, 5]);
  386. });
  387. describe('should delete not existing definitions', function () {
  388. var definitions = [
  389. Em.Object.create({id: 100500, type: 'PORT'})
  390. ];
  391. beforeEach(function () {
  392. sinon.stub(App.AlertDefinition, 'getAllDefinitions', function () {
  393. return definitions;
  394. });
  395. });
  396. afterEach(function() {
  397. App.AlertDefinition.getAllDefinitions.restore();
  398. });
  399. it('should delete PORT alert definition with id 100500', function () {
  400. App.alertDefinitionsMapper.map(json);
  401. expect(App.alertDefinitionsMapper.deleteRecord.calledOnce).to.be.true;
  402. expect(App.alertDefinitionsMapper.deleteRecord.args[0][0].id).to.equal(100500);
  403. });
  404. });
  405. });
  406. });