1
0

test_x509.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import os
  4. import logging
  5. import salt.utils.files
  6. from salt.ext import six
  7. from tests.support.helpers import with_tempfile
  8. from tests.support.paths import BASE_FILES
  9. from tests.support.case import ModuleCase
  10. from tests.support.unit import skipIf
  11. from tests.support.mixins import SaltReturnAssertsMixin
  12. try:
  13. import M2Crypto # pylint: disable=W0611
  14. HAS_M2CRYPTO = True
  15. except ImportError:
  16. HAS_M2CRYPTO = False
  17. log = logging.getLogger(__name__)
  18. @skipIf(not HAS_M2CRYPTO, 'Skip when no M2Crypto found')
  19. class x509Test(ModuleCase, SaltReturnAssertsMixin):
  20. @classmethod
  21. def setUpClass(cls):
  22. cert_path = os.path.join(BASE_FILES, 'x509_test.crt')
  23. with salt.utils.files.fopen(cert_path) as fp:
  24. cls.x509_cert_text = fp.read()
  25. def run_function(self, *args, **kwargs):
  26. ret = super(x509Test, self).run_function(*args, **kwargs)
  27. log.debug('ret = %s', ret)
  28. return ret
  29. @with_tempfile(suffix='.pem', create=False)
  30. def test_issue_49027(self, pemfile):
  31. ret = self.run_state(
  32. 'x509.pem_managed',
  33. name=pemfile,
  34. text=self.x509_cert_text)
  35. assert isinstance(ret, dict), ret
  36. ret = ret[next(iter(ret))]
  37. assert ret.get('result') is True, ret
  38. with salt.utils.files.fopen(pemfile) as fp:
  39. result = fp.readlines()
  40. self.assertEqual(self.x509_cert_text.splitlines(True), result)
  41. @with_tempfile(suffix='.crt', create=False)
  42. @with_tempfile(suffix='.key', create=False)
  43. def test_issue_49008(self, keyfile, crtfile):
  44. ret = self.run_function(
  45. 'state.apply',
  46. ['issue-49008'],
  47. pillar={'keyfile': keyfile, 'crtfile': crtfile})
  48. assert isinstance(ret, dict), ret
  49. for state_result in six.itervalues(ret):
  50. assert state_result['result'] is True, state_result
  51. assert os.path.exists(keyfile)
  52. assert os.path.exists(crtfile)