1
0

test_network_info.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 TestCase
  7. from tests.support.mock import 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. class NetworkInfoBeaconTestCase(TestCase, LoaderModuleMockMixin):
  22. '''
  23. Test case for salt.beacons.network_info
  24. '''
  25. def setup_loader_modules(self):
  26. return {
  27. network_info: {
  28. '__context__': {},
  29. '__salt__': {},
  30. }
  31. }
  32. def test_non_list_config(self):
  33. config = {}
  34. ret = network_info.validate(config)
  35. self.assertEqual(ret, (False, 'Configuration for network_info beacon'
  36. ' must be a list.'))
  37. def test_empty_config(self):
  38. config = [{}]
  39. ret = network_info.validate(config)
  40. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  41. def test_network_info_equal(self):
  42. with patch('salt.utils.psutil_compat.net_io_counters',
  43. MagicMock(return_value=STUB_NET_IO_COUNTERS)):
  44. config = [{'interfaces': {'eth0': {'type': 'equal',
  45. 'bytes_sent': 914626664,
  46. 'bytes_recv': 93662618,
  47. 'packets_sent': 465694,
  48. 'packets_recv': 903802,
  49. 'errin': 0,
  50. 'errout': 0,
  51. 'dropin': 0,
  52. 'dropout': 0}}}]
  53. ret = network_info.validate(config)
  54. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  55. _expected_return = [{'interface': 'eth0',
  56. 'network_info': {'bytes_recv': 914626664,
  57. 'bytes_sent': 93662618,
  58. 'dropin': 0,
  59. 'dropout': 0,
  60. 'errin': 0,
  61. 'errout': 0,
  62. 'packets_recv': 903802,
  63. 'packets_sent': 465694}}]
  64. ret = network_info.beacon(config)
  65. self.assertEqual(ret, _expected_return)
  66. def test_network_info_greater_than(self):
  67. with patch('salt.utils.psutil_compat.net_io_counters',
  68. MagicMock(return_value=STUB_NET_IO_COUNTERS)):
  69. config = [{'interfaces': {'eth0': {'type': 'greater',
  70. 'bytes_sent': 100000,
  71. 'bytes_recv': 100000,
  72. 'packets_sent': 100000,
  73. 'packets_recv': 100000,
  74. 'errin': 0,
  75. 'errout': 0,
  76. 'dropin': 0,
  77. 'dropout': 0}}}]
  78. ret = network_info.validate(config)
  79. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  80. _expected_return = [{'interface': 'eth0',
  81. 'network_info': {'bytes_recv': 914626664,
  82. 'bytes_sent': 93662618,
  83. 'dropin': 0,
  84. 'dropout': 0,
  85. 'errin': 0,
  86. 'errout': 0,
  87. 'packets_recv': 903802,
  88. 'packets_sent': 465694}}]
  89. ret = network_info.beacon(config)
  90. self.assertEqual(ret, _expected_return)