remove redundant getKey functions from tests

This commit is contained in:
Mayank Kumar
2018-05-12 00:15:57 -07:00
parent f5254dab8a
commit a1cd3a4bcc
9 changed files with 81 additions and 77 deletions

View File

@@ -20,7 +20,9 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"sync"
"testing"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -38,6 +40,7 @@ import (
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/fake"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/api/legacyscheme"
api "k8s.io/kubernetes/pkg/apis/core"
utilnode "k8s.io/kubernetes/pkg/util/node"
@@ -46,6 +49,10 @@ import (
"github.com/golang/glog"
)
var (
keyFunc = cache.DeletionHandlingMetaNamespaceKeyFunc
)
// FakeNodeHandler is a fake implementation of NodesInterface and NodeInterface. It
// allows test cases to have fine-grained control over mock behaviors. We also need
// PodsInterface and PodInterface to test list & delet pods, which is implemented in
@@ -485,3 +492,27 @@ func GetZones(nodeHandler *FakeNodeHandler) []string {
func CreateZoneID(region, zone string) string {
return region + ":\x00:" + zone
}
// GetKey is a helper function used by controllers unit tests to get the
// key for a given kubernetes resource.
func GetKey(obj interface{}, t *testing.T) string {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// if tombstone , try getting the value from tombstone.Obj
obj = tombstone.Obj
}
val := reflect.ValueOf(obj).Elem()
name := val.FieldByName("Name").String()
kind := val.FieldByName("Kind").String()
// Note kind is not always set in the tests, so ignoring that for now
if len(name) == 0 || len(kind) == 0 {
t.Errorf("Unexpected object %v", obj)
}
key, err := keyFunc(obj)
if err != nil {
t.Errorf("Unexpected error getting key for %v %v: %v", kind, name, err)
return ""
}
return key
}