Repository: kubernetes/kubernetes
File: staging/src/k8s.io/dynamic-resource-allocation/api/conversion_test.go (Go)

Code before:
func v1FillFuncs(codecs serializer.CodecFactory) []interface{} {
	return []interface{}{
		// The internal api types use bool instead of *bool for AllNodes.
		// Converting nil -> false -> &false means nil doesn't round-trip.
		// Ensure AllNodes is always non-nil to avoid this.
		func(s *resourceapi.ResourceSliceSpec, c randfill.Continue) {
			c.FillNoCustom(s)
			if s.AllNodes == nil {
				s.AllNodes = new(bool)
			}
		},
		// The internal api types use bool instead of *bool for BindsToNode.
		// Converting nil -> false -> &false means nil doesn't round-trip.
		// Ensure BindsToNode is always non-nil to avoid this.
		func(d *resourceapi.Device, c randfill.Continue) {
			c.FillNoCustom(d)
			if d.BindsToNode == nil {
				d.BindsToNode = new(bool)
			}
		},
	}
}

// TestConversionRoundTrip verifies that the conversion code for the internal
// DRA API correctly round-trips between v1 and the internal api types.
// For v1 -> api -> v1, the conversion should be lossless.
func TestConversionRoundTrip(t *testing.T) {
	scheme := runtime.NewScheme()
	if err := resourceapi.AddToScheme(scheme); err != nil {
		t.Fatal(err)
	}
	if err := AddToScheme(scheme); err != nil {
		t.Fatal(err)
	}

	filler := apitest.NewFiller(t, scheme, fuzzer.FuzzerFuncs(v1FillFuncs))

	// Test v1 -> api -> v1 roundtrip for types that have conversions.
	// Note: api -> v1 -> api is NOT lossless because api types use UniqueString
	// which has different identity semantics than regular strings.
	testCases := []struct {
		name   string
		v1Type func() runtime.Object
		apiNew func() interface{}
	}{
		{
			name:   "ResourceSlice",
			v1Type: func() runtime.Object { return &resourceapi.ResourceSlice{} },
			apiNew: func() interface{} { return &ResourceSlice{} },
		},
	}

Review comment (suggestion):
```suggestion
// For v1 -> api -> v1, the conversion should be lossless.
//
// api -> v1 -> api is not lossless because of
// additional fields in the internal representation.
// Those would get lost during conversion.
// But this doesn't matter because in practice,
// the internal representation is never converted back,
// therefore this direction is not tested.
```

Code after:
func v1FillFuncs(codecs serializer.CodecFactory) []interface{} {
	return []interface{}{
		// The internal api types use bool instead of *bool for AllNodes.
		// Converting nil -> false -> &false means nil doesn't round-trip.
		// Ensure AllNodes is always non-nil to avoid this.
		func(s *resourceapi.ResourceSliceSpec, c randfill.Continue) {
			c.FillNoCustom(s)
			if s.AllNodes == nil {
				s.AllNodes = new(bool)
			}
		},
		// The internal api types use bool instead of *bool for BindsToNode.
		// Converting nil -> false -> &false means nil doesn't round-trip.
		// Ensure BindsToNode is always non-nil to avoid this.
		func(d *resourceapi.Device, c randfill.Continue) {
			c.FillNoCustom(d)
			if d.BindsToNode == nil {
				d.BindsToNode = new(bool)
			}
		},
	}
}

// TestConversionRoundTrip verifies that the conversion code for the internal
// DRA API correctly round-trips between v1 and the internal api types.
// For v1 -> api -> v1, the conversion should be lossless.
//
// api -> v1 -> api is not lossless because of
// additional fields in the internal representation.
// Those would get lost during conversion.
// But this doesn't matter because in practice,
// the internal representation is never converted back,
// therefore this direction is not tested.
func TestConversionRoundTrip(t *testing.T) {
	scheme := runtime.NewScheme()
	if err := resourceapi.AddToScheme(scheme); err != nil {
		t.Fatal(err)
	}
	if err := AddToScheme(scheme); err != nil {
		t.Fatal(err)
	}

	filler := apitest.NewFiller(t, scheme, fuzzer.FuzzerFuncs(v1FillFuncs))

	testCases := []struct {
		name   string
		v1Type func() runtime.Object
		apiNew func() interface{}
	}{
		{
			name:   "ResourceSlice",