add a test

pull/6/head
Brendan Burns 2016-07-07 22:08:09 -07:00
parent cf5010ca6e
commit 891bd3e522
2 changed files with 86 additions and 2 deletions

View File

@ -22,7 +22,6 @@ import (
"os"
"sort"
"strings"
"sync"
"github.com/golang/glog"
@ -56,6 +55,10 @@ var (
)
func init() {
loadKubeAPIVersions()
}
func loadKubeAPIVersions() {
// Env var KUBE_API_VERSIONS is a comma separated list of API versions that
// should be registered in the scheme.
kubeAPIVersions := os.Getenv("KUBE_API_VERSIONS")
@ -71,6 +74,16 @@ func init() {
}
}
// Resets everything to clean room for the start of a test
func clearForTesting() {
registeredVersions = map[unversioned.GroupVersion]struct{}{}
thirdPartyGroupVersions = []unversioned.GroupVersion{}
enabledVersions = map[unversioned.GroupVersion]struct{}{}
groupMetaMap = map[string]*apimachinery.GroupMeta{}
envRequestedVersions = []unversioned.GroupVersion{}
loadKubeAPIVersions()
}
// RegisterVersions adds the given group versions to the list of registered group versions.
func RegisterVersions(availableVersions []unversioned.GroupVersion) {
for _, v := range availableVersions {
@ -209,7 +222,6 @@ func AddThirdPartyAPIGroupVersions(gvs ...unversioned.GroupVersion) []unversione
RegisterVersions(filteredGVs)
EnableVersions(filteredGVs...)
thirdPartyGroupVersions = append(thirdPartyGroupVersions, filteredGVs...)
return skippedGVs
}

View File

@ -23,6 +23,78 @@ import (
"k8s.io/kubernetes/pkg/apimachinery"
)
func TestAddThirdPartyVersionsBasic(t *testing.T) {
clearForTesting()
registered := []unversioned.GroupVersion{
{
Group: "",
Version: "v1",
},
}
skipped := registered
thirdParty := []unversioned.GroupVersion{
{
Group: "company.com",
Version: "v1",
},
{
Group: "company.com",
Version: "v2",
},
}
gvs := append(registered, thirdParty...)
RegisterVersions(registered)
wasSkipped := AddThirdPartyAPIGroupVersions(gvs...)
if len(wasSkipped) != len(skipped) {
t.Errorf("Expected %v, found %v", skipped, wasSkipped)
}
for ix := range wasSkipped {
found := false
for _, gv := range skipped {
if gv.String() == wasSkipped[ix].String() {
found = true
break
}
}
if !found {
t.Errorf("Couldn't find %v in %v", wasSkipped[ix], skipped)
}
}
for _, gv := range thirdParty {
if !IsThirdPartyAPIGroupVersion(gv) {
t.Errorf("Expected %v to be third party.", gv)
}
}
}
func TestAddThirdPartyVersionsMultiple(t *testing.T) {
clearForTesting()
thirdParty := []unversioned.GroupVersion{
{
Group: "company.com",
Version: "v1",
},
{
Group: "company.com",
Version: "v2",
},
}
for _, gv := range thirdParty {
wasSkipped := AddThirdPartyAPIGroupVersions(gv)
if len(wasSkipped) != 0 {
t.Errorf("Expected length 0, found %v", wasSkipped)
}
}
for _, gv := range thirdParty {
if !IsThirdPartyAPIGroupVersion(gv) {
t.Errorf("Expected %v to be third party.", gv)
}
}
}
func TestAllPreferredGroupVersions(t *testing.T) {
testCases := []struct {
groupMetas []apimachinery.GroupMeta