Skip to content

Commit

Permalink
test(refactor): move helper functions (#283)
Browse files Browse the repository at this point in the history
Signed-off-by: Vibhu Prashar <vibhu.sharma2929@gmail.com>
  • Loading branch information
vprashar2929 authored Oct 18, 2023
1 parent aab30a6 commit f9455e6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
42 changes: 42 additions & 0 deletions pkg/utils/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -195,6 +196,7 @@ func (f Framework) WaitUntilKeplerCondition(name string, t v1alpha1.ConditionTyp
}

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
Expand All @@ -204,6 +206,7 @@ func (f Framework) GetResourceNames(kind string) ([]string, error) {
}

func (f Framework) AddResourceLabels(kind, name string, l map[string]string) error {
f.T.Helper()
b := new(bytes.Buffer)
for label, value := range l {
fmt.Fprintf(b, "%s=%s ", label, value)
Expand All @@ -216,11 +219,13 @@ func (f Framework) AddResourceLabels(kind, name string, l map[string]string) err
}

func (f Framework) AddResourceLabelsStr(kind, name, l string) error {
f.T.Helper()
_, err := oc.Literal().From("oc label %s %s %s", kind, name, l).Run()
return err
}

func (f Framework) RemoveResourceLabels(kind, name string, l []string) error {
f.T.Helper()
b := new(bytes.Buffer)
for _, label := range l {
fmt.Fprintf(b, "%s- ", label)
Expand All @@ -230,10 +235,12 @@ func (f Framework) RemoveResourceLabels(kind, name string, l []string) error {
}

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()
f.T.Cleanup(func() {
// remove taint
Expand All @@ -242,3 +249,38 @@ 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 {
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)
}
return taints
}

func (f Framework) TolerateTaints(taints []corev1.Taint) []corev1.Toleration {
f.T.Helper()
var to []corev1.Toleration
for _, ta := range taints {
to = append(to, corev1.Toleration{
Key: ta.Key,
Value: ta.Value,
Operator: corev1.TolerationOpEqual,
Effect: ta.Effect,
})
}
return to
}
40 changes: 3 additions & 37 deletions tests/e2e/kepler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package e2e

import (
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -121,9 +120,9 @@ func TestTaint_WithToleration(t *testing.T) {

var err error
// choose one node
nodes := getNodes(f)
nodes := f.GetNodes()
node := nodes[0]
taints := getTaintsForNode(f, node)
taints := f.GetTaintsForNode(node)

e2eTestTaint := corev1.Taint{
Key: "key1",
Expand All @@ -135,7 +134,7 @@ func TestTaint_WithToleration(t *testing.T) {
assert.NoError(t, err, "failed to taint node %s", node)

f.CreateKepler("kepler", func(k *v1alpha1.Kepler) {
k.Spec.Exporter.Deployment.Tolerations = tolerateTaints(append(taints, e2eTestTaint))
k.Spec.Exporter.Deployment.Tolerations = f.TolerateTaints(append(taints, e2eTestTaint))
})

f.AssertResourceExists(components.Namespace, "", &corev1.Namespace{})
Expand All @@ -152,36 +151,3 @@ func TestTaint_WithToleration(t *testing.T) {
f.AssertNoResourceExists(exporter.DaemonSetName, components.Namespace, &ds)

}

func getNodes(f *test.Framework) []string {
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 getTaintsForNode(f *test.Framework, node string) []corev1.Taint {
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)
}
return taints
}

func tolerateTaints(taints []corev1.Taint) []corev1.Toleration {
var to []corev1.Toleration
for _, ta := range taints {
to = append(to, corev1.Toleration{
Key: ta.Key,
Value: ta.Value,
Operator: corev1.TolerationOpEqual,
Effect: ta.Effect,
})
}
return to
}

0 comments on commit f9455e6

Please sign in to comment.