test_sshconfig.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import collections
  5. # Import Salt Libs
  6. import salt.roster.sshconfig as sshconfig
  7. from tests.support import mixins
  8. # Import Salt Testing Libs
  9. from tests.support.mock import mock_open, patch
  10. from tests.support.unit import TestCase
  11. _SAMPLE_SSH_CONFIG = """
  12. Host *
  13. User user.mcuserface
  14. Host abc*
  15. IdentityFile ~/.ssh/id_rsa_abc
  16. Host def*
  17. IdentityFile ~/.ssh/id_rsa_def
  18. Host abc.asdfgfdhgjkl.com
  19. HostName 123.123.123.123
  20. Host abc123.asdfgfdhgjkl.com
  21. HostName 123.123.123.124
  22. Host def.asdfgfdhgjkl.com
  23. HostName 234.234.234.234
  24. """
  25. _TARGET_ABC = collections.OrderedDict(
  26. [
  27. ("user", "user.mcuserface"),
  28. ("priv", "~/.ssh/id_rsa_abc"),
  29. ("host", "abc.asdfgfdhgjkl.com"),
  30. ]
  31. )
  32. _TARGET_ABC123 = collections.OrderedDict(
  33. [
  34. ("user", "user.mcuserface"),
  35. ("priv", "~/.ssh/id_rsa_abc"),
  36. ("host", "abc123.asdfgfdhgjkl.com"),
  37. ]
  38. )
  39. _TARGET_DEF = collections.OrderedDict(
  40. [
  41. ("user", "user.mcuserface"),
  42. ("priv", "~/.ssh/id_rsa_def"),
  43. ("host", "def.asdfgfdhgjkl.com"),
  44. ]
  45. )
  46. _ALL = {
  47. "abc.asdfgfdhgjkl.com": _TARGET_ABC,
  48. "abc123.asdfgfdhgjkl.com": _TARGET_ABC123,
  49. "def.asdfgfdhgjkl.com": _TARGET_DEF,
  50. }
  51. _ABC_GLOB = {
  52. "abc.asdfgfdhgjkl.com": _TARGET_ABC,
  53. "abc123.asdfgfdhgjkl.com": _TARGET_ABC123,
  54. }
  55. class SSHConfigRosterTestCase(TestCase, mixins.LoaderModuleMockMixin):
  56. def setUp(self):
  57. self.mock_fp = mock_open(read_data=_SAMPLE_SSH_CONFIG)
  58. def setup_loader_modules(self):
  59. return {sshconfig: {}}
  60. def test_all(self):
  61. with patch("salt.utils.files.fopen", self.mock_fp):
  62. with patch("salt.roster.sshconfig._get_ssh_config_file"):
  63. targets = sshconfig.targets("*")
  64. self.assertEqual(targets, _ALL)
  65. def test_abc_glob(self):
  66. with patch("salt.utils.files.fopen", self.mock_fp):
  67. with patch("salt.roster.sshconfig._get_ssh_config_file"):
  68. targets = sshconfig.targets("abc*")
  69. self.assertEqual(targets, _ABC_GLOB)