Commit 4a873b9f authored by Phil Dawson's avatar Phil Dawson

tests: move templated tests from workspace.py into separate file

Move the templated tests in tests/frontend/workspace.py into
sources/generic/workspace.py

This is part of the preparation work for #944
parent 44cd48f6
Pipeline #51813933 passed with stages
in 38 minutes and 21 seconds
......@@ -169,12 +169,6 @@ def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir
return element_name, workspace_object.project_path, workspace
@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("kind", repo_kinds)
def test_open(cli, tmpdir, datafiles, kind):
open_workspace(cli, tmpdir, datafiles, kind, False)
@pytest.mark.datafiles(DATA_DIR)
def test_open_bzr_customize(cli, tmpdir, datafiles):
element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "bzr", False)
......
# elements/base.bst
kind: stack
depends:
- base/base-alpine.bst
kind: import
description: |
Alpine Linux base for tests
Generated using the `tests/integration-tests/base/generate-base.sh` script.
sources:
- kind: tar
base-dir: ''
(?):
- arch == "x86-64":
ref: 3eb559250ba82b64a68d86d0636a6b127aa5f6d25d3601a79f79214dc9703639
url: "alpine:integration-tests-base.v1.x86_64.tar.xz"
- arch == "aarch64":
ref: 431fb5362032ede6f172e70a3258354a8fd71fcbdeb1edebc0e20968c792329a
url: "alpine:integration-tests-base.v1.aarch64.tar.xz"
#
# Copyright (C) 2018 Codethink Limited
# Copyright (C) 2019 Bloomberg Finance LP
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
import os
import shutil
import pytest
from tests.testutils import create_repo, ALL_REPO_KINDS
from buildstream.plugintestutils import cli
from buildstream import _yaml
# Project directory
TOP_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(TOP_DIR, 'project')
class WorkspaceCreator():
def __init__(self, cli, tmpdir, datafiles, project_path=None):
self.cli = cli
self.tmpdir = tmpdir
self.datafiles = datafiles
if not project_path:
project_path = os.path.join(datafiles.dirname, datafiles.basename)
else:
shutil.copytree(os.path.join(datafiles.dirname, datafiles.basename), project_path)
self.project_path = project_path
self.bin_files_path = os.path.join(project_path, 'files', 'bin-files')
self.workspace_cmd = os.path.join(self.project_path, 'workspace_cmd')
def create_workspace_element(self, kind, track, suffix='', workspace_dir=None,
element_attrs=None):
element_name = 'workspace-test-{}{}.bst'.format(kind, suffix)
element_path = os.path.join(self.project_path, 'elements')
if not workspace_dir:
workspace_dir = os.path.join(self.workspace_cmd, element_name)
if workspace_dir[-4:] == '.bst':
workspace_dir = workspace_dir[:-4]
# Create our repo object of the given source type with
# the bin files, and then collect the initial ref.
repo = create_repo(kind, str(self.tmpdir))
ref = repo.create(self.bin_files_path)
if track:
ref = None
# Write out our test target
element = {
'kind': 'import',
'sources': [
repo.source_config(ref=ref)
]
}
if element_attrs:
element = {**element, **element_attrs}
_yaml.dump(element,
os.path.join(element_path,
element_name))
return element_name, element_path, workspace_dir
def create_workspace_elements(self, kinds, track, suffixs=None, workspace_dir_usr=None,
element_attrs=None):
element_tuples = []
if suffixs is None:
suffixs = ['', ] * len(kinds)
else:
if len(suffixs) != len(kinds):
raise "terable error"
for suffix, kind in zip(suffixs, kinds):
element_name, element_path, workspace_dir = \
self.create_workspace_element(kind, track, suffix, workspace_dir_usr,
element_attrs)
element_tuples.append((element_name, workspace_dir))
# Assert that there is no reference, a track & fetch is needed
states = self.cli.get_element_states(self.project_path, [
e for e, _ in element_tuples
])
if track:
assert not any(states[e] != 'no reference' for e, _ in element_tuples)
else:
assert not any(states[e] != 'fetch needed' for e, _ in element_tuples)
return element_tuples
def open_workspaces(self, kinds, track, suffixs=None, workspace_dir=None,
element_attrs=None, no_checkout=False):
element_tuples = self.create_workspace_elements(kinds, track, suffixs, workspace_dir,
element_attrs)
os.makedirs(self.workspace_cmd, exist_ok=True)
# Now open the workspace, this should have the effect of automatically
# tracking & fetching the source from the repo.
args = ['workspace', 'open']
if track:
args.append('--track')
if no_checkout:
args.append('--no-checkout')
if workspace_dir is not None:
assert len(element_tuples) == 1, "test logic error"
_, workspace_dir = element_tuples[0]
args.extend(['--directory', workspace_dir])
args.extend([element_name for element_name, workspace_dir_suffix in element_tuples])
result = self.cli.run(cwd=self.workspace_cmd, project=self.project_path, args=args)
result.assert_success()
if not no_checkout:
# Assert that we are now buildable because the source is now cached.
states = self.cli.get_element_states(self.project_path, [
e for e, _ in element_tuples
])
assert not any(states[e] != 'buildable' for e, _ in element_tuples)
# Check that the executable hello file is found in each workspace
for element_name, workspace_dir in element_tuples:
filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello')
assert os.path.exists(filename)
return element_tuples
def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir=None,
project_path=None, element_attrs=None, no_checkout=False):
workspace_object = WorkspaceCreator(cli, tmpdir, datafiles, project_path)
workspaces = workspace_object.open_workspaces((kind, ), track, (suffix, ), workspace_dir,
element_attrs, no_checkout)
assert len(workspaces) == 1
element_name, workspace = workspaces[0]
return element_name, workspace_object.project_path, workspace
@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("kind", ALL_REPO_KINDS)
def test_open(cli, tmpdir, datafiles, kind):
open_workspace(cli, tmpdir, datafiles, kind, False)
# elements/base.bst
kind: stack
depends:
- base/base-alpine.bst
kind: import
description: |
Alpine Linux base for tests
Generated using the `tests/integration-tests/base/generate-base.sh` script.
sources:
- kind: tar
base-dir: ''
(?):
- arch == "x86-64":
ref: 3eb559250ba82b64a68d86d0636a6b127aa5f6d25d3601a79f79214dc9703639
url: "alpine:integration-tests-base.v1.x86_64.tar.xz"
- arch == "aarch64":
ref: 431fb5362032ede6f172e70a3258354a8fd71fcbdeb1edebc0e20968c792329a
url: "alpine:integration-tests-base.v1.aarch64.tar.xz"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment