host_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 misc = require('utils/misc');
  20. require('models/host');
  21. describe('App.Host', function () {
  22. var data = [
  23. {
  24. id: 'host1',
  25. host_name: 'host1',
  26. memory: 200000,
  27. disk_total: 100.555,
  28. disk_free: 90.555,
  29. health_status: 'HEALTHY',
  30. last_heart_beat_time: (new Date()).getTime() - 18100000
  31. },
  32. {
  33. id: 'host2',
  34. host_name: 'host2',
  35. memory: 99999,
  36. disk_total: 90,
  37. disk_free: 90,
  38. health_status: 'HEALTHY',
  39. last_heart_beat_time: (new Date()).getTime() - 170000
  40. },
  41. {
  42. id: 'host3',
  43. host_name: 'host3',
  44. memory: 99999,
  45. disk_total: 99.999,
  46. disk_free: 0,
  47. health_status: 'UNKNOWN',
  48. last_heart_beat_time: (new Date()).getTime()
  49. }
  50. ];
  51. App.Host.reopen({
  52. hostComponents: []
  53. });
  54. App.store.loadMany(App.Host, data);
  55. var host1 = App.Host.find('host1');
  56. App.TestAliases.testAsComputedAlias(host1, 'componentsInPassiveStateCount', 'componentsInPassiveState.length', 'number');
  57. App.TestAliases.testAsComputedAlias(host1, 'componentsWithStaleConfigsCount', 'componentsWithStaleConfigs.length', 'number');
  58. App.TestAliases.testAsComputedAlias(host1, 'disksMounted', 'diskInfo.length', 'number');
  59. describe('#diskUsedFormatted', function () {
  60. it('host1 - 10GB ', function () {
  61. expect(host1.get('diskUsedFormatted')).to.equal('10GB');
  62. });
  63. it('host2 - 0GB', function () {
  64. var host = App.Host.find().findProperty('hostName', 'host2');
  65. expect(host.get('diskUsedFormatted')).to.equal('0GB');
  66. });
  67. it('host3 - 100GB', function () {
  68. var host = App.Host.find().findProperty('hostName', 'host3');
  69. expect(host.get('diskUsedFormatted')).to.equal('100GB');
  70. });
  71. });
  72. describe('#diskTotalFormatted', function () {
  73. it('host1 - 100.56GB ', function () {
  74. expect(host1.get('diskTotalFormatted')).to.equal('100.56GB');
  75. });
  76. it('host2 - 90GB', function () {
  77. var host = App.Host.find().findProperty('hostName', 'host2');
  78. expect(host.get('diskTotalFormatted')).to.equal('90GB');
  79. });
  80. it('host3 - 100GB', function () {
  81. var host = App.Host.find().findProperty('hostName', 'host3');
  82. expect(host.get('diskTotalFormatted')).to.equal('100GB');
  83. });
  84. });
  85. describe('#diskUsageFormatted', function () {
  86. it('host1 - 9.94% ', function () {
  87. expect(host1.get('diskUsageFormatted')).to.equal('9.94%');
  88. });
  89. it('host2 - 0%', function () {
  90. var host = App.Host.find().findProperty('hostName', 'host2');
  91. expect(host.get('diskUsageFormatted')).to.equal('0%');
  92. });
  93. it('host3 - 100%', function () {
  94. var host = App.Host.find().findProperty('hostName', 'host3');
  95. expect(host.get('diskUsageFormatted')).to.equal('100%');
  96. });
  97. });
  98. describe('#isNotHeartBeating', function () {
  99. it('host2 - false', function () {
  100. var host = App.Host.find().findProperty('hostName', 'host2');
  101. expect(host.get('isNotHeartBeating')).to.equal(false);
  102. });
  103. it('host3 - false', function () {
  104. var host = App.Host.find().findProperty('hostName', 'host3');
  105. expect(host.get('isNotHeartBeating')).to.equal(true);
  106. });
  107. });
  108. describe('#cpuUsage', function () {
  109. var testCases = [
  110. {
  111. params: {
  112. cpuSystem: undefined,
  113. cpuUser: undefined
  114. },
  115. result: 0
  116. },
  117. {
  118. params: {
  119. cpuSystem: 0,
  120. cpuUser: 0
  121. },
  122. result: 0
  123. },
  124. {
  125. params: {
  126. cpuSystem: 1,
  127. cpuUser: 0
  128. },
  129. result: 0
  130. },
  131. {
  132. params: {
  133. cpuSystem: 0,
  134. cpuUser: 1
  135. },
  136. result: 0
  137. },
  138. {
  139. params: {
  140. cpuSystem: 1,
  141. cpuUser: 1
  142. },
  143. result: 2
  144. }
  145. ];
  146. testCases.forEach(function (test) {
  147. it('cpuSystem - ' + test.params.cpuSystem + ', cpuUser - ' + test.params.cpuUser, function () {
  148. host1.set('cpuSystem', test.params.cpuSystem);
  149. host1.set('cpuUser', test.params.cpuUser);
  150. host1.propertyDidChange('cpuUsage');
  151. expect(host1.get('cpuUsage')).to.equal(test.result);
  152. });
  153. });
  154. });
  155. describe('#memoryUsage', function () {
  156. var testCases = [
  157. {
  158. params: {
  159. memFree: undefined,
  160. memTotal: undefined
  161. },
  162. result: 0
  163. },
  164. {
  165. params: {
  166. memFree: 0,
  167. memTotal: 0
  168. },
  169. result: 0
  170. },
  171. {
  172. params: {
  173. memFree: 1,
  174. memTotal: 0
  175. },
  176. result: 0
  177. },
  178. {
  179. params: {
  180. memFree: 0,
  181. memTotal: 1
  182. },
  183. result: 0
  184. },
  185. {
  186. params: {
  187. memFree: 1,
  188. memTotal: 2
  189. },
  190. result: 50
  191. }
  192. ];
  193. testCases.forEach(function (test) {
  194. it('memFree - ' + test.params.memFree + ', memTotal - ' + test.params.memTotal, function () {
  195. host1.set('memFree', test.params.memFree);
  196. host1.set('memTotal', test.params.memTotal);
  197. host1.propertyDidChange('memoryUsage');
  198. expect(host1.get('memoryUsage')).to.equal(test.result);
  199. });
  200. });
  201. });
  202. describe.skip('#componentsWithStaleConfigs', function () {
  203. it('One component with stale configs', function () {
  204. host1.set('hostComponents', [Em.Object.create({
  205. staleConfigs: true
  206. })]);
  207. host1.propertyDidChange('componentsWithStaleConfigs');
  208. expect(host1.get('componentsWithStaleConfigs')).to.eql([Em.Object.create({
  209. staleConfigs: true
  210. })]);
  211. });
  212. it('No components with stale configs', function () {
  213. host1.set('hostComponents', [Em.Object.create({
  214. staleConfigs: false
  215. })]);
  216. host1.propertyDidChange('componentsWithStaleConfigs');
  217. expect(host1.get('componentsWithStaleConfigs')).to.be.empty;
  218. });
  219. });
  220. describe.skip('#componentsInPassiveStateCount', function () {
  221. it('No component in passive state', function () {
  222. host1.set('hostComponents', [Em.Object.create({
  223. passiveState: 'OFF'
  224. })]);
  225. host1.propertyDidChange('componentsInPassiveStateCount');
  226. expect(host1.get('componentsInPassiveStateCount')).to.equal(0);
  227. });
  228. it('One component in passive state', function () {
  229. host1.set('hostComponents', [Em.Object.create({
  230. passiveState: 'ON'
  231. })]);
  232. host1.propertyDidChange('componentsInPassiveStateCount');
  233. expect(host1.get('componentsInPassiveStateCount')).to.equal(1);
  234. });
  235. });
  236. describe('#disksMounted', function () {
  237. it('depends on diskInfo count', function () {
  238. host1.set('diskInfo', [
  239. {}
  240. ]);
  241. host1.propertyDidChange('disksMounted');
  242. expect(host1.get('disksMounted')).to.equal(1);
  243. });
  244. });
  245. describe('#coresFormatted', function () {
  246. it('depends on cpu, cpuPhysical', function () {
  247. host1.set('cpu', 1);
  248. host1.set('cpuPhysical', 2);
  249. host1.propertyDidChange('coresFormatted');
  250. expect(host1.get('coresFormatted')).to.equal('1 (2)');
  251. });
  252. });
  253. describe('#diskUsed', function () {
  254. it('diskFree and diskTotal are 0', function () {
  255. host1.set('diskFree', 0);
  256. host1.set('diskTotal', 0);
  257. host1.propertyDidChange('diskUsed');
  258. expect(host1.get('diskUsed')).to.equal(0);
  259. });
  260. it('diskFree is 0 and diskTotal is 10', function () {
  261. host1.set('diskFree', 0);
  262. host1.set('diskTotal', 10);
  263. host1.propertyDidChange('diskUsed');
  264. expect(host1.get('diskUsed')).to.equal(10);
  265. });
  266. });
  267. describe('#diskUsage', function () {
  268. it('depends on diskTotal, diskUsed', function () {
  269. host1.reopen({
  270. diskUsed: 10
  271. });
  272. host1.set('diskTotal', 100);
  273. host1.propertyDidChange('diskUsage');
  274. expect(host1.get('diskUsage')).to.equal(10);
  275. });
  276. });
  277. describe('#memoryFormatted', function () {
  278. beforeEach(function () {
  279. sinon.stub(misc, 'formatBandwidth', Em.K);
  280. });
  281. afterEach(function () {
  282. misc.formatBandwidth.restore();
  283. });
  284. it('depends on memory', function () {
  285. host1.set('memory', 1024);
  286. host1.propertyDidChange('memoryFormatted');
  287. host1.get('memoryFormatted');
  288. expect(misc.formatBandwidth.calledWith(1048576)).to.be.true;
  289. });
  290. });
  291. describe('#loadAvg', function () {
  292. var testCases = [
  293. {
  294. params: {
  295. loadOne: null,
  296. loadFive: null,
  297. loadFifteen: null
  298. },
  299. result: null
  300. },
  301. {
  302. params: {
  303. loadOne: 1.111,
  304. loadFive: 5.555,
  305. loadFifteen: 15.555
  306. },
  307. result: '1.11'
  308. },
  309. {
  310. params: {
  311. loadOne: null,
  312. loadFive: 5.555,
  313. loadFifteen: 15.555
  314. },
  315. result: '5.55'
  316. },
  317. {
  318. params: {
  319. loadOne: null,
  320. loadFive: null,
  321. loadFifteen: 15.555
  322. },
  323. result: '15.55'
  324. }
  325. ];
  326. testCases.forEach(function (test) {
  327. it('loadOne - ' + test.params.loadOne + ', loadFive - ' + test.params.loadFive + ', loadFifteen - ' + test.params.loadFifteen, function () {
  328. host1.set('loadOne', test.params.loadOne);
  329. host1.set('loadFive', test.params.loadFive);
  330. host1.set('loadFifteen', test.params.loadFifteen);
  331. host1.propertyDidChange('loadAvg');
  332. expect(host1.get('loadAvg')).to.equal(test.result);
  333. });
  334. });
  335. });
  336. describe('#healthClass', function () {
  337. var testCases = [
  338. {
  339. params: {
  340. passiveState: 'ON',
  341. healthStatus: null
  342. },
  343. result: 'icon-medkit'
  344. },
  345. {
  346. params: {
  347. passiveState: 'OFF',
  348. healthStatus: 'UNKNOWN'
  349. },
  350. result: 'health-status-DEAD-YELLOW'
  351. },
  352. {
  353. params: {
  354. passiveState: 'OFF',
  355. healthStatus: 'HEALTHY'
  356. },
  357. result: 'health-status-LIVE'
  358. },
  359. {
  360. params: {
  361. passiveState: 'OFF',
  362. healthStatus: 'UNHEALTHY'
  363. },
  364. result: 'health-status-DEAD-RED'
  365. },
  366. {
  367. params: {
  368. passiveState: 'OFF',
  369. healthStatus: 'ALERT'
  370. },
  371. result: 'health-status-DEAD-ORANGE'
  372. },
  373. {
  374. params: {
  375. passiveState: 'OFF',
  376. healthStatus: null
  377. },
  378. result: 'health-status-DEAD-YELLOW'
  379. }
  380. ];
  381. testCases.forEach(function (test) {
  382. it('passiveState - ' + test.params.passiveState + ', healthStatus - ' + test.params.healthStatus, function () {
  383. host1.set('passiveState', test.params.passiveState);
  384. host1.set('healthStatus', test.params.healthStatus);
  385. host1.propertyDidChange('healthClass');
  386. expect(host1.get('healthClass')).to.equal(test.result);
  387. });
  388. });
  389. });
  390. App.TestAliases.testAsComputedGetByKey(host1, 'healthIconClass', 'healthIconClassMap', 'healthClass', {defaultValue: '', map: {
  391. 'health-status-LIVE': App.healthIconClassGreen,
  392. 'health-status-DEAD-RED': App.healthIconClassRed,
  393. 'health-status-DEAD-YELLOW': App.healthIconClassYellow,
  394. 'health-status-DEAD-ORANGE': App.healthIconClassOrange
  395. }});
  396. });