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

Code before:
			podMode:       v1.PersistentVolumeFilesystem,
			expectedError: true,
			numPods:       1,
		},
		{
			name:          "concurrent timeout - race condition test",
			podMode:       v1.PersistentVolumeFilesystem,
			expectedError: true,
			numPods:       10,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			var ctx context.Context = ktesting.Init(t)
			podManager := kubepod.NewBasicPodManager()

			node, pod, pv, claim := createObjects(test.podMode, test.podMode)

			pods := []*v1.Pod{pod}
			for i := 1; i < test.numPods; i++ {
				podCopy := pod.DeepCopy()
				podCopy.Name = fmt.Sprintf("%s-%d", pod.Name, i)
				podCopy.UID = kubetypes.UID(fmt.Sprintf("%s-%d", pod.UID, i))
				pods = append(pods, podCopy)
			}

			kubeClient := fake.NewSimpleClientset(node, pv, claim)
			manager := newTestVolumeManager(t, tmpDir, podManager, kubeClient, node)

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

			podManager.SetPods(pods)

			go simulateVolumeInUseUpdate(
				v1.UniqueVolumeName(node.Status.VolumesAttached[0].Name),
				ctx.Done(),
				manager)

			err := manager.WaitForAttachAndMount(ctx, pod)
			require.NoError(t, err, "Failed to wait for attach and mount")

			timeoutDuration := 1 * time.Second
			if test.numPods > 1 {
				timeoutDuration = 50 * time.Millisecond
			}

			timeoutCtx, cancel := context.WithTimeout(ctx, timeoutDuration)
			defer cancel()


Review comment (suggestion):
This looks a little bit hacky and makes me think that creating a separate test would be better. There we can use `FakeVolumeManager`, which looks better for this purpose.

Code after:
		{
			name:          "concurrent unmount - multiple pods (10) with timeout errors",
			numPods:       10,
			podMode:       v1.PersistentVolumeFilesystem,
			expectedError: true,
		},
		{
			name:          "concurrent unmount - many pods (50) with timeout errors",
			numPods:       50,
			podMode:       v1.PersistentVolumeFilesystem,
			expectedError: true,
		},
		{
			name:          "concurrent unmount - multiple pods without volumes",
			numPods:       10,
			podMode:       "",
			expectedError: false,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			var ctx context.Context = ktesting.Init(t)
			podManager := kubepod.NewBasicPodManager()

			node, pods, pvs, claims := createMultiplePodsWithVolumes(test.numPods, test.podMode)

			objects := []runtime.Object{node}
			for i := 0; i < test.numPods; i++ {
				objects = append(objects, pods[i], pvs[i], claims[i])
			}
			kubeClient := fake.NewClientset(objects...)

			manager := newTestVolumeManager(t, tmpDir, podManager, kubeClient, node)

			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)
				}
			}