ci: Make test independent from any changes in spack repo (#30150)
Fix test_ci_generate_prune_untouched(), which would fail if run when the latest commit changed the .gitlab-ci.yml. This change mocks the get_stack_changed() method in that test to disregard the state of the current spack repo in favor of a mock repo under test control.
This commit is contained in:
parent
5e014be8b7
commit
a710a2425a
@ -12,6 +12,8 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from jsonschema import ValidationError, validate
|
from jsonschema import ValidationError, validate
|
||||||
|
|
||||||
|
from llnl.util.filesystem import mkdirp, working_dir
|
||||||
|
|
||||||
import spack
|
import spack
|
||||||
import spack.binary_distribution
|
import spack.binary_distribution
|
||||||
import spack.ci as ci
|
import spack.ci as ci
|
||||||
@ -30,6 +32,7 @@
|
|||||||
from spack.schema.database_index import schema as db_idx_schema
|
from spack.schema.database_index import schema as db_idx_schema
|
||||||
from spack.schema.gitlab_ci import schema as gitlab_ci_schema
|
from spack.schema.gitlab_ci import schema as gitlab_ci_schema
|
||||||
from spack.spec import CompilerSpec, Spec
|
from spack.spec import CompilerSpec, Spec
|
||||||
|
from spack.util.executable import which
|
||||||
from spack.util.mock_package import MockPackageMultiRepo
|
from spack.util.mock_package import MockPackageMultiRepo
|
||||||
|
|
||||||
ci_cmd = spack.main.SpackCommand('ci')
|
ci_cmd = spack.main.SpackCommand('ci')
|
||||||
@ -50,6 +53,44 @@ def ci_base_environment(working_env, tmpdir):
|
|||||||
os.environ['CI_PROJECT_DIR'] = tmpdir.strpath
|
os.environ['CI_PROJECT_DIR'] = tmpdir.strpath
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def mock_git_repo(tmpdir):
|
||||||
|
"""Create a mock git repo with two commits, the last one creating
|
||||||
|
a .gitlab-ci.yml"""
|
||||||
|
|
||||||
|
repo_path = tmpdir.join('mockspackrepo').strpath
|
||||||
|
mkdirp(repo_path)
|
||||||
|
|
||||||
|
git = which('git', required=True)
|
||||||
|
with working_dir(repo_path):
|
||||||
|
git('init')
|
||||||
|
|
||||||
|
with open('README.md', 'w') as f:
|
||||||
|
f.write('# Introduction')
|
||||||
|
|
||||||
|
with open('.gitlab-ci.yml', 'w') as f:
|
||||||
|
f.write("""
|
||||||
|
testjob:
|
||||||
|
script:
|
||||||
|
- echo "success"
|
||||||
|
""")
|
||||||
|
|
||||||
|
git('config', '--local', 'user.email', 'testing@spack.io')
|
||||||
|
git('config', '--local', 'user.name', 'Spack Testing')
|
||||||
|
|
||||||
|
# initial commit with README
|
||||||
|
git('add', 'README.md')
|
||||||
|
git('-c', 'commit.gpgsign=false', 'commit',
|
||||||
|
'-m', 'initial commit')
|
||||||
|
|
||||||
|
# second commit, adding a .gitlab-ci.yml
|
||||||
|
git('add', '.gitlab-ci.yml')
|
||||||
|
git('-c', 'commit.gpgsign=false', 'commit',
|
||||||
|
'-m', 'add a .gitlab-ci.yml')
|
||||||
|
|
||||||
|
yield repo_path
|
||||||
|
|
||||||
|
|
||||||
def test_specs_staging(config):
|
def test_specs_staging(config):
|
||||||
"""Make sure we achieve the best possible staging for the following
|
"""Make sure we achieve the best possible staging for the following
|
||||||
spec DAG::
|
spec DAG::
|
||||||
@ -1432,6 +1473,13 @@ def fake_get_mirrors_for_spec(spec=None, full_hash_match=False,
|
|||||||
_validate_needs_graph(new_yaml_contents, needs_graph, False)
|
_validate_needs_graph(new_yaml_contents, needs_graph, False)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ci_get_stack_changed(mock_git_repo, monkeypatch):
|
||||||
|
"""Test that we can detect the change to .gitlab-ci.yml in a
|
||||||
|
mock spack git repo."""
|
||||||
|
monkeypatch.setattr(spack.paths, 'prefix', mock_git_repo)
|
||||||
|
assert ci.get_stack_changed('/no/such/env/path') is True
|
||||||
|
|
||||||
|
|
||||||
def test_ci_generate_prune_untouched(tmpdir, mutable_mock_env_path,
|
def test_ci_generate_prune_untouched(tmpdir, mutable_mock_env_path,
|
||||||
install_mockery, mock_packages,
|
install_mockery, mock_packages,
|
||||||
ci_base_environment, monkeypatch):
|
ci_base_environment, monkeypatch):
|
||||||
@ -1466,9 +1514,14 @@ def test_ci_generate_prune_untouched(tmpdir, mutable_mock_env_path,
|
|||||||
def fake_compute_affected(r1=None, r2=None):
|
def fake_compute_affected(r1=None, r2=None):
|
||||||
return ['libdwarf']
|
return ['libdwarf']
|
||||||
|
|
||||||
|
def fake_stack_changed(env_path, rev1='HEAD^', rev2='HEAD'):
|
||||||
|
return False
|
||||||
|
|
||||||
with ev.read('test'):
|
with ev.read('test'):
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
ci, 'compute_affected_packages', fake_compute_affected)
|
ci, 'compute_affected_packages', fake_compute_affected)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
ci, 'get_stack_changed', fake_stack_changed)
|
||||||
ci_cmd('generate', '--output-file', outputfile)
|
ci_cmd('generate', '--output-file', outputfile)
|
||||||
|
|
||||||
with open(outputfile) as f:
|
with open(outputfile) as f:
|
||||||
|
Loading…
Reference in New Issue
Block a user