func()

in registry/storage/driver/testsuites/testsuites.go [1024:1138]


func (s *DriverSuite) TestWalkOverlappingPaths() {
	rootDirectory := "/" + dtestutil.RandomFilenameRange(8, 8)
	defer s.deletePath(s.StorageDriver, rootDirectory)
	s.T().Logf("root directory path used for testing: %s", rootDirectory)

	// Create the structure
	// /rootDirectory/foo/bar/baz1/baza
	// /rootDirectory/foo/bar/baz2/bazb
	// /rootDirectory/foo/bar/baz3
	// /rootDirectory/foo/barman/baz3/baza
	// /rootDirectory/foo/barman/baz4/bazb
	// /rootDirectory/foo/barman/baz5

	basePaths := []string{
		filepath.Join(rootDirectory, "foo/bar/baz1/baza"),
		filepath.Join(rootDirectory, "foo/bar/baz2/bazb"),
		filepath.Join(rootDirectory, "foo/bar/baz3"),
		filepath.Join(rootDirectory, "foo/barman/baz3/baza"),
		filepath.Join(rootDirectory, "foo/barman/baz4/bazb"),
		filepath.Join(rootDirectory, "foo/barman/baz5"),
	}

	// Create files at each path
	contents := s.blobberFactory.GetBlobber(32).GetAllBytes()
	for _, basePath := range basePaths {
		err := s.StorageDriver.PutContent(s.ctx, basePath, contents)
		require.NoError(s.T(), err)
	}

	// Helper function to verify walking works correctly for a specific path
	verifyWalk := func(t *testing.T, startPath string, expectedFiles, expectedDirs []string, walkFn func(context.Context, string, storagedriver.WalkFn) error) {
		var mu sync.Mutex
		var actualFiles []string
		var actualDirs []string

		collectFn := func(fileInfo storagedriver.FileInfo) error {
			mu.Lock()
			defer mu.Unlock()

			if fileInfo.IsDir() {
				actualDirs = append(actualDirs, fileInfo.Path())
			} else {
				actualFiles = append(actualFiles, fileInfo.Path())
			}
			return nil
		}

		err := walkFn(s.ctx, startPath, collectFn)
		require.NoError(t, err)

		// Sort the actual results before comparison
		sort.Strings(actualFiles)
		sort.Strings(actualDirs)

		require.Equal(t, expectedFiles, actualFiles, "Unexpected files for path %s", startPath)
		require.Equal(t, expectedDirs, actualDirs, "Unexpected directories for path %s", startPath)
	}

	// Expected files and directories when walking from foo/bar
	barExpectedFiles := []string{
		filepath.Join(rootDirectory, "foo/bar/baz1/baza"),
		filepath.Join(rootDirectory, "foo/bar/baz2/bazb"),
		filepath.Join(rootDirectory, "foo/bar/baz3"),
	}
	sort.Strings(barExpectedFiles)

	barExpectedDirs := []string{
		filepath.Join(rootDirectory, "foo/bar/baz1"),
		filepath.Join(rootDirectory, "foo/bar/baz2"),
	}
	sort.Strings(barExpectedDirs)

	// Expected files and directories when walking from foo
	fooExpectedFiles := []string{
		filepath.Join(rootDirectory, "foo/barman/baz3/baza"),
		filepath.Join(rootDirectory, "foo/barman/baz4/bazb"),
		filepath.Join(rootDirectory, "foo/barman/baz5"),
	}
	fooExpectedFiles = append(fooExpectedFiles, barExpectedFiles...)
	sort.Strings(fooExpectedFiles)

	fooExpectedDirs := []string{
		filepath.Join(rootDirectory, "foo/bar"),
		filepath.Join(rootDirectory, "foo/barman"),
		filepath.Join(rootDirectory, "foo/barman/baz3"),
		filepath.Join(rootDirectory, "foo/barman/baz4"),
	}
	fooExpectedDirs = append(fooExpectedDirs, barExpectedDirs...)
	sort.Strings(fooExpectedDirs)

	// Run subtests for both Walk and WalkParallel on different paths
	s.Run("Walk_BarPath", func() {
		barPath := filepath.Join(rootDirectory, "foo/bar")
		verifyWalk(s.T(), barPath, barExpectedFiles, barExpectedDirs, s.StorageDriver.Walk)
	})

	s.Run("WalkParallel_BarPath", func() {
		s.skipIfWalkParallelIsNotSupported()

		barPath := filepath.Join(rootDirectory, "foo/bar")
		verifyWalk(s.T(), barPath, barExpectedFiles, barExpectedDirs, s.StorageDriver.WalkParallel)
	})

	s.Run("Walk_FooPath", func() {
		fooPath := filepath.Join(rootDirectory, "foo")
		verifyWalk(s.T(), fooPath, fooExpectedFiles, fooExpectedDirs, s.StorageDriver.Walk)
	})

	s.Run("WalkParallel_FooPath", func() {
		s.skipIfWalkParallelIsNotSupported()

		fooPath := filepath.Join(rootDirectory, "foo")
		verifyWalk(s.T(), fooPath, fooExpectedFiles, fooExpectedDirs, s.StorageDriver.WalkParallel)
	})
}