test_mac_keychain.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Validate the mac-keychain module
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.runtime import RUNTIME_VARS
  11. # Import Salt Libs
  12. from salt.exceptions import CommandExecutionError
  13. # Import 3rd-party libs
  14. import pytest
  15. from salt.ext import six
  16. @pytest.mark.destructive_test
  17. @pytest.mark.skip_if_not_root
  18. class MacKeychainModuleTest(ModuleCase):
  19. '''
  20. Integration tests for the mac_keychain module
  21. '''
  22. @classmethod
  23. def setUpClass(cls):
  24. cls.cert = os.path.join(
  25. RUNTIME_VARS.FILES,
  26. 'file',
  27. 'base',
  28. 'certs',
  29. 'salttest.p12'
  30. )
  31. cls.cert_alias = 'Salt Test'
  32. cls.passwd = 'salttest'
  33. def setUp(self):
  34. '''
  35. Sets up the test requirements
  36. '''
  37. os_grain = self.run_function('grains.item', ['kernel'])
  38. # Must be running on a mac
  39. if os_grain['kernel'] not in 'Darwin':
  40. self.skipTest(
  41. 'Test not applicable to \'{kernel}\' kernel'.format(
  42. **os_grain
  43. )
  44. )
  45. def tearDown(self):
  46. '''
  47. Clean up after tests
  48. '''
  49. # Remove the salttest cert, if left over.
  50. certs_list = self.run_function('keychain.list_certs')
  51. if self.cert_alias in certs_list:
  52. self.run_function('keychain.uninstall', [self.cert_alias])
  53. def test_mac_keychain_install(self):
  54. '''
  55. Tests that attempts to install a certificate
  56. '''
  57. install_cert = self.run_function('keychain.install', [self.cert, self.passwd])
  58. self.assertTrue(install_cert)
  59. # check to ensure the cert was installed
  60. certs_list = self.run_function('keychain.list_certs')
  61. self.assertIn(self.cert_alias, certs_list)
  62. def test_mac_keychain_uninstall(self):
  63. '''
  64. Tests that attempts to uninstall a certificate
  65. '''
  66. self.run_function('keychain.install', [self.cert, self.passwd])
  67. certs_list = self.run_function('keychain.list_certs')
  68. if self.cert_alias not in certs_list:
  69. self.run_function('keychain.uninstall', [self.cert_alias])
  70. self.skipTest('Failed to install keychain')
  71. # uninstall cert
  72. self.run_function('keychain.uninstall', [self.cert_alias])
  73. certs_list = self.run_function('keychain.list_certs')
  74. # check to ensure the cert was uninstalled
  75. try:
  76. self.assertNotIn(self.cert_alias, six.text_type(certs_list))
  77. except CommandExecutionError:
  78. self.run_function('keychain.uninstall', [self.cert_alias])
  79. def test_mac_keychain_get_friendly_name(self):
  80. '''
  81. Test that attempts to get friendly name of a cert
  82. '''
  83. self.run_function('keychain.install', [self.cert, self.passwd])
  84. certs_list = self.run_function('keychain.list_certs')
  85. if self.cert_alias not in certs_list:
  86. self.run_function('keychain.uninstall', [self.cert_alias])
  87. self.skipTest('Failed to install keychain')
  88. get_name = self.run_function('keychain.get_friendly_name', [self.cert, self.passwd])
  89. self.assertEqual(get_name, self.cert_alias)
  90. def test_mac_keychain_get_default_keychain(self):
  91. '''
  92. Test that attempts to get the default keychain
  93. '''
  94. salt_get_keychain = self.run_function('keychain.get_default_keychain')
  95. sys_get_keychain = self.run_function('cmd.run',
  96. ['security default-keychain -d user'])
  97. self.assertEqual(salt_get_keychain, sys_get_keychain)
  98. def test_mac_keychain_list_certs(self):
  99. '''
  100. Test that attempts to list certs
  101. '''
  102. cert_default = 'com.apple.systemdefault'
  103. certs = self.run_function('keychain.list_certs')
  104. self.assertIn(cert_default, certs)