From 07faf4a5c75fd9a679beb30d042f404e0a38734f Mon Sep 17 00:00:00 2001 From: Sjoerd Simons <sjoerd.simons@collabora.co.uk> Date: Mon, 12 Dec 2016 13:48:03 +0100 Subject: [PATCH] test: Don't hardcode the port to listen on Hardcoding the port to let the test server listen on is can be problematic in case the build hosts happens to have something on that port (e.g. another daemon or build running at the same time). Instead let the kernel pick a free port for us. Apart from that, this changes all configuration files to be templated, with HOST expected to be updated with the actual address/port picked. For explicit configuration files there is a new context manager which will provide a temporary config file with the right information while the sysroot http server will setup a temporary directory to serve files from (with all files updates with correct url information as well). Signed-off-by: Sjoerd Simons <sjoerd.simons@collabora.co.uk> Reviewed-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> Differential Revision: https://phabricator.apertis.org/D5243 --- tests/data/conf.in | 2 +- tests/data/invalid_archive.in | 2 +- tests/data/invalid_sysroot.in | 2 +- tests/data/invalid_version.in | 2 +- tests/data/latest.conf.in | 2 +- tests/data/missing_version.in | 2 +- tests/data/sections.conf.in | 6 +-- tests/data/version.in | 2 +- tests/data/wrong_distro.in | 2 +- tests/data/wrong_release.in | 2 +- tests/test-download | 25 ++++++------ tests/test-install | 24 ++++++------ tests/test-latest | 22 ++++++----- tests/test-update | 28 +++++++------- tests/test_util.py | 71 ++++++++++++++++++++++++++++------- 15 files changed, 124 insertions(+), 70 deletions(-) diff --git a/tests/data/conf.in b/tests/data/conf.in index d5ed59a..d46d1a1 100644 --- a/tests/data/conf.in +++ b/tests/data/conf.in @@ -1,3 +1,3 @@ [general] -url=http://127.0.0.1:1234/versions/@SYSROOT@ +url=http://HOST/versions/@SYSROOT@ diff --git a/tests/data/invalid_archive.in b/tests/data/invalid_archive.in index 29a0607..4f9db72 100644 --- a/tests/data/invalid_archive.in +++ b/tests/data/invalid_archive.in @@ -1,2 +1,2 @@ version=16.09 20161101.0 -url=http://127.0.0.1:1234/archives/eraroj-16.09-armhf_invalidarchive.tar.gz +url=http://HOST/archives/eraroj-16.09-armhf_invalidarchive.tar.gz diff --git a/tests/data/invalid_sysroot.in b/tests/data/invalid_sysroot.in index 504517d..7fbcea3 100644 --- a/tests/data/invalid_sysroot.in +++ b/tests/data/invalid_sysroot.in @@ -1,2 +1,2 @@ version=16.09 20161101.0 -url=http://127.0.0.1:1234/archives/eraroj-16.09-armhf_invalidversion.tar.gz +url=http://HOST/archives/eraroj-16.09-armhf_invalidversion.tar.gz diff --git a/tests/data/invalid_version.in b/tests/data/invalid_version.in index c623d39..ad9ce7d 100644 --- a/tests/data/invalid_version.in +++ b/tests/data/invalid_version.in @@ -1,2 +1,2 @@ version=20161101.0 -url=http://127.0.0.1:1234/data/sysroot-apertis-16.09-arm64_20161101.0.tar.gz +url=http://HOST/data/sysroot-apertis-16.09-arm64_20161101.0.tar.gz diff --git a/tests/data/latest.conf.in b/tests/data/latest.conf.in index 57a4060..33727f0 100644 --- a/tests/data/latest.conf.in +++ b/tests/data/latest.conf.in @@ -1,3 +1,3 @@ [general] -url=http://127.0.0.1:1234/versions/%(distro)-%(release)-%(arch)_latest +url=http://HOST/versions/%(distro)-%(release)-%(arch)_latest diff --git a/tests/data/missing_version.in b/tests/data/missing_version.in index 7eb59a6..1cb24fc 100644 --- a/tests/data/missing_version.in +++ b/tests/data/missing_version.in @@ -1 +1 @@ -url=http://127.0.0.1:1234/data/sysroot-apertis-16.09-arm64_20161101.0.tar.gz +url=http://HOST/data/sysroot-apertis-16.09-arm64_20161101.0.tar.gz diff --git a/tests/data/sections.conf.in b/tests/data/sections.conf.in index 2e326a4..18adcd5 100644 --- a/tests/data/sections.conf.in +++ b/tests/data/sections.conf.in @@ -2,10 +2,10 @@ url=http://example.com/sysroot [apertis] -url=http://127.0.0.1:1234/versions/apertis-%(release)-%(arch)_latest +url=http://HOST/versions/apertis-%(release)-%(arch)_latest [babiladis] -url=http://127.0.0.1:1234/versions/babiladis-%(release)-%(arch)_latest +url=http://HOST/versions/babiladis-%(release)-%(arch)_latest [fermis] -url=http://127.0.0.1:1234/versions/fermis-%(release)-%(arch)_latest +url=http://HOST/versions/fermis-%(release)-%(arch)_latest diff --git a/tests/data/version.in b/tests/data/version.in index 55f7a34..0553e02 100644 --- a/tests/data/version.in +++ b/tests/data/version.in @@ -1,2 +1,2 @@ version=@RELEASE@ @BUILD@ -url=http://127.0.0.1:1234/archives/@DISTRO@-@RELEASE@-@ARCH@_@BUILD@.tar.gz +url=http://HOST/archives/@DISTRO@-@RELEASE@-@ARCH@_@BUILD@.tar.gz diff --git a/tests/data/wrong_distro.in b/tests/data/wrong_distro.in index 35d0698..36a2fdc 100644 --- a/tests/data/wrong_distro.in +++ b/tests/data/wrong_distro.in @@ -1,2 +1,2 @@ version=16.09 20161101.0 -url=http://127.0.0.1:1234/archives/apertis-16.09-armhf_20161101.0.tar.gz +url=http://HOST/archives/apertis-16.09-armhf_20161101.0.tar.gz diff --git a/tests/data/wrong_release.in b/tests/data/wrong_release.in index 82d3aec..f4ecf4f 100644 --- a/tests/data/wrong_release.in +++ b/tests/data/wrong_release.in @@ -1,2 +1,2 @@ version=16.09 20161101.0 -url=http://127.0.0.1:1234/archives/eraroj-16.12-armhf_20161101.0.tar.gz +url=http://HOST/archives/eraroj-16.12-armhf_20161101.0.tar.gz diff --git a/tests/test-download b/tests/test-download index 631116f..eb64a32 100755 --- a/tests/test-download +++ b/tests/test-download @@ -13,8 +13,9 @@ import os import tempfile from test_util import SysrootServer +from test_util import templatedconfig from test_util import should_succeed, should_fail -from test_util import BASE_URL, BASE_ARCHIVE, BASE_CONFIG, \ +from test_util import BASE_ARCHIVE, BASE_CONFIG, \ SYSROOTS, LATEST_SYSROOTS, \ CONFIG_FILES, BAD_URLS @@ -33,7 +34,7 @@ server.start() # Test "ade sysroot download --url" usage for sysroot in SYSROOTS: with tempfile.TemporaryDirectory() as tmpdir: - params = ['--url', BASE_URL.format(*sysroot), '--distro', sysroot[0], + params = ['--url', server.base_url().format(*sysroot), '--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] should_succeed('download', *params, path=tmpdir, check=lambda x: check_archive(x, sysroot)) @@ -41,22 +42,24 @@ for sysroot in SYSROOTS: # Test "ade sysroot download --distro --release --arch" usage for sysroot in SYSROOTS: with tempfile.TemporaryDirectory() as tmpdir: - params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] - should_succeed('download', *params, config=BASE_CONFIG.format(*sysroot), path=tmpdir, - check=lambda x: check_archive(x, sysroot)) + with templatedconfig(server, BASE_CONFIG.format(*sysroot)) as config: + params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] + should_succeed('download', *params, config=config, path=tmpdir, + check=lambda x: check_archive(x, sysroot)) # Test "ade sysroot download --distro --release --arch" usage with different config files for config_file in CONFIG_FILES: - config = os.path.join(os.getcwd(), "configs", config_file) - for sysroot in LATEST_SYSROOTS: - with tempfile.TemporaryDirectory() as tmpdir: - params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] - should_succeed('download', *params, config=config, path=tmpdir, - check=lambda x: check_archive(x, sysroot)) + with templatedconfig(server, config_file) as config: + for sysroot in LATEST_SYSROOTS: + with tempfile.TemporaryDirectory() as tmpdir: + params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] + should_succeed('download', *params, config=config, path=tmpdir, + check=lambda x: check_archive(x, sysroot)) # Test error cases with tempfile.TemporaryDirectory() as tmpdir: for url in BAD_URLS: + url.replace("HOST", server.host()) params = ['--url', url, '--distro', 'eraroj', '--release', '16.09', '--arch', 'armhf'] should_fail('download', *params, path=tmpdir) diff --git a/tests/test-install b/tests/test-install index 02bdda5..b4aa011 100755 --- a/tests/test-install +++ b/tests/test-install @@ -13,8 +13,9 @@ import os import tempfile from test_util import SysrootServer +from test_util import templatedconfig from test_util import should_succeed, should_fail -from test_util import BASE_TAG, BASE_URL, BASE_ARCHIVE, BASE_CONFIG, \ +from test_util import BASE_TAG, BASE_ARCHIVE, BASE_CONFIG, \ SYSROOTS, LATEST_SYSROOTS, SYSROOT1, SYSROOT2, \ CONFIG_FILES, BAD_URLS @@ -39,7 +40,7 @@ server.start() # Test "ade sysroot install --url" usage for sysroot in SYSROOTS: with tempfile.TemporaryDirectory() as tmpdir: - params = ['--url', BASE_URL.format(*sysroot), '--distro', sysroot[0], + params = ['--url', server.base_url().format(*sysroot), '--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] should_succeed('install', *params, path=tmpdir, check=lambda x: check_sysroot(x, tmpdir, sysroot)) @@ -53,19 +54,20 @@ for sysroot in SYSROOTS: # Test "ade sysroot install --distro --release --arch" usage for sysroot in SYSROOTS: with tempfile.TemporaryDirectory() as tmpdir: - params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] - should_succeed('install', *params, config=BASE_CONFIG.format(*sysroot), path=tmpdir, - check=lambda x: check_sysroot(x, tmpdir, sysroot)) - -# Test "ade sysroot install --distro --release --arch" usage with different config files -for config_file in CONFIG_FILES: - config = os.path.join(os.getcwd(), "configs", config_file) - for sysroot in LATEST_SYSROOTS: - with tempfile.TemporaryDirectory() as tmpdir: + with templatedconfig(server, BASE_CONFIG.format(*sysroot)) as config: params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] should_succeed('install', *params, config=config, path=tmpdir, check=lambda x: check_sysroot(x, tmpdir, sysroot)) +# Test "ade sysroot install --distro --release --arch" usage with different config files +for config_file in CONFIG_FILES: + with templatedconfig(server, config_file) as config: + for sysroot in LATEST_SYSROOTS: + with tempfile.TemporaryDirectory() as tmpdir: + params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] + should_succeed('install', *params, config=config, path=tmpdir, + check=lambda x: check_sysroot(x, tmpdir, sysroot)) + # Test "ade sysroot install --force" usage with tempfile.TemporaryDirectory() as tmpdir: should_succeed('install', '--file', BASE_ARCHIVE.format(*SYSROOT1), path=tmpdir, diff --git a/tests/test-latest b/tests/test-latest index 3394ad7..878ef3e 100755 --- a/tests/test-latest +++ b/tests/test-latest @@ -12,8 +12,9 @@ import os from test_util import SysrootServer +from test_util import templatedconfig from test_util import should_succeed, should_fail -from test_util import BASE_TAG, BASE_URL, BASE_ARCHIVE, BASE_CONFIG, \ +from test_util import BASE_TAG, BASE_ARCHIVE, BASE_CONFIG, \ SYSROOTS, LATEST_SYSROOTS, \ CONFIG_FILES, BAD_VERSION_URLS @@ -28,25 +29,26 @@ server.start() # Test "ade sysroot latest --url" usage for sysroot in SYSROOTS: - params = ['--url', BASE_URL.format(*sysroot), '--distro', sysroot[0], + params = ['--url', server.base_url().format(*sysroot), '--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] should_succeed('latest', *params, check=lambda x: check_version(x, sysroot)) # Test "ade sysroot latest --distro --release --arch" usage for sysroot in SYSROOTS: - params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] - should_succeed('latest', *params, config=BASE_CONFIG.format(*sysroot), - check=lambda x: check_version(x, sysroot)) - -# Test "ade sysroot latest --distro --release --arch" usage with different config files -for config_file in CONFIG_FILES: - config = os.path.join(os.getcwd(), "configs", config_file) - for sysroot in LATEST_SYSROOTS: + with templatedconfig(server, BASE_CONFIG.format(*sysroot)) as config: params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] should_succeed('latest', *params, config=config, check=lambda x: check_version(x, sysroot)) +# Test "ade sysroot latest --distro --release --arch" usage with different config files +for config_file in CONFIG_FILES: + with templatedconfig(server, config_file) as config: + for sysroot in LATEST_SYSROOTS: + params = ['--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] + should_succeed('latest', *params, config=config, + check=lambda x: check_version(x, sysroot)) + # Test error cases for url in BAD_VERSION_URLS: params = ['--url', url, '--distro', 'eraroj', '--release', '16.09', '--arch', 'armhf'] diff --git a/tests/test-update b/tests/test-update index 5393c99..01785c0 100755 --- a/tests/test-update +++ b/tests/test-update @@ -13,8 +13,9 @@ import os import tempfile from test_util import SysrootServer +from test_util import templatedconfig from test_util import should_succeed, should_fail -from test_util import BASE_TAG, BASE_URL, BASE_ARCHIVE, BASE_CONFIG, \ +from test_util import BASE_TAG, BASE_ARCHIVE, BASE_CONFIG, \ SYSROOTS, LATEST_SYSROOTS, SYSROOT1, SYSROOT2, \ SYSROOT_VERSIONS, CONFIG_FILES, BAD_URLS @@ -33,7 +34,7 @@ def check_sysroot(result, path, sysroot): return check_result(result, sysroot) and check_install(path, sysroot) def install_initial_release(path): - params = ['--url', BASE_URL.format(*SYSROOT1), '--distro', SYSROOT1[0], + params = ['--url', server.base_url().format(*SYSROOT1), '--distro', SYSROOT1[0], '--release', SYSROOT1[1], '--arch', SYSROOT1[2]] should_succeed('install', *params, path=path) @@ -45,30 +46,31 @@ server.start() with tempfile.TemporaryDirectory() as tmpdir: install_initial_release(tmpdir) for sysroot in SYSROOT_VERSIONS: - params = ['--url', BASE_URL.format(*sysroot), + params = ['--url', server.base_url().format(*sysroot), '--distro', sysroot[0], '--release', sysroot[1], '--arch', sysroot[2]] should_succeed('update', *params, path=tmpdir, check=lambda x: check_sysroot(x, tmpdir, sysroot)) # Test "ade sysroot update --distro --release --arch" usage with tempfile.TemporaryDirectory() as tmpdir: - install_initial_release(tmpdir) - params = ['--distro', SYSROOT2[0], '--release', SYSROOT2[1], '--arch', SYSROOT2[2]] - should_succeed('update', *params, config=BASE_CONFIG.format(*SYSROOT2), path=tmpdir, - check=lambda x: check_sysroot(x, tmpdir, SYSROOT2)) + with templatedconfig(server, BASE_CONFIG.format(*SYSROOT2)) as config: + install_initial_release(tmpdir) + params = ['--distro', SYSROOT2[0], '--release', SYSROOT2[1], '--arch', SYSROOT2[2]] + should_succeed('update', *params, config=config, path=tmpdir, + check=lambda x: check_sysroot(x, tmpdir, SYSROOT2)) # Test "ade sysroot update --distro --release --arch" usage with different config files for config_file in CONFIG_FILES: with tempfile.TemporaryDirectory() as tmpdir: install_initial_release(tmpdir) - config = os.path.join(os.getcwd(), "configs", config_file) - params = ['--distro', SYSROOT2[0], '--release', SYSROOT2[1], '--arch', SYSROOT2[2]] - should_succeed('update', *params, config=config, path=tmpdir, - check=lambda x: check_sysroot(x, tmpdir, SYSROOT2)) + with templatedconfig(server, config_file) as config: + params = ['--distro', SYSROOT2[0], '--release', SYSROOT2[1], '--arch', SYSROOT2[2]] + should_succeed('update', *params, config=config, path=tmpdir, + check=lambda x: check_sysroot(x, tmpdir, SYSROOT2)) # Test "ade sysroot update --force" usage with tempfile.TemporaryDirectory() as tmpdir: - params = ['--url', BASE_URL.format(*SYSROOT2), '--distro', SYSROOT2[0], + params = ['--url', server.base_url().format(*SYSROOT2), '--distro', SYSROOT2[0], '--release', SYSROOT2[1], '--arch', SYSROOT2[2]] # Trying to update non-installed sysroot @@ -91,7 +93,7 @@ with tempfile.TemporaryDirectory() as tmpdir: if not os.path.exists(stamp.name): exit(1) - params = ['--url', BASE_URL.format(*SYSROOT1), '--distro', SYSROOT1[0], + params = ['--url', server.base_url().format(*SYSROOT1), '--distro', SYSROOT1[0], '--release', SYSROOT1[1], '--arch', SYSROOT1[2]] # Try to downgrade to older version diff --git a/tests/test_util.py b/tests/test_util.py index fbfe1f5..76dafcc 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -13,9 +13,13 @@ import os import socket import subprocess import threading +import tempfile +import shutil +import atexit +from pathlib import Path +from contextlib import contextmanager BASE_TAG = "{0}-{1}-{2}_{3}" -BASE_URL = "http://127.0.0.1:1234/versions/{0}".format(BASE_TAG) BASE_ARCHIVE = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files', 'archives', "{0}.tar.gz".format(BASE_TAG)) BASE_CONFIG = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files', 'configs', "{0}.conf".format(BASE_TAG)) BASE_INSTALL = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files', 'install') @@ -68,18 +72,18 @@ CONFIG_FILES = [ BAD_VERSION_URLS = [ 'invalidformat', - 'http://127.0.0.1:1234/not_found', - 'http://127.0.0.1:1234/versions/empty', - 'http://127.0.0.1:1234/versions/missing_version', - 'http://127.0.0.1:1234/versions/missing_url', - 'http://127.0.0.1:1234/versions/invalid_version' + 'http://HOST/not_found', + 'http://HOST/versions/empty', + 'http://HOST/versions/missing_version', + 'http://HOST/versions/missing_url', + 'http://HOST/versions/invalid_version' ] BAD_SYSROOT_URLS = [ - 'http://127.0.0.1:1234/versions/wrong_distro', - 'http://127.0.0.1:1234/versions/wrong_release', - 'http://127.0.0.1:1234/versions/invalid_archive', - 'http://127.0.0.1:1234/versions/invalid_sysroot' + 'http://HOST/versions/wrong_distro', + 'http://HOST/versions/wrong_release', + 'http://HOST/versions/invalid_archive', + 'http://HOST/versions/invalid_sysroot' ] BAD_URLS = BAD_VERSION_URLS + BAD_SYSROOT_URLS @@ -130,14 +134,39 @@ class SysrootHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): def log_message(self, *args): pass +def template_config_file(server, src, dst): + with open(src) as fsrc: + host = "{}:{}".format(*server.server_address) + output = [] + for line in fsrc.readlines(): + output.append((line.replace("HOST", host))) + + with open(dst, 'w') as fdst: + fdst.writelines(output) + class SysrootServer(http.server.HTTPServer): def __init__(self): - os.chdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files')) - super().__init__(("", 1234), SysrootHTTPRequestHandler) + contentdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), + 'files') + tmpdir = tempfile.TemporaryDirectory() + atexit.register (tmpdir.cleanup) + + self.tmpcontentdir = os.path.join(tmpdir.name, 'files') + shutil.copytree(contentdir, self.tmpcontentdir) + os.chdir(self.tmpcontentdir) + + # This binds so we can determine the port that's used + super().__init__(("127.0.0.1", 0), SysrootHTTPRequestHandler) + + for f in Path(self.tmpcontentdir).rglob("versions/*"): + template_config_file (self, f.as_posix(), f.as_posix()) + + for f in Path(self.tmpcontentdir).rglob("configs/*"): + template_config_file (self, f.as_posix(), f.as_posix()) def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.socket.bind(self.server_address) + super().server_bind() def start(self): self.thread = threading.Thread(target=self.serve_forever) @@ -147,3 +176,19 @@ class SysrootServer(http.server.HTTPServer): def stop(self): self.shutdown() self.thread.join() + + def host(self): + return "{}:{}".format(*self.server_address) + + def base_url(self): + url = "http://{}/versions/{}" + return url.format(self.host(), BASE_TAG) + +@contextmanager +def templatedconfig(server, config_file): + config = os.path.join(os.getcwd(), "configs", config_file) + with tempfile.TemporaryDirectory() as t: + tmpconfig = os.path.join(t, os.path.basename(config_file)) + + template_config_file(server, config, tmpconfig) + yield tmpconfig -- GitLab