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 Testing Libs
  6. from tests.support.mock import (
  7. mock_open,
  8. NO_MOCK,
  9. NO_MOCK_REASON,
  10. patch
  11. )
  12. from tests.support import mixins
  13. from tests.support.unit import skipIf, TestCase
  14. # Import Salt Libs
  15. import salt.roster.sshconfig as sshconfig
  16. _SAMPLE_SSH_CONFIG = """
  17. Host *
  18. User user.mcuserface
  19. Host abc*
  20. IdentityFile ~/.ssh/id_rsa_abc
  21. Host def*
  22. IdentityFile ~/.ssh/id_rsa_def
  23. Host abc.asdfgfdhgjkl.com
  24. HostName 123.123.123.123
  25. Host abc123.asdfgfdhgjkl.com
  26. HostName 123.123.123.124
  27. Host def.asdfgfdhgjkl.com
  28. HostName 234.234.234.234
  29. """
  30. _TARGET_ABC = collections.OrderedDict([
  31. ('user', 'user.mcuserface'),
  32. ('priv', '~/.ssh/id_rsa_abc'),
  33. ('host', 'abc.asdfgfdhgjkl.com')
  34. ])
  35. _TARGET_ABC123 = collections.OrderedDict([
  36. ('user', 'user.mcuserface'),
  37. ('priv', '~/.ssh/id_rsa_abc'),
  38. ('host', 'abc123.asdfgfdhgjkl.com')
  39. ])
  40. _TARGET_DEF = collections.OrderedDict([
  41. ('user', 'user.mcuserface'),
  42. ('priv', '~/.ssh/id_rsa_def'),
  43. ('host', 'def.asdfgfdhgjkl.com')
  44. ])
  45. _ALL = {
  46. 'abc.asdfgfdhgjkl.com': _TARGET_ABC,
  47. 'abc123.asdfgfdhgjkl.com': _TARGET_ABC123,
  48. 'def.asdfgfdhgjkl.com': _TARGET_DEF
  49. }
  50. _ABC_GLOB = {
  51. 'abc.asdfgfdhgjkl.com': _TARGET_ABC,
  52. 'abc123.asdfgfdhgjkl.com': _TARGET_ABC123
  53. }
  54. @skipIf(NO_MOCK, NO_MOCK_REASON)
  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)