Skip to content

Commit

Permalink
Merge pull request #49 from linki/util
Browse files Browse the repository at this point in the history
Extract utility function into util package
  • Loading branch information
linki authored Dec 6, 2017
2 parents 9bf8c56 + eedf139 commit fc73a95
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
28 changes: 6 additions & 22 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"strings"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/pkg/api/v1"

"github.com/linki/chaoskube/util"
)

var logOutput = bytes.NewBuffer([]byte{})
Expand Down Expand Up @@ -200,7 +201,7 @@ func TestNoVictimReturnsError(t *testing.T) {
func TestDeletePod(t *testing.T) {
chaoskube := setup(t, labels.Everything(), labels.Everything(), labels.Everything(), false, 0)

victim := newPod("default", "foo")
victim := util.NewPod("default", "foo")

if err := chaoskube.DeletePod(victim); err != nil {
t.Fatal(err)
Expand All @@ -217,7 +218,7 @@ func TestDeletePod(t *testing.T) {
func TestDeletePodDryRun(t *testing.T) {
chaoskube := setup(t, labels.Everything(), labels.Everything(), labels.Everything(), true, 0)

victim := newPod("default", "foo")
victim := util.NewPod("default", "foo")

if err := chaoskube.DeletePod(victim); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -300,27 +301,10 @@ func validateLog(t *testing.T, msg string) {
}
}

func newPod(namespace, name string) v1.Pod {
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: map[string]string{
"app": name,
},
Annotations: map[string]string{
"chaos": name,
},
},
}

return pod
}

func setup(t *testing.T, labelSelector labels.Selector, annotations labels.Selector, namespaces labels.Selector, dryRun bool, seed int64) *Chaoskube {
pods := []v1.Pod{
newPod("default", "foo"),
newPod("testing", "bar"),
util.NewPod("default", "foo"),
util.NewPod("testing", "bar"),
}

client := fake.NewSimpleClientset()
Expand Down
22 changes: 22 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package util

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/api/v1"
)

// NewPod returns a new pod instance for testing purposes.
func NewPod(namespace, name string) v1.Pod {
return v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: map[string]string{
"app": name,
},
Annotations: map[string]string{
"chaos": name,
},
},
}
}

0 comments on commit fc73a95

Please sign in to comment.