test_sshconfig.py 2.1 KB

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