def test_clear_failures_success(install_mockery): """Test the clear_failures happy path.""" # Set up a test prefix failure lock lock = lk.Lock(spack.store.db.prefix_fail_path, start=1, length=1, default_timeout=1e-9, desc='test') try: lock.acquire_write() except lk.LockTimeoutError: tty.warn('Failed to write lock the test install failure') spack.store.db._prefix_failures['test'] = lock # Set up a fake failure mark (or file) fs.touch(os.path.join(spack.store.db._failure_dir, 'test')) # Now clear failure tracking inst.clear_failures() # Ensure there are no cached failure locks or failure marks assert len(spack.store.db._prefix_failures) == 0 assert len(os.listdir(spack.store.db._failure_dir)) == 0 # Ensure the core directory and failure lock file still exist assert os.path.isdir(spack.store.db._failure_dir) assert os.path.isfile(spack.store.db.prefix_fail_path)
def test_ensure_locked_have(install_mockery, tmpdir, capsys): """Test _ensure_locked when already have lock.""" const_arg = installer_args(['trivial-install-test-package'], {}) installer = create_installer(const_arg) spec = installer.build_requests[0].pkg.spec pkg_id = inst.package_id(spec.package) with tmpdir.as_cwd(): # Test "downgrade" of a read lock (to a read lock) lock = lk.Lock('./test', default_timeout=1e-9, desc='test') lock_type = 'read' tpl = (lock_type, lock) installer.locks[pkg_id] = tpl assert installer._ensure_locked(lock_type, spec.package) == tpl # Test "upgrade" of a read lock without read count to a write lock_type = 'write' err = 'Cannot upgrade lock' with pytest.raises(ulk.LockUpgradeError, match=err): installer._ensure_locked(lock_type, spec.package) out = str(capsys.readouterr()[1]) assert 'Failed to upgrade to a write lock' in out assert 'exception when releasing read lock' in out # Test "upgrade" of the read lock *with* read count to a write lock._reads = 1 tpl = (lock_type, lock) assert installer._ensure_locked(lock_type, spec.package) == tpl # Test "downgrade" of the write lock to a read lock lock_type = 'read' tpl = (lock_type, lock) assert installer._ensure_locked(lock_type, spec.package) == tpl
def test_ensure_locked_have(install_mockery, tmpdir): """Test to cover _ensure_locked when already have lock.""" spec, installer = create_installer('trivial-install-test-package') with tmpdir.as_cwd(): lock = lk.Lock('./test', default_timeout=1e-9, desc='test') lock_type = 'read' tpl = (lock_type, lock) installer.locks[installer.pkg_id] = tpl assert installer._ensure_locked(lock_type, spec.package) == tpl
def test_release_lock_write_n_exception(install_mockery, tmpdir, capsys): """Test _release_lock for supposed write lock with exception.""" spec, installer = create_installer('trivial-install-test-package') pkg_id = 'test' with tmpdir.as_cwd(): lock = lk.Lock('./test', default_timeout=1e-9, desc='test') installer.locks[pkg_id] = ('write', lock) assert lock._writes == 0 installer._release_lock(pkg_id) out = str(capsys.readouterr()[1]) msg = 'exception when releasing write lock for {0}'.format(pkg_id) assert msg in out
def test_cleanup_failed_err(install_mockery, tmpdir, monkeypatch, capsys): """Test _cleanup_failed exception path.""" msg = 'Fake release_write exception' def _raise_except(lock): raise RuntimeError(msg) spec, installer = create_installer('trivial-install-test-package') monkeypatch.setattr(lk.Lock, 'release_write', _raise_except) pkg_id = 'test' with tmpdir.as_cwd(): lock = lk.Lock('./test', default_timeout=1e-9, desc='test') installer.failed[pkg_id] = lock installer._cleanup_failed(pkg_id) out = str(capsys.readouterr()[1]) assert 'exception when removing failure tracking' in out assert msg in out
def test_disable_locking(tmpdir): """Ensure that locks do no real locking when disabled.""" lock_path = str(tmpdir.join('lockfile')) old_value = spack.config.get('config:locks') with spack.config.override('config:locks', False): lock = lk.Lock(lock_path) lock.acquire_read() assert not os.path.exists(lock_path) lock.acquire_write() assert not os.path.exists(lock_path) lock.release_write() assert not os.path.exists(lock_path) lock.release_read() assert not os.path.exists(lock_path) assert old_value == spack.config.get('config:locks')