Skip to content

Commit 6de9e6e

Browse files
committed
Remove existing catalog entities in dest dir for clean sync with the content of the index image
This is to take into account the case where the catalog index image is updated and contains completely different catalog entities. Without this, we might still see the previous extensions displayed in the Extensions UI
1 parent 23e7688 commit 6de9e6e

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

docker/install-dynamic-plugins.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,10 +1005,14 @@ def extract_catalog_index(catalog_index_image: str, catalog_index_mount: str, ca
10051005
marketplace_dir_from_catalog_index = os.path.join(catalog_index_temp_dir, 'catalog-entities', 'marketplace')
10061006
if os.path.isdir(marketplace_dir_from_catalog_index):
10071007
os.makedirs(catalog_entities_parent_dir, exist_ok=True)
1008-
shutil.copytree(marketplace_dir_from_catalog_index, os.path.join(catalog_entities_parent_dir, 'catalog-entities'), dirs_exist_ok=True)
1008+
catalog_entities_dest = os.path.join(catalog_entities_parent_dir, 'catalog-entities')
1009+
# Ensure the destination directory is is sync with the catalog entities from the index image
1010+
if os.path.exists(catalog_entities_dest):
1011+
shutil.rmtree(catalog_entities_dest, ignore_errors=True, onerror=None)
1012+
shutil.copytree(marketplace_dir_from_catalog_index, catalog_entities_dest, dirs_exist_ok=True)
10091013
print("\t==> Successfully extracted extensions catalog entities from index image", flush=True)
10101014
else:
1011-
print(f"\t==> WARNING: Catalog index image {catalog_index_image} does not contain the expected 'catalog-entities/marketplace' directory", flush=True)
1015+
print(f"\t==> WARNING: Catalog index image {catalog_index_image} does not have a 'catalog-entities/marketplace' directory", flush=True)
10121016

10131017
return default_plugins_file
10141018

docker/test_install-dynamic-plugins.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,59 @@ def test_extract_catalog_index_creates_entities_directory(self, tmp_path, mocker
26222622
entity_file = entities_dir / "test-entity.yaml"
26232623
assert entity_file.exists(), "Entity file should be copied"
26242624

2625+
def test_extract_catalog_index_removes_existing_destination(self, tmp_path, mocker, mock_oci_image):
2626+
"""Test that existing catalog-entities directory is removed before copying."""
2627+
catalog_mount = tmp_path / "catalog-mount"
2628+
catalog_mount.mkdir()
2629+
catalog_entities_parent_dir = tmp_path / "existing-dir"
2630+
catalog_entities_parent_dir.mkdir()
2631+
2632+
# Create an existing catalog-entities directory with old content
2633+
existing_entities_dir = catalog_entities_parent_dir / "catalog-entities"
2634+
existing_entities_dir.mkdir()
2635+
old_file = existing_entities_dir / "old-file.yaml"
2636+
old_file.write_text("old content")
2637+
old_subdir = existing_entities_dir / "old-subdir"
2638+
old_subdir.mkdir()
2639+
(old_subdir / "old-nested.yaml").write_text("old nested content")
2640+
2641+
# Verify old content exists
2642+
assert existing_entities_dir.exists()
2643+
assert old_file.exists()
2644+
assert old_subdir.exists()
2645+
2646+
mocker.patch('shutil.which', return_value='/usr/bin/skopeo')
2647+
2648+
mock_result = mocker.Mock()
2649+
mock_result.returncode = 0
2650+
mock_subprocess_run = create_mock_skopeo_copy(
2651+
mock_oci_image['manifest_path'],
2652+
mock_oci_image['layer_tarball'],
2653+
mock_result
2654+
)
2655+
mocker.patch('subprocess.run', side_effect=mock_subprocess_run)
2656+
2657+
install_dynamic_plugins.extract_catalog_index(
2658+
"quay.io/test/catalog-index:1.9",
2659+
str(catalog_mount),
2660+
str(catalog_entities_parent_dir)
2661+
)
2662+
2663+
# Verify old content was removed
2664+
assert not old_file.exists(), "Old file should have been removed"
2665+
assert not old_subdir.exists(), "Old subdirectory should have been removed"
2666+
2667+
# Verify new content exists
2668+
entities_dir = catalog_entities_parent_dir / "catalog-entities"
2669+
assert entities_dir.exists(), "Catalog entities directory should exist"
2670+
entity_file = entities_dir / "test-entity.yaml"
2671+
assert entity_file.exists(), "New entity file should exist"
2672+
assert "kind: Component" in entity_file.read_text()
2673+
2674+
# Verify old content is definitely gone
2675+
assert not (entities_dir / "old-file.yaml").exists(), "Old file should not exist"
2676+
assert not (entities_dir / "old-subdir").exists(), "Old subdirectory should not exist"
2677+
26252678
def test_extract_catalog_index_without_catalog_entities(self, tmp_path, mocker, capsys):
26262679
"""Test that extraction succeeds with warning if catalog-entities directory doesn't exist in image."""
26272680
import tarfile
@@ -2677,6 +2730,7 @@ def test_extract_catalog_index_without_catalog_entities(self, tmp_path, mocker,
26772730
# Verify warning was printed
26782731
captured = capsys.readouterr()
26792732
assert 'WARNING' in captured.out
2733+
assert 'does not have a' in captured.out
26802734
assert 'catalog-entities/marketplace' in captured.out
26812735

26822736
# Verify catalog entities directory was not created

0 commit comments

Comments
 (0)