2014-09-09 04:33:17 +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 dockertools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/adler32"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-04-02 20:14:52 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
2014-11-15 13:50:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider"
|
2015-01-14 23:22:21 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
2015-01-05 23:24:49 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-11-15 13:50:59 +00:00
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2014-09-09 04:33:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func verifyCalls(t *testing.T, fakeDocker *FakeDockerClient, calls []string) {
|
|
|
|
fakeDocker.Lock()
|
|
|
|
defer fakeDocker.Unlock()
|
|
|
|
verifyStringArrayEquals(t, fakeDocker.called, calls)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyStringArrayEquals(t *testing.T, actual, expected []string) {
|
|
|
|
invalid := len(actual) != len(expected)
|
|
|
|
if !invalid {
|
|
|
|
for ix, value := range actual {
|
|
|
|
if expected[ix] != value {
|
|
|
|
invalid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
t.Errorf("Expected: %#v, Actual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerID(t *testing.T) {
|
|
|
|
fakeDocker := &FakeDockerClient{}
|
|
|
|
fakeDocker.ContainerList = []docker.APIContainers{
|
|
|
|
{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2014-09-09 04:33:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_bar_qux_ns_2565_42"},
|
2014-09-09 04:33:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
fakeDocker.Container = &docker.Container{
|
|
|
|
ID: "foobar",
|
|
|
|
}
|
|
|
|
|
2014-09-29 21:38:31 +00:00
|
|
|
dockerContainers, err := GetKubeletDockerContainers(fakeDocker, false)
|
2014-09-09 04:33:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, Got %#v", err)
|
|
|
|
}
|
|
|
|
if len(dockerContainers) != 2 {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", fakeDocker.ContainerList, dockerContainers)
|
|
|
|
}
|
|
|
|
verifyCalls(t, fakeDocker, []string{"list"})
|
2015-02-26 20:27:14 +00:00
|
|
|
dockerContainer, found, _ := dockerContainers.FindPodContainer("qux_ns", "", "foo")
|
2014-09-09 04:33:17 +00:00
|
|
|
if dockerContainer == nil || !found {
|
|
|
|
t.Errorf("Failed to find container %#v", dockerContainer)
|
|
|
|
}
|
|
|
|
|
2014-12-17 05:11:27 +00:00
|
|
|
fakeDocker.ClearCalls()
|
2014-09-09 04:33:17 +00:00
|
|
|
dockerContainer, found, _ = dockerContainers.FindPodContainer("foobar", "", "foo")
|
|
|
|
verifyCalls(t, fakeDocker, []string{})
|
|
|
|
if dockerContainer != nil || found {
|
|
|
|
t.Errorf("Should not have found container %#v", dockerContainer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 01:30:30 +00:00
|
|
|
func verifyPackUnpack(t *testing.T, podNamespace, podUID, podName, containerName string) {
|
2014-09-09 04:33:17 +00:00
|
|
|
container := &api.Container{Name: containerName}
|
|
|
|
hasher := adler32.New()
|
2015-01-05 23:24:49 +00:00
|
|
|
util.DeepHashObject(hasher, *container)
|
2014-09-09 04:33:17 +00:00
|
|
|
computedHash := uint64(hasher.Sum32())
|
2015-02-26 20:27:14 +00:00
|
|
|
podFullName := fmt.Sprintf("%s_%s", podName, podNamespace)
|
2015-03-20 11:55:02 +00:00
|
|
|
name := BuildDockerName(KubeletContainerName{podFullName, types.UID(podUID), container.Name}, container)
|
|
|
|
returned, hash, err := ParseDockerName(name)
|
2015-03-12 23:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to parse Docker container name %q: %v", name, err)
|
|
|
|
}
|
2015-03-20 11:55:02 +00:00
|
|
|
if podFullName != returned.PodFullName || podUID != string(returned.PodUID) || containerName != returned.ContainerName || computedHash != hash {
|
|
|
|
t.Errorf("For (%s, %s, %s, %d), unpacked (%s, %s, %s, %d)", podFullName, podUID, containerName, computedHash, returned.PodFullName, returned.PodUID, returned.ContainerName, hash)
|
2014-09-09 04:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerManifestNaming(t *testing.T) {
|
2015-01-10 01:19:31 +00:00
|
|
|
podUID := "12345678"
|
|
|
|
verifyPackUnpack(t, "file", podUID, "name", "container")
|
|
|
|
verifyPackUnpack(t, "file", podUID, "name-with-dashes", "container")
|
2015-01-05 01:30:30 +00:00
|
|
|
// UID is same as pod name
|
2015-01-10 01:19:31 +00:00
|
|
|
verifyPackUnpack(t, "file", podUID, podUID, "container")
|
2014-09-25 00:05:53 +00:00
|
|
|
// No Container name
|
2015-01-10 01:19:31 +00:00
|
|
|
verifyPackUnpack(t, "other", podUID, "name", "")
|
2014-09-09 04:33:17 +00:00
|
|
|
|
|
|
|
container := &api.Container{Name: "container"}
|
|
|
|
podName := "foo"
|
|
|
|
podNamespace := "test"
|
2015-02-26 20:27:14 +00:00
|
|
|
name := fmt.Sprintf("k8s_%s_%s_%s_%s_42", container.Name, podName, podNamespace, podUID)
|
|
|
|
podFullName := fmt.Sprintf("%s_%s", podName, podNamespace)
|
2015-01-10 01:19:31 +00:00
|
|
|
|
2015-03-20 11:55:02 +00:00
|
|
|
returned, hash, err := ParseDockerName(name)
|
2015-03-12 23:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to parse Docker container name %q: %v", name, err)
|
|
|
|
}
|
2015-03-20 11:55:02 +00:00
|
|
|
if returned.PodFullName != podFullName || string(returned.PodUID) != podUID || returned.ContainerName != container.Name || hash != 0 {
|
|
|
|
t.Errorf("unexpected parse: %s %s %s %d", returned.PodFullName, returned.PodUID, returned.ContainerName, hash)
|
2014-09-09 04:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 10:08:48 +00:00
|
|
|
func TestGetDockerServerVersion(t *testing.T) {
|
|
|
|
fakeDocker := &FakeDockerClient{VersionInfo: docker.Env{"Client version=1.2", "Server version=1.1.3", "Server API version=1.15"}}
|
|
|
|
runner := dockerContainerCommandRunner{fakeDocker}
|
2015-02-04 19:50:21 +00:00
|
|
|
version, err := runner.GetDockerServerVersion()
|
2014-10-14 10:08:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got error while getting docker server version - %s", err)
|
|
|
|
}
|
2014-12-16 21:06:58 +00:00
|
|
|
expectedVersion := []uint{1, 15}
|
2014-10-14 10:08:48 +00:00
|
|
|
if len(expectedVersion) != len(version) {
|
|
|
|
t.Errorf("invalid docker server version. expected: %v, got: %v", expectedVersion, version)
|
|
|
|
} else {
|
|
|
|
for idx, val := range expectedVersion {
|
|
|
|
if version[idx] != val {
|
|
|
|
t.Errorf("invalid docker server version. expected: %v, got: %v", expectedVersion, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecSupportExists(t *testing.T) {
|
2014-12-15 18:07:33 +00:00
|
|
|
fakeDocker := &FakeDockerClient{VersionInfo: docker.Env{"Client version=1.2", "Server version=1.3.0", "Server API version=1.15"}}
|
2014-10-14 10:08:48 +00:00
|
|
|
runner := dockerContainerCommandRunner{fakeDocker}
|
|
|
|
useNativeExec, err := runner.nativeExecSupportExists()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got error while checking for exec support - %s", err)
|
|
|
|
}
|
|
|
|
if !useNativeExec {
|
|
|
|
t.Errorf("invalid exec support check output. Expected true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecSupportNotExists(t *testing.T) {
|
2014-12-16 21:06:58 +00:00
|
|
|
fakeDocker := &FakeDockerClient{VersionInfo: docker.Env{"Client version=1.2", "Server version=1.1.2", "Server API version=1.14"}}
|
2014-10-14 10:08:48 +00:00
|
|
|
runner := dockerContainerCommandRunner{fakeDocker}
|
|
|
|
useNativeExec, _ := runner.nativeExecSupportExists()
|
|
|
|
if useNativeExec {
|
|
|
|
t.Errorf("invalid exec support check output.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 04:33:17 +00:00
|
|
|
func TestDockerContainerCommand(t *testing.T) {
|
|
|
|
runner := dockerContainerCommandRunner{}
|
|
|
|
containerID := "1234"
|
|
|
|
command := []string{"ls"}
|
|
|
|
cmd, _ := runner.getRunInContainerCommand(containerID, command)
|
|
|
|
if cmd.Dir != "/var/lib/docker/execdriver/native/"+containerID {
|
|
|
|
t.Errorf("unexpected command CWD: %s", cmd.Dir)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(cmd.Args, []string{"/usr/sbin/nsinit", "exec", "ls"}) {
|
2015-01-18 07:32:34 +00:00
|
|
|
t.Errorf("unexpected command args: %s", cmd.Args)
|
2014-09-09 04:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 00:51:20 +00:00
|
|
|
func TestParseImageName(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
imageName string
|
|
|
|
name string
|
|
|
|
tag string
|
|
|
|
}{
|
|
|
|
{"ubuntu", "ubuntu", ""},
|
|
|
|
{"ubuntu:2342", "ubuntu", "2342"},
|
|
|
|
{"ubuntu:latest", "ubuntu", "latest"},
|
|
|
|
{"foo/bar:445566", "foo/bar", "445566"},
|
|
|
|
{"registry.example.com:5000/foobar", "registry.example.com:5000/foobar", ""},
|
|
|
|
{"registry.example.com:5000/foobar:5342", "registry.example.com:5000/foobar", "5342"},
|
|
|
|
{"registry.example.com:5000/foobar:latest", "registry.example.com:5000/foobar", "latest"},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
name, tag := parseImageName(test.imageName)
|
|
|
|
if name != test.name || tag != test.tag {
|
|
|
|
t.Errorf("Expected name/tag: %s/%s, got %s/%s", test.name, test.tag, name, tag)
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 22:16:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 00:51:20 +00:00
|
|
|
func TestPull(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
imageName string
|
|
|
|
expectedImage string
|
|
|
|
}{
|
|
|
|
{"ubuntu", "ubuntu:latest"},
|
|
|
|
{"ubuntu:2342", "ubuntu:2342"},
|
|
|
|
{"ubuntu:latest", "ubuntu:latest"},
|
|
|
|
{"foo/bar:445566", "foo/bar:445566"},
|
|
|
|
{"registry.example.com:5000/foobar", "registry.example.com:5000/foobar:latest"},
|
|
|
|
{"registry.example.com:5000/foobar:5342", "registry.example.com:5000/foobar:5342"},
|
|
|
|
{"registry.example.com:5000/foobar:latest", "registry.example.com:5000/foobar:latest"},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
fakeKeyring := &credentialprovider.FakeKeyring{}
|
|
|
|
fakeClient := &FakeDockerClient{}
|
|
|
|
|
|
|
|
dp := dockerPuller{
|
|
|
|
client: fakeClient,
|
|
|
|
keyring: fakeKeyring,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := dp.Pull(test.imageName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected non-nil err: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := 1, len(fakeClient.pulled); e != a {
|
|
|
|
t.Errorf("%s: expected 1 pulled image, got %d: %v", test.imageName, a, fakeClient.pulled)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := test.expectedImage, fakeClient.pulled[0]; e != a {
|
|
|
|
t.Errorf("%s: expected pull of %q, but got %q", test.imageName, e, a)
|
2015-03-17 22:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:23:32 +00:00
|
|
|
func TestDockerKeyringLookupFails(t *testing.T) {
|
|
|
|
fakeKeyring := &credentialprovider.FakeKeyring{}
|
|
|
|
fakeClient := &FakeDockerClient{
|
|
|
|
Err: fmt.Errorf("test error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
dp := dockerPuller{
|
|
|
|
client: fakeClient,
|
|
|
|
keyring: fakeKeyring,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := dp.Pull("host/repository/image:version")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
2015-03-17 00:51:20 +00:00
|
|
|
msg := "image pull failed for host/repository/image:version, this may be because there are no credentials on this request. details: (test error)"
|
2015-02-10 15:23:32 +00:00
|
|
|
if err.Error() != msg {
|
|
|
|
t.Errorf("expected: %s, saw: %s", msg, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 01:13:19 +00:00
|
|
|
func TestDockerKeyringLookup(t *testing.T) {
|
|
|
|
empty := docker.AuthConfiguration{}
|
|
|
|
|
|
|
|
ada := docker.AuthConfiguration{
|
|
|
|
Username: "ada",
|
|
|
|
Password: "smash",
|
|
|
|
Email: "ada@example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
grace := docker.AuthConfiguration{
|
|
|
|
Username: "grace",
|
|
|
|
Password: "squash",
|
|
|
|
Email: "grace@example.com",
|
|
|
|
}
|
|
|
|
|
2014-11-15 13:50:59 +00:00
|
|
|
dk := &credentialprovider.BasicDockerKeyring{}
|
|
|
|
dk.Add(credentialprovider.DockerConfig{
|
|
|
|
"bar.example.com/pong": credentialprovider.DockerConfigEntry{
|
|
|
|
Username: grace.Username,
|
|
|
|
Password: grace.Password,
|
|
|
|
Email: grace.Email,
|
|
|
|
},
|
|
|
|
"bar.example.com": credentialprovider.DockerConfigEntry{
|
|
|
|
Username: ada.Username,
|
|
|
|
Password: ada.Password,
|
|
|
|
Email: ada.Email,
|
|
|
|
},
|
|
|
|
})
|
2014-09-06 01:13:19 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
image string
|
|
|
|
match docker.AuthConfiguration
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
// direct match
|
|
|
|
{"bar.example.com", ada, true},
|
|
|
|
|
|
|
|
// direct match deeper than other possible matches
|
|
|
|
{"bar.example.com/pong", grace, true},
|
|
|
|
|
|
|
|
// no direct match, deeper path ignored
|
|
|
|
{"bar.example.com/ping", ada, true},
|
|
|
|
|
|
|
|
// match first part of path token
|
|
|
|
{"bar.example.com/pongz", grace, true},
|
|
|
|
|
|
|
|
// match regardless of sub-path
|
|
|
|
{"bar.example.com/pong/pang", grace, true},
|
|
|
|
|
|
|
|
// no host match
|
|
|
|
{"example.com", empty, false},
|
|
|
|
{"foo.example.com", empty, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-11-15 13:50:59 +00:00
|
|
|
match, ok := dk.Lookup(tt.image)
|
2014-09-06 01:13:19 +00:00
|
|
|
if tt.ok != ok {
|
|
|
|
t.Errorf("case %d: expected ok=%t, got %t", i, tt.ok, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.match, match) {
|
|
|
|
t.Errorf("case %d: expected match=%#v, got %#v", i, tt.match, match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 20:33:38 +00:00
|
|
|
|
2015-01-26 22:06:12 +00:00
|
|
|
// This validates that dockercfg entries with a scheme and url path are properly matched
|
|
|
|
// by images that only match the hostname.
|
|
|
|
// NOTE: the above covers the case of a more specific match trumping just hostname.
|
|
|
|
func TestIssue3797(t *testing.T) {
|
|
|
|
rex := docker.AuthConfiguration{
|
|
|
|
Username: "rex",
|
|
|
|
Password: "tiny arms",
|
|
|
|
Email: "rex@example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
dk := &credentialprovider.BasicDockerKeyring{}
|
|
|
|
dk.Add(credentialprovider.DockerConfig{
|
|
|
|
"https://quay.io/v1/": credentialprovider.DockerConfigEntry{
|
|
|
|
Username: rex.Username,
|
|
|
|
Password: rex.Password,
|
|
|
|
Email: rex.Email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
image string
|
|
|
|
match docker.AuthConfiguration
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
// direct match
|
|
|
|
{"quay.io", rex, true},
|
|
|
|
|
|
|
|
// partial matches
|
|
|
|
{"quay.io/foo", rex, true},
|
|
|
|
{"quay.io/foo/bar", rex, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
match, ok := dk.Lookup(tt.image)
|
|
|
|
if tt.ok != ok {
|
|
|
|
t.Errorf("case %d: expected ok=%t, got %t", i, tt.ok, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.match, match) {
|
|
|
|
t.Errorf("case %d: expected match=%#v, got %#v", i, tt.match, match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 20:33:38 +00:00
|
|
|
type imageTrackingDockerClient struct {
|
|
|
|
*FakeDockerClient
|
|
|
|
imageName string
|
|
|
|
}
|
2014-12-13 01:43:07 +00:00
|
|
|
|
2014-12-10 20:33:38 +00:00
|
|
|
func (f *imageTrackingDockerClient) InspectImage(name string) (image *docker.Image, err error) {
|
|
|
|
image, err = f.FakeDockerClient.InspectImage(name)
|
|
|
|
f.imageName = name
|
|
|
|
return
|
|
|
|
}
|
2014-12-13 01:43:07 +00:00
|
|
|
|
2014-12-10 20:33:38 +00:00
|
|
|
func TestIsImagePresent(t *testing.T) {
|
2014-12-13 01:43:07 +00:00
|
|
|
cl := &imageTrackingDockerClient{&FakeDockerClient{}, ""}
|
2014-12-10 20:33:38 +00:00
|
|
|
puller := &dockerPuller{
|
|
|
|
client: cl,
|
|
|
|
}
|
|
|
|
_, _ = puller.IsImagePresent("abc:123")
|
|
|
|
if cl.imageName != "abc:123" {
|
|
|
|
t.Errorf("expected inspection of image abc:123, instead inspected image %v", cl.imageName)
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 20:14:16 +00:00
|
|
|
|
|
|
|
func TestGetRunningContainers(t *testing.T) {
|
|
|
|
fakeDocker := &FakeDockerClient{}
|
2015-04-02 20:14:52 +00:00
|
|
|
fakeRecorder := &record.FakeRecorder{}
|
2015-04-09 01:56:58 +00:00
|
|
|
containerManager := NewDockerManager(fakeDocker, fakeRecorder, PodInfraContainerImage)
|
2015-02-03 20:14:16 +00:00
|
|
|
tests := []struct {
|
|
|
|
containers map[string]*docker.Container
|
|
|
|
inputIDs []string
|
|
|
|
expectedIDs []string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
containers: map[string]*docker.Container{
|
|
|
|
"foobar": {
|
|
|
|
ID: "foobar",
|
|
|
|
State: docker.State{
|
|
|
|
Running: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"baz": {
|
|
|
|
ID: "baz",
|
|
|
|
State: docker.State{
|
|
|
|
Running: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputIDs: []string{"foobar", "baz"},
|
|
|
|
expectedIDs: []string{"baz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
containers: map[string]*docker.Container{
|
|
|
|
"foobar": {
|
|
|
|
ID: "foobar",
|
|
|
|
State: docker.State{
|
|
|
|
Running: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"baz": {
|
|
|
|
ID: "baz",
|
|
|
|
State: docker.State{
|
|
|
|
Running: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputIDs: []string{"foobar", "baz"},
|
|
|
|
expectedIDs: []string{"foobar", "baz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
containers: map[string]*docker.Container{
|
|
|
|
"foobar": {
|
|
|
|
ID: "foobar",
|
|
|
|
State: docker.State{
|
|
|
|
Running: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"baz": {
|
|
|
|
ID: "baz",
|
|
|
|
State: docker.State{
|
|
|
|
Running: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputIDs: []string{"foobar", "baz"},
|
|
|
|
expectedIDs: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
containers: map[string]*docker.Container{
|
|
|
|
"foobar": {
|
|
|
|
ID: "foobar",
|
|
|
|
State: docker.State{
|
|
|
|
Running: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"baz": {
|
|
|
|
ID: "baz",
|
|
|
|
State: docker.State{
|
|
|
|
Running: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputIDs: []string{"foobar", "baz"},
|
|
|
|
err: fmt.Errorf("test error"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
fakeDocker.ContainerMap = test.containers
|
|
|
|
fakeDocker.Err = test.err
|
2015-04-02 20:14:52 +00:00
|
|
|
if results, err := containerManager.GetRunningContainers(test.inputIDs); err == nil {
|
2015-02-03 20:14:16 +00:00
|
|
|
resultIDs := []string{}
|
|
|
|
for _, result := range results {
|
|
|
|
resultIDs = append(resultIDs, result.ID)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resultIDs, test.expectedIDs) {
|
|
|
|
t.Errorf("expected: %v, saw: %v", test.expectedIDs, resultIDs)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != test.err {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 01:33:48 +00:00
|
|
|
|
|
|
|
func TestFindContainersByPod(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
testContainers DockerContainers
|
|
|
|
inputPodID types.UID
|
|
|
|
inputPodFullName string
|
|
|
|
expectedContainers DockerContainers
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.UID("1234"),
|
|
|
|
"",
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_2343_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.UID("1234"),
|
|
|
|
"",
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_2343_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.UID("5678"),
|
|
|
|
"",
|
|
|
|
DockerContainers{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
|
|
|
Names: nil,
|
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_5678_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.UID("5678"),
|
|
|
|
"",
|
|
|
|
DockerContainers{
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_5678_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DockerContainers{
|
|
|
|
"foobar": &docker.APIContainers{
|
|
|
|
ID: "foobar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_abc_ns_5678_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
"baz": &docker.APIContainers{
|
|
|
|
ID: "baz",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_qux_ns_5678_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
2015-02-26 20:27:14 +00:00
|
|
|
"abc_ns",
|
2015-03-04 01:33:48 +00:00
|
|
|
DockerContainers{
|
|
|
|
"barbar": &docker.APIContainers{
|
|
|
|
ID: "barbar",
|
2015-02-26 20:27:14 +00:00
|
|
|
Names: []string{"/k8s_foo_abc_ns_5678_42"},
|
2015-03-04 01:33:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
result := test.testContainers.FindContainersByPod(test.inputPodID, test.inputPodFullName)
|
|
|
|
if !reflect.DeepEqual(result, test.expectedContainers) {
|
|
|
|
t.Errorf("expected: %v, saw: %v", test.expectedContainers, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 18:59:41 +00:00
|
|
|
|
|
|
|
func TestMakePortsAndBindings(t *testing.T) {
|
|
|
|
container := api.Container{
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: 80,
|
|
|
|
HostPort: 8080,
|
|
|
|
HostIP: "127.0.0.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ContainerPort: 443,
|
|
|
|
HostPort: 443,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ContainerPort: 444,
|
|
|
|
HostPort: 444,
|
|
|
|
Protocol: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ContainerPort: 445,
|
|
|
|
HostPort: 445,
|
|
|
|
Protocol: "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
exposedPorts, bindings := makePortsAndBindings(&container)
|
|
|
|
if len(container.Ports) != len(exposedPorts) ||
|
|
|
|
len(container.Ports) != len(bindings) {
|
|
|
|
t.Errorf("Unexpected ports and bindings, %#v %#v %#v", container, exposedPorts, bindings)
|
|
|
|
}
|
|
|
|
for key, value := range bindings {
|
|
|
|
switch value[0].HostPort {
|
|
|
|
case "8080":
|
|
|
|
if !reflect.DeepEqual(docker.Port("80/tcp"), key) {
|
|
|
|
t.Errorf("Unexpected docker port: %#v", key)
|
|
|
|
}
|
|
|
|
if value[0].HostIP != "127.0.0.1" {
|
|
|
|
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
|
|
|
}
|
|
|
|
case "443":
|
|
|
|
if !reflect.DeepEqual(docker.Port("443/tcp"), key) {
|
|
|
|
t.Errorf("Unexpected docker port: %#v", key)
|
|
|
|
}
|
|
|
|
if value[0].HostIP != "" {
|
|
|
|
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
|
|
|
}
|
|
|
|
case "444":
|
|
|
|
if !reflect.DeepEqual(docker.Port("444/udp"), key) {
|
|
|
|
t.Errorf("Unexpected docker port: %#v", key)
|
|
|
|
}
|
|
|
|
if value[0].HostIP != "" {
|
|
|
|
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
|
|
|
}
|
|
|
|
case "445":
|
|
|
|
if !reflect.DeepEqual(docker.Port("445/tcp"), key) {
|
|
|
|
t.Errorf("Unexpected docker port: %#v", key)
|
|
|
|
}
|
|
|
|
if value[0].HostIP != "" {
|
|
|
|
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|