diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2017-11-28 12:03:10 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2018-01-11 18:18:13 +0000 |
commit | 1f39477605f5a8caba487a6ebcf3bf4559babe5f (patch) | |
tree | a8d583fc395c8eb304fc27deb2999e6ba2b6c2d9 /tests/artifactcache/config.py | |
parent | 51c17e1147392f15580fb2dd925055ad8863ab3e (diff) | |
download | buildstream-1f39477605f5a8caba487a6ebcf3bf4559babe5f.tar.gz |
tests: Exercise the new multiple cache support
This adds a new test for parsing artifact cache configuration, which
calls the helper function from the 'artifactcache' module directly
rather than trying to assert based on blind push and pull commands
whether or not we got the complex precedence rules exactly right.
This means frontend push/pull tests no longer need to be so thorough
about testing precedence but they are instead expanded to assert that
multiple caches work correctly.
Diffstat (limited to 'tests/artifactcache/config.py')
-rw-r--r-- | tests/artifactcache/config.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/artifactcache/config.py b/tests/artifactcache/config.py new file mode 100644 index 000000000..3416f6c10 --- /dev/null +++ b/tests/artifactcache/config.py @@ -0,0 +1,106 @@ +import pytest + +import itertools +import os + +from buildstream._artifactcache import ArtifactCacheSpec, configured_remote_artifact_cache_specs +from buildstream._context import Context +from buildstream._project import Project +from buildstream.utils import _deduplicate +from buildstream import _yaml + + +cache1 = ArtifactCacheSpec(url='https://example.com/cache1', push=True) +cache2 = ArtifactCacheSpec(url='https://example.com/cache2', push=False) +cache3 = ArtifactCacheSpec(url='https://example.com/cache3', push=False) +cache4 = ArtifactCacheSpec(url='https://example.com/cache4', push=False) +cache5 = ArtifactCacheSpec(url='https://example.com/cache5', push=False) +cache6 = ArtifactCacheSpec(url='https://example.com/cache6', push=True) + + +# Generate cache configuration fragments for the user config and project config files. +# +def configure_remote_caches(override_caches, project_caches=[], user_caches=[]): + user_config = {} + if len(user_caches) == 1: + user_config['artifacts'] = { + 'url': user_caches[0].url, + 'push': user_caches[0].push, + } + elif len(user_caches) > 1: + user_config['artifacts'] = [ + {'url': cache.url, 'push': cache.push} for cache in user_caches + ] + + if len(override_caches) == 1: + user_config['projects'] = { + 'test': { + 'artifacts': { + 'url': override_caches[0].url, + 'push': override_caches[0].push, + } + } + } + elif len(override_caches) > 1: + user_config['projects'] = { + 'test': { + 'artifacts': [ + {'url': cache.url, 'push': cache.push} for cache in override_caches + ] + } + } + + project_config = {} + if len(project_caches) > 0: + if len(project_caches) == 1: + project_config.update({ + 'artifacts': { + 'url': project_caches[0].url, + 'push': project_caches[0].push, + } + }) + elif len(project_caches) > 1: + project_config.update({ + 'artifacts': [ + {'url': cache.url, 'push': cache.push} for cache in project_caches + ] + }) + + return user_config, project_config + + +# Test that parsing the remote artifact cache locations produces the +# expected results. +@pytest.mark.parametrize( + 'override_caches, project_caches, user_caches', + [ + # The leftmost cache is the highest priority one in all cases here. + pytest.param([], [], [], id='empty-config'), + pytest.param([], [], [cache1, cache2], id='user-config'), + pytest.param([], [cache1, cache2], [cache3], id='project-config'), + pytest.param([cache1], [cache2], [cache3], id='project-override-in-user-config'), + pytest.param([cache1, cache2], [cache3, cache4], [cache5, cache6], id='list-order'), + pytest.param([cache1, cache2, cache1], [cache2], [cache2, cache1], id='duplicates'), + ]) +def test_artifact_cache_precedence(tmpdir, override_caches, project_caches, user_caches): + # 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' + + user_config_file = tmpdir.join('buildstream.conf') + _yaml.dump(_yaml.node_sanitize(user_config), filename=user_config_file) + + project_dir = tmpdir.mkdir('project') + project_config_file = project_dir.join('project.conf') + _yaml.dump(_yaml.node_sanitize(project_config), filename=project_config_file) + + context = Context([]) + context.load(config=user_config_file) + project = Project(project_dir, context) + + # Use the helper from the artifactcache module to parse our configuration. + parsed_cache_specs = configured_remote_artifact_cache_specs(context, project) + + # Verify that it was correctly read. + expected_cache_specs = list(_deduplicate(itertools.chain(override_caches, project_caches, user_caches))) + assert parsed_cache_specs == expected_cache_specs |