test_scan.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Test the scan roster.
  3. """
  4. import socket
  5. import salt.roster.scan as scan_
  6. from tests.support import mixins
  7. from tests.support.mock import MagicMock, patch
  8. from tests.support.unit import TestCase
  9. class ScanRosterTestCase(TestCase, mixins.LoaderModuleMockMixin):
  10. """Test the directory roster"""
  11. def setup_loader_modules(self):
  12. return {scan_: {"__opts__": {"ssh_scan_ports": "22", "ssh_scan_timeout": 0.01}}}
  13. def test_single_ip(self):
  14. """Test that minion files in the directory roster match and render."""
  15. with patch("salt.utils.network.get_socket"):
  16. ret = scan_.targets("127.0.0.1")
  17. self.assertEqual(ret, {"127.0.0.1": {"host": "127.0.0.1", "port": 22}})
  18. def test_single_network(self):
  19. """Test that minion files in the directory roster match and render."""
  20. with patch("salt.utils.network.get_socket"):
  21. ret = scan_.targets("127.0.0.0/30")
  22. self.assertEqual(
  23. ret,
  24. {
  25. "127.0.0.1": {"host": "127.0.0.1", "port": 22},
  26. "127.0.0.2": {"host": "127.0.0.2", "port": 22},
  27. },
  28. )
  29. def test_multiple_ips(self):
  30. """Test that minion files in the directory roster match and render."""
  31. with patch("salt.utils.network.get_socket"):
  32. ret = scan_.targets(["127.0.0.1", "127.0.0.2"], tgt_type="list")
  33. self.assertEqual(
  34. ret,
  35. {
  36. "127.0.0.1": {"host": "127.0.0.1", "port": 22},
  37. "127.0.0.2": {"host": "127.0.0.2", "port": 22},
  38. },
  39. )
  40. def test_multiple_networks(self):
  41. """Test that minion files in the directory roster match and render."""
  42. with patch("salt.utils.network.get_socket"):
  43. ret = scan_.targets(
  44. ["127.0.0.0/30", "127.0.2.1", "127.0.1.0/30"], tgt_type="list"
  45. )
  46. self.assertEqual(
  47. ret,
  48. {
  49. "127.0.0.1": {"host": "127.0.0.1", "port": 22},
  50. "127.0.0.2": {"host": "127.0.0.2", "port": 22},
  51. "127.0.2.1": {"host": "127.0.2.1", "port": 22},
  52. "127.0.1.1": {"host": "127.0.1.1", "port": 22},
  53. "127.0.1.2": {"host": "127.0.1.2", "port": 22},
  54. },
  55. )
  56. def test_malformed_ip(self):
  57. """Test that minion files in the directory roster match and render."""
  58. with patch("salt.utils.network.get_socket"):
  59. ret = scan_.targets("127001")
  60. self.assertEqual(ret, {})
  61. def test_multiple_with_malformed(self):
  62. """Test that minion files in the directory roster match and render."""
  63. with patch("salt.utils.network.get_socket"):
  64. ret = scan_.targets(
  65. ["127.0.0.1", "127002", "127.0.1.0/30"], tgt_type="list"
  66. )
  67. self.assertEqual(
  68. ret,
  69. {
  70. "127.0.0.1": {"host": "127.0.0.1", "port": 22},
  71. "127.0.1.1": {"host": "127.0.1.1", "port": 22},
  72. "127.0.1.2": {"host": "127.0.1.2", "port": 22},
  73. },
  74. )
  75. def test_multiple_no_connection(self):
  76. """Test that minion files in the directory roster match and render."""
  77. socket_mock = MagicMock()
  78. socket_mock.connect = MagicMock(
  79. side_effect=[None, socket.error(), None, socket.error(), None]
  80. )
  81. with patch("salt.utils.network.get_socket", return_value=socket_mock):
  82. ret = scan_.targets(
  83. ["127.0.0.0/30", "127.0.2.1", "127.0.1.0/30"], tgt_type="list"
  84. )
  85. self.assertEqual(
  86. ret,
  87. {
  88. "127.0.0.1": {"host": "127.0.0.1", "port": 22},
  89. "127.0.0.2": {},
  90. "127.0.2.1": {"host": "127.0.2.1", "port": 22},
  91. "127.0.1.1": {},
  92. "127.0.1.2": {"host": "127.0.1.2", "port": 22},
  93. },
  94. )