Skip to content

Commit 4305df5

Browse files
add more tests for auto-cache
1 parent d216e54 commit 4305df5

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

tests/test_project_caching.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from conftest import (
1010
GIT,
1111
add_commit,
12+
chdir,
1213
cmd,
1314
create_branch,
1415
create_repo,
@@ -314,6 +315,104 @@ def test_update_auto_cache(tmpdir):
314315
assert bar_head_newer in rev_list(auto_cache_dir / "bar" / bar_hash)
315316

316317

318+
def test_update_auto_cache_robustness(tmpdir):
319+
foo_remote = Path(tmpdir / 'remotes' / 'foo')
320+
bar_remote = Path(tmpdir / 'remotes' / 'bar')
321+
auto_cache_dir = Path(tmpdir / 'auto_cache_dir')
322+
323+
def create_foo_bar_commits():
324+
add_commit(foo_remote, 'new commit')
325+
add_commit(bar_remote, 'new commit')
326+
foo_head = rev_parse(foo_remote, 'HEAD')
327+
bar_head = rev_parse(bar_remote, 'HEAD')
328+
return foo_head, bar_head
329+
330+
def setup_workspace_and_west_update(workspace, foo_head, bar_head):
331+
setup_cache_workspace(
332+
workspace,
333+
foo_remote=foo_remote,
334+
foo_head=foo_head,
335+
bar_remote=bar_remote,
336+
bar_head=bar_head,
337+
)
338+
with chdir(workspace):
339+
stdout = cmd(['-v', 'update', '--auto-cache', auto_cache_dir])
340+
return stdout
341+
342+
create_repo(foo_remote)
343+
create_repo(bar_remote)
344+
foo_commit1, bar_commit1 = create_foo_bar_commits()
345+
foo_commit2, bar_commit2 = create_foo_bar_commits()
346+
347+
# run initial west update to setup auto-cache and get cache directories
348+
setup_workspace_and_west_update(
349+
tmpdir / 'workspace1',
350+
foo_head=foo_commit1,
351+
bar_head=bar_commit1,
352+
)
353+
354+
# read the auto-cache hashes from foo and bar
355+
(bar_hash,) = [p for p in (auto_cache_dir / 'bar').iterdir() if p.is_dir()]
356+
auto_cache_dir_bar = auto_cache_dir / 'bar' / bar_hash
357+
(foo_hash,) = [p for p in (auto_cache_dir / 'foo').iterdir() if p.is_dir()]
358+
auto_cache_dir_foo = auto_cache_dir / 'foo' / foo_hash
359+
360+
# Imitate that foo remote is temporarily offline by moving it temporarily.
361+
# Since foo and bar revisions are used which are already contained in the auto-cache,.
362+
# west update should work with according messages as there is no need to update remotes.
363+
foo_moved = Path(tmpdir / 'remotes' / 'foo.moved')
364+
shutil.move(foo_remote, foo_moved)
365+
stdout = setup_workspace_and_west_update(
366+
tmpdir / 'workspace2',
367+
foo_head=foo_commit2,
368+
bar_head=bar_commit2,
369+
)
370+
shutil.move(foo_moved, foo_remote)
371+
msgs = [
372+
f"foo: remote (file://{foo_remote}) not reachable. Skip auto-cache update with remote.",
373+
f"foo: cloning from {auto_cache_dir_foo}",
374+
f"bar: auto-cache remote update is skipped as it already contains revision {bar_commit2}",
375+
f"bar: cloning from {auto_cache_dir_bar}",
376+
]
377+
for msg in msgs:
378+
assert msg in stdout
379+
380+
# Make the auto-cache from bar corrupt by removing some refs
381+
# Use new commits to enforce that the auto-cache tries to update with remote.
382+
# west update should detect the corrupt auto-cache, reset it and re-create it
383+
shutil.rmtree(auto_cache_dir_bar / 'refs')
384+
foo_commit3, bar_commit3 = create_foo_bar_commits()
385+
stdout = setup_workspace_and_west_update(
386+
tmpdir / 'workspace3',
387+
foo_head=foo_commit3,
388+
bar_head=bar_commit3,
389+
)
390+
msgs = [
391+
f"foo: update auto-cache ({auto_cache_dir_foo}) with remote",
392+
f"foo: cloning from {auto_cache_dir_foo}",
393+
f"bar: reset corrupt auto-cache {auto_cache_dir_bar}",
394+
f'bar: create auto-cache for file://{bar_remote} in {auto_cache_dir_bar}',
395+
f"bar: cloning from {auto_cache_dir_bar}",
396+
]
397+
for msg in msgs:
398+
assert msg in stdout
399+
400+
# Make bar auto-cache corrupt by changing the remote HEAD
401+
# When trying to clone from this auto-cache, this would leads to git warning:
402+
# warning: remote HEAD refers to nonexistent ref, unable to checkout
403+
# Use new commits to enforce that the auto-cache tries to update with remote.
404+
# west update should detect the corrupt auto-cache, reset it and re-create it
405+
Path(auto_cache_dir_bar / bar_hash / 'HEAD').write_text("ref: refs/heads/nonexistent-branch")
406+
foo_commit4, bar_commit4 = create_foo_bar_commits()
407+
stdout = setup_workspace_and_west_update(
408+
tmpdir / 'workspace4',
409+
foo_head=foo_commit4,
410+
bar_head=bar_commit4,
411+
)
412+
for msg in msgs:
413+
assert msg in stdout
414+
415+
317416
def test_update_caches_priorities(tmpdir):
318417
# Test that the correct cache is used if multiple caches are specified
319418
# e.g. if 'west update --name-cache X --path-cache Y --auto-cache Z'

0 commit comments

Comments
 (0)