mirror of
https://github.com/project-zot/zot.git
synced 2026-06-17 21:17:58 +08:00
fix(meta): handle cases when substores are nested (#3598)
fix(meta): handle cases where repositories when substores are nested Note this is a case of bad configuration: having multiple stores in the same tree structure. Guard against it in parse.go. Fix getAllRepos to prevent duplicate repositories in metaDB when substore directories are nested under the default store root directory. The fix processes substores first, then the default store, using a map-based deduplication approach to skip repositories that have already been added. This ensures that when both the default store and substores contain repositories with the same name (e.g., when a substore is nested within the default store), only one instance is added to the repository list. Add test TestNoDuplicateReposWithSubstoresAndNestedRepoNames to verify the deduplication logic works correctly with nested substores. Also update the other tests to avoid these issues in the future this is not a vali configuration. This is not the intended use case for substores, and it may have caused: https://github.com/project-zot/zot/actions/runs/19665302669/job/56320640980 Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
This commit is contained in:
@@ -721,6 +721,72 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB, log log.Logger)
|
||||
So(repoMeta.Tags, ShouldContainKey, tag)
|
||||
So(repoMeta.Tags, ShouldNotContainKey, tag2)
|
||||
})
|
||||
|
||||
Convey("Test no duplicate repos with substores and nested repo names", func() {
|
||||
// Create nested directories - substore is a subdirectory of default store
|
||||
defaultStoreDir := rootDir
|
||||
substoreDir := filepath.Join(rootDir, "a")
|
||||
|
||||
defaultStore := local.NewImageStore(defaultStoreDir, false, false,
|
||||
log, monitoring.NewMetricsServer(false, log), nil, nil, nil, nil)
|
||||
substore := local.NewImageStore(substoreDir, false, false,
|
||||
log, monitoring.NewMetricsServer(false, log), nil, nil, nil, nil)
|
||||
|
||||
storeController := storage.StoreController{
|
||||
DefaultStore: defaultStore,
|
||||
SubStore: map[string]storageTypes.ImageStore{
|
||||
"/a": substore,
|
||||
},
|
||||
}
|
||||
|
||||
// Create a repo in default store (regular repo name, no route prefix)
|
||||
defaultRepo := "repo-in-default"
|
||||
image1 := CreateRandomImage()
|
||||
err := WriteImageToFileSystem(image1, defaultRepo, "tag1", storeController)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Create repos in substore (these will be returned by substore.GetRepositories())
|
||||
// Repos in substore should have the "a" prefix to match the substore route
|
||||
substoreRepo1 := "a/repo-in-substore-1"
|
||||
substoreRepo2 := "a/repo-in-substore-2"
|
||||
image2 := CreateRandomImage()
|
||||
err = WriteImageToFileSystem(image2, substoreRepo1, "tag1", storeController)
|
||||
So(err, ShouldBeNil)
|
||||
image3 := CreateRandomImage()
|
||||
err = WriteImageToFileSystem(image3, substoreRepo2, "tag1", storeController)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Parse storage
|
||||
err = meta.ParseStorage(metaDB, storeController, log)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Get all repos from metaDB
|
||||
repoMetaList, err := metaDB.SearchRepos(ctx, "")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Collect all repo names and count occurrences
|
||||
repoNames := make(map[string]int)
|
||||
for _, repoMeta := range repoMetaList {
|
||||
repoNames[repoMeta.Name]++
|
||||
}
|
||||
|
||||
// Verify expected repos are present
|
||||
// Substore repos are processed first (with "a/" prefix), then default store repos
|
||||
expectedRepos := []string{substoreRepo1, substoreRepo2, defaultRepo}
|
||||
for _, expectedRepo := range expectedRepos {
|
||||
So(repoNames, ShouldContainKey, expectedRepo)
|
||||
}
|
||||
|
||||
// Verify no duplicates - each repo should appear exactly once
|
||||
for _, count := range repoNames {
|
||||
So(count, ShouldEqual, 1)
|
||||
}
|
||||
|
||||
// Verify total count - should be 3 repos:
|
||||
// - substoreRepo1, substoreRepo2 (from substore with "a/" prefix)
|
||||
// - defaultRepo (from default store, no prefix)
|
||||
So(len(repoMetaList), ShouldEqual, 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetSignatureLayersInfo(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user