2014-06-18 22:18:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package master
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
2014-07-01 02:46:10 +00:00
|
|
|
"github.com/fsouza/go-dockerclient"
|
2014-06-18 22:18:38 +00:00
|
|
|
)
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
type FakePodInfoGetter struct {
|
2014-06-18 22:18:38 +00:00
|
|
|
host string
|
|
|
|
id string
|
2014-07-01 22:35:56 +00:00
|
|
|
data api.PodInfo
|
2014-06-18 22:18:38 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
func (f *FakePodInfoGetter) GetPodInfo(host, id string) (api.PodInfo, error) {
|
2014-06-18 22:18:38 +00:00
|
|
|
f.host = host
|
|
|
|
f.id = id
|
|
|
|
return f.data, f.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPodCacheGet(t *testing.T) {
|
2014-08-20 00:33:54 +00:00
|
|
|
cache := NewPodCache(nil, nil)
|
2014-06-18 22:18:38 +00:00
|
|
|
|
2014-09-16 20:21:04 +00:00
|
|
|
expected := api.PodInfo{
|
|
|
|
"foo": api.ContainerStatus{
|
|
|
|
DetailInfo: docker.Container{ID: "foo"},
|
|
|
|
},
|
|
|
|
}
|
2014-07-01 02:46:10 +00:00
|
|
|
cache.podInfo["foo"] = expected
|
2014-06-18 22:18:38 +00:00
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
info, err := cache.GetPodInfo("host", "foo")
|
2014-06-18 22:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
2014-07-01 22:35:56 +00:00
|
|
|
if !reflect.DeepEqual(info, expected) {
|
2014-07-01 02:46:10 +00:00
|
|
|
t.Errorf("Unexpected mismatch. Expected: %#v, Got: #%v", &expected, info)
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPodCacheGetMissing(t *testing.T) {
|
2014-08-20 00:33:54 +00:00
|
|
|
cache := NewPodCache(nil, nil)
|
2014-06-18 22:18:38 +00:00
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
info, err := cache.GetPodInfo("host", "foo")
|
2014-07-01 02:46:10 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Unexpected non-error: %#v", err)
|
2014-06-19 05:24:06 +00:00
|
|
|
}
|
|
|
|
if info != nil {
|
|
|
|
t.Errorf("Unexpected info: %#v", info)
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
func TestPodGetPodInfoGetter(t *testing.T) {
|
2014-09-16 20:21:04 +00:00
|
|
|
expected := api.PodInfo{
|
|
|
|
"foo": api.ContainerStatus{
|
|
|
|
DetailInfo: docker.Container{ID: "foo"},
|
|
|
|
},
|
|
|
|
}
|
2014-07-01 22:35:56 +00:00
|
|
|
fake := FakePodInfoGetter{
|
|
|
|
data: expected,
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
2014-08-20 00:33:54 +00:00
|
|
|
cache := NewPodCache(&fake, nil)
|
2014-06-18 22:18:38 +00:00
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
cache.updatePodInfo("host", "foo")
|
2014-06-18 22:18:38 +00:00
|
|
|
|
|
|
|
if fake.host != "host" || fake.id != "foo" {
|
|
|
|
t.Errorf("Unexpected access: %#v", fake)
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
info, err := cache.GetPodInfo("host", "foo")
|
2014-06-18 22:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
2014-07-01 22:35:56 +00:00
|
|
|
if !reflect.DeepEqual(info, expected) {
|
2014-07-01 02:46:10 +00:00
|
|
|
t.Errorf("Unexpected mismatch. Expected: %#v, Got: #%v", &expected, info)
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPodUpdateAllContainers(t *testing.T) {
|
|
|
|
pod := api.Pod{
|
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
|
|
|
CurrentState: api.PodState{
|
|
|
|
Host: "machine",
|
|
|
|
},
|
|
|
|
}
|
2014-07-01 02:46:10 +00:00
|
|
|
|
2014-06-18 22:18:38 +00:00
|
|
|
pods := []api.Pod{pod}
|
2014-08-29 00:48:07 +00:00
|
|
|
mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods})
|
2014-07-01 02:46:10 +00:00
|
|
|
|
2014-09-16 20:21:04 +00:00
|
|
|
expected := api.PodInfo{
|
|
|
|
"foo": api.ContainerStatus{
|
|
|
|
DetailInfo: docker.Container{ID: "foo"},
|
|
|
|
},
|
|
|
|
}
|
2014-07-01 22:35:56 +00:00
|
|
|
fake := FakePodInfoGetter{
|
|
|
|
data: expected,
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
2014-08-20 00:33:54 +00:00
|
|
|
cache := NewPodCache(&fake, mockRegistry)
|
2014-06-18 22:18:38 +00:00
|
|
|
|
|
|
|
cache.UpdateAllContainers()
|
|
|
|
|
|
|
|
if fake.host != "machine" || fake.id != "foo" {
|
|
|
|
t.Errorf("Unexpected access: %#v", fake)
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
info, err := cache.GetPodInfo("machine", "foo")
|
2014-06-18 22:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %#v", err)
|
|
|
|
}
|
2014-07-01 22:35:56 +00:00
|
|
|
if !reflect.DeepEqual(info, expected) {
|
2014-07-01 02:46:10 +00:00
|
|
|
t.Errorf("Unexpected mismatch. Expected: %#v, Got: #%v", &expected, info)
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|