Repository: kubernetes/kubernetes
File: pkg/kubelet/volumemanager/volume_manager_test.go (Go)

Code before:
			sourcesReady := config.NewSourcesReady(func(_ sets.Set[string]) bool { return true })
			go manager.Run(ctx, sourcesReady)

			podManager.SetPods(pods)

			if test.podMode != "" {
				for i := 0; i < test.numPods; i++ {
					volumeName := v1.UniqueVolumeName(node.Status.VolumesAttached[i].Name)
					go simulateVolumeInUseUpdate(volumeName, ctx.Done(), manager)
				}

				for _, pod := range pods {
					err := manager.WaitForAttachAndMount(ctx, pod)
					require.NoError(t, err, "Failed to wait for attach and mount for pod %s", pod.Name)
				}
			}

			unmountCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
			defer cancel()

			err := manager.WaitForAllPodsUnmount(unmountCtx, pods)

			if test.expectedError {
				require.ErrorIs(t, err, context.DeadlineExceeded, "Expected error due to timeout")
			} else {
				require.NoError(t, err, "Expected no error for test: %s", test.name)
			}
		})
	}
}

func createMultiplePodsWithVolumes(numPods int, pvMode v1.PersistentVolumeMode) (*v1.Node, []*v1.Pod, []*v1.PersistentVolume, []*v1.PersistentVolumeClaim) {
	attachedVolumes := make([]v1.AttachedVolume, numPods)
	for i := 0; i < numPods; i++ {
		attachedVolumes[i] = v1.AttachedVolume{
			Name:       v1.UniqueVolumeName(fmt.Sprintf("fake/fake-device-%d", i)),
			DevicePath: fmt.Sprintf("fake/path-%d", i),
		}
	}

	node := &v1.Node{
		ObjectMeta: metav1.ObjectMeta{Name: testHostname},
		Status: v1.NodeStatus{
			VolumesAttached: attachedVolumes,
		},
	}

	pods := make([]*v1.Pod, numPods)
	pvs := make([]*v1.PersistentVolume, numPods)
	claims := make([]*v1.PersistentVolumeClaim, numPods)


Review comment (suggestion):
we don't need to include test name. `t.Run(test.name, func(t *testing.T) ` contains test name for each case.

Code after:

			sourcesReady := config.NewSourcesReady(func(_ sets.Set[string]) bool { return true })
			go manager.Run(ctx, sourcesReady)

			podManager.SetPods(pods)

			if test.podMode != "" {
				for i := 0; i < test.numPods; i++ {
					volumeName := v1.UniqueVolumeName(node.Status.VolumesAttached[i].Name)
					go simulateVolumeInUseUpdate(volumeName, ctx.Done(), manager)
				}

				for _, pod := range pods {
					err := manager.WaitForAttachAndMount(ctx, pod)
					require.NoError(t, err, "Failed to wait for attach and mount for pod %s", pod.Name)
				}
			}

			unmountCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
			defer cancel()

			err := manager.WaitForAllPodsUnmount(unmountCtx, pods)

			if test.expectedError {
				require.ErrorIs(t, err, context.DeadlineExceeded, "Expected error due to timeout")
				// Verify that we get exactly numPods errors in the aggregate
				var aggErr utilerrors.Aggregate
				require.ErrorAs(t, err, &aggErr, "Expected error to be an Aggregate error")
				errs := aggErr.Errors()
				require.Len(t, errs, test.numPods, "Expected %d errors but got %d", test.numPods, len(errs))
			} else {
				require.NoError(t, err, "Expected no error")
			}
		})
	}
}

func createMultiplePodsWithVolumes(numPods int, pvMode v1.PersistentVolumeMode) (*v1.Node, []*v1.Pod, []*v1.PersistentVolume, []*v1.PersistentVolumeClaim) {
	attachedVolumes := make([]v1.AttachedVolume, numPods)
	for i := 0; i < numPods; i++ {
		attachedVolumes[i] = v1.AttachedVolume{
			Name:       v1.UniqueVolumeName(fmt.Sprintf("fake/fake-device-%d", i)),
			DevicePath: fmt.Sprintf("fake/path-%d", i),
		}
	}

	node := &v1.Node{
		ObjectMeta: metav1.ObjectMeta{Name: testHostname},
		Status: v1.NodeStatus{
			VolumesAttached: attachedVolumes,
		},