Improve certonly renewal test coverage

This commit is contained in:
Peter Eckersley
2016-02-03 12:57:49 -08:00
parent bfd182ae39
commit c0d55c7c33
2 changed files with 15 additions and 4 deletions
+3
View File
@@ -287,12 +287,15 @@ def _should_renew(config, lineage):
"Return true if any of the circumstances for automatic renewal apply." "Return true if any of the circumstances for automatic renewal apply."
if config.renew_by_default: if config.renew_by_default:
logger.info("Auto-renewal forced with --renew-by-default...") logger.info("Auto-renewal forced with --renew-by-default...")
print("forced")
return True return True
if lineage.should_autorenew(interactive=True): if lineage.should_autorenew(interactive=True):
logger.info("Cert is due for renewal, auto-renewing...") logger.info("Cert is due for renewal, auto-renewing...")
print("due")
return True return True
if config.dry_run: if config.dry_run:
logger.info("Cert not due for renewal, but simulating renewal for dry run") logger.info("Cert not due for renewal, but simulating renewal for dry run")
print("dry")
return True return True
return False return False
+12 -4
View File
@@ -531,10 +531,11 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
self._certonly_new_request_common, mock_client) self._certonly_new_request_common, mock_client)
@mock.patch('letsencrypt.cli._find_duplicative_certs') @mock.patch('letsencrypt.cli._find_duplicative_certs')
def _test_certonly_renewal_common(self, extra_args, mock_fdc): def _test_renewal_common(self, due_for_renewal, extra_args, outstring, mock_fdc):
cert_path = 'letsencrypt/tests/testdata/cert.pem' cert_path = 'letsencrypt/tests/testdata/cert.pem'
chain_path = '/etc/letsencrypt/live/foo.bar/fullchain.pem' chain_path = '/etc/letsencrypt/live/foo.bar/fullchain.pem'
mock_lineage = mock.MagicMock(cert=cert_path, fullchain=chain_path) mock_lineage = mock.MagicMock(cert=cert_path, fullchain=chain_path)
mock_lineage.should_autorenew.return_value = due_for_renewal
mock_certr = mock.MagicMock() mock_certr = mock.MagicMock()
mock_key = mock.MagicMock(pem='pem_key') mock_key = mock.MagicMock(pem='pem_key')
mock_fdc.return_value = (mock_lineage, None) mock_fdc.return_value = (mock_lineage, None)
@@ -553,12 +554,16 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
args += extra_args args += extra_args
self._call(args) self._call(args)
if outstring:
with open(os.path.join(self.logs_dir, "letsencrypt.log")) as lf:
self.assertTrue(outstring in lf.read())
mock_client.obtain_certificate.assert_called_once_with(['foo.bar']) mock_client.obtain_certificate.assert_called_once_with(['foo.bar'])
return mock_lineage, mock_get_utility return mock_lineage, mock_get_utility
def test_certonly_renewal(self): def test_certonly_renewal(self):
lineage, get_utility = self._test_certonly_renewal_common([]) lineage, get_utility = self._test_renewal_common(True, [], None)
self.assertEqual(lineage.save_successor.call_count, 1) self.assertEqual(lineage.save_successor.call_count, 1)
lineage.update_all_links_to.assert_called_once_with( lineage.update_all_links_to.assert_called_once_with(
lineage.latest_common_version()) lineage.latest_common_version())
@@ -566,11 +571,14 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
self.assertTrue('fullchain.pem' in cert_msg) self.assertTrue('fullchain.pem' in cert_msg)
self.assertTrue('donate' in get_utility().add_message.call_args[0][0]) self.assertTrue('donate' in get_utility().add_message.call_args[0][0])
def test_certonly_dry_run_reinstall_is_renewal(self): def test_certonly_renewal_triggers(self):
_, get_utility = self._test_certonly_renewal_common(['--dry-run']) # --dry-run should force renewal
_, get_utility = self._test_renewal_common(False, ['--dry-run'], None)
self.assertEqual(get_utility().add_message.call_count, 1) self.assertEqual(get_utility().add_message.call_count, 1)
self.assertTrue('dry run' in get_utility().add_message.call_args[0][0]) self.assertTrue('dry run' in get_utility().add_message.call_args[0][0])
_, _ = self._test_renewal_common(False, ['--renew-by-default', '-tvv', '--debug'], "Auto-renewal forced")
self.assertEqual(get_utility().add_message.call_count, 1)
@mock.patch('letsencrypt.cli.zope.component.getUtility') @mock.patch('letsencrypt.cli.zope.component.getUtility')
@mock.patch('letsencrypt.cli._treat_as_renewal') @mock.patch('letsencrypt.cli._treat_as_renewal')