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 testHelpers = require('test/helpers');
  20. require('utils/batch_scheduled_requests');
  21. require('controllers/main/host');
  22. require('mappers/server_data_mapper');
  23. describe('MainHostController', function () {
  24. var hostController, db;
  25. beforeEach(function () {
  26. hostController = App.MainHostController.create({});
  27. });
  28. afterEach(function () {
  29. hostController.destroy();
  30. });
  31. describe("#totalCount()", function () {
  32. it("TOTAL is undefined", function () {
  33. hostController.set('hostsCountMap', {});
  34. hostController.propertyDidChange('totalCount');
  35. expect(hostController.get('totalCount')).to.be.equal(0);
  36. });
  37. it("TOTAL is 1", function () {
  38. hostController.set('hostsCountMap', {TOTAL: 1});
  39. hostController.propertyDidChange('totalCount');
  40. expect(hostController.get('totalCount')).to.be.equal(1);
  41. });
  42. });
  43. describe('#getRegExp()', function () {
  44. var message = '`{0}` should convert to `{1}`',
  45. tests = [
  46. {value: '.*', expected: '.*'},
  47. {value: '.', expected: '.*'},
  48. {value: '.*.*', expected: '.*'},
  49. {value: '*', expected: '^$'},
  50. {value: '........', expected: '.*'},
  51. {value: '........*', expected: '.*'},
  52. {value: 'a1', expected: '.*a1.*'},
  53. {value: 'a1.', expected: '.*a1.*'},
  54. {value: 'a1...', expected: '.*a1.*'},
  55. {value: 'a1.*', expected: '.*a1.*'},
  56. {value: 'a1.*.a2.a3', expected: '.*a1.*.a2.a3.*'},
  57. {value: 'a1.*.a2...a3', expected: '.*a1.*.a2...a3.*'}
  58. ];
  59. tests.forEach(function (test) {
  60. it(message.format(test.value, test.expected), function () {
  61. expect(hostController.getRegExp(test.value)).to.be.equal(test.expected);
  62. });
  63. });
  64. });
  65. describe('#getQueryParameters', function () {
  66. beforeEach(function () {
  67. sinon.spy(hostController, 'getRegExp');
  68. sinon.stub(App.db, 'getFilterConditions', function () {
  69. return [{
  70. iColumn: 1,
  71. skipFilter: false,
  72. type: "string",
  73. value: "someval"
  74. }];
  75. });
  76. });
  77. afterEach(function () {
  78. App.db.getFilterConditions.restore();
  79. hostController.getRegExp.restore();
  80. });
  81. it('should call #getRegExp with value `someval` on host name filter', function () {
  82. hostController.getQueryParameters();
  83. expect(hostController.getRegExp.calledWith('someval')).to.ok;
  84. });
  85. it('result should include host name filter converted value', function () {
  86. expect(hostController.getQueryParameters().findProperty('key', 'Hosts/host_name').value).to.equal('.*someval.*');
  87. });
  88. });
  89. describe('#getSortProps', function () {
  90. beforeEach(function () {
  91. db = {
  92. mainHostController: [
  93. {name: 'hostName', status: 'sorting'}
  94. ]
  95. };
  96. sinon.stub(App.db, 'getSortingStatuses', function (k) {
  97. return db[k];
  98. });
  99. sinon.stub(App.db, 'setSortingStatuses', function (k, v) {
  100. db[k] = Em.typeOf(v) === 'array' ? v : [v];
  101. });
  102. });
  103. afterEach(function () {
  104. App.db.getSortingStatuses.restore();
  105. App.db.setSortingStatuses.restore();
  106. });
  107. it('should set default sorting condition', function () {
  108. hostController.getSortProps();
  109. expect(db.mainHostController).to.eql([{name: 'hostName', status: 'sorting_asc'}]);
  110. });
  111. });
  112. describe("#updateStatusCounters()", function() {
  113. it("isCountersUpdating is false", function() {
  114. hostController.set('isCountersUpdating', false);
  115. hostController.updateStatusCounters();
  116. expect(testHelpers.findAjaxRequest('name', 'host.status.counters')).to.be.undefined;
  117. });
  118. it("isCountersUpdating is true", function() {
  119. hostController.set('isCountersUpdating', true);
  120. hostController.updateStatusCounters();
  121. expect(testHelpers.findAjaxRequest('name', 'host.status.counters')).to.be.exist;
  122. });
  123. });
  124. describe("#updateStatusCountersSuccessCallback()", function() {
  125. var data = {
  126. Clusters: {
  127. health_report: {
  128. 'Host/host_status/HEALTHY': 1,
  129. 'Host/host_status/UNHEALTHY': 2,
  130. 'Host/host_status/ALERT': 3,
  131. 'Host/host_status/UNKNOWN': 4,
  132. 'Host/stale_config': 5,
  133. 'Host/maintenance_state': 6
  134. },
  135. total_hosts: 21
  136. }
  137. };
  138. it("hostsCountMap should be set", function() {
  139. hostController.updateStatusCountersSuccessCallback(data);
  140. expect(hostController.get('hostsCountMap')).to.be.eql({
  141. "HEALTHY": 1,
  142. "UNHEALTHY": 2,
  143. "ALERT": 3,
  144. "UNKNOWN": 4,
  145. "health-status-RESTART": 5,
  146. "health-status-PASSIVE_STATE": 6,
  147. "TOTAL": 21
  148. });
  149. });
  150. });
  151. describe("#getProperValue()", function() {
  152. var testCases = [
  153. {
  154. input: '>1',
  155. expected: '1'
  156. },
  157. {
  158. input: '<1',
  159. expected: '1'
  160. },
  161. {
  162. input: '=1',
  163. expected: '1'
  164. },
  165. {
  166. input: '1',
  167. expected: '1'
  168. }
  169. ];
  170. testCases.forEach(function(test) {
  171. it("value =" + test.input, function() {
  172. expect(hostController.getProperValue(test.input)).to.be.equal(test.expected);
  173. });
  174. });
  175. });
  176. describe("#convertMemory()", function() {
  177. beforeEach(function() {
  178. sinon.stub(hostController, 'getProperValue', function(input) {
  179. return input;
  180. })
  181. });
  182. afterEach(function() {
  183. hostController.getProperValue.restore();
  184. });
  185. var testCases = [
  186. {
  187. input: 'm',
  188. expected: 'm'
  189. },
  190. {
  191. input: '1',
  192. expected: 1048576
  193. },
  194. {
  195. input: '1g',
  196. expected: 1048576
  197. },
  198. {
  199. input: '1m',
  200. expected: 1024
  201. },
  202. {
  203. input: '1k',
  204. expected: 1
  205. }
  206. ];
  207. testCases.forEach(function(test) {
  208. it("value =" + test.input, function() {
  209. expect(hostController.convertMemory(test.input)).to.be.equal(test.expected);
  210. });
  211. });
  212. });
  213. describe("#convertMemoryToRange()", function() {
  214. beforeEach(function() {
  215. sinon.stub(hostController, 'rangeConvertNumber', function(arg1) {
  216. return [arg1, arg1];
  217. })
  218. });
  219. afterEach(function() {
  220. hostController.rangeConvertNumber.restore();
  221. });
  222. var testCases = [
  223. {
  224. input: 'm',
  225. expected: [0, 0]
  226. },
  227. {
  228. input: '1',
  229. expected: [1048576, 1048576]
  230. },
  231. {
  232. input: '1g',
  233. expected: [1048576, 1048576]
  234. },
  235. {
  236. input: '1m',
  237. expected: [1024, 1024]
  238. },
  239. {
  240. input: '1k',
  241. expected: [1, 1]
  242. }
  243. ];
  244. testCases.forEach(function(test) {
  245. it("value =" + test.input, function() {
  246. expect(hostController.convertMemoryToRange(test.input)).to.be.eql(test.expected);
  247. });
  248. });
  249. });
  250. describe("#rangeConvertNumber()", function() {
  251. var testCases = [
  252. {
  253. value: 'm',
  254. scale: '',
  255. expected: [0, 0]
  256. },
  257. {
  258. value: 1,
  259. scale: '',
  260. expected: [0.995, 1.004999999]
  261. },
  262. {
  263. value: 1,
  264. scale: 'g',
  265. expected: [0.995, 1.004999999]
  266. },
  267. {
  268. value: 1,
  269. scale: 'm',
  270. expected: [0.95, 1.04999]
  271. },
  272. {
  273. value: 1,
  274. scale: 'k',
  275. expected: [0.95, 1.04999]
  276. }
  277. ];
  278. testCases.forEach(function(test) {
  279. it("value = " + test.value + 'scale = ' + test.scale, function() {
  280. expect(hostController.rangeConvertNumber(test.value, test.scale)).to.be.eql(test.expected);
  281. });
  282. });
  283. });
  284. describe("#getComparisonType()", function() {
  285. var testCases = [
  286. {
  287. value: '1',
  288. expected: 'EQUAL'
  289. },
  290. {
  291. value: '>',
  292. expected: 'MORE'
  293. },
  294. {
  295. value: '<',
  296. expected: 'LESS'
  297. },
  298. {
  299. value: '=',
  300. expected: 'EQUAL'
  301. }
  302. ];
  303. testCases.forEach(function(test) {
  304. it("value = " + test.value, function() {
  305. expect(hostController.getComparisonType(test.value)).to.be.equal(test.expected);
  306. });
  307. });
  308. });
  309. describe("#filterByComponent()", function() {
  310. beforeEach(function() {
  311. sinon.stub(App.db, 'setFilterConditions');
  312. });
  313. afterEach(function() {
  314. App.db.setFilterConditions.restore();
  315. });
  316. it("component is null", function() {
  317. hostController.filterByComponent();
  318. expect(App.db.setFilterConditions.called).to.be.false;
  319. });
  320. it("component exist", function() {
  321. hostController.set('name', 'ctrl1');
  322. hostController.filterByComponent(Em.Object.create({
  323. componentName: 'C1'
  324. }));
  325. expect(App.db.setFilterConditions.calledWith('ctrl1', [{
  326. iColumn: 6,
  327. value: ['C1'],
  328. type: 'multiple'
  329. }])).to.be.true;
  330. });
  331. });
  332. describe("#filterByStack()", function() {
  333. beforeEach(function() {
  334. sinon.stub(App.db, 'setFilterConditions');
  335. });
  336. afterEach(function() {
  337. App.db.setFilterConditions.restore();
  338. });
  339. it("displayName is null", function() {
  340. hostController.filterByStack(null, 'INSTALLED');
  341. expect(App.db.setFilterConditions.called).to.be.false;
  342. });
  343. it("state is null", function() {
  344. hostController.filterByStack('stack1', null);
  345. expect(App.db.setFilterConditions.called).to.be.false;
  346. });
  347. it("stack and displayName exist", function() {
  348. hostController.set('name', 'ctrl1');
  349. hostController.filterByStack('stack1', 'INSTALLED');
  350. expect(App.db.setFilterConditions.calledWith('ctrl1', [
  351. {
  352. iColumn: 16,
  353. value: 'stack1',
  354. type: 'string'
  355. },
  356. {
  357. iColumn: 17,
  358. value: 'INSTALLED',
  359. type: 'string'
  360. }])).to.be.true;
  361. });
  362. });
  363. describe("#goToHostAlerts()", function() {
  364. beforeEach(function() {
  365. sinon.stub(App.router, 'transitionTo');
  366. });
  367. afterEach(function() {
  368. App.router.transitionTo.restore();
  369. });
  370. it("event is null", function() {
  371. hostController.goToHostAlerts(null);
  372. expect(App.router.transitionTo.called).to.be.false;
  373. });
  374. it("event.context is null", function() {
  375. hostController.goToHostAlerts({context: null});
  376. expect(App.router.transitionTo.called).to.be.false;
  377. });
  378. it("event.context is exist", function() {
  379. hostController.goToHostAlerts({context: {}});
  380. expect(App.router.transitionTo.calledWith('main.hosts.hostDetails.alerts', {})).to.be.true;
  381. });
  382. });
  383. describe("#removeHosts()", function() {
  384. it("host should be removed", function() {
  385. var host1 = Em.Object.create({id: 'host1', isChecked: true});
  386. hostController.set('content', [host1]);
  387. hostController.set('fullContent', [host1]);
  388. hostController.removeHosts();
  389. expect(hostController.get('fullContent')).to.be.empty;
  390. });
  391. });
  392. describe("#checkRemoved()", function() {
  393. it("host should be removed", function() {
  394. var host1 = Em.Object.create({id: 'host1', isChecked: true});
  395. hostController.set('content', [host1]);
  396. hostController.set('fullContent', [host1]);
  397. hostController.checkRemoved('host1');
  398. expect(hostController.get('fullContent')).to.be.empty;
  399. });
  400. });
  401. });