heatmap_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. require('models/rack');
  21. require('controllers/main/charts/heatmap');
  22. function getController() {
  23. return App.MainChartsHeatmapController.create();
  24. }
  25. describe('MainChartsHeatmapController', function () {
  26. before(function () {
  27. controller = getController();
  28. });
  29. App.TestAliases.testAsComputedAlias(getController(), 'activeWidget', 'widgets.firstObject', 'object');
  30. App.TestAliases.testAsComputedAlias(getController(), 'hostToSlotMap', 'selectedMetric.hostToSlotMap', 'object');
  31. describe('#validation()', function () {
  32. beforeEach(function() {
  33. controller.setProperties({
  34. allMetrics: [],
  35. selectedMetric: Ember.Object.create({maximumValue: 100})
  36. });
  37. });
  38. it('should set maximumValue if inputMaximum consists only of digits', function () {
  39. controller.set("inputMaximum", 5);
  40. expect(controller.get('selectedMetric.maximumValue')).to.equal(5);
  41. });
  42. it('should not set maximumValue if inputMaximum consists not only of digits', function () {
  43. controller.set("inputMaximum", 'qwerty');
  44. expect(controller.get('selectedMetric.maximumValue')).to.equal(100);
  45. });
  46. it('should not set maximumValue if inputMaximum consists not only of digits', function () {
  47. controller.set("inputMaximum", '100%');
  48. expect(controller.get('selectedMetric.maximumValue')).to.equal(100);
  49. });
  50. it('should set maximumValue if inputMaximum consists only of digits', function () {
  51. controller.set("inputMaximum", 1000);
  52. expect(controller.get('selectedMetric.maximumValue')).to.equal(1000);
  53. })
  54. });
  55. describe('#showHeatMapMetric()', function () {
  56. beforeEach(function () {
  57. sinon.stub(App.ajax, 'send', function () {
  58. return {
  59. done: function (callback) {
  60. callback();
  61. }
  62. }
  63. });
  64. controller.setProperties({
  65. activeWidgetLayout: Em.Object.create({
  66. displayName: 'widget',
  67. id: '1',
  68. scope: 'CLUSTER',
  69. layoutName: 'defualt_layout',
  70. sectionName: 'default_section'
  71. })
  72. });
  73. });
  74. afterEach(function () {
  75. App.ajax.send.restore();
  76. });
  77. it('should call App.ajax', function () {
  78. controller.showHeatMapMetric({context:{id: 2}});
  79. expect(App.ajax.send.called).to.be.true;
  80. });
  81. });
  82. describe('#rackClass', function () {
  83. beforeEach(function () {
  84. controller.setProperties({
  85. allMetrics: [],
  86. racks: [1]
  87. });
  88. });
  89. it('should return "span12" for 1 cluster rack', function () {
  90. expect(controller.get('rackClass')).to.equal('span12');
  91. });
  92. it('should return "span6" for 2 cluster racks', function () {
  93. controller.set('racks', [1, 2]);
  94. expect(controller.get('rackClass')).to.equal('span6');
  95. });
  96. it('should return "span4" for 3 cluster racks', function () {
  97. controller.set('racks', [1, 2, 3]);
  98. expect(controller.get('rackClass')).to.equal('span4');
  99. });
  100. });
  101. describe("#loadHeatmapsUrlParams", function() {
  102. it("content.serviceName is null", function() {
  103. controller.set('content', Em.Object.create({serviceName: null}));
  104. expect(controller.get('loadHeatmapsUrlParams')).to.equal('WidgetInfo/widget_type=HEATMAP&WidgetInfo/scope=CLUSTER&fields=WidgetInfo/metrics');
  105. });
  106. it("content.serviceName is correct", function() {
  107. controller.set('content', Em.Object.create({serviceName: 'S1'}));
  108. expect(controller.get('loadHeatmapsUrlParams')).to.equal('WidgetInfo/widget_type=HEATMAP&WidgetInfo/scope=CLUSTER&WidgetInfo/metrics.matches(.*\"service_name\":\"S1\".*)&fields=WidgetInfo/metrics');
  109. });
  110. });
  111. describe("#loadPageData()", function() {
  112. var allHeatmapData = {
  113. items: [
  114. {
  115. WidgetInfo: 'info'
  116. }
  117. ]
  118. };
  119. beforeEach(function(){
  120. sinon.stub(controller, 'loadRacks').returns({
  121. always: function(callback) {
  122. callback();
  123. }
  124. });
  125. sinon.stub(controller, 'getAllHeatMaps').returns({
  126. done: function(callback) {
  127. callback(allHeatmapData);
  128. }
  129. });
  130. sinon.stub(controller, 'resetPageData');
  131. sinon.stub(controller, 'categorizeByServiceName').returns('categories');
  132. sinon.stub(controller, 'getActiveWidgetLayout');
  133. controller.get('allHeatmaps').clear();
  134. controller.loadPageData();
  135. });
  136. afterEach(function() {
  137. controller.loadRacks.restore();
  138. controller.resetPageData.restore();
  139. controller.getAllHeatMaps.restore();
  140. controller.categorizeByServiceName.restore();
  141. controller.getActiveWidgetLayout.restore();
  142. });
  143. it("loadRacks() should be called", function() {
  144. expect(controller.loadRacks.calledOnce).to.be.true;
  145. expect(controller.resetPageData.calledOnce).to.be.true;
  146. });
  147. it("getAllHeatMaps() should be called", function() {
  148. expect(controller.getAllHeatMaps.calledOnce).to.be.true;
  149. expect(controller.get('isLoaded')).to.be.true;
  150. expect(controller.get('allHeatmaps')[0]).to.equal('info')
  151. });
  152. it("categorizeByServiceName() should be called", function() {
  153. expect(controller.categorizeByServiceName.calledOnce).to.be.true;
  154. expect(controller.get('heatmapCategories')).to.equal('categories');
  155. });
  156. it("getActiveWidgetLayout() should be called", function() {
  157. expect(controller.getActiveWidgetLayout.calledOnce).to.be.true;
  158. });
  159. });
  160. describe("#categorizeByServiceName()", function() {
  161. beforeEach(function() {
  162. sinon.stub(App.format, 'role').returns('S1');
  163. });
  164. afterEach(function() {
  165. App.format.role.restore();
  166. });
  167. it("single category", function() {
  168. var allHeatmaps = [
  169. {
  170. metrics: JSON.stringify([{service_name: 'S1'}])
  171. }
  172. ];
  173. var categories = controller.categorizeByServiceName(allHeatmaps);
  174. expect(categories[0].get('serviceName')).to.equal('S1');
  175. expect(categories[0].get('displayName')).to.equal('S1');
  176. expect(categories[0].get('heatmaps')).to.eql(allHeatmaps);
  177. });
  178. it("two categories", function() {
  179. var allHeatmaps = [
  180. {
  181. metrics: JSON.stringify([{service_name: 'S1'}])
  182. },
  183. {
  184. metrics: JSON.stringify([{service_name: 'S1'}])
  185. }
  186. ];
  187. var categories = controller.categorizeByServiceName(allHeatmaps);
  188. expect(categories[0].get('serviceName')).to.equal('S1');
  189. expect(categories[0].get('displayName')).to.equal('S1');
  190. expect(categories[0].get('heatmaps')[0]).to.eql(allHeatmaps[0]);
  191. expect(categories[0].get('heatmaps')[1]).to.eql(allHeatmaps[1]);
  192. });
  193. });
  194. describe("#resetPageData()", function() {
  195. it("should clean heatmapCategories and allHeatmaps", function() {
  196. controller.set('heatmapCategories', [{}]);
  197. controller.set('allHeatmaps', [{}]);
  198. controller.resetPageData();
  199. expect(controller.get('heatmapCategories')).to.be.empty;
  200. expect(controller.get('allHeatmaps')).to.be.empty;
  201. });
  202. });
  203. describe("#getAllHeatMaps()", function() {
  204. beforeEach(function() {
  205. sinon.stub(App.ajax, 'send');
  206. });
  207. afterEach(function() {
  208. App.ajax.send.restore();
  209. });
  210. it("should call App.ajax.send", function() {
  211. controller.reopen({
  212. loadHeatmapsUrlParams: 'url',
  213. sectionName: 's1'
  214. });
  215. controller.getAllHeatMaps();
  216. expect(App.ajax.send.calledWith({
  217. name: 'widgets.get',
  218. sender: controller,
  219. data: {
  220. urlParams: 'url',
  221. sectionName: 's1'
  222. }
  223. })).to.be.true;
  224. });
  225. });
  226. describe("#loadRacks()", function() {
  227. beforeEach(function() {
  228. sinon.stub(App.ajax, 'send');
  229. });
  230. afterEach(function() {
  231. App.ajax.send.restore();
  232. });
  233. it("should call App.ajax.send", function() {
  234. controller.reopen({
  235. loadRacksUrlParams: 'url'
  236. });
  237. controller.loadRacks();
  238. expect(App.ajax.send.calledWith({
  239. name: 'hosts.heatmaps',
  240. sender: controller,
  241. data: {
  242. urlParams: 'url'
  243. },
  244. success: 'loadRacksSuccessCallback'
  245. })).to.be.true;
  246. });
  247. });
  248. describe("#loadRacksSuccessCallback()", function() {
  249. var data = {
  250. items: [
  251. {
  252. Hosts: {
  253. host_name: 'host1',
  254. public_host_name: 'host1',
  255. os_type: 'os1',
  256. ip: 'ip1',
  257. rack_info: 'info'
  258. },
  259. host_components: [
  260. {
  261. HostRoles: {
  262. component_name: 'c1'
  263. }
  264. }
  265. ]
  266. }
  267. ]
  268. };
  269. beforeEach(function() {
  270. sinon.stub(controller, 'indexByRackId').returns({rack: {}});
  271. sinon.stub(controller, 'toList').returns(['rack']);
  272. controller.loadRacksSuccessCallback(data);
  273. });
  274. afterEach(function(){
  275. controller.indexByRackId.restore();
  276. controller.toList.restore();
  277. });
  278. it("indexByRackId should be called", function() {
  279. expect(controller.indexByRackId.calledWith([{
  280. hostName: 'host1',
  281. publicHostName: 'host1',
  282. osType: 'os1',
  283. ip: 'ip1',
  284. rack: 'info',
  285. diskTotal: 0,
  286. diskFree: 0,
  287. cpuSystem: 0,
  288. cpuUser: 0,
  289. memTotal: 0,
  290. memFree: 0,
  291. hostComponents: ['c1']
  292. }])).to.be.true;
  293. });
  294. it("toList should be called", function() {
  295. expect(controller.toList.calledWith({rack: {}})).to.be.true;
  296. expect(controller.get('rackMap')).to.eql({rack: {}});
  297. expect(controller.get('racks')).to.eql(['rack']);
  298. });
  299. });
  300. describe("#indexByRackId()", function() {
  301. it("should return rack map", function() {
  302. var hosts = [
  303. {rack: 'r1'},
  304. {rack: 'r1'}
  305. ];
  306. var rackMap = controller.indexByRackId(hosts);
  307. expect(rackMap['r1'].name).to.equal('r1');
  308. expect(rackMap['r1'].rackId).to.equal('r1');
  309. expect(rackMap['r1'].hosts).to.eql([{rack: 'r1'}, {rack: 'r1'}]);
  310. });
  311. });
  312. describe("#toList()", function() {
  313. it("", function() {
  314. var rackMap = {'r1': {
  315. name: 'r1',
  316. rackId: 'r1',
  317. hosts: [{rack: 'r1'}, {rack: 'r1'}]
  318. }};
  319. expect(controller.toList(rackMap)).to.eql([Em.Object.create({
  320. name: 'r1',
  321. rackId: 'r1',
  322. hosts: [{rack: 'r1'}, {rack: 'r1'}],
  323. isLoaded: false,
  324. index: 0
  325. })]);
  326. });
  327. });
  328. describe("#addRackView()", function() {
  329. beforeEach(function() {
  330. sinon.stub(controller, 'displayAllRacks');
  331. });
  332. afterEach(function() {
  333. controller.displayAllRacks.restore();
  334. });
  335. it("displayAllRacks should be called", function() {
  336. controller.set('racks', [{}]);
  337. controller.set('rackViews', []);
  338. controller.addRackView({});
  339. expect(controller.displayAllRacks.calledOnce).to.be.true;
  340. });
  341. });
  342. describe("#displayAllRacks", function() {
  343. var rackView = {
  344. displayHosts: Em.K
  345. };
  346. beforeEach(function() {
  347. sinon.spy(controller, 'displayAllRacks');
  348. sinon.spy(rackView, 'displayHosts');
  349. });
  350. afterEach(function() {
  351. controller.displayAllRacks.restore();
  352. rackView.displayHosts.restore();
  353. });
  354. it("displayAllRacks should be called again", function() {
  355. controller.set('rackViews', [rackView]);
  356. controller.displayAllRacks();
  357. expect(controller.displayAllRacks.calledTwice).to.be.true;
  358. expect(rackView.displayHosts.calledOnce).to.be.true;
  359. });
  360. it("displayAllRacks should not be called again", function() {
  361. controller.set('rackViews', []);
  362. controller.displayAllRacks();
  363. expect(controller.displayAllRacks.calledOnce).to.be.true;
  364. });
  365. });
  366. });