Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ci): update taints and tolerations test #288

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 20 additions & 34 deletions pkg/utils/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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
}
12 changes: 5 additions & 7 deletions tests/e2e/kepler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -120,21 +119,20 @@ 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",
Value: "value1",
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{})
Expand Down