diff options
author | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2015-05-05 15:27:43 +0000 |
---|---|---|
committer | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2015-05-12 12:26:10 +0000 |
commit | 9cc12c9cd75ad98d18cbd9c277cd31bf5ed55adf (patch) | |
tree | 86aa234acdfbaca28394d16c88d2f1fda95f2b96 /distbuild/artifact_reference_tests.py | |
parent | 2d19611ace78aec071607fafd0e2798412cc4286 (diff) | |
download | morph-9cc12c9cd75ad98d18cbd9c277cd31bf5ed55adf.tar.gz |
Clean up artifact serialisation
We no longer serialise whole artifacts, so it doesn't make sense
for things to still refer to serialise-artifact and similar.
Change-Id: Id4d563a07041bbce77f13ac71dc3f7de39df5e23
Diffstat (limited to 'distbuild/artifact_reference_tests.py')
-rw-r--r-- | distbuild/artifact_reference_tests.py | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/distbuild/artifact_reference_tests.py b/distbuild/artifact_reference_tests.py new file mode 100644 index 00000000..e21918a1 --- /dev/null +++ b/distbuild/artifact_reference_tests.py @@ -0,0 +1,126 @@ +# distbuild/artifact_reference_tests.py -- unit tests for Artifact encoding +# +# Copyright (C) 2012, 2014-2015 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see <http://www.gnu.org/licenses/>. + + +import unittest + +import distbuild + + +class MockSource(object): + + build_mode = 'staging' + prefix = '/usr' + def __init__(self, name, kind): + self.name = name + self.repo = None + self.repo_name = '%s.source.repo_name' % name + self.original_ref = '%s.source.original_ref' % name + self.sha1 = '%s.source.sha1' % name + self.tree = '%s.source.tree' % name + self.morphology = {'kind': kind} + self.filename = '%s.source.filename' % name + self.dependencies = [] + self.cache_id = { + 'blip': '%s.blip' % name, + 'integer': 42, + } + self.cache_key = '%s.cache_key' % name + self.artifacts = {} + + +class MockArtifact(object): + + arch = 'testarch' + + def __init__(self, name, kind): + self.source = MockSource(name, kind) + self.source.artifacts = {name: self} + self.name = name + self.dependents = [] + + def basename(self): + return '%s.%s.%s' % (self.source.cache_key, + self.source.morphology['kind'], + self.name) + + def walk(self): # pragma: no cover + done = set() + + def depth_first(a): + if a not in done: + done.add(a) + for dep in a.source.dependencies: + for ret in depth_first(dep): + yield ret + yield a + + return list(depth_first(self)) + + +class ArtifactReferenceTests(unittest.TestCase): + + def setUp(self): + self.art1 = MockArtifact('name1', 'stratum') + self.art2 = MockArtifact('name2', 'chunk') + self.art3 = MockArtifact('name3', 'chunk') + self.art4 = MockArtifact('name4', 'chunk') + + def verify_round_trip(self, artifact): + encoded = distbuild.encode_artifact(artifact, + artifact.source.repo_name, + artifact.source.sha1) + decoded = distbuild.decode_artifact_reference(encoded) + self.assertEqual(artifact.basename(), decoded.basename()) + + objs = {} + queue = [decoded] + while queue: + obj = queue.pop() + k = obj.cache_key + if k in objs: + self.assertTrue(obj is objs[k]) + else: + objs[k] = obj + queue.extend(obj.dependencies) + + def test_returns_string(self): + encoded = distbuild.encode_artifact(self.art1, + self.art1.source.repo_name, + self.art1.source.sha1) + self.assertEqual(type(encoded), str) + + def test_works_without_dependencies(self): + self.verify_round_trip(self.art1) + + def test_works_with_single_dependency(self): + self.art1.source.dependencies = [self.art2] + self.verify_round_trip(self.art1) + + def test_works_with_two_dependencies(self): + self.art1.source.dependencies = [self.art2, self.art3] + self.verify_round_trip(self.art1) + + def test_works_with_two_levels_of_dependencies(self): + self.art2.source.dependencies = [self.art4] + self.art1.source.dependencies = [self.art2, self.art3] + self.verify_round_trip(self.art1) + + def test_works_with_dag(self): + self.art2.source.dependencies = [self.art4] + self.art3.source.dependencies = [self.art4] + self.art1.source.dependencies = [self.art2, self.art3] + self.verify_round_trip(self.art1) |