Repository: kubernetes/kubernetes
File: test/integration/dra/partitionable_devices_test.go (Go)

Code before:
*/

package dra

import (
	"fmt"

	"github.com/onsi/gomega"
	"github.com/stretchr/testify/require"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	st "k8s.io/kubernetes/pkg/scheduler/testing"
	"k8s.io/kubernetes/test/utils/ktesting"
)

func testPartitionableDevices(tCtx ktesting.TContext, enabled bool) {
	if enabled {
		tCtx.Run("PerDeviceNodeSelection", testPerDeviceNodeSelection)
		tCtx.Run("MultiHostDevice", testPartitionableDevicesWithMultiHostDevice)
	} else {
		testDisabled(tCtx)
	}
}

// testDisabled verifies that creating ResourceSlices with node selection
// perDeviceNodeSelection fails when the Partitinable Devices feature is
// disabled.
func testDisabled(tCtx ktesting.TContext) {
	namespace := createTestNamespace(tCtx, nil)
	_, driverName := createTestClass(tCtx, namespace)

	slice := st.MakeResourceSliceWithPerDeviceNodeSelection("slice", driverName)
	_, err := tCtx.Client().ResourceV1().ResourceSlices().Create(tCtx, slice.Obj(), metav1.CreateOptions{})
	require.Error(tCtx, err, "slice should have become invalid after dropping PartitionableDevices")
}

// testPerDeviceNodeSelection verifies that pods are scheduled
// on the correct nodes when they are allocated devices that
// speficy node selection using the perDeviceNodeSelection field
// that was introduced as part of the Partitionable Devices
// feature.
func testPerDeviceNodeSelection(tCtx ktesting.TContext) {
	namespace := createTestNamespace(tCtx, nil)
	class, driverName := createTestClass(tCtx, namespace)

	nodes, err := tCtx.Client().CoreV1().Nodes().List(tCtx, metav1.ListOptions{})
	tCtx.ExpectNoError(err, "list nodes")

	slice := st.MakeResourceSliceWithPerDeviceNodeSelection("slice", driverName)
	for _, node := range nodes.Items {
		slice.Device(fmt.Sprintf("device-for-%s", node.Name), st.NodeName(node.Name))

Review comment (bug):
```suggestion
// perDeviceNodeSelection fails when the Partitionable Devices feature is
```

Code after:
*/

package dra

import (
	"fmt"

	"github.com/onsi/gomega"
	"github.com/stretchr/testify/require"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	st "k8s.io/kubernetes/pkg/scheduler/testing"
	"k8s.io/kubernetes/test/utils/ktesting"
)

func testPartitionableDevices(tCtx ktesting.TContext, enabled bool) {
	if enabled {
		tCtx.Run("PerDeviceNodeSelection", testPerDeviceNodeSelection)
		tCtx.Run("MultiHostDevice", testPartitionableDevicesWithMultiHostDevice)
	} else {
		testDisabled(tCtx)
	}
}

// testDisabled verifies that creating ResourceSlices with node selection
// perDeviceNodeSelection fails when the Partitionable Devices feature is
// disabled.
func testDisabled(tCtx ktesting.TContext) {
	namespace := createTestNamespace(tCtx, nil)
	_, driverName := createTestClass(tCtx, namespace)

	slice := st.MakeResourceSliceWithPerDeviceNodeSelection("slice", driverName)
	_, err := tCtx.Client().ResourceV1().ResourceSlices().Create(tCtx, slice.Obj(), metav1.CreateOptions{})
	require.Error(tCtx, err, "slice should have become invalid after dropping PartitionableDevices")
}

// testPerDeviceNodeSelection verifies that pods are scheduled
// on the correct nodes when they are allocated devices that
// speficy node selection using the perDeviceNodeSelection field
// that was introduced as part of the Partitionable Devices
// feature.
func testPerDeviceNodeSelection(tCtx ktesting.TContext) {
	namespace := createTestNamespace(tCtx, nil)
	class, driverName := createTestClass(tCtx, namespace)

	nodes, err := tCtx.Client().CoreV1().Nodes().List(tCtx, metav1.ListOptions{})
	tCtx.ExpectNoError(err, "list nodes")

	slice := st.MakeResourceSliceWithPerDeviceNodeSelection("slice", driverName)
	for _, node := range nodes.Items {
		slice.Device(fmt.Sprintf("device-for-%s", node.Name), st.NodeName(node.Name))