k3s/pkg/registry/pod_registry_test.go

215 lines
5.0 KiB
Go
Raw Normal View History

2014-06-09 04:42:09 +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 registry
import (
"encoding/json"
"fmt"
2014-06-09 06:29:04 +00:00
"reflect"
2014-06-09 04:42:09 +00:00
"testing"
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
2014-06-17 02:10:43 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
2014-06-09 04:42:09 +00:00
)
type MockPodRegistry struct {
2014-06-09 05:38:45 +00:00
err error
pod *api.Pod
2014-06-12 20:17:34 +00:00
pods []api.Pod
2014-06-09 04:42:09 +00:00
}
func expectNoError(t *testing.T, err error) {
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
}
func (registry *MockPodRegistry) ListPods(query labels.Query) ([]api.Pod, error) {
if registry.err != nil {
return registry.pods, registry.err
}
var filtered []api.Pod
for _, pod := range registry.pods {
if query.Matches(labels.Set(pod.Labels)) {
filtered = append(filtered, pod)
}
}
return filtered, nil
2014-06-09 04:42:09 +00:00
}
2014-06-12 20:17:34 +00:00
func (registry *MockPodRegistry) GetPod(podId string) (*api.Pod, error) {
return registry.pod, registry.err
2014-06-09 04:42:09 +00:00
}
2014-06-12 20:17:34 +00:00
func (registry *MockPodRegistry) CreatePod(machine string, pod api.Pod) error {
2014-06-09 04:42:09 +00:00
return registry.err
}
2014-06-12 20:17:34 +00:00
func (registry *MockPodRegistry) UpdatePod(pod api.Pod) error {
2014-06-09 04:42:09 +00:00
return registry.err
}
2014-06-09 05:38:45 +00:00
func (registry *MockPodRegistry) DeletePod(podId string) error {
2014-06-09 04:42:09 +00:00
return registry.err
}
2014-06-09 05:38:45 +00:00
func TestListPodsError(t *testing.T) {
2014-06-09 04:42:09 +00:00
mockRegistry := MockPodRegistry{
err: fmt.Errorf("test error"),
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
storage := PodRegistryStorage{
2014-06-09 04:42:09 +00:00
registry: &mockRegistry,
}
pods, err := storage.List(labels.Everything())
2014-06-09 04:42:09 +00:00
if err != mockRegistry.err {
t.Errorf("Expected %#v, Got %#v", mockRegistry.err, err)
}
2014-06-12 20:17:34 +00:00
if len(pods.(api.PodList).Items) != 0 {
2014-06-09 04:42:09 +00:00
t.Errorf("Unexpected non-zero pod list: %#v", pods)
}
}
2014-06-09 05:38:45 +00:00
func TestListEmptyPodList(t *testing.T) {
2014-06-09 04:42:09 +00:00
mockRegistry := MockPodRegistry{}
2014-06-09 05:38:45 +00:00
storage := PodRegistryStorage{
2014-06-09 04:42:09 +00:00
registry: &mockRegistry,
}
pods, err := storage.List(labels.Everything())
2014-06-09 04:42:09 +00:00
expectNoError(t, err)
2014-06-12 20:17:34 +00:00
if len(pods.(api.PodList).Items) != 0 {
2014-06-09 04:42:09 +00:00
t.Errorf("Unexpected non-zero pod list: %#v", pods)
}
}
2014-06-09 05:38:45 +00:00
func TestListPodList(t *testing.T) {
2014-06-09 04:42:09 +00:00
mockRegistry := MockPodRegistry{
2014-06-12 20:17:34 +00:00
pods: []api.Pod{
2014-06-12 21:09:40 +00:00
{
2014-06-12 20:17:34 +00:00
JSONBase: api.JSONBase{
2014-06-09 04:42:09 +00:00
ID: "foo",
},
},
2014-06-12 21:09:40 +00:00
{
2014-06-12 20:17:34 +00:00
JSONBase: api.JSONBase{
2014-06-09 04:42:09 +00:00
ID: "bar",
},
},
},
}
2014-06-09 05:38:45 +00:00
storage := PodRegistryStorage{
2014-06-09 04:42:09 +00:00
registry: &mockRegistry,
}
podsObj, err := storage.List(labels.Everything())
2014-06-12 20:17:34 +00:00
pods := podsObj.(api.PodList)
2014-06-09 04:42:09 +00:00
expectNoError(t, err)
if len(pods.Items) != 2 {
t.Errorf("Unexpected pod list: %#v", pods)
}
if pods.Items[0].ID != "foo" {
t.Errorf("Unexpected pod: %#v", pods.Items[0])
}
if pods.Items[1].ID != "bar" {
t.Errorf("Unexpected pod: %#v", pods.Items[1])
}
}
func TestExtractJson(t *testing.T) {
mockRegistry := MockPodRegistry{}
2014-06-09 05:38:45 +00:00
storage := PodRegistryStorage{
2014-06-09 04:42:09 +00:00
registry: &mockRegistry,
}
2014-06-12 20:17:34 +00:00
pod := api.Pod{
JSONBase: api.JSONBase{
2014-06-09 04:42:09 +00:00
ID: "foo",
},
}
body, err := json.Marshal(pod)
expectNoError(t, err)
podOut, err := storage.Extract(string(body))
expectNoError(t, err)
2014-06-09 06:29:04 +00:00
// Extract adds in a kind
pod.Kind = "cluster#pod"
if !reflect.DeepEqual(pod, podOut) {
2014-06-09 04:42:09 +00:00
t.Errorf("Expected %#v, found %#v", pod, podOut)
}
}
func TestGetPod(t *testing.T) {
mockRegistry := MockPodRegistry{
pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
},
}
storage := PodRegistryStorage{
registry: &mockRegistry,
}
obj, err := storage.Get("foo")
pod := obj.(*api.Pod)
expectNoError(t, err)
if !reflect.DeepEqual(*mockRegistry.pod, *pod) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", *mockRegistry.pod, *pod)
}
}
func TestGetPodCloud(t *testing.T) {
fakeCloud := &cloudprovider.FakeCloud{}
mockRegistry := MockPodRegistry{
pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
},
}
storage := PodRegistryStorage{
registry: &mockRegistry,
cloud: fakeCloud,
}
obj, err := storage.Get("foo")
pod := obj.(*api.Pod)
expectNoError(t, err)
if !reflect.DeepEqual(*mockRegistry.pod, *pod) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", *mockRegistry.pod, *pod)
}
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "ip-address" {
t.Errorf("Unexpected calls: %#v", fakeCloud.Calls)
}
}
2014-06-13 03:53:10 +00:00
func TestMakePodStatus(t *testing.T) {
status := makePodStatus(map[string]interface{}{})
if status != "Pending" {
t.Errorf("Expected 'Pending', got '%s'", status)
}
status = makePodStatus(map[string]interface{}{
"State": map[string]interface{}{
"Running": false,
},
})
2014-06-09 04:42:09 +00:00
2014-06-13 03:53:10 +00:00
if status != "Stopped" {
t.Errorf("Expected 'Stopped', got '%s'", status)
}
status = makePodStatus(map[string]interface{}{
"State": map[string]interface{}{
"Running": true,
},
})
if status != "Running" {
t.Errorf("Expected 'Running', got '%s'", status)
}
2014-06-09 04:42:09 +00:00
}