widget_mixin_test.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(mixinObject, 'getHostComponentMetrics').returns({complete: function(callback){
  26. callback();
  27. }});
  28. sinon.stub(mixinObject, 'getServiceComponentMetrics').returns({complete: function(callback){
  29. callback();
  30. }});
  31. sinon.stub(mixinObject, 'onMetricsLoaded');
  32. });
  33. afterEach(function () {
  34. this.mock.restore();
  35. mixinObject.getHostComponentMetrics.restore();
  36. mixinObject.getServiceComponentMetrics.restore();
  37. mixinObject.onMetricsLoaded.restore();
  38. });
  39. it('has host_component_criteria', function () {
  40. this.mock.returns({'key1': {host_component_criteria: 'criteria'}});
  41. mixinObject.set('isLoaded', false);
  42. mixinObject.loadMetrics();
  43. expect(mixinObject.getHostComponentMetrics.calledWith({host_component_criteria: 'criteria'})).to.be.true;
  44. expect(mixinObject.onMetricsLoaded.calledOnce).to.be.true;
  45. });
  46. it('host_component_criteria is absent', function () {
  47. this.mock.returns({'key1': {}});
  48. mixinObject.set('isLoaded', false);
  49. mixinObject.loadMetrics();
  50. expect(mixinObject.getServiceComponentMetrics.calledWith({})).to.be.true;
  51. expect(mixinObject.onMetricsLoaded.calledOnce).to.be.true;
  52. });
  53. });
  54. describe("#extractExpressions()", function() {
  55. var mixinObject = mixinClass.create();
  56. var testCases = [
  57. {
  58. data: '',
  59. result: []
  60. },
  61. {
  62. data: 'text',
  63. result: []
  64. },
  65. {
  66. data: 'text${a}',
  67. result: ['a']
  68. },
  69. {
  70. data: 'text${a} - ${a.b}',
  71. result: ['a', 'a.b']
  72. },
  73. {
  74. data: '${o.a-(b+4)/cc*tt}',
  75. result: ['o.a-(b+4)/cc*tt']
  76. }
  77. ];
  78. testCases.forEach(function (test) {
  79. it('input: ' + test.data, function () {
  80. var input = {value: test.data};
  81. expect(mixinObject.extractExpressions(input)).to.eql(test.result);
  82. });
  83. });
  84. });
  85. describe("#getRequestData()", function() {
  86. var mixinObject = mixinClass.create();
  87. it("", function() {
  88. var data = [
  89. {
  90. "name": "regionserver.Server.percentFilesLocal",
  91. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  92. "service_name": "HBASE",
  93. "component_name": "HBASE_REGIONSERVER"
  94. },
  95. {
  96. "name": "regionserver.Server.percentFilesLocal2",
  97. "metric_path": "w2",
  98. "service_name": "HBASE",
  99. "component_name": "HBASE_REGIONSERVER"
  100. },
  101. {
  102. "name": "regionserver.Server.percentFilesLocal",
  103. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  104. "service_name": "HBASE",
  105. "component_name": "HBASE_REGIONSERVER",
  106. "host_component_criteria": 'c1'
  107. },
  108. {
  109. "name": "regionserver.Server.percentFilesLocal",
  110. "metric_path": "metrics/hbase/regionserver/percentFilesLocal",
  111. "service_name": "HDFS",
  112. "component_name": "DATANODE",
  113. "host_component_criteria": 'c1'
  114. }
  115. ];
  116. expect(mixinObject.getRequestData(data)).to.eql({
  117. "HBASE_HBASE_REGIONSERVER": {
  118. "name": "regionserver.Server.percentFilesLocal",
  119. "service_name": "HBASE",
  120. "component_name": "HBASE_REGIONSERVER",
  121. "metric_paths": [
  122. "metrics/hbase/regionserver/percentFilesLocal",
  123. "w2"
  124. ]
  125. },
  126. "HBASE_HBASE_REGIONSERVER_c1": {
  127. "name": "regionserver.Server.percentFilesLocal",
  128. "service_name": "HBASE",
  129. "component_name": "HBASE_REGIONSERVER",
  130. "host_component_criteria": "c1",
  131. "metric_paths": [
  132. "metrics/hbase/regionserver/percentFilesLocal"
  133. ]
  134. },
  135. "HDFS_DATANODE_c1": {
  136. "name": "regionserver.Server.percentFilesLocal",
  137. "service_name": "HDFS",
  138. "component_name": "DATANODE",
  139. "host_component_criteria": "c1",
  140. "metric_paths": [
  141. "metrics/hbase/regionserver/percentFilesLocal"
  142. ]
  143. }
  144. });
  145. });
  146. });
  147. describe("#getServiceComponentMetrics()", function () {
  148. var mixinObject = mixinClass.create();
  149. before(function () {
  150. sinon.stub(App.ajax, 'send');
  151. });
  152. after(function () {
  153. App.ajax.send.restore();
  154. });
  155. it("", function () {
  156. var request = {
  157. service_name: 'S1',
  158. component_name: 'C1',
  159. metric_paths: ['w1', 'w2']
  160. };
  161. mixinObject.getServiceComponentMetrics(request);
  162. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  163. name: 'widgets.serviceComponent.metrics.get',
  164. sender: mixinObject,
  165. data: {
  166. serviceName: 'S1',
  167. componentName: 'C1',
  168. metricPaths: 'w1,w2'
  169. },
  170. success: 'getMetricsSuccessCallback'
  171. })
  172. });
  173. });
  174. describe("#getMetricsSuccessCallback()", function () {
  175. var mixinObject = mixinClass.create();
  176. it("", function () {
  177. var data = {
  178. metrics: {
  179. "hbase": {
  180. "ipc": {
  181. "IPC": {
  182. "numOpenConnections": 11.5
  183. }
  184. }
  185. }
  186. }
  187. };
  188. mixinObject.set('content.metrics', [
  189. {
  190. metric_path: 'metrics/hbase/ipc/IPC/numOpenConnections'
  191. }
  192. ]);
  193. mixinObject.getMetricsSuccessCallback(data);
  194. expect(mixinObject.get('metrics').findProperty('metric_path', 'metrics/hbase/ipc/IPC/numOpenConnections').data).to.equal(11.5);
  195. });
  196. });
  197. describe("#getHostComponentMetrics()", function () {
  198. var mixinObject = mixinClass.create();
  199. before(function () {
  200. sinon.stub(App.ajax, 'send');
  201. });
  202. after(function () {
  203. App.ajax.send.restore();
  204. });
  205. it("", function () {
  206. var request = {
  207. service_name: 'S1',
  208. component_name: 'C1',
  209. metric_paths: ['w1', 'w2'],
  210. host_component_criteria: 'c1'
  211. };
  212. mixinObject.getHostComponentMetrics(request);
  213. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  214. name: 'widgets.hostComponent.metrics.get',
  215. sender: mixinObject,
  216. data: {
  217. serviceName: 'S1',
  218. componentName: 'C1',
  219. metricPaths: 'w1,w2',
  220. hostComponentCriteria: 'host_components/HostRoles/c1'
  221. },
  222. success: 'getMetricsSuccessCallback'
  223. })
  224. });
  225. });
  226. describe("#calculateValues()", function() {
  227. var mixinObject = mixinClass.create();
  228. beforeEach(function () {
  229. sinon.stub(mixinObject, 'extractExpressions');
  230. this.mock = sinon.stub(mixinObject, 'computeExpression');
  231. });
  232. afterEach(function () {
  233. mixinObject.extractExpressions.restore();
  234. this.mock.restore();
  235. });
  236. it("value compute correctly", function() {
  237. this.mock.returns({'${a}': 1});
  238. mixinObject.set('content.values', [{
  239. value: '${a}'
  240. }]);
  241. mixinObject.calculateValues();
  242. expect(mixinObject.get('content.values')[0].computedValue).to.equal('1');
  243. });
  244. it("value not available", function() {
  245. this.mock.returns({});
  246. mixinObject.set('content.values', [{
  247. value: '${a}'
  248. }]);
  249. mixinObject.calculateValues();
  250. expect(mixinObject.get('content.values')[0].computedValue).to.equal(Em.I18n.t('common.na'));
  251. });
  252. });
  253. describe("#computeExpression()", function() {
  254. var mixinObject = mixinClass.create();
  255. it("expression missing metrics", function() {
  256. var expressions = ['e.m1'];
  257. var metrics = [];
  258. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  259. "${e.m1}": ""
  260. });
  261. });
  262. it("Value is not correct mathematical expression", function() {
  263. var expressions = ['e.m1'];
  264. var metrics = [{
  265. name: 'e.m1',
  266. data: 'a+1'
  267. }];
  268. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  269. "${e.m1}": ""
  270. });
  271. });
  272. it("correct expression", function() {
  273. var expressions = ['e.m1+e.m1'];
  274. var metrics = [{
  275. name: 'e.m1',
  276. data: 1
  277. }];
  278. expect(mixinObject.computeExpression(expressions, metrics)).to.eql({
  279. "${e.m1+e.m1}": "2"
  280. });
  281. });
  282. });
  283. });