Merge pull request #2821 from lavalamp/fix

Allow, when testing, SelfLinks to be unset.
This commit is contained in:
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" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
) )
// ErrNilObject indicates an error that the obj passed to GetReference is nil. var (
var ErrNilObject = errors.New("Can't reference a nil object") // 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/([^/]*)/") 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 // GetReference returns an ObjectReference which refers to the given
// object, or an error if the object doesn't follow the conventions // object, or an error if the object doesn't follow the conventions
// that would allow this. // that would allow this.
@ -49,13 +56,22 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
version := versionFromSelfLink.FindStringSubmatch(meta.SelfLink()) version := ""
if len(version) < 2 { 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) return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", meta.SelfLink(), version)
} }
} else {
version = parsedSelfLink[1]
}
return &ObjectReference{ return &ObjectReference{
Kind: kind, Kind: kind,
APIVersion: version[1], APIVersion: version,
Name: meta.Name(), Name: meta.Name(),
Namespace: meta.Namespace(), Namespace: meta.Namespace(),
UID: meta.UID(), UID: meta.UID(),

View File

@ -96,7 +96,6 @@ func NewIntegrationTestKubelet(hn string, rd string, dc dockertools.DockerInterf
networkContainerImage: NetworkContainerImage, networkContainerImage: NetworkContainerImage,
resyncInterval: 3 * time.Second, resyncInterval: 3 * time.Second,
podWorkers: newPodWorkers(), 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) { func (kl *Kubelet) setRef(id dockertools.DockerID, ref *api.ObjectReference) {
kl.refLock.Lock() kl.refLock.Lock()
defer kl.refLock.Unlock() defer kl.refLock.Unlock()
if kl.dockerIDToRef == nil {
kl.dockerIDToRef = map[dockertools.DockerID]*api.ObjectReference{}
}
kl.dockerIDToRef[id] = ref kl.dockerIDToRef[id] = ref
} }

View File

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