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

Code before:
		{
			desc:     "VolumeLifecycleModes default to VolumeLifecyclePersistent",
			field:    "VolumeLifecycleModes",
			wantSpec: &storagev1.CSIDriverSpec{VolumeLifecycleModes: []storagev1.VolumeLifecycleMode{storagev1.VolumeLifecyclePersistent}},
		},
		{
			desc:     "RequiresRepublish default to false",
			field:    "RequiresRepublish",
			wantSpec: &storagev1.CSIDriverSpec{RequiresRepublish: &disabled},
		},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			gotSpec := roundTrip(t, runtime.Object(&storagev1.CSIDriver{})).(*storagev1.CSIDriver).Spec
			got := reflect.Indirect(reflect.ValueOf(gotSpec)).FieldByName(test.field).Interface()
			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):
you shouldn't need to set this to set to true (applies everywhere you're emulating before setting to true), can drop the SetFeatureGateDuringTest=true instead for a locked-true gate

Code after:
		{
			desc:     "VolumeLifecycleModes default to VolumeLifecyclePersistent",
			field:    "VolumeLifecycleModes",
			wantSpec: &storagev1.CSIDriverSpec{VolumeLifecycleModes: []storagev1.VolumeLifecycleMode{storagev1.VolumeLifecyclePersistent}},
		},
		{
			desc:     "RequiresRepublish default to false",
			field:    "RequiresRepublish",
			wantSpec: &storagev1.CSIDriverSpec{RequiresRepublish: &disabled},
		},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			gotSpec := roundTrip(t, runtime.Object(&storagev1.CSIDriver{})).(*storagev1.CSIDriver).Spec
			got := reflect.Indirect(reflect.ValueOf(gotSpec)).FieldByName(test.field).Interface()
			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)
	}
}