This is an automated email from the ASF dual-hosted git repository. github-bot pushed a commit to branch jmac/cas_to_cas_oct_v2 in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit ef7c4cdbe7b686eabdacec59950e088091c5fe4c Author: Jim MacArthur <[email protected]> AuthorDate: Fri Oct 5 13:43:49 2018 +0100 virtual_directory_import.py: Add random test generator --- tests/storage/virtual_directory_import.py | 37 ++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/tests/storage/virtual_directory_import.py b/tests/storage/virtual_directory_import.py index b76fef7..2e5163c 100644 --- a/tests/storage/virtual_directory_import.py +++ b/tests/storage/virtual_directory_import.py @@ -1,5 +1,6 @@ import os import pytest +import random from tests.testutils import cli from buildstream.storage import CasBasedDirectory @@ -27,6 +28,7 @@ root_filesets = [ ] empty_hash_ref = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" +RANDOM_SEED = 69105 def generate_import_roots(directory): @@ -48,6 +50,33 @@ def generate_import_roots(directory): os.symlink(content, os.path.join(rootdir, path)) +def generate_random_root(directory): + random.seed(RANDOM_SEED) + rootname = "root6" + rootdir = os.path.join(directory, "content", rootname) + things = [] + locations = ['.'] + for i in range(0, 100): + location = random.choice(locations) + thingname = "node{}".format(i) + thing = random.choice(['dir', 'link', 'file']) + target = os.path.join(rootdir, location, thingname) + if thing == 'dir': + os.makedirs(target) + locations.append(os.path.join(location, thingname)) + elif thing == 'file': + with open(target, "wt") as f: + f.write("This is node {}\n".format(i)) + elif thing == 'link': + # TODO: Make some relative symlinks + if random.randint(1, 3) == 1 or len(things) == 0: + os.symlink("/broken", target) + else: + os.symlink(random.choice(things), target) + things.append(os.path.join(location, thingname)) + print("Generated {}/{} ".format(rootdir, things[-1])) + + def file_contents(path): with open(path, "r") as f: result = f.read() @@ -64,6 +93,7 @@ def create_new_casdir(root_number, fake_context, tmpdir): assert d.ref.hash != empty_hash_ref return d + def create_new_filedir(root_number, tmpdir): root = os.path.join(tmpdir, "vdir") os.makedirs(root) @@ -117,7 +147,7 @@ def test_cas_import(cli, tmpdir, original, overlay): fake_context.artifactdir = tmpdir # Create some fake content generate_import_roots(tmpdir) - + generate_random_root(tmpdir) d = create_new_casdir(original, fake_context, tmpdir) d2 = create_new_casdir(overlay, fake_context, tmpdir) d.import_files(d2) @@ -145,12 +175,13 @@ def test_cas_import(cli, tmpdir, original, overlay): assert os.path.isdir(realpath) [email protected]("root", [1, 2, 3, 4, 5]) [email protected]("root", [1, 2, 3, 4, 5, 6]) def test_directory_listing(cli, tmpdir, root): fake_context = FakeContext() fake_context.artifactdir = tmpdir # Create some fake content generate_import_roots(tmpdir) + generate_random_root(tmpdir) d = create_new_filedir(root, tmpdir) filelist = list(d.list_relative_paths()) @@ -162,4 +193,4 @@ def test_directory_listing(cli, tmpdir, root): print("{}".format(filelist)) print("filelist for root {} via CasBasedDirectory:".format(root)) print("{}".format(filelist2)) - assert(filelist==filelist2) + assert filelist == filelist2
