1
0

test_network_info.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from collections import namedtuple
  5. # Salt testing libs
  6. from tests.support.unit import skipIf, TestCase
  7. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. # Salt libs
  10. import salt.beacons.network_info as network_info
  11. import logging
  12. log = logging.getLogger(__name__)
  13. STUB_NET_IO_COUNTERS = {'eth0': namedtuple('snetio',
  14. 'bytes_sent bytes_recv \
  15. packets_sent packets_recv \
  16. errin errout \
  17. dropin \
  18. dropout')(93662618, 914626664,
  19. 465694, 903802, 0,
  20. 0, 0, 0)}
  21. @skipIf(NO_MOCK, NO_MOCK_REASON)
  22. class NetworkInfoBeaconTestCase(TestCase, LoaderModuleMockMixin):
  23. '''
  24. Test case for salt.beacons.network_info
  25. '''
  26. def setup_loader_modules(self):
  27. return {
  28. network_info: {
  29. '__context__': {},
  30. '__salt__': {},
  31. }
  32. }
  33. def test_non_list_config(self):
  34. config = {}
  35. ret = network_info.validate(config)
  36. self.assertEqual(ret, (False, 'Configuration for network_info beacon'
  37. ' must be a list.'))
  38. def test_empty_config(self):
  39. config = [{}]
  40. ret = network_info.validate(config)
  41. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  42. def test_network_info_equal(self):
  43. with patch('salt.utils.psutil_compat.net_io_counters',
  44. MagicMock(return_value=STUB_NET_IO_COUNTERS)):
  45. config = [{'interfaces': {'eth0': {'type': 'equal',
  46. 'bytes_sent': 914626664,
  47. 'bytes_recv': 93662618,
  48. 'packets_sent': 465694,
  49. 'packets_recv': 903802,
  50. 'errin': 0,
  51. 'errout': 0,
  52. 'dropin': 0,
  53. 'dropout': 0}}}]
  54. ret = network_info.validate(config)
  55. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  56. _expected_return = [{'interface': 'eth0',
  57. 'network_info': {'bytes_recv': 914626664,
  58. 'bytes_sent': 93662618,
  59. 'dropin': 0,
  60. 'dropout': 0,
  61. 'errin': 0,
  62. 'errout': 0,
  63. 'packets_recv': 903802,
  64. 'packets_sent': 465694}}]
  65. ret = network_info.beacon(config)
  66. self.assertEqual(ret, _expected_return)
  67. def test_network_info_greater_than(self):
  68. with patch('salt.utils.psutil_compat.net_io_counters',
  69. MagicMock(return_value=STUB_NET_IO_COUNTERS)):
  70. config = [{'interfaces': {'eth0': {'type': 'greater',
  71. 'bytes_sent': 100000,
  72. 'bytes_recv': 100000,
  73. 'packets_sent': 100000,
  74. 'packets_recv': 100000,
  75. 'errin': 0,
  76. 'errout': 0,
  77. 'dropin': 0,
  78. 'dropout': 0}}}]
  79. ret = network_info.validate(config)
  80. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  81. _expected_return = [{'interface': 'eth0',
  82. 'network_info': {'bytes_recv': 914626664,
  83. 'bytes_sent': 93662618,
  84. 'dropin': 0,
  85. 'dropout': 0,
  86. 'errin': 0,
  87. 'errout': 0,
  88. 'packets_recv': 903802,
  89. 'packets_sent': 465694}}]
  90. ret = network_info.beacon(config)
  91. self.assertEqual(ret, _expected_return)