Repository: kubernetes/kubernetes
File: pkg/registry/storage/csidriver/strategy_test.go (Go)

Code before:
			csiServiceAccountTokenSecretsEnabled: false,
			old:                                  driverWithNothing,
			update:                               driverWithServiceAccountTokenInSecretsEnabled,
			wantServiceAccountTokenInSecrets:     nil,
			wantTokenRequests:                    []storage.TokenRequest{{Audience: gcp}},
			wantGeneration:                       1,
		},
		{
			name:                                 "ServiceAccountTokenInSecrets feature disabled, before: enabled, update: enabled",
			csiServiceAccountTokenSecretsEnabled: false,
			old:                                  driverWithServiceAccountTokenInSecretsEnabled,
			update:                               driverWithServiceAccountTokenInSecretsEnabled,
			wantServiceAccountTokenInSecrets:     &enabled,
			wantTokenRequests:                    []storage.TokenRequest{{Audience: gcp}},
			wantGeneration:                       0,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if !test.seLinuxMountReadWriteOncePodEnabled {
				featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.34"))
			}
			if !test.csiServiceAccountTokenSecretsEnabled {
				featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.35"))
			}
			featuregatetesting.SetFeatureGatesDuringTest(t, utilfeature.DefaultFeatureGate, featuregatetesting.FeatureOverrides{
				features.SELinuxMountReadWriteOncePod:   test.seLinuxMountReadWriteOncePodEnabled,
				features.MutableCSINodeAllocatableCount: test.mutableCSINodeAllocatableCountEnabled,
				features.CSIServiceAccountTokenSecrets:  test.csiServiceAccountTokenSecretsEnabled,
			})

			csiDriver := test.update.DeepCopy()
			Strategy.PrepareForUpdate(ctx, csiDriver, test.old)
			require.Equal(t, test.wantGeneration, csiDriver.GetGeneration())
			require.Equal(t, test.wantCapacity, csiDriver.Spec.StorageCapacity)
			require.Equal(t, test.wantModes, csiDriver.Spec.VolumeLifecycleModes)
			require.Equal(t, test.wantTokenRequests, csiDriver.Spec.TokenRequests)
			require.Equal(t, test.wantRequiresRepublish, csiDriver.Spec.RequiresRepublish)
			require.Equal(t, test.wantSELinuxMount, csiDriver.Spec.SELinuxMount)
			require.Equal(t, test.wantNodeAllocatableUpdatePeriodSeconds, csiDriver.Spec.NodeAllocatableUpdatePeriodSeconds)
			require.Equal(t, test.wantServiceAccountTokenInSecrets, csiDriver.Spec.ServiceAccountTokenInSecrets)
		})
	}
}

func TestCSIDriverValidation(t *testing.T) {
	enabled := true
	disabled := true
	gcp := "gcp"
	validNodeAllocatableUpdatePeriodSeconds := int64(30)

Review comment (security):
only the last SetFeatureGateEmulationVersionDuringTest call is effective, unify these:

```suggestion
			if !test.csiServiceAccountTokenSecretsEnabled || !test.seLinuxMountReadWriteOncePodEnabled {
				featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.35"))
			}
```

Code after:
			csiServiceAccountTokenSecretsEnabled: false,
			old:                                  driverWithNothing,
			update:                               driverWithServiceAccountTokenInSecretsEnabled,
			wantServiceAccountTokenInSecrets:     nil,
			wantTokenRequests:                    []storage.TokenRequest{{Audience: gcp}},
			wantGeneration:                       1,
		},
		{
			name:                                 "ServiceAccountTokenInSecrets feature disabled, before: enabled, update: enabled",
			csiServiceAccountTokenSecretsEnabled: false,
			old:                                  driverWithServiceAccountTokenInSecretsEnabled,
			update:                               driverWithServiceAccountTokenInSecretsEnabled,
			wantServiceAccountTokenInSecrets:     &enabled,
			wantTokenRequests:                    []storage.TokenRequest{{Audience: gcp}},
			wantGeneration:                       0,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if !test.csiServiceAccountTokenSecretsEnabled || !test.seLinuxMountReadWriteOncePodEnabled {
				featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.35"))
			}
			featuregatetesting.SetFeatureGatesDuringTest(t, utilfeature.DefaultFeatureGate, featuregatetesting.FeatureOverrides{
				features.SELinuxMountReadWriteOncePod:   test.seLinuxMountReadWriteOncePodEnabled,
				features.MutableCSINodeAllocatableCount: test.mutableCSINodeAllocatableCountEnabled,
				features.CSIServiceAccountTokenSecrets:  test.csiServiceAccountTokenSecretsEnabled,
			})

			csiDriver := test.update.DeepCopy()
			Strategy.PrepareForUpdate(ctx, csiDriver, test.old)
			require.Equal(t, test.wantGeneration, csiDriver.GetGeneration())
			require.Equal(t, test.wantCapacity, csiDriver.Spec.StorageCapacity)
			require.Equal(t, test.wantModes, csiDriver.Spec.VolumeLifecycleModes)
			require.Equal(t, test.wantTokenRequests, csiDriver.Spec.TokenRequests)
			require.Equal(t, test.wantRequiresRepublish, csiDriver.Spec.RequiresRepublish)
			require.Equal(t, test.wantSELinuxMount, csiDriver.Spec.SELinuxMount)
			require.Equal(t, test.wantNodeAllocatableUpdatePeriodSeconds, csiDriver.Spec.NodeAllocatableUpdatePeriodSeconds)
			require.Equal(t, test.wantServiceAccountTokenInSecrets, csiDriver.Spec.ServiceAccountTokenInSecrets)
		})
	}
}

func TestCSIDriverValidation(t *testing.T) {
	enabled := true
	disabled := true
	gcp := "gcp"
	validNodeAllocatableUpdatePeriodSeconds := int64(30)
	invalidNodeAllocatableUpdatePeriodSeconds := int64(3)
	tokenRequests := []storage.TokenRequest{{Audience: gcp}}