def test_validate_template(): """Test template validation. It validates each local template. """ with TemporaryDirectory() as tempdir: temppath = Path(tempdir) # file error with raises(errors.InvalidTemplateError): validate_template(temppath) # folder error shutil.rmtree(str(tempdir)) renku_dir = temppath / RENKU_HOME renku_dir.mkdir(parents=True) with raises(errors.InvalidTemplateError): validate_template(temppath) # valid template shutil.rmtree(str(tempdir)) shutil.copytree(str(template_local), str(tempdir)) manifest = read_template_manifest(Path(tempdir)) for template in manifest: template_folder = temppath / template['folder'] assert validate_template(template_folder) is True
def test_read_template_manifest(): """Test reading template manifest file. It creates a fake manifest file and it verifies it's read property. """ with TemporaryDirectory() as tempdir: template_file = Path(tempdir) / TEMPLATE_MANIFEST # error on missing template file with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False) template_file.touch(exist_ok=True) # error on invalid template file with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False) template_file.write_text("-\n" " folder: first\n" " name: Basic Project 1\n" " description: Description 1\n" " variables: {}\n" "-\n" " folder: second\n" " name: Basic Project 2\n" " description: Description 2\n" " variables:\n" " custom: Custom Value\n") manifest = read_template_manifest(Path(tempdir), checkout=False) assert 2 == len(manifest) assert "first" == manifest[0]["folder"] assert "second" == manifest[1]["folder"] assert "Basic Project 1" == manifest[0]["name"] assert "Description 2" == manifest[1]["description"] variables1 = manifest[0]["variables"] variables2 = manifest[1]["variables"] assert 0 == len(variables1) assert 1 == len(variables2) assert 1 == len(variables2.keys()) assert "custom" in variables2.keys() assert "Custom Value" == variables2["custom"] template_file.write_text("-\n" " folder: first\n" " description: Description 1\n") with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False) template_file.write_text("-\n" " name: Basic Project 2\n" " description: Description 2\n") with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False)
def test_read_template_manifest(): """Test reading template manifest file. It creates a fake manifest file and it verifies it's read property. """ with TemporaryDirectory() as tempdir: template_file = Path(tempdir) / TEMPLATE_MANIFEST # error on missing template file with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False) template_file.touch(exist_ok=True) # error on invalid template file with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False) template_file.write_text('-\n' ' folder: first\n' ' name: Basic Project 1\n' ' description: Description 1\n' ' variables: {}\n' '-\n' ' folder: second\n' ' name: Basic Project 2\n' ' description: Description 2\n' ' variables:\n' ' custom: Custom Value\n') manifest = read_template_manifest(Path(tempdir), checkout=False) assert 2 == len(manifest) assert 'first' == manifest[0]['folder'] assert 'second' == manifest[1]['folder'] assert 'Basic Project 1' == manifest[0]['name'] assert 'Description 2' == manifest[1]['description'] variables1 = manifest[0]['variables'] variables2 = manifest[1]['variables'] assert 0 == len(variables1) assert 1 == len(variables2) assert 1 == len(variables2.keys()) assert 'custom' in variables2.keys() assert 'Custom Value' == variables2['custom'] template_file.write_text('-\n' ' folder: first\n' ' description: Description 1\n') with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False) template_file.write_text('-\n' ' name: Basic Project 2\n' ' description: Description 2\n') with raises(errors.InvalidTemplateError): manifest = read_template_manifest(Path(tempdir), checkout=False)
def test_data_add(scheme, path, overwrite, error, client, data_file, directory_tree, dataset_responses): """Test data import.""" with raises(error): if path == "temp": path = str(data_file) elif path == "tempdir": path = str(directory_tree) with client.with_dataset("dataset", create=True) as d: d.creators = [Person(name="me", email="*****@*****.**", id="me_id")] client.add_data_to_dataset(d, ["{}{}".format(scheme, path)], overwrite=overwrite) target_path = os.path.join(DATA_DIR, "dataset", "file") with open(target_path) as f: assert f.read() == "1234" assert d.find_file(target_path) # check that the imported file is read-only assert not os.access(target_path, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) # check the linking if scheme in ("", "file://"): shutil.rmtree("./data/dataset") with client.with_dataset("dataset") as d: d.creators = [Person(name="me", email="*****@*****.**", id="me_id")] client.add_data_to_dataset(d, ["{}{}".format(scheme, path)], overwrite=True) assert os.path.exists(target_path)
def test_fetch_template(template): """Test fetching a template. It fetches a template from remote and verifies that the manifest file is there. """ # ? Can't parametrize with fixtures yet # ? REF: https://docs.pytest.org/en/stable/proposals/parametrize_with_fixtures.html parametrize = [ { "url": template["url"], "ref": template["ref"], "error": None }, { "url": "fake", "ref": template["ref"], "error": errors.GitError }, { "url": template["url"], "ref": "fake", "error": errors.GitError }, ] for parameters in parametrize: with TemporaryDirectory() as tempdir: with raises(parameters["error"]): manifest_file = fetch_template(parameters["url"], parameters["ref"], Path(tempdir)) assert Path(tempdir) / TEMPLATE_MANIFEST == manifest_file assert manifest_file.exists()
def test_parse_parameters(): def clean_param(p): return [v for v in p if v != '--parameter'] parsed = parse_parameters(None, None, clean_param(PARAMETERS_CORRECT)) keys = parsed.keys() assert 2 == len(keys) assert 'p1' in keys assert 'p2' in keys assert 'v1' == parsed['p1'] assert 'v2' == parsed['p2'] with raises(errors.ParameterError): parsed = parse_parameters( None, None, clean_param(PARAMETERS_CORRECT) + clean_param(PARAMETER_NO_EQUAL)) with raises(errors.ParameterError): parsed = parse_parameters( None, None, clean_param(PARAMETERS_CORRECT) + clean_param(PARAMETER_EARLY_EQUAL))
def test_fetch_template(url, ref, result, error): """Test fetching a template. It fetches a template from remote and verifies that the manifest file is there. """ with TemporaryDirectory() as tempdir: with raises(error): manifest_file = fetch_template(url, ref, Path(tempdir)) assert Path(tempdir) / TEMPLATE_MANIFEST == manifest_file assert manifest_file.exists()
def test_parse_parameters(project_init): def clean_param(p): return [v for v in p if v != "--parameter"] data, commands = project_init parsed = parse_parameters(None, None, clean_param(commands["parameters"])) keys = parsed.keys() assert 2 == len(keys) assert "p1" in keys assert "p2" in keys assert "v1" == parsed["p1"] assert "v2" == parsed["p2"] with raises(errors.ParameterError): parse_parameters( None, None, clean_param(commands["parameters"]) + clean_param(commands["parameters_equal_missing"])) with raises(errors.ParameterError): parse_parameters( None, None, clean_param(commands["parameters"]) + clean_param(commands["parameters_equal_early"]))
def test_data_add(scheme, path, force, error, client, data_file, directory_tree, dataset_responses): """Test data import.""" with raises(error): if path == 'temp': path = str(data_file) elif path == 'tempdir': path = str(directory_tree) with client.with_dataset('dataset', create=True) as d: d.creator = [{ 'name': 'me', 'email': '*****@*****.**', 'identifier': 'me_id' }] client.add_data_to_dataset(d, ['{}{}'.format(scheme, path)], force=force) with open('data/dataset/file') as f: assert f.read() == '1234' assert d.find_file('data/dataset/file') # check that the imported file is read-only assert not os.access('data/dataset/file', stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) # check the linking if scheme in ('', 'file://'): shutil.rmtree('./data/dataset') with client.with_dataset('dataset') as d: d.creator = [{ 'name': 'me', 'email': '*****@*****.**', 'identifier': 'me_id' }] client.add_data_to_dataset(d, ['{}{}'.format(scheme, path)], force=True) assert os.path.exists('data/dataset/file')
def test_data_add(scheme, path, overwrite, error, client, data_file, directory_tree, dataset_responses): """Test data import.""" with raises(error): if path == 'temp': path = str(data_file) elif path == 'tempdir': path = str(directory_tree) with client.with_dataset('dataset', create=True) as d: d.creator = [Person(name='me', email='*****@*****.**', id='me_id')] client.add_data_to_dataset(d, ['{}{}'.format(scheme, path)], overwrite=overwrite) target_path = os.path.join(DATA_DIR, 'dataset', 'file') with open(target_path) as f: assert f.read() == '1234' assert d.find_file(target_path) # check that the imported file is read-only assert not os.access(target_path, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) # check the linking if scheme in ('', 'file://'): shutil.rmtree('./data/dataset') with client.with_dataset('dataset') as d: d.creator = [ Person(name='me', email='*****@*****.**', id='me_id') ] client.add_data_to_dataset(d, ['{}{}'.format(scheme, path)], overwrite=True) assert os.path.exists(target_path)