1
0

test_network_info.py 4.4 KB

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