host_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. 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('#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('#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('', 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('', 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('', 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. it('', function () {
  279. host1.set('memory', 1024);
  280. sinon.stub(misc, 'formatBandwidth', Em.K);
  281. host1.propertyDidChange('memoryFormatted');
  282. host1.get('memoryFormatted');
  283. expect(misc.formatBandwidth.calledWith(1048576)).to.be.true;
  284. misc.formatBandwidth.restore()
  285. });
  286. });
  287. describe('#loadAvg', function () {
  288. var testCases = [
  289. {
  290. params: {
  291. loadOne: null,
  292. loadFive: null,
  293. loadFifteen: null
  294. },
  295. result: null
  296. },
  297. {
  298. params: {
  299. loadOne: 1.111,
  300. loadFive: 5.555,
  301. loadFifteen: 15.555
  302. },
  303. result: '1.11'
  304. },
  305. {
  306. params: {
  307. loadOne: null,
  308. loadFive: 5.555,
  309. loadFifteen: 15.555
  310. },
  311. result: '5.55'
  312. },
  313. {
  314. params: {
  315. loadOne: null,
  316. loadFive: null,
  317. loadFifteen: 15.555
  318. },
  319. result: '15.55'
  320. }
  321. ];
  322. testCases.forEach(function (test) {
  323. it('loadOne - ' + test.params.loadOne + ', loadFive - ' + test.params.loadFive + ', loadFifteen - ' + test.params.loadFifteen, function () {
  324. host1.set('loadOne', test.params.loadOne);
  325. host1.set('loadFive', test.params.loadFive);
  326. host1.set('loadFifteen', test.params.loadFifteen);
  327. host1.propertyDidChange('loadAvg');
  328. expect(host1.get('loadAvg')).to.equal(test.result);
  329. });
  330. });
  331. });
  332. describe('#healthClass', function () {
  333. var testCases = [
  334. {
  335. params: {
  336. passiveState: 'ON',
  337. healthStatus: null
  338. },
  339. result: 'icon-medkit'
  340. },
  341. {
  342. params: {
  343. passiveState: 'OFF',
  344. healthStatus: 'UNKNOWN'
  345. },
  346. result: 'health-status-DEAD-YELLOW'
  347. },
  348. {
  349. params: {
  350. passiveState: 'OFF',
  351. healthStatus: 'HEALTHY'
  352. },
  353. result: 'health-status-LIVE'
  354. },
  355. {
  356. params: {
  357. passiveState: 'OFF',
  358. healthStatus: 'UNHEALTHY'
  359. },
  360. result: 'health-status-DEAD-RED'
  361. },
  362. {
  363. params: {
  364. passiveState: 'OFF',
  365. healthStatus: 'ALERT'
  366. },
  367. result: 'health-status-DEAD-ORANGE'
  368. },
  369. {
  370. params: {
  371. passiveState: 'OFF',
  372. healthStatus: null
  373. },
  374. result: 'health-status-DEAD-YELLOW'
  375. }
  376. ];
  377. testCases.forEach(function (test) {
  378. it('passiveState - ' + test.params.passiveState + ', healthStatus - ' + test.params.healthStatus, function () {
  379. host1.set('passiveState', test.params.passiveState);
  380. host1.set('healthStatus', test.params.healthStatus);
  381. host1.propertyDidChange('healthClass');
  382. expect(host1.get('healthClass')).to.equal(test.result);
  383. });
  384. });
  385. });
  386. describe('#healthIconClass', function () {
  387. var testCases = [
  388. {
  389. params: {
  390. healthClass: 'health-status-LIVE'
  391. },
  392. result: 'icon-ok-sign'
  393. },
  394. {
  395. params: {
  396. healthClass: 'health-status-DEAD-RED'
  397. },
  398. result: 'icon-warning-sign'
  399. },
  400. {
  401. params: {
  402. healthClass: 'health-status-DEAD-YELLOW'
  403. },
  404. result: 'icon-question-sign'
  405. },
  406. {
  407. params: {
  408. healthClass: 'health-status-DEAD-ORANGE'
  409. },
  410. result: 'icon-minus-sign'
  411. },
  412. {
  413. params: {
  414. healthClass: ''
  415. },
  416. result: ''
  417. }
  418. ];
  419. it('reset healthClass to plain property', function(){
  420. host1.reopen({
  421. healthClass: ''
  422. });
  423. });
  424. testCases.forEach(function (test) {
  425. it('healthClass - ' + test.params.healthClass, function () {
  426. host1.set('healthClass', test.params.healthClass);
  427. host1.propertyDidChange('healthIconClass');
  428. expect(host1.get('healthIconClass')).to.equal(test.result);
  429. });
  430. });
  431. });
  432. });