test_fakeopensslclasses.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. '''
  3. Unit tests for the FakeOpenSSL classes
  4. '''
  5. import os
  6. import sys
  7. import unittest
  8. import pytest
  9. # Disable import-error b/c our libraries aren't loaded in jenkins
  10. # pylint: disable=import-error,wrong-import-position
  11. # place class in our python path
  12. module_path = os.path.join('/'.join(os.path.realpath(__file__).split(os.path.sep)[:-1]), 'library')
  13. sys.path.insert(0, module_path)
  14. openshift_cert_expiry = pytest.importorskip("openshift_cert_expiry")
  15. @pytest.mark.skip('Skipping all tests because of unresolved import errors')
  16. class TestFakeOpenSSLClasses(unittest.TestCase):
  17. '''
  18. Test class for FakeOpenSSL classes
  19. '''
  20. def setUp(self):
  21. ''' setup method for other tests '''
  22. with open('test/system-node-m01.example.com.crt.txt', 'r') as fp:
  23. self.cert_string = fp.read()
  24. self.fake_cert = openshift_cert_expiry.FakeOpenSSLCertificate(self.cert_string)
  25. with open('test/master.server.crt.txt', 'r') as fp:
  26. self.cert_san_string = fp.read()
  27. self.fake_san_cert = openshift_cert_expiry.FakeOpenSSLCertificate(self.cert_san_string)
  28. def test_FakeOpenSSLCertificate_get_serial_number(self):
  29. """We can read the serial number from the cert"""
  30. self.assertEqual(11, self.fake_cert.get_serial_number())
  31. def test_FakeOpenSSLCertificate_get_notAfter(self):
  32. """We can read the cert expiry date"""
  33. expiry = self.fake_cert.get_notAfter()
  34. self.assertEqual('20190207181935Z', expiry)
  35. def test_FakeOpenSSLCertificate_get_sans(self):
  36. """We can read Subject Alt Names from a cert"""
  37. ext = self.fake_san_cert.get_extension(0)
  38. if ext.get_short_name() == 'subjectAltName':
  39. sans = str(ext)
  40. self.assertEqual('DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, DNS:kubernetes.default.svc.cluster.local, DNS:m01.example.com, DNS:openshift, DNS:openshift.default, DNS:openshift.default.svc, DNS:openshift.default.svc.cluster.local, DNS:172.30.0.1, DNS:192.168.122.241, IP Address:172.30.0.1, IP Address:192.168.122.241', sans)
  41. def test_FakeOpenSSLCertificate_get_sans_no_sans(self):
  42. """We can tell when there are no Subject Alt Names in a cert"""
  43. with self.assertRaises(IndexError):
  44. self.fake_cert.get_extension(0)
  45. def test_FakeOpenSSLCertificate_get_subject(self):
  46. """We can read the Subject from a cert"""
  47. # Subject: O=system:nodes, CN=system:node:m01.example.com
  48. subject = self.fake_cert.get_subject()
  49. subjects = []
  50. for name, value in subject.get_components():
  51. subjects.append('{}={}'.format(name, value))
  52. self.assertEqual('O=system:nodes, CN=system:node:m01.example.com', ', '.join(subjects))
  53. def test_FakeOpenSSLCertificate_get_subject_san_cert(self):
  54. """We can read the Subject from a cert with sans"""
  55. # Subject: O=system:nodes, CN=system:node:m01.example.com
  56. subject = self.fake_san_cert.get_subject()
  57. subjects = []
  58. for name, value in subject.get_components():
  59. subjects.append('{}={}'.format(name, value))
  60. self.assertEqual('CN=172.30.0.1', ', '.join(subjects))
  61. if __name__ == "__main__":
  62. unittest.main()