host_test.js 12 KB

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