2015-04-30 17:12:23 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-04-30 17:12:23 +00:00
|
|
|
|
|
|
|
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 dockertools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2015-08-05 22:03:47 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2015-04-30 17:12:23 +00:00
|
|
|
)
|
|
|
|
|
2015-12-05 00:06:25 +00:00
|
|
|
func TestMapState(t *testing.T) {
|
2015-11-06 02:19:45 +00:00
|
|
|
testCases := []struct {
|
|
|
|
input string
|
2015-12-05 00:06:25 +00:00
|
|
|
expected kubecontainer.ContainerState
|
2015-11-06 02:19:45 +00:00
|
|
|
}{
|
2015-12-05 00:06:25 +00:00
|
|
|
{input: "Up 5 hours", expected: kubecontainer.ContainerStateRunning},
|
|
|
|
{input: "Exited (0) 2 hours ago", expected: kubecontainer.ContainerStateExited},
|
|
|
|
{input: "Created", expected: kubecontainer.ContainerStateUnknown},
|
|
|
|
{input: "Random string", expected: kubecontainer.ContainerStateUnknown},
|
2015-11-06 02:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
2015-12-05 00:06:25 +00:00
|
|
|
if actual := mapState(test.input); actual != test.expected {
|
2015-11-06 02:19:45 +00:00
|
|
|
t.Errorf("Test[%d]: expected %q, got %q", i, test.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 17:12:23 +00:00
|
|
|
func TestToRuntimeContainer(t *testing.T) {
|
|
|
|
original := &docker.APIContainers{
|
|
|
|
ID: "ab2cdf",
|
|
|
|
Image: "bar_image",
|
|
|
|
Created: 12345,
|
|
|
|
Names: []string{"/k8s_bar.5678_foo_ns_1234_42"},
|
2015-11-06 02:19:45 +00:00
|
|
|
Status: "Up 5 hours",
|
2015-04-30 17:12:23 +00:00
|
|
|
}
|
|
|
|
expected := &kubecontainer.Container{
|
2015-10-07 17:58:05 +00:00
|
|
|
ID: kubecontainer.ContainerID{"docker", "ab2cdf"},
|
2015-04-30 17:12:23 +00:00
|
|
|
Name: "bar",
|
|
|
|
Image: "bar_image",
|
|
|
|
Hash: 0x5678,
|
|
|
|
Created: 12345,
|
2015-12-05 00:06:25 +00:00
|
|
|
State: kubecontainer.ContainerStateRunning,
|
2015-04-30 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := toRuntimeContainer(original)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToRuntimeImage(t *testing.T) {
|
|
|
|
original := &docker.APIImages{
|
2015-05-11 17:50:14 +00:00
|
|
|
ID: "aeeea",
|
|
|
|
RepoTags: []string{"abc", "def"},
|
|
|
|
VirtualSize: 1234,
|
2015-04-30 17:12:23 +00:00
|
|
|
}
|
|
|
|
expected := &kubecontainer.Image{
|
|
|
|
ID: "aeeea",
|
|
|
|
Tags: []string{"abc", "def"},
|
|
|
|
Size: 1234,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := toRuntimeImage(original)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|