widget_mixin_test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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('#beforeRender()', 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. });
  32. afterEach(function () {
  33. this.mock.restore();
  34. mixinObject.getHostComponentMetrics.restore();
  35. mixinObject.getServiceComponentMetrics.restore();
  36. });
  37. it('has host_component_criteria', function () {
  38. this.mock.returns({'key1': {host_component_criteria: 'criteria'}});
  39. mixinObject.set('isLoaded', false);
  40. mixinObject.beforeRender();
  41. expect(mixinObject.getHostComponentMetrics.calledWith({host_component_criteria: 'criteria'})).to.be.true;
  42. expect(mixinObject.get('isLoaded')).to.be.true;
  43. });
  44. it('host_component_criteria is absent', function () {
  45. this.mock.returns({'key1': {}});
  46. mixinObject.set('isLoaded', false);
  47. mixinObject.beforeRender();
  48. expect(mixinObject.getServiceComponentMetrics.calledWith({})).to.be.true;
  49. expect(mixinObject.get('isLoaded')).to.be.true;
  50. });
  51. });
  52. describe("#extractExpressions()", function() {
  53. var mixinObject = mixinClass.create();
  54. var testCases = [
  55. {
  56. data: '',
  57. result: []
  58. },
  59. {
  60. data: 'text',
  61. result: []
  62. },
  63. {
  64. data: 'text${a}',
  65. result: ['a']
  66. },
  67. {
  68. data: 'text${a} - ${a.b}',
  69. result: ['a', 'a.b']
  70. },
  71. {
  72. data: '${o.a-(b+4)/cc*tt}',
  73. result: ['o.a-(b+4)/cc*tt']
  74. }
  75. ];
  76. testCases.forEach(function (test) {
  77. it('input: ' + test.data, function () {
  78. var input = {value: test.data};
  79. expect(mixinObject.extractExpressions(input)).to.eql(test.result);
  80. });
  81. });
  82. });
  83. describe("#getRequestData()", function() {
  84. var mixinObject = mixinClass.create();
  85. it("", function() {
  86. var data = [
  87. {
  88. "name": "regionserver.Server.percentFilesLocal",
  89. "widget_id": "metrics/hbase/regionserver/percentFilesLocal",
  90. "service_name": "HBASE",
  91. "component_name": "HBASE_REGIONSERVER"
  92. },
  93. {
  94. "name": "regionserver.Server.percentFilesLocal2",
  95. "widget_id": "w2",
  96. "service_name": "HBASE",
  97. "component_name": "HBASE_REGIONSERVER"
  98. },
  99. {
  100. "name": "regionserver.Server.percentFilesLocal",
  101. "widget_id": "metrics/hbase/regionserver/percentFilesLocal",
  102. "service_name": "HBASE",
  103. "component_name": "HBASE_REGIONSERVER",
  104. "host_component_criteria": 'c1'
  105. },
  106. {
  107. "name": "regionserver.Server.percentFilesLocal",
  108. "widget_id": "metrics/hbase/regionserver/percentFilesLocal",
  109. "service_name": "HDFS",
  110. "component_name": "DATANODE",
  111. "host_component_criteria": 'c1'
  112. }
  113. ];
  114. expect(mixinObject.getRequestData(data)).to.eql({
  115. "HBASE_HBASE_REGIONSERVER_undefined": {
  116. "name": "regionserver.Server.percentFilesLocal",
  117. "service_name": "HBASE",
  118. "component_name": "HBASE_REGIONSERVER",
  119. "widget_ids": [
  120. "metrics/hbase/regionserver/percentFilesLocal",
  121. "w2"
  122. ]
  123. },
  124. "HBASE_HBASE_REGIONSERVER_c1": {
  125. "name": "regionserver.Server.percentFilesLocal",
  126. "service_name": "HBASE",
  127. "component_name": "HBASE_REGIONSERVER",
  128. "host_component_criteria": "c1",
  129. "widget_ids": [
  130. "metrics/hbase/regionserver/percentFilesLocal"
  131. ]
  132. },
  133. "HDFS_DATANODE_c1": {
  134. "name": "regionserver.Server.percentFilesLocal",
  135. "service_name": "HDFS",
  136. "component_name": "DATANODE",
  137. "host_component_criteria": "c1",
  138. "widget_ids": [
  139. "metrics/hbase/regionserver/percentFilesLocal"
  140. ]
  141. }
  142. });
  143. });
  144. });
  145. describe("#getServiceComponentMetrics()", function () {
  146. var mixinObject = mixinClass.create();
  147. before(function () {
  148. sinon.stub(App.ajax, 'send');
  149. });
  150. after(function () {
  151. App.ajax.send.restore();
  152. });
  153. it("", function () {
  154. var request = {
  155. service_name: 'S1',
  156. component_name: 'C1',
  157. widget_ids: ['w1', 'w2']
  158. };
  159. mixinObject.getServiceComponentMetrics(request);
  160. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  161. name: 'widgets.serviceComponent.metrics.get',
  162. sender: mixinObject,
  163. data: {
  164. serviceName: 'S1',
  165. componentName: 'C1',
  166. widgetIds: 'w1,w2'
  167. },
  168. success: 'getServiceComponentMetricsSuccessCallback'
  169. })
  170. });
  171. });
  172. describe("#getServiceComponentMetricsSuccessCallback()", function() {
  173. var mixinObject = mixinClass.create();
  174. it("", function() {
  175. var data = {
  176. metrics: {
  177. "hbase" : {
  178. "ipc" : {
  179. "IPC" : {
  180. "numOpenConnections" : 11.5
  181. }
  182. }
  183. }
  184. }
  185. };
  186. mixinObject.set('content.metrics', [
  187. {
  188. name: 'ipc.IPC.numOpenConnections'
  189. }
  190. ]);
  191. mixinObject.getServiceComponentMetricsSuccessCallback(data, {}, {serviceName: 'hbase'});
  192. expect(mixinObject.get('metrics').findProperty('name', 'ipc.IPC.numOpenConnections').data).to.equal(11.5);
  193. });
  194. });
  195. describe("#getHostComponentMetrics()", function () {
  196. var mixinObject = mixinClass.create();
  197. before(function () {
  198. sinon.stub(App.ajax, 'send');
  199. });
  200. after(function () {
  201. App.ajax.send.restore();
  202. });
  203. it("", function () {
  204. var request = {
  205. service_name: 'S1',
  206. component_name: 'C1',
  207. widget_ids: ['w1', 'w2'],
  208. host_component_criteria: 'c1'
  209. };
  210. mixinObject.getHostComponentMetrics(request);
  211. expect(App.ajax.send.getCall(0).args[0]).to.eql({
  212. name: 'widgets.hostComponent.metrics.get',
  213. sender: mixinObject,
  214. data: {
  215. serviceName: 'S1',
  216. componentName: 'C1',
  217. widgetIds: 'w1,w2',
  218. hostComponentCriteria: 'host_components/HostRoles/c1'
  219. },
  220. success: 'getHostComponentMetricsSuccessCallback'
  221. })
  222. });
  223. });
  224. });