Merge pull request #2821 from lavalamp/fix

Allow, when testing, SelfLinks to be unset.
pull/6/head
Dawn Chen 2014-12-10 12:10:51 -08:00
commit 17475cdbe7
3 changed files with 30 additions and 7 deletions

View File

@ -25,11 +25,18 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
// ErrNilObject indicates an error that the obj passed to GetReference is nil.
var ErrNilObject = errors.New("Can't reference a nil object")
var (
// Errors that could be returned by GetReference.
ErrNilObject = errors.New("can't reference a nil object")
ErrNoSelfLink = errors.New("selfLink was empty, can't make reference")
)
var versionFromSelfLink = regexp.MustCompile("/api/([^/]*)/")
// ForTesting_ReferencesAllowBlankSelfLinks can be set to true in tests to avoid
// "ErrNoSelfLink" errors.
var ForTesting_ReferencesAllowBlankSelfLinks = false
// GetReference returns an ObjectReference which refers to the given
// object, or an error if the object doesn't follow the conventions
// that would allow this.
@ -49,13 +56,22 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
if err != nil {
return nil, err
}
version := versionFromSelfLink.FindStringSubmatch(meta.SelfLink())
if len(version) < 2 {
return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", meta.SelfLink(), version)
version := ""
parsedSelfLink := versionFromSelfLink.FindStringSubmatch(meta.SelfLink())
if len(parsedSelfLink) < 2 {
if ForTesting_ReferencesAllowBlankSelfLinks {
version = "testing"
} else if meta.SelfLink() == "" {
return nil, ErrNoSelfLink
} else {
return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", meta.SelfLink(), version)
}
} else {
version = parsedSelfLink[1]
}
return &ObjectReference{
Kind: kind,
APIVersion: version[1],
APIVersion: version,
Name: meta.Name(),
Namespace: meta.Namespace(),
UID: meta.UID(),

View File

@ -96,7 +96,6 @@ func NewIntegrationTestKubelet(hn string, rd string, dc dockertools.DockerInterf
networkContainerImage: NetworkContainerImage,
resyncInterval: 3 * time.Second,
podWorkers: newPodWorkers(),
dockerIDToRef: map[dockertools.DockerID]*api.ObjectReference{},
}
}
@ -456,6 +455,9 @@ func containerRef(pod *api.BoundPod, container *api.Container) (*api.ObjectRefer
func (kl *Kubelet) setRef(id dockertools.DockerID, ref *api.ObjectReference) {
kl.refLock.Lock()
defer kl.refLock.Unlock()
if kl.dockerIDToRef == nil {
kl.dockerIDToRef = map[dockertools.DockerID]*api.ObjectReference{}
}
kl.dockerIDToRef[id] = ref
}

View File

@ -40,6 +40,11 @@ import (
"github.com/stretchr/testify/mock"
)
func init() {
api.ForTesting_ReferencesAllowBlankSelfLinks = true
util.ReallyCrash = true
}
func newTestKubelet(t *testing.T) (*Kubelet, *tools.FakeEtcdClient, *dockertools.FakeDockerClient) {
fakeEtcdClient := tools.NewFakeEtcdClient(t)
fakeDocker := &dockertools.FakeDockerClient{}