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

Code before:
		tCtx.ExpectNoError(err, fmt.Sprintf("claim name %q", createdClaim.Name))

		pod := st.MakePod().Name(podName).Namespace(namespace).
			Container("my-container").
			Obj()
		createdPod := createPod(tCtx, namespace, fmt.Sprintf("-%d", i), pod, claim)

		scheduledPod := waitForPodScheduled(tCtx, namespace, createdPod.Name)
		allocatedClaim := waitForClaimAllocatedToDevice(tCtx, namespace, createdClaim.Name, time.Second)

		nodeName := scheduledPod.Spec.NodeName
		expectedAllocatedDevice := fmt.Sprintf("device-for-%s", nodeName)
		tCtx.Expect(allocatedClaim.Status.Allocation.Devices.Results[0].Device).To(gomega.Equal(expectedAllocatedDevice))
	}
}

func testPartitionableDevicesWithMultiHostDevice(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")

	minNodeCount := 4
	if nodeCount := len(nodes.Items); nodeCount < minNodeCount {
		tCtx.Skipf("found only %d nodes, need at least %d", nodeCount, minNodeCount)
	}

	deviceNodes := []string{
		nodes.Items[0].Name,
		nodes.Items[1].Name,
		nodes.Items[2].Name,
		nodes.Items[3].Name,
	}

	slice := st.MakeResourceSliceWithPerDeviceNodeSelection("slice", driverName)
	slice.Device("multi-host-device", &v1.NodeSelector{
		NodeSelectorTerms: []v1.NodeSelectorTerm{{
			MatchExpressions: []v1.NodeSelectorRequirement{{
				Key:      "kubernetes.io/hostname",
				Operator: v1.NodeSelectorOpIn,
				Values:   deviceNodes,
			}},
		}},
	})
	createSlice(tCtx, slice.Obj())

	startScheduler(tCtx)

	claim := st.MakeResourceClaim().
		Name("multi-host-claim").

Review comment (bug):
Let's make this a test failure. If we change something that causes the test to be skipped, we might not notice.


Code after:
	for i := range nodes.Items {
		claim := st.MakeResourceClaim().
			Name(fmt.Sprintf("claim-%d", i)).
			Namespace(namespace).
			Request(class.Name).
			Obj()
		createdClaim, err := tCtx.Client().ResourceV1().ResourceClaims(namespace).Create(tCtx, claim, metav1.CreateOptions{})
		tCtx.ExpectNoError(err, fmt.Sprintf("claim name %q", createdClaim.Name))

		pod := st.MakePod().Name(podName).Namespace(namespace).
			Container("my-container").
			Obj()
		createdPod := createPod(tCtx, namespace, fmt.Sprintf("-%d", i), pod, claim)

		scheduledPod := waitForPodScheduled(tCtx, namespace, createdPod.Name)

		allocatedClaim, err := tCtx.Client().ResourceV1().ResourceClaims(namespace).Get(tCtx, createdClaim.Name, metav1.GetOptions{})
		tCtx.ExpectNoError(err, fmt.Sprintf("get claim %q", createdClaim.Name))
		tCtx.Expect(allocatedClaim).To(gomega.HaveField("Status.Allocation", gomega.Not(gomega.BeNil())), "Claim should have been allocated.")

		nodeName := scheduledPod.Spec.NodeName
		expectedAllocatedDevice := fmt.Sprintf("device-for-%s", nodeName)
		tCtx.Expect(allocatedClaim.Status.Allocation.Devices.Results[0].Device).To(gomega.Equal(expectedAllocatedDevice))
	}
}

// testPartitionableDevicesWithMultiHostDevice verifies that multiple pods sharing
// a ResourceClaim that is assigned a multi-host devices gets scheduled correctly
// on the nodes selected by the node selector on the device.
func testPartitionableDevicesWithMultiHostDevice(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")

	minNodeCount := 4
	if nodeCount := len(nodes.Items); nodeCount < minNodeCount {
		tCtx.Errorf("found only %d nodes, need at least %d", nodeCount, minNodeCount)
	}

	deviceNodes := []string{
		nodes.Items[0].Name,
		nodes.Items[1].Name,
		nodes.Items[2].Name,
		nodes.Items[3].Name,
	}

	slice := st.MakeResourceSliceWithPerDeviceNodeSelection("slice", driverName)
	slice.Device("multi-host-device", &v1.NodeSelector{
		NodeSelectorTerms: []v1.NodeSelectorTerm{{