diff options
author | Jason DeTiberus <jdetiber@redhat.com> | 2017-02-21 20:23:34 -0500 |
---|---|---|
committer | Jason DeTiberus <jdetiber@redhat.com> | 2017-03-02 12:13:47 -0500 |
commit | 293f18593307f5a90890848d77c439b3d3f1dd0d (patch) | |
tree | 013cc0534f852eb587387734745a97023d7dfb52 /roles/openshift_certificate_expiry/test/test_load_and_handle_cert.py | |
parent | 5a91f31b65a4bb0ec524aee9ba1c6d4e4030d8d2 (diff) | |
download | openshift-293f18593307f5a90890848d77c439b3d3f1dd0d.tar.gz openshift-293f18593307f5a90890848d77c439b3d3f1dd0d.tar.bz2 openshift-293f18593307f5a90890848d77c439b3d3f1dd0d.tar.xz openshift-293f18593307f5a90890848d77c439b3d3f1dd0d.zip |
test fixes for openshift_certificates_expiry
- create pytest fixtures for building certs at runtime
- update tests to use the fixtures
- add tests for load_and_handle_cert
- fix py2/py3 encode/decode issues raised by tests
- add get_extension_count method to fakeOpenSSLCertificate
- avoid using a temp file for passing ssl certificate to openssl
subprocess
- other test tweaks:
- exclude conftest.py and tests from coverage report
- reduce the fail_under to 26%, since the tests being included were
inflating our coverage
Diffstat (limited to 'roles/openshift_certificate_expiry/test/test_load_and_handle_cert.py')
-rw-r--r-- | roles/openshift_certificate_expiry/test/test_load_and_handle_cert.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/roles/openshift_certificate_expiry/test/test_load_and_handle_cert.py b/roles/openshift_certificate_expiry/test/test_load_and_handle_cert.py new file mode 100644 index 000000000..98792e2ee --- /dev/null +++ b/roles/openshift_certificate_expiry/test/test_load_and_handle_cert.py @@ -0,0 +1,67 @@ +''' + Unit tests for the load_and_handle_cert method +''' +import datetime +import os +import sys + +import pytest + +MODULE_PATH = os.path.realpath(os.path.join(__file__, os.pardir, os.pardir, 'library')) +sys.path.insert(1, MODULE_PATH) + +# pylint: disable=import-error,wrong-import-position,missing-docstring +# pylint: disable=invalid-name,redefined-outer-name +import openshift_cert_expiry # noqa: E402 + +# TODO: More testing on the results of the load_and_handle_cert function +# could be implemented here as well, such as verifying subjects +# match up. + + +@pytest.fixture(params=['OpenSSLCertificate', 'FakeOpenSSLCertificate']) +def loaded_cert(request, valid_cert): + """ parameterized fixture to provide load_and_handle_cert results + for both OpenSSL and FakeOpenSSL parsed certificates + """ + now = datetime.datetime.now() + + openshift_cert_expiry.HAS_OPENSSL = request.param == 'OpenSSLCertificate' + + # valid_cert['cert_file'] is a `py.path.LocalPath` object and + # provides a read_text() method for reading the file contents. + cert_string = valid_cert['cert_file'].read_text('utf8') + + (subject, + expiry_date, + time_remaining, + serial) = openshift_cert_expiry.load_and_handle_cert(cert_string, now) + + return { + 'now': now, + 'subject': subject, + 'expiry_date': expiry_date, + 'time_remaining': time_remaining, + 'serial': serial, + } + + +def test_serial(loaded_cert, valid_cert): + """Params: + + * `loaded_cert` comes from the `loaded_cert` fixture in this file + * `valid_cert` comes from the 'valid_cert' fixture in conftest.py + """ + valid_cert_serial = valid_cert['cert'].get_serial_number() + assert loaded_cert['serial'] == valid_cert_serial + + +def test_expiry(loaded_cert): + """Params: + + * `loaded_cert` comes from the `loaded_cert` fixture in this file + """ + expiry_date = loaded_cert['expiry_date'] + time_remaining = loaded_cert['time_remaining'] + now = loaded_cert['now'] + assert expiry_date == now + time_remaining |