Repository: containerd/containerd
File: core/mount/mount_linux_test.go (Go)

Code before:
			errorMsg:    "no common prefix found",
		},
		{
			name: "normal snapshots",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/37712/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16590/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585/fs",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/",
		},
		{
			name: "no common prefix at all",
			lowerDirs: []string{
				"/completely/different/path/1",
				"/totally/unrelated/path/2",
			},
			expectError: true,
			errorMsg:    "invalid common directory:",
		},
		{
			name: "single dir",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585/fs",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585",
		},
		{
			name: "single dir with ending slash",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585/fs/",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585/fs/",
		},
		{
			name: "snapshots with common prefix in snapshot id",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/79/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/78/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/77/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/76/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/75/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/73/fs",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			result, err := getCommonDirectory(tc.lowerDirs)

Review comment (bug):
This behavior is technically incorrect. The lower dir is a directory, it's just not ending with a trailing "/".

We could detect it if we add a trailing slash to all the lowerdirs, before getting the max longest prefix. But it's just more complexity for no good reason. Returning the parent here and using the right path is still correct, I wouldn't complicate the function. Also, I like how simple and straight forward it is now :)

Code after:
			errorMsg:    "no common prefix found",
		},
		{
			name: "normal snapshots",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/37712/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16590/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585/fs",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots",
		},
		{
			name: "no common prefix at all",
			lowerDirs: []string{
				"/completely/different/path/1",
				"/totally/unrelated/path/2",
			},
			expectError: true,
			errorMsg:    "invalid common directory:",
		},
		{
			name: "single dir",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585/fs",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/16585",
		},
		{
			name: "snapshots with common prefix in snapshot id",
			lowerDirs: []string{
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/79/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/78/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/77/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/76/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/75/fs",
				"/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/73/fs",
			},
			expected: "/mnt/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			result, err := getCommonDirectory(tc.lowerDirs)

			if tc.expectError {
				if err == nil {
					t.Fatalf("expected error containing %q, got nil", tc.errorMsg)
				}
				if !strings.Contains(err.Error(), tc.errorMsg) {
					t.Errorf("expected error containing %q, got %q", tc.errorMsg, err.Error())