heatmap_host_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 date = require('utils/date/date');
  20. require('models/host');
  21. require('views/main/charts/heatmap/heatmap_host');
  22. describe('App.MainChartsHeatmapHostView', function () {
  23. var view = App.MainChartsHeatmapHostView.create({
  24. templateName: '',
  25. controller: Em.Object.create(),
  26. content: {}
  27. });
  28. describe('#hostTemperatureStyle', function () {
  29. var testCases = [
  30. {
  31. title: 'if hostToSlotMap is null then hostTemperatureStyle should be empty',
  32. hostName: 'host',
  33. controller: Em.Object.create({
  34. hostToSlotMap: null,
  35. selectedMetric: {
  36. slotDefinitions: []
  37. }
  38. }),
  39. result: ''
  40. },
  41. {
  42. title: 'if hostName is null then hostTemperatureStyle should be empty',
  43. hostName: '',
  44. controller: Em.Object.create({
  45. hostToSlotMap: {},
  46. selectedMetric: {
  47. slotDefinitions: []
  48. }
  49. }),
  50. result: ''
  51. },
  52. {
  53. title: 'if slot less than 0 then hostTemperatureStyle should be empty',
  54. hostName: 'host1',
  55. controller: Em.Object.create({
  56. hostToSlotMap: {
  57. "host1": -1
  58. },
  59. selectedMetric: {
  60. slotDefinitions: []
  61. }
  62. }),
  63. result: ''
  64. },
  65. {
  66. title: 'if slotDefinitions is null then hostTemperatureStyle should be empty',
  67. hostName: 'host1',
  68. controller: Em.Object.create({
  69. hostToSlotMap: {
  70. "host1": 1
  71. },
  72. selectedMetric: {
  73. slotDefinitions: null
  74. }
  75. }),
  76. result: ''
  77. },
  78. {
  79. title: 'if slotDefinitions length not more than slot number then hostTemperatureStyle should be empty',
  80. hostName: 'host1',
  81. controller: Em.Object.create({
  82. hostToSlotMap: {
  83. "host1": 1
  84. },
  85. selectedMetric: {
  86. slotDefinitions: [{}]
  87. }
  88. }),
  89. result: ''
  90. },
  91. {
  92. title: 'if slotDefinitions correct then hostTemperatureStyle should be "style1"',
  93. hostName: 'host1',
  94. controller: Em.Object.create({
  95. hostToSlotMap: {
  96. "host1": 1
  97. },
  98. selectedMetric: {
  99. slotDefinitions: [
  100. Em.Object.create({cssStyle: 'style0'}),
  101. Em.Object.create({cssStyle: 'style1'})
  102. ]
  103. }
  104. }),
  105. result: 'style1'
  106. }
  107. ];
  108. testCases.forEach(function (test) {
  109. it(test.title, function () {
  110. view.set('content.hostName', test.hostName);
  111. view.set('controller', test.controller);
  112. expect(view.get('hostTemperatureStyle')).to.equal(test.result);
  113. });
  114. });
  115. });
  116. describe("#hostModelLink", function () {
  117. before(function () {
  118. sinon.stub(App.Host, 'find').returns(Em.Object.create({id: 'host1'}));
  119. });
  120. after(function () {
  121. App.Host.find.restore();
  122. });
  123. it("should return hostname", function () {
  124. view.set('content.hostName', 'host1');
  125. view.propertyDidChange('hostModelLink');
  126. expect(view.get('hostModelLink.id')).to.equal('host1');
  127. });
  128. });
  129. describe("#mouseEnter()", function () {
  130. beforeEach(function () {
  131. sinon.stub(view, 'getUsage').returns('usage');
  132. sinon.stub(view, 'getCpuUsage').returns('cpu_usage');
  133. sinon.stub(view, 'getHostComponents').returns(['c1']);
  134. sinon.stub(view, 'setMetric');
  135. sinon.stub(view, 'openDetailsBlock');
  136. this.mock = sinon.stub(App.MainChartsHeatmapHostDetailView, 'create');
  137. view.set('details', {});
  138. });
  139. afterEach(function () {
  140. view.getUsage.restore();
  141. view.getCpuUsage.restore();
  142. view.getHostComponents.restore();
  143. view.setMetric.restore();
  144. view.openDetailsBlock.restore();
  145. this.mock.restore();
  146. });
  147. it("set diskUsage", function () {
  148. var childView = Em.Object.create({
  149. details: {
  150. diskUsage: ''
  151. }
  152. });
  153. this.mock.returns(childView);
  154. view.set('content', {
  155. diskTotal: 100,
  156. diskFree: 50
  157. });
  158. view.mouseEnter();
  159. expect(childView.get('details.diskUsage')).to.equal('usage');
  160. expect(view.getUsage.calledWith(100, 50)).to.be.true;
  161. expect(view.setMetric.calledOnce).to.be.true;
  162. expect(view.openDetailsBlock.calledOnce).to.be.true;
  163. });
  164. it("set cpuUsage", function () {
  165. var childView = Em.Object.create({
  166. details: {
  167. cpuUsage: ''
  168. }
  169. });
  170. this.mock.returns(childView);
  171. view.set('content', {
  172. cpuSystem: 100,
  173. cpuUser: 50
  174. });
  175. view.mouseEnter();
  176. expect(childView.get('details.cpuUsage')).to.equal('cpu_usage');
  177. expect(view.getCpuUsage.calledWith(100, 50)).to.be.true;
  178. expect(view.setMetric.calledOnce).to.be.true;
  179. expect(view.openDetailsBlock.calledOnce).to.be.true;
  180. });
  181. it("set memoryUsage", function () {
  182. var childView = Em.Object.create({
  183. details: {
  184. memoryUsage: ''
  185. }
  186. });
  187. this.mock.returns(childView);
  188. view.set('content', {
  189. memTotal: 100,
  190. memFree: 50
  191. });
  192. view.mouseEnter();
  193. expect(childView.get('details.memoryUsage')).to.equal('usage');
  194. expect(view.getUsage.calledWith(100, 50)).to.be.true;
  195. expect(view.setMetric.calledOnce).to.be.true;
  196. expect(view.openDetailsBlock.calledOnce).to.be.true;
  197. });
  198. it("set hostComponents", function () {
  199. var childView = Em.Object.create({
  200. details: {
  201. hostComponents: ''
  202. }
  203. });
  204. this.mock.returns(childView);
  205. view.set('content', {
  206. hostComponents: ['host1']
  207. });
  208. view.mouseEnter();
  209. expect(childView.get('details.hostComponents')).to.eql(['c1']);
  210. expect(view.getHostComponents.calledWith(['host1'])).to.be.true;
  211. expect(view.setMetric.calledOnce).to.be.true;
  212. expect(view.openDetailsBlock.calledOnce).to.be.true;
  213. });
  214. it("set hostName", function () {
  215. var childView = Em.Object.create({
  216. details: {
  217. hostName: ''
  218. }
  219. });
  220. this.mock.returns(childView);
  221. view.set('content', {
  222. hostName: 'host1'
  223. });
  224. view.mouseEnter();
  225. expect(childView.get('details.hostName')).to.equal('host1');
  226. expect(view.setMetric.calledOnce).to.be.true;
  227. expect(view.openDetailsBlock.calledOnce).to.be.true;
  228. });
  229. });
  230. describe("#getUsage()", function () {
  231. var testCases = [
  232. {
  233. input: {
  234. total: null,
  235. free: null
  236. },
  237. expected: '0.0'
  238. },
  239. {
  240. input: {
  241. total: 100,
  242. free: null
  243. },
  244. expected: '0.0'
  245. },
  246. {
  247. input: {
  248. total: null,
  249. free: 50
  250. },
  251. expected: '0.0'
  252. },
  253. {
  254. input: {
  255. total: 0,
  256. free: 0
  257. },
  258. expected: '0.0'
  259. },
  260. {
  261. input: {
  262. total: 100,
  263. free: 50
  264. },
  265. expected: '50.0'
  266. }
  267. ];
  268. testCases.forEach(function (test) {
  269. it("total = " + test.input.total + "; free = " + test.input.free, function () {
  270. expect(view.getUsage(test.input.total, test.input.free)).to.equal(test.expected);
  271. });
  272. });
  273. });
  274. describe("#getCpuUsage()", function () {
  275. var testCases = [
  276. {
  277. input: {
  278. cpuSystem: null,
  279. cpuUser: null
  280. },
  281. expected: '0.0'
  282. },
  283. {
  284. input: {
  285. cpuSystem: 1.0,
  286. cpuUser: null
  287. },
  288. expected: '0.0'
  289. },
  290. {
  291. input: {
  292. cpuSystem: null,
  293. cpuUser: 1.0
  294. },
  295. expected: '0.0'
  296. },
  297. {
  298. input: {
  299. cpuSystem: 2.22,
  300. cpuUser: 1.0
  301. },
  302. expected: '3.2'
  303. }
  304. ];
  305. testCases.forEach(function (test) {
  306. it("cpuSystem = " + test.input.cpuSystem + "; cpuUser = " + test.input.cpuUser, function () {
  307. expect(view.getCpuUsage(test.input.cpuSystem, test.input.cpuUser)).to.equal(test.expected);
  308. });
  309. });
  310. });
  311. describe("#getHostComponents()", function () {
  312. beforeEach(function () {
  313. sinon.stub(App.format, 'role', function (name) {
  314. return name;
  315. });
  316. sinon.stub(App, 'get').returns('non-client');
  317. });
  318. afterEach(function () {
  319. App.format.role.restore();
  320. App.get.restore();
  321. });
  322. it("should return host-components", function () {
  323. expect(view.getHostComponents(['is-client', 'non-client', 'non-client'])).to.equal('non-client, non-client');
  324. });
  325. });
  326. describe("#setMetric()", function () {
  327. var viewObject;
  328. beforeEach(function () {
  329. sinon.stub(date, 'timingFormat').returns('time');
  330. viewObject = Em.Object.create({
  331. details: {}
  332. });
  333. });
  334. afterEach(function () {
  335. date.timingFormat.restore();
  336. });
  337. it("selected metric is null", function () {
  338. view.set('controller.selectedMetric', null);
  339. view.setMetric(viewObject, {});
  340. expect(viewObject.get('details')).to.be.empty;
  341. });
  342. it("metric name is null", function () {
  343. view.set('controller.selectedMetric', Em.Object.create({
  344. name: null,
  345. hostToValueMap: {}
  346. }));
  347. view.setMetric(viewObject, {});
  348. expect(viewObject.get('details')).to.be.empty;
  349. });
  350. it("host value is undefined", function () {
  351. view.set('controller.selectedMetric', Em.Object.create({
  352. name: 'm1',
  353. hostToValueMap: {}
  354. }));
  355. view.setMetric(viewObject, {hostName: 'host1'});
  356. expect(viewObject.get('details')).to.eql({
  357. metricName: 'm1',
  358. metricValue: Em.I18n.t('charts.heatmap.unknown')
  359. });
  360. });
  361. it("metric name is 'Garbage Collection Time'", function () {
  362. view.set('controller.selectedMetric', Em.Object.create({
  363. name: 'Garbage Collection Time',
  364. hostToValueMap: {
  365. host1: 'val'
  366. }
  367. }));
  368. view.setMetric(viewObject, {hostName: 'host1'});
  369. expect(viewObject.get('details')).to.eql({
  370. metricName: 'Garbage Collection Time',
  371. metricValue: 'time'
  372. });
  373. });
  374. it("metric value is NaN", function () {
  375. view.set('controller.selectedMetric', Em.Object.create({
  376. name: 'm1',
  377. hostToValueMap: {
  378. host1: 'val'
  379. }
  380. }));
  381. view.setMetric(viewObject, {hostName: 'host1'});
  382. expect(viewObject.get('details')).to.eql({
  383. metricName: 'm1',
  384. metricValue: Em.I18n.t('charts.heatmap.unknown')
  385. });
  386. });
  387. it("metric value is number", function () {
  388. view.set('controller.selectedMetric', Em.Object.create({
  389. name: 'm1',
  390. hostToValueMap: {
  391. host1: 10
  392. },
  393. units: 'mb'
  394. }));
  395. view.setMetric(viewObject, {hostName: 'host1'});
  396. expect(viewObject.get('details')).to.eql({
  397. metricName: 'm1',
  398. metricValue: '10mb'
  399. });
  400. });
  401. });
  402. });