diff options
author | Benjamin Schubert <contact@benschubert.me> | 2019-11-08 17:53:56 +0000 |
---|---|---|
committer | Benjamin Schubert <contact@benschubert.me> | 2019-11-08 17:53:56 +0000 |
commit | f7733570f655ee3f6b0fa2c42b218eae7b9ca893 (patch) | |
tree | 6fdfaf7d25aadce14354298865ecd7cac15cc084 | |
parent | a6c9c52a578e953bc4af64e300ca24cbde8eea4e (diff) | |
download | buildstream-bschubert/expand-path-configs.tar.gz |
_remote.py: Expand user in certificates pathsbschubert/expand-path-configs
This ensures we correctly expand "~" in paths to certificates
-rw-r--r-- | src/buildstream/_remote.py | 2 | ||||
-rw-r--r-- | tests/artifactcache/config.py | 76 |
2 files changed, 77 insertions, 1 deletions
diff --git a/src/buildstream/_remote.py b/src/buildstream/_remote.py index 8527ca4cc..28c3167eb 100644 --- a/src/buildstream/_remote.py +++ b/src/buildstream/_remote.py @@ -74,7 +74,7 @@ class RemoteSpec(namedtuple('RemoteSpec', 'url push server_cert client_key clien cert = spec_node.get_str(key, default=None) if cert and basedir: cert = os.path.join(basedir, cert) - return cert + return os.path.expanduser(cert) cert_keys = ('server-cert', 'client-key', 'client-cert') server_cert, client_key, client_cert = tuple(parse_cert(key) for key in cert_keys) diff --git a/tests/artifactcache/config.py b/tests/artifactcache/config.py index 8b01a9ebe..75cce267c 100644 --- a/tests/artifactcache/config.py +++ b/tests/artifactcache/config.py @@ -214,3 +214,79 @@ def test_only_one(cli, datafiles, override_caches, project_caches, user_caches): # This does not happen for a simple `bst show`. result = cli.run(project=project, args=['artifact', 'pull', 'element.bst']) result.assert_main_error(ErrorDomain.STREAM, None) + + +@pytest.mark.datafiles(DATA_DIR) +@pytest.mark.parametrize( + "artifacts_config", + ( + { + "url": "http://localhost.test", + "server-cert": "~/server.crt", + "client-cert": "~/client.crt", + "client-key": "~/client.key", + }, + [ + { + "url": "http://localhost.test", + "server-cert": "~/server.crt", + "client-cert": "~/client.crt", + "client-key": "~/client.key", + }, + { + "url": "http://localhost2.test", + "server-cert": "~/server2.crt", + "client-cert": "~/client2.crt", + "client-key": "~/client2.key", + }, + ] + ) +) +@pytest.mark.parametrize("in_user_config", [True, False]) +def test_paths_for_artifact_config_are_expanded(tmpdir, monkeypatch, artifacts_config, in_user_config): + # Produce a fake user and project config with the cache configuration. + # user_config, project_config = configure_remote_caches(override_caches, project_caches, user_caches) + # project_config['name'] = 'test' + + monkeypatch.setenv("HOME", tmpdir.join("homedir")) + + if in_user_config: + user_config = {"artifacts": artifacts_config} + project_config = {"name": "test"} + else: + user_config = {} + project_config = { + "name": "test", + "artifacts": artifacts_config, + } + + user_config_file = str(tmpdir.join('buildstream.conf')) + _yaml.roundtrip_dump(user_config, file=user_config_file) + + project_dir = tmpdir.mkdir('project') + project_config_file = str(project_dir.join('project.conf')) + _yaml.roundtrip_dump(project_config, file=project_config_file) + + with dummy_context(config=user_config_file) as context: + project = Project(str(project_dir), context) + project.ensure_fully_loaded() + + # Use the helper from the artifactcache module to parse our configuration. + parsed_cache_specs = ArtifactCache._configured_remote_cache_specs(context, project) + + if isinstance(artifacts_config, dict): + artifacts_config = [artifacts_config] + + # Build expected artifact config + artifacts_config = [ + RemoteSpec( + url=config["url"], + push=False, + server_cert=os.path.expanduser(config["server-cert"]), + client_cert=os.path.expanduser(config["client-cert"]), + client_key=os.path.expanduser(config["client-key"]), + ) + for config in artifacts_config + ] + + assert parsed_cache_specs == artifacts_config |