diff options
author | Tristan Maat <tm@tlater.net> | 2018-03-16 13:28:20 +0000 |
---|---|---|
committer | Tristan Maat <tm@tlater.net> | 2018-03-16 15:05:36 +0000 |
commit | 81ea9a80e666bae3cc7ebd9886d63dc18fd2f70a (patch) | |
tree | bce252d266514ca8cf5bd69b1e460a6b0cdcb170 | |
parent | 33d56425963609e536f6f9d0458eecc737600a6f (diff) | |
download | buildstream-81ea9a80e666bae3cc7ebd9886d63dc18fd2f70a.tar.gz |
frontend/workspace.py: Add tests for loading more versions
-rw-r--r-- | tests/frontend/workspace.py | 137 |
1 files changed, 106 insertions, 31 deletions
diff --git a/tests/frontend/workspace.py b/tests/frontend/workspace.py index ecda49c51..39f1f08a9 100644 --- a/tests/frontend/workspace.py +++ b/tests/frontend/workspace.py @@ -5,7 +5,7 @@ import subprocess from tests.testutils import cli, create_repo, ALL_REPO_KINDS from buildstream import _yaml -from buildstream._exceptions import ErrorDomain, LoadErrorReason +from buildstream._exceptions import ErrorDomain, LoadError, LoadErrorReason repo_kinds = [(kind) for kind in ALL_REPO_KINDS] @@ -213,26 +213,82 @@ def test_build(cli, tmpdir, datafiles, kind): assert not os.path.exists(os.path.join(workspace, 'usr', 'bin', 'hello')) +# Ensure that various versions that should not be accepted raise a +# LoadError.INVALID_DATA @pytest.mark.datafiles(DATA_DIR) -def test_open_old_format(cli, tmpdir, datafiles): +@pytest.mark.parametrize("workspace_cfg", [ + # Test loading a negative workspace version + {"version": -1}, + # Test loading version 0 with two sources + { + "version": 0, + "alpha.bst": { + 0: "/workspaces/bravo", + 1: "/workspaces/charlie", + } + }, + # Test loading a version with decimals + {"version": 0.5}, + # Test loading a future version + {"version": 2} +]) +def test_list_unsupported_workspace(cli, tmpdir, datafiles, workspace_cfg): project = os.path.join(datafiles.dirname, datafiles.basename) + bin_files_path = os.path.join(project, 'files', 'bin-files') + element_path = os.path.join(project, 'elements') + element_name = 'workspace-version.bst' os.makedirs(os.path.join(project, '.bst')) workspace_config_path = os.path.join(project, '.bst', 'workspaces.yml') - workspace_dict_old = { + _yaml.dump(workspace_cfg, workspace_config_path) + + result = cli.run(project=project, args=['workspace', 'list']) + result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA) + + +# Ensure that various versions that should be accepted are parsed +# correctly. +@pytest.mark.datafiles(DATA_DIR) +@pytest.mark.parametrize("workspace_cfg,expected", [ + # Test loading version 0 without a dict + ({ + "alpha.bst": "/workspaces/bravo" + }, { + "version": '1', "alpha.bst": { - 0: "/workspaces/bravo", + "path": "/workspaces/bravo" } - } - - workspace_dict_expected = { + }), + # Test loading version 0 with only one source + ({ + "alpha.bst": { + 0: "/workspaces/bravo" + } + }, { + "version": '1', + "alpha.bst": { + "path": "/workspaces/bravo" + } + }), + # Test loading version 1 + ({ "version": 1, "alpha.bst": { - "path": "/workspaces/bravo", - }, - } + "path": "/workspaces/bravo" + } + }, { + "version": '1', + "alpha.bst": { + "path": "/workspaces/bravo" + } + }) +]) +def test_list_supported_workspace(cli, tmpdir, datafiles, workspace_cfg, expected): + project = os.path.join(datafiles.dirname, datafiles.basename) + os.makedirs(os.path.join(project, '.bst')) + workspace_config_path = os.path.join(project, '.bst', 'workspaces.yml') - _yaml.dump(workspace_dict_old, workspace_config_path) + _yaml.dump(workspace_cfg, workspace_config_path) # Check that we can still read workspace config that is in old format result = cli.run(project=project, args=['workspace', 'list']) @@ -242,31 +298,50 @@ def test_open_old_format(cli, tmpdir, datafiles): # Check that workspace config has been converted to the new format after # running a workspace command - elements = list(_yaml.node_items(loaded_config)) - assert len(elements) == len(workspace_dict_expected) - for element, config in workspace_dict_expected.items(): - if element == "version": - continue - assert config["path"] == _yaml.node_get(loaded_config[element], str, "path") - assert workspace_dict_expected["version"] == _yaml.node_get(loaded_config, int, "version") + def commentmap_to_dict(node): + ret = {} + + for key, val in _yaml.node_items(node): + try: + ret[key] = commentmap_to_dict(val) + except AttributeError: + ret[key] = val + + return ret + + assert commentmap_to_dict(loaded_config) == expected @pytest.mark.datafiles(DATA_DIR) -def test_open_old_format_fail(cli, tmpdir, datafiles): +def test_open_version_element(cli, tmpdir, datafiles): project = os.path.join(datafiles.dirname, datafiles.basename) - os.makedirs(os.path.join(project, '.bst')) - workspace_config_path = os.path.join(project, '.bst', 'workspaces.yml') + bin_files_path = os.path.join(project, 'files', 'bin-files') + element_path = os.path.join(project, 'elements') + element_name = 'version' + workspace = os.path.join(str(tmpdir), 'workspace') - workspace_dict_old = { - "alpha.bst": { - 0: "/workspaces/bravo", - 1: "/workspaces/charlie", - } - } + # Create our repo object of the given source type with + # the bin files, and then collect the initial ref. + # + repo = create_repo('git', str(tmpdir)) + ref = repo.create(bin_files_path) - _yaml.dump(workspace_dict_old, workspace_config_path) + # Write out our test target + element = { + 'kind': 'import', + 'sources': [ + repo.source_config(ref=ref) + ] + } + _yaml.dump(element, + os.path.join(element_path, + element_name)) - # Check that trying to load workspace config in old format raises an error - # if there are workspaces open for more than one source for an element - result = cli.run(project=project, args=['workspace', 'list']) + # Ensure that, when we open a workspace on an element named + # 'version', buildstream doesn't break + result = cli.run(project=project, args=['workspace', 'open', element_name, workspace]) result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA) + + # No additional testing should be required here, since the user + # should never be able to open a workspace named 'version' in the + # first place. |