From 0710a1529a7af7e33179c71a30d362dd46281d3f Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Fri, 8 Aug 2014 09:58:59 +0000 Subject: unittests: Make the unittests use morphloader This commit removes all use of morph2 from the unittests, replacing it with morphloader/morph3. It also converts the test morphologies to YAML. --- morphlib/artifact_tests.py | 34 ++-- morphlib/artifactresolver_tests.py | 322 ++++++++++++++++------------------ morphlib/cachekeycomputer_tests.py | 118 ++++++------- morphlib/localartifactcache_tests.py | 30 ++-- morphlib/remoteartifactcache_tests.py | 36 ++-- morphlib/source_tests.py | 11 +- 6 files changed, 254 insertions(+), 297 deletions(-) diff --git a/morphlib/artifact_tests.py b/morphlib/artifact_tests.py index d4b15cba..62b1bfb9 100644 --- a/morphlib/artifact_tests.py +++ b/morphlib/artifact_tests.py @@ -23,24 +23,22 @@ import morphlib class ArtifactTests(unittest.TestCase): def setUp(self): - morph = morphlib.morph2.Morphology( - ''' - { - "name": "chunk", - "kind": "chunk", - "chunks": { - "chunk-runtime": [ - "usr/bin", - "usr/sbin", - "usr/lib", - "usr/libexec" - ], - "chunk-devel": [ - "usr/include" - ] - } - } - ''') + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( + ''' + name: chunk + kind: chunk + products: + - artifact: chunk-runtime + include: + - usr/bin + - usr/sbin + - usr/lib + - usr/libexec + - artifact: chunk-devel + include: + - usr/include + ''') self.source = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') self.artifact_name = 'chunk-runtime' diff --git a/morphlib/artifactresolver_tests.py b/morphlib/artifactresolver_tests.py index 6f62b4d1..96f7ced8 100644 --- a/morphlib/artifactresolver_tests.py +++ b/morphlib/artifactresolver_tests.py @@ -15,84 +15,68 @@ import itertools -import json import unittest +import yaml import morphlib -class FakeChunkMorphology(morphlib.morph2.Morphology): - - def __init__(self, name, artifact_names=[]): - assert(isinstance(artifact_names, list)) - - if artifact_names: - # fake a list of artifacts - artifacts = [] - for artifact_name in artifact_names: - artifacts.append({'artifact': artifact_name, - 'include': artifact_name}) - text = json.dumps({ - "name": name, - "kind": "chunk", - "products": artifacts - }) - self.builds_artifacts = artifact_names - else: - text = (''' - { - "name": "%s", - "kind": "chunk" - } - ''' % name) - self.builds_artifacts = [name] - morphlib.morph2.Morphology.__init__(self, text) - - -class FakeStratumMorphology(morphlib.morph2.Morphology): - - def __init__(self, name, chunks=[], build_depends=[]): - assert(isinstance(chunks, list)) - assert(isinstance(build_depends, list)) - - chunks_list = [] - for source_name, morph, repo, ref in chunks: - chunks_list.append({ - 'name': source_name, - 'morph': morph, - 'repo': repo, - 'ref': ref, - 'build-depends': [], - }) - build_depends_list = [] - for morph, repo, ref in build_depends: - build_depends_list.append({ - 'morph': morph, - 'repo': repo, - 'ref': ref - }) - if chunks_list: - text = (''' - { - "name": "%s", - "kind": "stratum", - "build-depends": %s, - "chunks": %s - } - ''' % (name, - json.dumps(build_depends_list), - json.dumps(chunks_list))) - else: - text = (''' - { - "name": "%s", - "kind": "stratum", - "build-depends": %s - } - ''' % (name, - json.dumps(build_depends_list))) - self.builds_artifacts = [name] - morphlib.morph2.Morphology.__init__(self, text) +def get_chunk_morphology(name, artifact_names=[]): + assert(isinstance(artifact_names, list)) + + if artifact_names: + # fake a list of artifacts + artifacts = [] + for artifact_name in artifact_names: + artifacts.append({'artifact': artifact_name, + 'include': [artifact_name]}) + text = yaml.dump({"name": name, + "kind": "chunk", + "products": artifacts}, default_flow_style=False) + builds_artifacts = artifact_names + else: + text = yaml.dump({'name': name, + 'kind': 'chunk'}, default_flow_style=False) + builds_artifacts = [name] + + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string(text) + morph.builds_artifacts = builds_artifacts + return morph + +def get_stratum_morphology(name, chunks=[], build_depends=[]): + assert(isinstance(chunks, list)) + assert(isinstance(build_depends, list)) + + chunks_list = [] + for source_name, morph, repo, ref in chunks: + chunks_list.append({ + 'name': source_name, + 'morph': morph, + 'repo': repo, + 'ref': ref, + 'build-depends': [], + }) + build_depends_list = [] + for morph in build_depends: + build_depends_list.append({ + 'morph': morph, + }) + if chunks_list: + text = yaml.dump({"name": name, + "kind": "stratum", + "build-depends": build_depends_list, + "chunks": chunks_list,}, default_flow_style=False) + else: + text = yaml.dump({"name": name, + "kind": "stratum", + "build-depends": build_depends_list}, + default_flow_style=False) + + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string(text) + morph.builds_artifacts = [name] + return morph class ArtifactResolverTests(unittest.TestCase): @@ -108,7 +92,7 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolve_single_chunk_with_no_subartifacts(self): pool = morphlib.sourcepool.SourcePool() - morph = FakeChunkMorphology('chunk') + morph = get_chunk_morphology('chunk') source = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') pool.add(source) @@ -127,7 +111,7 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolve_single_chunk_with_one_new_artifact(self): pool = morphlib.sourcepool.SourcePool() - morph = FakeChunkMorphology('chunk', ['chunk-foobar']) + morph = get_chunk_morphology('chunk', ['chunk-foobar']) source = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') pool.add(source) @@ -145,7 +129,7 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolve_single_chunk_with_two_new_artifacts(self): pool = morphlib.sourcepool.SourcePool() - morph = FakeChunkMorphology('chunk', ['chunk-baz', 'chunk-qux']) + morph = get_chunk_morphology('chunk', ['chunk-baz', 'chunk-qux']) source = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') pool.add(source) @@ -165,12 +149,12 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolve_stratum_and_chunk(self): pool = morphlib.sourcepool.SourcePool() - morph = FakeChunkMorphology('chunk') + morph = get_chunk_morphology('chunk') chunk = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') pool.add(chunk) - morph = FakeStratumMorphology( + morph = get_stratum_morphology( 'stratum', chunks=[('chunk', 'chunk', 'repo', 'ref')]) stratum = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'stratum.morph') @@ -199,12 +183,12 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolve_stratum_and_chunk_with_two_new_artifacts(self): pool = morphlib.sourcepool.SourcePool() - morph = FakeChunkMorphology('chunk', ['chunk-foo', 'chunk-bar']) + morph = get_chunk_morphology('chunk', ['chunk-foo', 'chunk-bar']) chunk = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') pool.add(chunk) - morph = FakeStratumMorphology( + morph = get_stratum_morphology( 'stratum', chunks=[ ('chunk', 'chunk', 'repo', 'ref'), @@ -236,51 +220,42 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolving_artifacts_for_a_system_with_two_dependent_strata(self): pool = morphlib.sourcepool.SourcePool() - morph = FakeChunkMorphology('chunk1') + morph = get_chunk_morphology('chunk1') chunk1 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk1.morph') pool.add(chunk1) - morph = FakeStratumMorphology( + morph = get_stratum_morphology( 'stratum1', chunks=[('chunk1', 'chunk1', 'repo', 'original/ref')]) stratum1 = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'stratum1.morph') pool.add(stratum1) - morph = morphlib.morph2.Morphology( + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( ''' - { - "name": "system", - "kind": "system", - "strata": [ - { - "repo": "repo", - "ref": "ref", - "morph": "stratum1" - }, - { - "repo": "repo", - "ref": "ref", - "morph": "stratum2" - } - ] - } + name: system + kind: system + arch: testarch + strata: + - morph: stratum1 + - morph: stratum2 ''') morph.builds_artifacts = ['system-rootfs'] system = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'system.morph') pool.add(system) - morph = FakeChunkMorphology('chunk2') + morph = get_chunk_morphology('chunk2') chunk2 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk2.morph') pool.add(chunk2) - morph = FakeStratumMorphology( + morph = get_stratum_morphology( 'stratum2', chunks=[('chunk2', 'chunk2', 'repo', 'original/ref')], - build_depends=[('stratum1', 'repo', 'ref')]) + build_depends=['stratum1']) stratum2 = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'stratum2.morph') pool.add(stratum2) @@ -337,52 +312,44 @@ class ArtifactResolverTests(unittest.TestCase): def test_resolving_stratum_with_explicit_chunk_dependencies(self): pool = morphlib.sourcepool.SourcePool() - morph = morphlib.morph2.Morphology( + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( ''' - { - "name": "stratum", - "kind": "stratum", - "chunks": [ - { - "name": "chunk1", - "repo": "repo", - "ref": "original/ref", - "build-depends": [] - }, - { - "name": "chunk2", - "repo": "repo", - "ref": "original/ref", - "build-depends": [] - }, - { - "name": "chunk3", - "repo": "repo", - "ref": "original/ref", - "build-depends": [ - "chunk1", - "chunk2" - ] - } - ] - } + name: stratum + kind: stratum + build-depends: [] + chunks: + - name: chunk1 + repo: repo + ref: original/ref + build-depends: [] + - name: chunk2 + repo: repo + ref: original/ref + build-depends: [] + - name: chunk3 + repo: repo + ref: original/ref + build-depends: + - chunk1 + - chunk2 ''') morph.builds_artifacts = ['stratum'] stratum = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'stratum.morph') pool.add(stratum) - morph = FakeChunkMorphology('chunk1') + morph = get_chunk_morphology('chunk1') chunk1 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk1.morph') pool.add(chunk1) - morph = FakeChunkMorphology('chunk2') + morph = get_chunk_morphology('chunk2') chunk2 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk2.morph') pool.add(chunk2) - morph = FakeChunkMorphology('chunk3') + morph = get_chunk_morphology('chunk3') chunk3 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk3.morph') pool.add(chunk3) @@ -418,20 +385,33 @@ class ArtifactResolverTests(unittest.TestCase): for c3a in chunk_artifacts[2])) def test_detection_of_mutual_dependency_between_two_strata(self): + loader = morphlib.morphloader.MorphologyLoader() pool = morphlib.sourcepool.SourcePool() - morph = FakeStratumMorphology( + chunk = get_chunk_morphology('chunk1') + chunk1 = morphlib.source.Source( + 'repo', 'original/ref', 'sha1', 'tree', chunk, 'chunk1.morph') + pool.add(chunk1) + + morph = get_stratum_morphology( 'stratum1', - chunks=[], - build_depends=[('stratum2', 'repo', 'original/ref')]) + chunks=[(loader.save_to_string(chunk), 'chunk1.morph', + 'repo', 'original/ref')], + build_depends=['stratum2']) stratum1 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'stratum1.morph') pool.add(stratum1) - morph = FakeStratumMorphology( + chunk = get_chunk_morphology('chunk2') + chunk2 = morphlib.source.Source( + 'repo', 'original/ref', 'sha1', 'tree', chunk, 'chunk2.morph') + pool.add(chunk2) + + morph = get_stratum_morphology( 'stratum2', - chunks=[], - build_depends=[('stratum1', 'repo', 'original/ref')]) + chunks=[(loader.save_to_string(chunk), 'chunk2.morph', + 'repo', 'original/ref')], + build_depends=['stratum1']) stratum2 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'stratum2.morph') pool.add(stratum2) @@ -442,39 +422,34 @@ class ArtifactResolverTests(unittest.TestCase): def test_detection_of_chunk_dependencies_in_invalid_order(self): pool = morphlib.sourcepool.SourcePool() - morph = morphlib.morph2.Morphology( + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( ''' - { - "name": "stratum", - "kind": "stratum", - "chunks": [ - { - "name": "chunk1", - "repo": "repo", - "ref": "original/ref", - "build-depends": [ - "chunk2" - ] - }, - { - "name": "chunk2", - "repo": "repo", - "ref": "original/ref" - } - ] - } + name: stratum + kind: stratum + build-depends: [] + chunks: + - name: chunk1 + repo: repo + ref: original/ref + build-depends: + - chunk2 + - name: chunk2 + repo: repo + ref: original/ref + build-depends: [] ''') morph.builds_artifacts = ['stratum'] stratum = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'stratum.morph') pool.add(stratum) - morph = FakeChunkMorphology('chunk1') + morph = get_chunk_morphology('chunk1') chunk1 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk1.morph') pool.add(chunk1) - morph = FakeChunkMorphology('chunk2') + morph = get_chunk_morphology('chunk2') chunk2 = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk2.morph') pool.add(chunk2) @@ -485,27 +460,24 @@ class ArtifactResolverTests(unittest.TestCase): def test_detection_of_invalid_build_depends_format(self): pool = morphlib.sourcepool.SourcePool() - morph = morphlib.morph2.Morphology( + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( ''' - { - "name": "stratum", - "kind": "stratum", - "chunks": [ - { - "name": "chunk", - "repo": "repo", - "ref": "original/ref", - "build-depends": "whatever" - } - ] - } + name: stratum + kind: stratum + build-depends: [] + chunks: + - name: chunk + repo: repo + ref: original/ref + build-depends: whatever ''') morph.builds_artifacts = ['stratum'] stratum = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'stratum.morph') pool.add(stratum) - morph = FakeChunkMorphology('chunk') + morph = get_chunk_morphology('chunk') chunk = morphlib.source.Source( 'repo', 'original/ref', 'sha1', 'tree', morph, 'chunk.morph') pool.add(chunk) diff --git a/morphlib/cachekeycomputer_tests.py b/morphlib/cachekeycomputer_tests.py index 9e18b19d..8558db6d 100644 --- a/morphlib/cachekeycomputer_tests.py +++ b/morphlib/cachekeycomputer_tests.py @@ -32,73 +32,60 @@ class DummyBuildEnvironment: class CacheKeyComputerTests(unittest.TestCase): def setUp(self): + loader = morphlib.morphloader.MorphologyLoader() self.source_pool = morphlib.sourcepool.SourcePool() for name, text in { - 'chunk.morph': '''{ - "name": "chunk", - "kind": "chunk", - "description": "A test chunk" - }''', - 'chunk2.morph': '''{ - "name": "chunk2", - "kind": "chunk", - "description": "A test chunk" - }''', - 'chunk3.morph': '''{ - "name": "chunk3", - "kind": "chunk", - "description": "A test chunk" - }''', - 'stratum.morph': '''{ - "name": "stratum", - "kind": "stratum", - "chunks": [ - { - "name": "chunk", - "repo": "repo", - "ref": "original/ref", - "build-depends": [] - } - ] - }''', - 'stratum2.morph': '''{ - "name": "stratum2", - "kind": "stratum", - "chunks": [ - { - "name": "chunk2", - "repo": "repo", - "ref": "original/ref", - "build-depends": [] - }, - { - "name": "chunk3", - "repo": "repo", - "ref": "original/ref", - "build-depends": [] - } - ] - }''', - 'system.morph': '''{ - "name": "system", - "kind": "system", - "strata": [ - { - "morph": "stratum", - "repo": "repo", - "ref": "original/ref" - }, - { - "morph": "stratum2", - "repo": "repo", - "ref": "original/ref" - } - ] - }''', + 'chunk.morph': ''' + name: chunk + kind: chunk + description: A test chunk + ''', + 'chunk2.morph': ''' + name: chunk2 + kind: chunk + description: A test chunk + ''', + 'chunk3.morph': ''' + name: chunk3 + kind: chunk + description: A test chunk + ''', + 'stratum.morph': ''' + name: stratum + kind: stratum + build-depends: [] + chunks: + - name: chunk + repo: repo + ref: original/ref + build-depends: [] + ''', + 'stratum2.morph': ''' + name: stratum2 + kind: stratum + build-depends: [] + chunks: + - name: chunk2 + repo: repo + ref: original/ref + build-depends: [] + - name: chunk3 + repo: repo + ref: original/ref + build-depends: [] + ''', + 'system.morph': ''' + name: system + kind: system + arch: testarch + strata: + - morph: stratum + - morph: stratum2 + ''', }.iteritems(): source = morphlib.source.Source( 'repo', 'original/ref', 'sha', 'tree', - morphlib.morph2.Morphology(text), name) + loader.load_from_string(text), name) self.source_pool.add(source) # FIXME: This should use MorphologyFactory m = source.morphology @@ -202,7 +189,12 @@ class CacheKeyComputerTests(unittest.TestCase): self.assertEqual(old_sha, new_sha) def test_same_morphology_added_to_source_pool_only_appears_once(self): - m = morphlib.morph2.Morphology('{"name": "chunk", "kind": "chunk"}') + loader = morphlib.morphloader.MorphologyLoader() + m = loader.load_from_string( + ''' + name: chunk + kind: chunk + ''') src = morphlib.source.Source('repo', 'original/ref', 'sha', 'tree', m, 'chunk.morph') sp = morphlib.sourcepool.SourcePool() diff --git a/morphlib/localartifactcache_tests.py b/morphlib/localartifactcache_tests.py index f400a645..6283c833 100644 --- a/morphlib/localartifactcache_tests.py +++ b/morphlib/localartifactcache_tests.py @@ -27,23 +27,21 @@ class LocalArtifactCacheTests(unittest.TestCase): def setUp(self): self.tempfs = fs.tempfs.TempFS() - morph = morphlib.morph2.Morphology( + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( ''' - { - "name": "chunk", - "kind": "chunk", - "artifacts": { - "chunk-runtime": [ - "usr/bin", - "usr/sbin", - "usr/lib", - "usr/libexec" - ], - "chunk-devel": [ - "usr/include" - ] - } - } + name: chunk + kind: chunk + products: + - artifact: chunk-runtime + include: + - usr/bin + - usr/sbin + - usr/lib + - usr/libexec + - artifact: chunk-devel + include: + - usr/include ''') self.source = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') diff --git a/morphlib/remoteartifactcache_tests.py b/morphlib/remoteartifactcache_tests.py index d11bf264..ca959ebf 100644 --- a/morphlib/remoteartifactcache_tests.py +++ b/morphlib/remoteartifactcache_tests.py @@ -24,26 +24,24 @@ import morphlib class RemoteArtifactCacheTests(unittest.TestCase): def setUp(self): - morph = morphlib.morph2.Morphology( + loader = morphlib.morphloader.MorphologyLoader() + morph = loader.load_from_string( ''' - { - "name": "chunk", - "kind": "chunk", - "artifacts": { - "chunk-runtime": [ - "usr/bin", - "usr/sbin", - "usr/lib", - "usr/libexec" - ], - "chunk-devel": [ - "usr/include" - ], - "chunk-doc": [ - "usr/share/doc" - ] - } - } + name: chunk + kind: chunk + products: + - artifact: chunk-runtime + include: + - usr/bin + - usr/sbin + - usr/lib + - usr/libexec + - artifact: chunk-devel + include: + - usr/include + - artifact: chunk-doc + include: + - usr/share/doc ''') self.source = morphlib.source.Source( 'repo', 'ref', 'sha1', 'tree', morph, 'chunk.morph') diff --git a/morphlib/source_tests.py b/morphlib/source_tests.py index 6643f0fc..f5ce5d4d 100644 --- a/morphlib/source_tests.py +++ b/morphlib/source_tests.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012 Codethink Limited +# Copyright (C) 2012-2014 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 @@ -22,10 +22,8 @@ import morphlib class SourceTests(unittest.TestCase): morphology_text = ''' - { - "name": "foo", - "kind": "chunk" - } + name: foo + kind: chunk ''' def setUp(self): @@ -33,7 +31,8 @@ class SourceTests(unittest.TestCase): self.original_ref = 'original/ref' self.sha1 = 'CAFEF00D' self.tree = 'F000000D' - self.morphology = morphlib.morph2.Morphology(self.morphology_text) + loader = morphlib.morphloader.MorphologyLoader() + self.morphology = loader.load_from_string(self.morphology_text) self.filename = 'foo.morph' self.source = morphlib.source.Source( self.repo_name, self.original_ref, self.sha1, self.tree, -- cgit v1.2.1