Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import os
  2. import shutil
  3. import tempfile
  4. import unittest
  5. from dockersible.ssl import copy_certificates
  6. from dockersible.files import find, copy, template
  7. class DockersibleTestCase(unittest.TestCase):
  8. def test_find(self):
  9. parent_directory = os.path.dirname(os.path.realpath(__file__))
  10. ssl_directory = os.path.join(parent_directory, 'ssl')
  11. certificates = find(paths=ssl_directory, patterns='*.key,*.crt')
  12. for certificate in certificates:
  13. expected_path = [os.path.join(ssl_directory, 'test.key'), os.path.join(ssl_directory, 'test.crt')]
  14. self.assertTrue(certificate['path'] in expected_path)
  15. def test_copy_certificates(self):
  16. temp_folder = tempfile.mkdtemp()
  17. private_key_origin = os.path.join(temp_folder, 'test.key')
  18. with open(private_key_origin, 'w') as f: f.write('')
  19. certificate_origin = os.path.join(temp_folder, 'test.crt')
  20. with open(certificate_origin, 'w') as f: f.write('')
  21. certificates = {
  22. 'key': private_key_origin,
  23. 'crt': certificate_origin,
  24. }
  25. copy_certificates(certificates, temp_folder, 'oslab.fr')
  26. private_key = os.path.join(temp_folder, 'private', 'oslab.fr.key')
  27. self.assertTrue(os.path.exists(private_key))
  28. self.assertTrue((os.stat(private_key).st_mode & 0o777) == 0o600)
  29. self.assertTrue(os.path.exists(private_key_origin))
  30. certificate = os.path.join(temp_folder, 'certs', 'oslab.fr.crt')
  31. self.assertTrue(os.path.exists(certificate))
  32. self.assertTrue((os.stat(certificate).st_mode & 0o777) == 0o644)
  33. self.assertTrue(os.path.exists(certificate_origin))
  34. shutil.rmtree(temp_folder)
  35. def test_copy_file(self):
  36. private_key_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ssl', 'test.key')
  37. destination = tempfile.mkdtemp()
  38. copy(source=private_key_path, destination=destination)
  39. self.assertTrue(os.path.exists(os.path.join(destination, 'test.key')))
  40. shutil.rmtree(destination)
  41. def test_copy_file_change_basename(self):
  42. private_key_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ssl', 'test.key')
  43. destination = tempfile.mkdtemp()
  44. copy(source=private_key_path, destination=destination, basename='lesspass.com.key', mode='0600')
  45. self.assertTrue(os.path.exists(os.path.join(destination, 'lesspass.com.key')))
  46. shutil.rmtree(destination)
  47. def test_copy_file_change_mode(self):
  48. private_key_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ssl', 'test.key')
  49. destination = tempfile.mkdtemp()
  50. copy(source=private_key_path, destination=destination)
  51. expected_private_key_path = os.path.join(destination, 'test.key')
  52. self.assertTrue((os.stat(expected_private_key_path).st_mode & 0o777) == 0o755)
  53. copy(source=private_key_path, destination=destination, basename='lesspass.com.key', mode='0600')
  54. expected_private_key_path = os.path.join(destination, 'lesspass.com.key')
  55. self.assertTrue((os.stat(expected_private_key_path).st_mode & 0o777) == 0o600)
  56. shutil.rmtree(destination)
  57. def test_template_module_with_source_file(self):
  58. template_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates', 'test.j2')
  59. destination = tempfile.mkdtemp()
  60. context = {
  61. 'dhparam': True,
  62. 'dhparam_path': '/etc/ssl/certs/dhparam.pem'
  63. }
  64. destination_file = os.path.join(destination, 'test.txt')
  65. template(source=template_path, context=context, destination=destination_file)
  66. self.assertEqual('\nssl_dhparam /etc/ssl/certs/dhparam.pem;\n', open(destination_file).read())
  67. shutil.rmtree(destination)
  68. if __name__ == '__main__':
  69. unittest.main()