2019-04-11 21:07:36 -04:00
|
|
|
"""Module to handle the context of integration tests."""
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import sys
|
|
|
|
|
import tempfile
|
2021-11-29 16:24:39 -05:00
|
|
|
from typing import Iterable
|
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
|
|
import pytest
|
2019-04-11 21:07:36 -04:00
|
|
|
|
2019-08-02 14:46:12 -04:00
|
|
|
from certbot_integration_tests.utils import certbot_call
|
2019-04-11 21:07:36 -04:00
|
|
|
|
|
|
|
|
|
2021-02-25 17:59:00 -05:00
|
|
|
class IntegrationTestsContext:
|
2019-03-01 16:18:06 -05:00
|
|
|
"""General fixture describing a certbot integration tests context"""
|
2021-11-29 16:24:39 -05:00
|
|
|
def __init__(self, request: pytest.FixtureRequest) -> None:
|
2019-03-01 16:18:06 -05:00
|
|
|
self.request = request
|
2019-04-11 21:07:36 -04:00
|
|
|
|
2021-03-23 14:29:01 -04:00
|
|
|
if hasattr(request.config, 'workerinput'): # Worker node
|
2022-11-11 02:03:57 -05:00
|
|
|
self.worker_id = request.config.workerinput['workerid']
|
|
|
|
|
acme_xdist = request.config.workerinput['acme_xdist']
|
2019-03-01 16:18:06 -05:00
|
|
|
else: # Primary node
|
|
|
|
|
self.worker_id = 'primary'
|
2021-11-29 16:24:39 -05:00
|
|
|
acme_xdist = request.config.acme_xdist # type: ignore[attr-defined]
|
2019-04-11 21:07:36 -04:00
|
|
|
|
2019-08-02 14:46:12 -04:00
|
|
|
self.acme_server = acme_xdist['acme_server']
|
2019-04-11 21:07:36 -04:00
|
|
|
self.directory_url = acme_xdist['directory_url']
|
|
|
|
|
self.tls_alpn_01_port = acme_xdist['https_port'][self.worker_id]
|
|
|
|
|
self.http_01_port = acme_xdist['http_port'][self.worker_id]
|
[Unix] Create a framework for certbot integration tests: PART 4 (#6958)
This PR is the part 4 to implement #6541. It adds the integration tests for the nginx certbot plugin, and corresponds to the certbot-ci translation of certbot-nginx/tests/boulder-integration.sh that is executed for each PR.
As with certbot core tests, tests are written in Python, and executed by pytest, against a dynamic Boulder/Pebble instance setup. Tests are parallelized, of course, and a specific IntegrationTestsContext class, extended the one from certbot core tests, is crafter for these specific tests: its main goal is to setup a specific nginx instance for the current test.
On top of that, I use the test parametrization feature of Pytest, to drastically reduce the size of the actual code: indeed, the 6 tests from the original bash script share the same logic. So using a parametrization, one unique test is written, that is then executed 6 times against 6 different sets of parameters.
Note that the module integration_tests.nginx_tests.nginx_config do the same, but in Python, than certbot-nginx/tests/boulder-integration.conf.sh. The latter will be removed in a future PR, with all other bash scripts.
* Add nginx tests
* Distribute the other_port
* Load a pre-generated key/cert for nginx config
* Correct preload, remove a test, simplify a variable
* Integrate assertion directly in the test function
* Check process is not terminated
* Add spaces in the nginx config
* Add comments
* Use indirection
* Allow external cert
* Add coverage threshold for certbot-nginx
2019-04-23 16:29:48 -04:00
|
|
|
self.other_port = acme_xdist['other_port'][self.worker_id]
|
2019-04-11 21:07:36 -04:00
|
|
|
# Challtestsrv REST API, that exposes entrypoints to register new DNS entries,
|
2022-03-15 17:50:26 -04:00
|
|
|
# is listening on challtestsrv_url.
|
|
|
|
|
self.challtestsrv_url = acme_xdist['challtestsrv_url']
|
2019-04-11 21:07:36 -04:00
|
|
|
|
|
|
|
|
self.workspace = tempfile.mkdtemp()
|
|
|
|
|
self.config_dir = os.path.join(self.workspace, 'conf')
|
2019-08-06 18:02:16 -04:00
|
|
|
|
|
|
|
|
probe = tempfile.mkstemp(dir=self.workspace)
|
|
|
|
|
os.close(probe[0])
|
|
|
|
|
self.hook_probe = probe[1]
|
2019-04-11 21:07:36 -04:00
|
|
|
|
|
|
|
|
self.manual_dns_auth_hook = (
|
|
|
|
|
'{0} -c "import os; import requests; import json; '
|
|
|
|
|
"assert not os.environ.get('CERTBOT_DOMAIN').startswith('fail'); "
|
|
|
|
|
"data = {{'host':'_acme-challenge.{{0}}.'.format(os.environ.get('CERTBOT_DOMAIN')),"
|
|
|
|
|
"'value':os.environ.get('CERTBOT_VALIDATION')}}; "
|
2022-03-15 17:50:26 -04:00
|
|
|
"request = requests.post('{1}/set-txt', data=json.dumps(data)); "
|
2019-04-11 21:07:36 -04:00
|
|
|
"request.raise_for_status(); "
|
|
|
|
|
'"'
|
2022-03-15 17:50:26 -04:00
|
|
|
).format(sys.executable, self.challtestsrv_url)
|
2019-04-11 21:07:36 -04:00
|
|
|
self.manual_dns_cleanup_hook = (
|
|
|
|
|
'{0} -c "import os; import requests; import json; '
|
|
|
|
|
"data = {{'host':'_acme-challenge.{{0}}.'.format(os.environ.get('CERTBOT_DOMAIN'))}}; "
|
2022-03-15 17:50:26 -04:00
|
|
|
"request = requests.post('{1}/clear-txt', data=json.dumps(data)); "
|
2019-04-11 21:07:36 -04:00
|
|
|
"request.raise_for_status(); "
|
|
|
|
|
'"'
|
2022-03-15 17:50:26 -04:00
|
|
|
).format(sys.executable, self.challtestsrv_url)
|
2019-03-01 16:18:06 -05:00
|
|
|
|
2021-11-29 16:24:39 -05:00
|
|
|
def cleanup(self) -> None:
|
2019-04-11 21:07:36 -04:00
|
|
|
"""Cleanup the integration test context."""
|
|
|
|
|
shutil.rmtree(self.workspace)
|
|
|
|
|
|
2021-11-29 16:24:39 -05:00
|
|
|
def certbot(self, args: Iterable[str], force_renew: bool = True) -> Tuple[str, str]:
|
2019-04-11 21:07:36 -04:00
|
|
|
"""
|
|
|
|
|
Execute certbot with given args, not renewing certificates by default.
|
|
|
|
|
:param args: args to pass to certbot
|
2021-11-29 16:24:39 -05:00
|
|
|
:param bool force_renew: set to False to not renew by default
|
2021-05-31 03:01:01 -04:00
|
|
|
:return: stdout and stderr from certbot execution
|
2021-11-29 16:24:39 -05:00
|
|
|
:rtype: Tuple of `str`
|
2019-04-11 21:07:36 -04:00
|
|
|
"""
|
|
|
|
|
command = ['--authenticator', 'standalone', '--installer', 'null']
|
|
|
|
|
command.extend(args)
|
Add executable scripts to start certbot and acme server in certbot-ci (#7073)
During review of #6989, we saw that some of our test bash scripts were still used in the Boulder project in particular. It is about `tests/integration/_common.sh` in particular, to expose the `certbot_test` bash function, that is an appropriate way to execute a local version of certbot in test mode: define a custom server, remove several checks, full log and so on.
This PR is an attempt to assert this goal: exposing a new `certbot_test` executable for test purpose. More generally, this PR is about giving well suited scripts to quickly make manual tests against certbot without launching the full automated pytest suite.
The idea here is to leverage the existing logic in certbot-ci, and expose it as executable scripts. This is done thanks to the `console_scripts` entry of setuptools entrypoint feature, that install scripts in the `PATH`, when `pip install` is invoked, that delegate to specific functions in the installed packages.
Two scripts are defined this way:
* `certbot_test`: it executes certbot in test mode in a very similar way than the original `certbot_test` in `_common.sh`, by delegating to `certbot_integration_tests.utils.certbot_call:main`. By default this execution will target a pebble directory url started locally. The url, and also http-01/tls-alpn-01 challenge ports can be configured using ad-hoc environment variables. All arguments passed to `certbot_test` are transferred to the underlying certbot command.
* `acme_server`: it set up a fully running instance of an ACME server, ready for tests (in particular, all FQDN resolves to localhost in order to target a locally running `certbot_test` command) by delegating to `certbot_integration_tests.utils.acme_server:main`. The choice of the ACME server is given by the first parameter passed to `acme_server`, it can be `pebble`, `boulder-v1` or `boulder-v2`. The command keeps running on foreground, displaying the logs of the ACME server on stdout/stderr. The server is shut down and resources cleaned upon entering CTRL+C.
This two commands can be run also through the underlying python modules, that are executable.
Finally, a typical workflow on certbot side to run manual tests would be:
```
cd certbot
tools/venv.py
source venv/bin/activate
acme_server pebble &
certbot_test certonly --standalone -d test.example.com
```
On boulder side it could be:
```
# Follow certbot dev environment setup instructions, then ...
cd boulder
docker-compose run --use-aliases -e FAKE_DNS=172.17.0.1 --service-ports boulder ./start.py
SERVER=http://localhost:4001/directory certbot_test certonly --standalone -d test.example.com
```
* Configure certbot-ci to expose a certbot_test console script calling certbot in test mode against a local pebble instance
* Add a command to start pebble/boulder
* Use explicit start
* Add execution permission to acme_server
* Add a docstring to certbot_test function
* Change executable name
* Increase sleep to 3600s
* Implement a context manager to handle the acme server
* Add certbot_test workspace in .gitignore
* Add documentation
* Remove one function in context, split logic of certbot_test towards capturing non capturing
* Use an explicit an properly configured ACMEServer as handler.
* Add doc. Put constants.
2019-06-12 20:19:23 -04:00
|
|
|
return certbot_call.certbot_test(
|
|
|
|
|
command, self.directory_url, self.http_01_port, self.tls_alpn_01_port,
|
|
|
|
|
self.config_dir, self.workspace, force_renew=force_renew)
|
2019-04-11 21:07:36 -04:00
|
|
|
|
2021-11-29 16:24:39 -05:00
|
|
|
def get_domain(self, subdomain: str = 'le') -> str:
|
2019-04-11 21:07:36 -04:00
|
|
|
"""
|
|
|
|
|
Generate a certificate domain name suitable for distributed certbot integration tests.
|
|
|
|
|
This is a requirement to let the distribution know how to redirect the challenge check
|
|
|
|
|
from the ACME server to the relevant pytest-xdist worker. This resolution is done by
|
|
|
|
|
appending the pytest worker id to the subdomain, using this pattern:
|
|
|
|
|
{subdomain}.{worker_id}.wtf
|
2021-11-29 16:24:39 -05:00
|
|
|
:param str subdomain: the subdomain to use in the generated domain (default 'le')
|
2020-12-16 14:34:12 -05:00
|
|
|
:return: the well-formed domain suitable for redirection on
|
2021-11-29 16:24:39 -05:00
|
|
|
:rtype: str
|
2019-04-11 21:07:36 -04:00
|
|
|
"""
|
|
|
|
|
return '{0}.{1}.wtf'.format(subdomain, self.worker_id)
|