1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

test/e2e: rework node capacity matching

Add new MatchCapacity matcher replacing the old waitForCapacity helper
function.
This commit is contained in:
Markus Lehtonen 2023-04-21 13:27:24 +03:00
parent a85e396200
commit f93ab9d423
2 changed files with 28 additions and 30 deletions

View file

@ -81,6 +81,26 @@ func MatchAnnotations(expectedNew map[string]k8sAnnotations, oldNodes []corev1.N
}
}
// MatchCapacity returns a specialized Gomega matcher for checking if a list of
// nodes have resource capacity as expected.
func MatchCapacity(expectedNew map[string]corev1.ResourceList, oldNodes []corev1.Node, ignoreUnexpected bool) gomegatypes.GomegaMatcher {
matcher := &nodeIterablePropertyMatcher[corev1.ResourceList]{
propertyName: "resource capacity",
ignoreUnexpected: ignoreUnexpected,
matchFunc: func(newNode, oldNode corev1.Node, expected corev1.ResourceList) ([]string, []string, []string) {
expectedAll := oldNode.Status.DeepCopy().Capacity
maps.Copy(expectedAll, expected)
return matchMap(newNode.Status.Capacity, expectedAll)
},
}
return &nodeListPropertyMatcher[corev1.ResourceList]{
expected: expectedNew,
oldNodes: oldNodes,
matcher: matcher,
}
}
// nodeListPropertyMatcher is a generic Gomega matcher for asserting one property a group of nodes.
type nodeListPropertyMatcher[T any] struct {
expected map[string]T

View file

@ -766,10 +766,12 @@ core:
expectedAnnotations["*"] = k8sAnnotations{"nfd.node.kubernetes.io/extended-resources": "nons,vendor.io/dynamic,vendor.io/static"}
expectedCapacity := corev1.ResourceList{
"feature.node.kubernetes.io/nons": resourcev1.MustParse("123"),
"vendor.io/dynamic": resourcev1.MustParse("10"),
"vendor.io/static": resourcev1.MustParse("123"),
expectedCapacity := map[string]corev1.ResourceList{
"*": {
"feature.node.kubernetes.io/nons": resourcev1.MustParse("123"),
"vendor.io/dynamic": resourcev1.MustParse("10"),
"vendor.io/static": resourcev1.MustParse("123"),
},
}
By("Creating NodeFeatureRules #4")
@ -779,14 +781,14 @@ core:
eventuallyNonControlPlaneNodes(ctx, f.ClientSet).Should(MatchAnnotations(expectedAnnotations, nodes, true))
By("Verfiying node status capacity from NodeFeatureRules #4")
Expect(waitForCapacity(ctx, f.ClientSet, expectedCapacity, nodes)).NotTo(HaveOccurred())
eventuallyNonControlPlaneNodes(ctx, f.ClientSet).Should(MatchCapacity(expectedCapacity, nodes, false))
By("Deleting NodeFeatureRule object")
err = nfdClient.NfdV1alpha1().NodeFeatureRules().Delete(ctx, "e2e-extened-resource-test", metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())
By("Verfiying node status capacity from NodeFeatureRules #4")
Expect(waitForCapacity(ctx, f.ClientSet, nil, nodes)).NotTo(HaveOccurred())
eventuallyNonControlPlaneNodes(ctx, f.ClientSet).Should(MatchCapacity(expectedCapacity, nodes, false))
By("Deleting nfd-worker daemonset")
err = f.ClientSet.AppsV1().DaemonSets(f.Namespace.Name).Delete(ctx, workerDS.Name, metav1.DeleteOptions{})
@ -942,30 +944,6 @@ func simplePoll(poll func() error, wait time.Duration) error {
return err
}
// waitForCapacity waits for the capacity to be updated in the node status
func waitForCapacity(ctx context.Context, cli clientset.Interface, expectedNewERs corev1.ResourceList, oldNodes []corev1.Node) error {
poll := func() error {
nodes, err := getNonControlPlaneNodes(ctx, cli)
if err != nil {
return err
}
for _, node := range nodes {
oldNode := getNode(oldNodes, node.Name)
expected := oldNode.Status.DeepCopy().Capacity
for k, v := range expectedNewERs {
expected[k] = v
}
capacity := node.Status.Capacity
if !cmp.Equal(expected, capacity) {
return fmt.Errorf("node %q capacity does not match expected, diff (expected vs. received): %s", node.Name, cmp.Diff(expected, capacity))
}
}
return nil
}
return simplePoll(poll, 10)
}
// waitForNfdNodeTaints waits for node to be tainted as expected.
func waitForNfdNodeTaints(ctx context.Context, cli clientset.Interface, expectedNewTaints []corev1.Taint, oldNodes []corev1.Node) error {
poll := func() error {