widget_mixin_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. describe('App.WidgetMixin', function () {
  20. var mixinClass = Em.Object.extend(App.WidgetMixin, {metrics: [], content: {}});
  21. describe('#loadMetrics()', function () {
  22. var mixinObject = mixinClass.create();
  23. beforeEach(function () {
  24. this.mock = sinon.stub(mixinObject, 'getRequestData');
  25. sinon.stub(App.WidgetLoadAggregator, 'add');
  26. });
  27. afterEach(function () {
  28. this.mock.restore();
  29. App.WidgetLoadAggregator.add.restore();
  30. });
  31. it('has host_component_criteria', function () {
  32. this.mock.returns({'key1': {host_component_criteria: 'criteria'}});
  33. mixinObject.set('isLoaded', false);
  34. mixinObject.loadMetrics();
  35. expect(App.WidgetLoadAggregator.add.calledOnce).to.be.true;
  36. });
  37. it('host_component_criteria is absent', function () {
  38. this.mock.returns({'key1': {}});
  39. mixinObject.set('isLoaded', false);
  40. mixinObject.loadMetrics();
  41. expect(App.WidgetLoadAggregator.add.calledOnce).to.be.true;
  42. });
  43. });
  44. describe("#extractExpressions()", function () {
  45. var mixinObject = mixinClass.create();
  46. var testCases = [
  47. {
  48. data: '',
  49. result: []
  50. },
  51. {
  52. data: 'text',
  53. result: []
  54. },
  55. {
  56. data: 'text${a}',
  57. result: ['a']
  58. },
  59. {
  60. data: 'text${a} - ${a.b}',
  61. result: ['a', 'a.b']
  62. },
  63. {
  64. data: '${o.a-(b+4)/cc*tt}',
  65. result: ['o.a-(b+4)/cc*tt']
  66. }
  67. ];
  68. testCases.forEach(function (test) {
  69. it('input: ' + test.data, function () {
  70. var input = {value: test.data};
  71. expect(mixinObject.extractExpressions(input)).to.eql(test.result);
  72. });
  73. });
  74. it('input is null', function () {
  75. var input = null;
  76. expect(mixinObject.extractExpressions(input)).to.be.empty;
  77. });
  78. });
  79. describe("#getRequestData()", function () {
  80. var data = [
  81. {
  82. "name": "regionserver.Server.percentFilesLocal",
  83. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  84. "service_name": "HBASE",
  85. "component_name": "HBASE_REGIONSERVER"
  86. },
  87. {
  88. "name": "regionserver.Server.percentFilesLocal2",
  89. "metric_path": "w2",
  90. "service_name": "HBASE",
  91. "component_name": "HBASE_REGIONSERVER"
  92. },
  93. {
  94. "name": "regionserver.Server.percentFilesLocal",
  95. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  96. "service_name": "HBASE",
  97. "component_name": "HBASE_REGIONSERVER",
  98. "host_component_criteria": 'c1'
  99. },
  100. {
  101. "name": "regionserver.Server.percentFilesLocal",
  102. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  103. "service_name": "HDFS",
  104. "component_name": "DATANODE",
  105. "host_component_criteria": 'c1'
  106. }
  107. ];
  108. beforeEach(function () {
  109. this.requestData = mixinClass.create().getRequestData(data);
  110. });
  111. it('HBASE_HBASE_REGIONSERVER', function () {
  112. var hbaseRegionServer = {
  113. "name": "regionserver.Server.percentFilesLocal",
  114. "service_name": "HBASE",
  115. "component_name": "HBASE_REGIONSERVER",
  116. "metric_paths": [
  117. {
  118. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  119. "metric_type": "POINT_IN_TIME",
  120. "id": "metrics/hbase/regionserver/percentFilesLocal_POINT_IN_TIME",
  121. "context": {}
  122. },
  123. {
  124. "metric_path": "w2",
  125. "metric_type": "POINT_IN_TIME",
  126. "id": "w2_POINT_IN_TIME",
  127. "context": {}
  128. }
  129. ]
  130. };
  131. expect(JSON.stringify(this.requestData.HBASE_HBASE_REGIONSERVER)).to.equal(JSON.stringify(hbaseRegionServer));
  132. });
  133. it('HBASE_HBASE_REGIONSERVER_c1', function () {
  134. var hbaseRegionServerC1 = {
  135. "name": "regionserver.Server.percentFilesLocal",
  136. "service_name": "HBASE",
  137. "component_name": "HBASE_REGIONSERVER",
  138. "host_component_criteria": "c1",
  139. "metric_paths": [
  140. {
  141. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  142. "metric_type": "POINT_IN_TIME",
  143. "id": "metrics/hbase/regionserver/percentFilesLocal_POINT_IN_TIME",
  144. "context": {}
  145. }
  146. ]
  147. };
  148. expect(JSON.stringify(this.requestData.HBASE_HBASE_REGIONSERVER_c1)).to.equal(JSON.stringify(hbaseRegionServerC1));
  149. });
  150. it('HDFS_DATANODE_c1', function () {
  151. var hdfsDataNodeC1 = {
  152. "name": "regionserver.Server.percentFilesLocal",
  153. "service_name": "HDFS",
  154. "component_name": "DATANODE",
  155. "host_component_criteria": "c1",
  156. "metric_paths": [
  157. {
  158. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  159. "metric_type": "POINT_IN_TIME",
  160. "id": "metrics/hbase/regionserver/percentFilesLocal_POINT_IN_TIME",
  161. "context": {}
  162. }
  163. ]
  164. };
  165. expect(JSON.stringify(this.requestData.HDFS_DATANODE_c1)).to.equal(JSON.stringify(hdfsDataNodeC1));
  166. });
  167. });
  168. describe("#getServiceComponentMetrics()", function () {
  169. var mixinObject = mixinClass.create();
  170. before(function () {
  171. sinon.stub(App.ajax, 'send');
  172. });
  173. after(function () {
  174. App.ajax.send.restore();
  175. });
  176. it("valid request is sent", function () {
  177. var request = {
  178. service_name: 'S1',
  179. component_name: 'C1',
  180. metric_paths: [
  181. {
  182. "metric_path": "w1",
  183. "metric_type": "POINT_IN_TIME",
  184. "id": "w1_POINT_IN_TIME",
  185. "context": {}
  186. },
  187. {
  188. "metric_path": "w2",
  189. "metric_type": "POINT_IN_TIME",
  190. "id": "w2_POINT_IN_TIME",
  191. "context": {}
  192. }
  193. ]
  194. };
  195. mixinObject.getServiceComponentMetrics(request);
  196. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  197. name: 'widgets.serviceComponent.metrics.get',
  198. sender: mixinObject,
  199. data: {
  200. serviceName: 'S1',
  201. componentName: 'C1',
  202. metricPaths: 'w1,w2'
  203. }
  204. })
  205. });
  206. });
  207. describe("#getMetricsSuccessCallback()", function () {
  208. var mixinObject = mixinClass.create();
  209. it("metric is mapped from provided path", function () {
  210. var data = {
  211. metrics: {
  212. "hbase": {
  213. "ipc": {
  214. "IPC": {
  215. "numOpenConnections": 11.5
  216. }
  217. }
  218. }
  219. }
  220. };
  221. mixinObject.set('content.metrics', [
  222. {
  223. metric_path: 'metrics/hbase/ipc/IPC/numOpenConnections'
  224. }
  225. ]);
  226. mixinObject.getMetricsSuccessCallback(data);
  227. expect(mixinObject.get('metrics').findProperty('metric_path', 'metrics/hbase/ipc/IPC/numOpenConnections').data).to.equal(11.5);
  228. });
  229. });
  230. describe("#getHostComponentMetrics()", function () {
  231. var mixinObject = mixinClass.create();
  232. before(function () {
  233. sinon.stub(App.ajax, 'send');
  234. sinon.stub(mixinObject, 'computeHostComponentCriteria').returns('criteria')
  235. });
  236. after(function () {
  237. App.ajax.send.restore();
  238. mixinObject.computeHostComponentCriteria.restore();
  239. });
  240. it("valid request is sent", function () {
  241. var request = {
  242. component_name: 'C1',
  243. metric_paths: [
  244. {
  245. "metric_path": "w1",
  246. "metric_type": "POINT_IN_TIME",
  247. "id": "w1_POINT_IN_TIME",
  248. "context": {}
  249. },
  250. {
  251. "metric_path": "w2",
  252. "metric_type": "POINT_IN_TIME",
  253. "id": "w2_POINT_IN_TIME",
  254. "context": {}
  255. }
  256. ],
  257. host_component_criteria: 'c1'
  258. };
  259. mixinObject.getHostComponentMetrics(request);
  260. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  261. name: 'widgets.hostComponent.metrics.get',
  262. sender: mixinObject,
  263. data: {
  264. componentName: 'C1',
  265. metricPaths: 'w1,w2',
  266. hostComponentCriteria: 'criteria'
  267. }
  268. })
  269. });
  270. });
  271. describe("#calculateValues()", function () {
  272. var mixinObject = mixinClass.create();
  273. beforeEach(function () {
  274. sinon.stub(mixinObject, 'extractExpressions');
  275. this.mock = sinon.stub(mixinObject, 'computeExpression');
  276. });
  277. afterEach(function () {
  278. mixinObject.extractExpressions.restore();
  279. this.mock.restore();
  280. });
  281. it("value compute correctly", function () {
  282. this.mock.returns({'${a}': 1});
  283. mixinObject.set('content.values', [{
  284. value: '${a}'
  285. }]);
  286. mixinObject.calculateValues();
  287. expect(mixinObject.get('content.values')[0].computedValue).to.equal('1');
  288. });
  289. it("value not available", function () {
  290. this.mock.returns({});
  291. mixinObject.set('content.values', [{
  292. value: '${a}'
  293. }]);
  294. mixinObject.calculateValues();
  295. expect(mixinObject.get('content.values')[0].computedValue).to.equal('<span class="grey">n/a</span>');
  296. });
  297. it("value is null", function () {
  298. this.mock.returns({'${a}': null});
  299. mixinObject.set('content.values', [{
  300. value: '${a}'
  301. }]);
  302. mixinObject.calculateValues();
  303. expect(mixinObject.get('content.values')[0].computedValue).to.equal('<span class="grey">n/a</span>');
  304. });
  305. });
  306. describe("#computeExpression()", function () {
  307. var mixinObject = mixinClass.create();
  308. it("expression missing metrics", function () {
  309. var expressions = ['e.m1'];
  310. var metrics = [];
  311. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  312. "${e.m1}": ""
  313. });
  314. });
  315. it("Value is not correct mathematical expression", function () {
  316. var expressions = ['e.m1'];
  317. var metrics = [{
  318. name: 'e.m1',
  319. data: 'a+1'
  320. }];
  321. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  322. "${e.m1}": ""
  323. });
  324. });
  325. it("correct expression", function () {
  326. var expressions = ['e.m1+e.m1'];
  327. var metrics = [{
  328. name: 'e.m1',
  329. data: 1
  330. }];
  331. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  332. "${e.m1+e.m1}": "2"
  333. });
  334. });
  335. });
  336. describe("#cloneWidget()", function () {
  337. var mixinObject = mixinClass.create();
  338. var popup;
  339. before(function () {
  340. sinon.spy(App, 'showConfirmationPopup');
  341. sinon.stub(mixinObject, 'postWidgetDefinition', Em.K);
  342. popup = mixinObject.cloneWidget();
  343. });
  344. after(function () {
  345. App.showConfirmationPopup.restore();
  346. mixinObject.postWidgetDefinition.restore();
  347. });
  348. it("popup is shown", function () {
  349. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  350. });
  351. it('postWidgetDefinition is called', function () {
  352. popup.onPrimary();
  353. expect(mixinObject.postWidgetDefinition.calledOnce).to.be.true;
  354. });
  355. });
  356. describe("#postWidgetDefinition()", function () {
  357. var mixinObject = mixinClass.create();
  358. before(function () {
  359. sinon.spy(App.ajax, 'send');
  360. sinon.stub(mixinObject, 'collectWidgetData').returns({});
  361. });
  362. after(function () {
  363. App.ajax.send.restore();
  364. mixinObject.collectWidgetData.restore();
  365. });
  366. it("valid request is sent", function () {
  367. mixinObject.postWidgetDefinition();
  368. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  369. name: 'widgets.wizard.add',
  370. sender: mixinObject,
  371. data: {
  372. data: {}
  373. },
  374. success: 'postWidgetDefinitionSuccessCallback'
  375. });
  376. });
  377. });
  378. });
  379. describe('App.WidgetLoadAggregator', function () {
  380. var aggregator = App.WidgetLoadAggregator;
  381. describe("#add()", function () {
  382. beforeEach(function () {
  383. sinon.stub(window, 'setTimeout').returns('timeId');
  384. });
  385. afterEach(function () {
  386. window.setTimeout.restore();
  387. });
  388. it("timeout started", function () {
  389. aggregator.set('timeoutId', 'timeId');
  390. aggregator.get('requests').clear();
  391. aggregator.add({});
  392. expect(aggregator.get('requests')).to.not.be.empty;
  393. expect(window.setTimeout.called).to.be.false;
  394. });
  395. it("timeout started (2)", function () {
  396. aggregator.set('timeoutId', null);
  397. aggregator.get('requests').clear();
  398. aggregator.add({});
  399. expect(aggregator.get('requests')).to.not.be.empty;
  400. expect(window.setTimeout.calledOnce).to.be.true;
  401. expect(aggregator.get('timeoutId')).to.equal('timeId');
  402. });
  403. });
  404. describe("#groupRequests()", function () {
  405. var result;
  406. beforeEach(function () {
  407. var requests = [
  408. {
  409. startCallName: 'n1',
  410. data: {
  411. component_name: 'C1',
  412. metric_paths: ['m1']
  413. },
  414. context: Em.Object.create({
  415. content: {
  416. widgetType: 'GRAPH'
  417. }
  418. })
  419. },
  420. {
  421. startCallName: 'n1',
  422. data: {
  423. component_name: 'C1',
  424. metric_paths: ['m2']
  425. },
  426. context: Em.Object.create({
  427. content: {
  428. widgetType: 'NUMBER'
  429. }
  430. })
  431. },
  432. {
  433. startCallName: 'n2',
  434. data: {
  435. component_name: 'C1',
  436. metric_paths: ['m3']
  437. },
  438. context: Em.Object.create({
  439. content: {
  440. widgetType: 'TEMPLATE'
  441. }
  442. })
  443. },
  444. {
  445. startCallName: 'n1',
  446. data: {
  447. component_name: 'C2',
  448. metric_paths: ['m4']
  449. },
  450. context: Em.Object.create({
  451. content: {
  452. widgetType: 'GAUGE'
  453. }
  454. })
  455. }
  456. ];
  457. result = aggregator.groupRequests(requests);
  458. });
  459. it("result.n1_C1.subRequests.length", function () {
  460. expect(result.n1_C1.subRequests.length).to.equal(1);
  461. });
  462. it("result.n1_C1.data.metric_paths.length", function () {
  463. expect(result.n1_C1.data.metric_paths.length).to.equal(1);
  464. });
  465. it("result.n1_C1_graph.subRequests.length", function () {
  466. expect(result.n1_C1_graph.subRequests.length).to.equal(1);
  467. });
  468. it("result.n1_C1_graph.data.metric_paths.length", function () {
  469. expect(result.n1_C1_graph.data.metric_paths.length).to.equal(1);
  470. });
  471. it("result.n2_C1.subRequests.length", function () {
  472. expect(result.n2_C1.subRequests.length).to.equal(1);
  473. });
  474. it("result.n2_C1.data.metric_paths.length", function () {
  475. expect(result.n2_C1.data.metric_paths.length).to.equal(1);
  476. });
  477. it("result.n1_C2.subRequests.length", function () {
  478. expect(result.n1_C2.subRequests.length).to.equal(1);
  479. });
  480. it("result.n1_C2.data.metric_paths.length", function () {
  481. expect(result.n1_C2.data.metric_paths.length).to.equal(1);
  482. });
  483. });
  484. describe("#runRequests()", function () {
  485. var mock = Em.Object.create({
  486. f1: function () {
  487. return {
  488. done: Em.K,
  489. complete: Em.K
  490. }
  491. },
  492. state: 'inDOM'
  493. });
  494. beforeEach(function () {
  495. sinon.stub(aggregator, 'groupRequests', function (requests) {
  496. return requests;
  497. });
  498. sinon.spy(mock, 'f1');
  499. });
  500. afterEach(function () {
  501. aggregator.groupRequests.restore();
  502. mock.f1.restore();
  503. });
  504. it("view in DOM", function () {
  505. var requests = {
  506. 'r1': {
  507. data: {
  508. metric_paths: ['m1', 'm1', 'm2']
  509. },
  510. context: mock,
  511. startCallName: 'f1'
  512. }
  513. };
  514. aggregator.runRequests(requests);
  515. expect(mock.f1.calledWith(requests.r1.data)).to.be.true;
  516. });
  517. it("view destroyed", function () {
  518. var requests = {
  519. 'r1': {
  520. data: {
  521. metric_paths: ['m1', 'm1', 'm2']
  522. },
  523. context: mock,
  524. startCallName: 'f1'
  525. }
  526. };
  527. mock.set('state', 'destroyed');
  528. aggregator.runRequests(requests);
  529. expect(mock.f1.called).to.be.false;
  530. });
  531. });
  532. });