From 691744c15f482c884eb2b8c8660aae4b17659a02 Mon Sep 17 00:00:00 2001 From: Vibhu Prashar Date: Mon, 23 Oct 2023 22:44:38 +0530 Subject: [PATCH] test(ci): update taints and tolerations test Signed-off-by: Vibhu Prashar --- go.mod | 2 +- pkg/utils/test/framework.go | 54 ++++++++++++++----------------------- tests/e2e/kepler_test.go | 12 ++++----- 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/go.mod b/go.mod index 88a529e6..3d38e0bb 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.66.0 github.com/stretchr/testify v1.8.2 + golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e golang.org/x/net v0.17.0 k8s.io/api v0.28.1 k8s.io/apimachinery v0.28.1 @@ -51,7 +52,6 @@ require ( github.com/spf13/pflag v1.0.5 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.25.0 // indirect - golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect golang.org/x/oauth2 v0.8.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/term v0.13.0 // indirect diff --git a/pkg/utils/test/framework.go b/pkg/utils/test/framework.go index 252b1f3a..73f318dc 100644 --- a/pkg/utils/test/framework.go +++ b/pkg/utils/test/framework.go @@ -19,13 +19,13 @@ package test import ( "bytes" "context" - "encoding/json" "fmt" - "strings" "sync" "testing" "time" + "golang.org/x/exp/slices" + "github.com/stretchr/testify/assert" "github.com/sustainable.computing.io/kepler-operator/pkg/api/v1alpha1" "github.com/sustainable.computing.io/kepler-operator/pkg/utils/k8s" @@ -195,16 +195,6 @@ func (f Framework) WaitUntilKeplerCondition(name string, t v1alpha1.ConditionTyp return &k } -func (f Framework) GetResourceNames(kind string) ([]string, error) { - f.T.Helper() - res, err := oc.Get().Resource(kind, "").OutputJsonpath("{.items[*].metadata.name}").Run() - if err != nil { - return []string{}, err - } - nodes := strings.Split(res, " ") - return nodes, nil -} - func (f Framework) AddResourceLabels(kind, name string, l map[string]string) error { f.T.Helper() b := new(bytes.Buffer) @@ -234,11 +224,6 @@ func (f Framework) RemoveResourceLabels(kind, name string, l []string) error { return err } -func (f Framework) GetTaints(node string) (string, error) { - f.T.Helper() - return oc.Get().Resource("node", node).OutputJsonpath("{.spec.taints}").Run() -} - func (f Framework) TaintNode(node, taintStr string) error { f.T.Helper() _, err := oc.Literal().From("oc adm taint node %s %s", node, taintStr).Run() @@ -249,26 +234,20 @@ func (f Framework) TaintNode(node, taintStr string) error { }) return err } -func (f Framework) GetNodes() []string { - f.T.Helper() - f.T.Logf("%s: getting nodes", time.Now().UTC().Format(time.RFC3339)) - nodes, err := f.GetResourceNames("node") - assert.NoError(f.T, err, "failed to get node names") - assert.NotZero(f.T, len(nodes), "got zero nodes") - return nodes -} -func (f Framework) GetTaintsForNode(node string) []corev1.Taint { +func (f Framework) GetSchedulableNodes() []corev1.Node { f.T.Helper() - f.T.Logf("%s: getting taints for node: %s", time.Now().UTC().Format(time.RFC3339), node) - taintsStr, err := f.GetTaints(node) - assert.NoError(f.T, err, "failed to get taint for node %s", node) - var taints []corev1.Taint - if taintsStr != "" { - err = json.Unmarshal([]byte(taintsStr), &taints) - assert.NoError(f.T, err, "failed to unmarshal taints %s", taintsStr) + var nodes corev1.NodeList + err := f.client.List(context.TODO(), &nodes) + assert.NoError(f.T, err, "failed to get nodes") + + var ret []corev1.Node + for _, n := range nodes.Items { + if isSchedulableNode(n) { + ret = append(ret, n) + } } - return taints + return ret } func (f Framework) TolerateTaints(taints []corev1.Taint) []corev1.Toleration { @@ -284,3 +263,10 @@ func (f Framework) TolerateTaints(taints []corev1.Taint) []corev1.Toleration { } return to } + +func isSchedulableNode(n corev1.Node) bool { + return slices.IndexFunc(n.Spec.Taints, func(t corev1.Taint) bool { + return t.Effect == corev1.TaintEffectNoSchedule || + t.Effect == corev1.TaintEffectNoExecute + }) == -1 +} diff --git a/tests/e2e/kepler_test.go b/tests/e2e/kepler_test.go index 14805749..129d568b 100644 --- a/tests/e2e/kepler_test.go +++ b/tests/e2e/kepler_test.go @@ -85,13 +85,12 @@ func TestNodeSelector(t *testing.T) { // Ensure Kepler is not deployed (by any chance) f.AssertNoResourceExists("kepler", "", &v1alpha1.Kepler{}, test.Timeout(10*time.Second)) - nodes, err := f.GetResourceNames("node") - assert.NoError(t, err, "failed to get node names") + nodes := f.GetSchedulableNodes() assert.NotZero(t, len(nodes), "got zero nodes") node := nodes[0] var labels k8s.StringMap = map[string]string{"e2e-test": "true"} - err = f.AddResourceLabels("node", node, labels) + err := f.AddResourceLabels("node", node.Name, labels) assert.NoError(t, err, "could not label node") f.CreateKepler("kepler", func(k *v1alpha1.Kepler) { @@ -120,9 +119,8 @@ func TestTaint_WithToleration(t *testing.T) { var err error // choose one node - nodes := f.GetNodes() + nodes := f.GetSchedulableNodes() node := nodes[0] - taints := f.GetTaintsForNode(node) e2eTestTaint := corev1.Taint{ Key: "key1", @@ -130,11 +128,11 @@ func TestTaint_WithToleration(t *testing.T) { Effect: corev1.TaintEffectNoSchedule, } - err = f.TaintNode(node, e2eTestTaint.ToString()) + err = f.TaintNode(node.Name, e2eTestTaint.ToString()) assert.NoError(t, err, "failed to taint node %s", node) f.CreateKepler("kepler", func(k *v1alpha1.Kepler) { - k.Spec.Exporter.Deployment.Tolerations = f.TolerateTaints(append(taints, e2eTestTaint)) + k.Spec.Exporter.Deployment.Tolerations = f.TolerateTaints(append(node.Spec.Taints, e2eTestTaint)) }) f.AssertResourceExists(components.Namespace, "", &corev1.Namespace{})