Repository: kubernetes/kubernetes
File: pkg/apis/storage/v1/defaults_test.go (Go)

Code before:
			want := reflect.Indirect(reflect.ValueOf(test.wantSpec)).FieldByName(test.field).Interface()
			if diff := cmp.Diff(want, got); diff != "" {
				t.Errorf("CSIDriver defaults diff (-want +got):\n%s", diff)
			}
		})
	}
}

func TestSetDefaultSELinuxMountReadWriteOncePodEnabled(t *testing.T) {
	featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.34"))
	featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, true)
	driver := &storagev1.CSIDriver{}

	// field should be defaulted
	defaultSELinuxMount := false
	output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver)
	outSELinuxMount := output.Spec.SELinuxMount
	if outSELinuxMount == nil {
		t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: nil", defaultSELinuxMount)
	} else if *outSELinuxMount != defaultSELinuxMount {
		t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: %+v", defaultSELinuxMount, outSELinuxMount)
	}
}

func TestSetDefaultSELinuxMountReadWriteOncePodDisabled(t *testing.T) {
	featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.34"))
	featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, false)
	driver := &storagev1.CSIDriver{}

	// field should not be defaulted
	output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver)
	outSELinuxMount := output.Spec.SELinuxMount
	if outSELinuxMount != nil {
		t.Errorf("Expected SELinuxMount to remain nil, got: %+v", outSELinuxMount)
	}
}


Review comment (suggestion):
emulate the last version where the gate was able to be disabled (applies everywhere you're emulating before setting to false)
```suggestion
	featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.35"))
```

Code after:
			want := reflect.Indirect(reflect.ValueOf(test.wantSpec)).FieldByName(test.field).Interface()
			if diff := cmp.Diff(want, got); diff != "" {
				t.Errorf("CSIDriver defaults diff (-want +got):\n%s", diff)
			}
		})
	}
}

func TestSetDefaultSELinuxMountReadWriteOncePodEnabled(t *testing.T) {
	driver := &storagev1.CSIDriver{}

	// field should be defaulted
	defaultSELinuxMount := false
	output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver)
	outSELinuxMount := output.Spec.SELinuxMount
	if outSELinuxMount == nil {
		t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: nil", defaultSELinuxMount)
	} else if *outSELinuxMount != defaultSELinuxMount {
		t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: %+v", defaultSELinuxMount, outSELinuxMount)
	}
}

func TestSetDefaultSELinuxMountReadWriteOncePodDisabled(t *testing.T) {
	featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse("1.35"))
	featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, false)
	driver := &storagev1.CSIDriver{}

	// field should not be defaulted
	output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver)
	outSELinuxMount := output.Spec.SELinuxMount
	if outSELinuxMount != nil {
		t.Errorf("Expected SELinuxMount to remain nil, got: %+v", outSELinuxMount)
	}
}