2014-06-24 01:28:06 +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.
|
|
|
|
*/
|
|
|
|
|
2014-09-09 04:33:17 +00:00
|
|
|
package dockertools
|
2014-06-24 01:28:06 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-19 12:29:42 +00:00
|
|
|
"fmt"
|
2014-09-09 04:33:17 +00:00
|
|
|
"reflect"
|
2014-07-23 16:53:31 +00:00
|
|
|
"sync"
|
2014-06-19 12:29:42 +00:00
|
|
|
|
2014-06-24 01:28:06 +00:00
|
|
|
"github.com/fsouza/go-dockerclient"
|
|
|
|
)
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// FakeDockerClient is a simple fake docker client, so that kubelet can be run for testing without requiring a real docker setup.
|
2014-06-24 01:28:06 +00:00
|
|
|
type FakeDockerClient struct {
|
2014-09-09 04:33:17 +00:00
|
|
|
sync.Mutex
|
|
|
|
ContainerList []docker.APIContainers
|
|
|
|
Container *docker.Container
|
2014-10-28 00:29:55 +00:00
|
|
|
ContainerMap map[string]*docker.Container
|
2014-10-03 07:34:18 +00:00
|
|
|
Image *docker.Image
|
2014-09-09 04:33:17 +00:00
|
|
|
Err error
|
2014-06-24 01:28:06 +00:00
|
|
|
called []string
|
2014-09-09 04:33:17 +00:00
|
|
|
Stopped []string
|
2014-06-19 12:29:42 +00:00
|
|
|
pulled []string
|
2014-06-24 01:28:06 +00:00
|
|
|
Created []string
|
2014-10-28 00:29:55 +00:00
|
|
|
Removed []string
|
2014-10-14 10:08:48 +00:00
|
|
|
VersionInfo docker.Env
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeDockerClient) clearCalls() {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-06-24 01:28:06 +00:00
|
|
|
f.called = []string{}
|
|
|
|
}
|
|
|
|
|
2014-09-09 04:33:17 +00:00
|
|
|
func (f *FakeDockerClient) AssertCalls(calls []string) (err error) {
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(calls, f.called) {
|
|
|
|
err = fmt.Errorf("expected %#v, got %#v", calls, f.called)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// ListContainers is a test-spy implementation of DockerInterface.ListContainers.
|
|
|
|
// It adds an entry "list" to the internal method call record.
|
2014-06-24 01:28:06 +00:00
|
|
|
func (f *FakeDockerClient) ListContainers(options docker.ListContainersOptions) ([]docker.APIContainers, error) {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-07-23 16:53:31 +00:00
|
|
|
f.called = append(f.called, "list")
|
2014-09-09 04:33:17 +00:00
|
|
|
return f.ContainerList, f.Err
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// InspectContainer is a test-spy implementation of DockerInterface.InspectContainer.
|
|
|
|
// It adds an entry "inspect" to the internal method call record.
|
2014-06-24 01:28:06 +00:00
|
|
|
func (f *FakeDockerClient) InspectContainer(id string) (*docker.Container, error) {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-10-03 07:34:18 +00:00
|
|
|
f.called = append(f.called, "inspect_container")
|
2014-10-28 00:29:55 +00:00
|
|
|
if f.ContainerMap != nil {
|
|
|
|
if container, ok := f.ContainerMap[id]; ok {
|
|
|
|
return container, f.Err
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 04:33:17 +00:00
|
|
|
return f.Container, f.Err
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 07:34:18 +00:00
|
|
|
// InspectImage is a test-spy implementation of DockerInterface.InspectImage.
|
|
|
|
// It adds an entry "inspect" to the internal method call record.
|
|
|
|
func (f *FakeDockerClient) InspectImage(name string) (*docker.Image, error) {
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
f.called = append(f.called, "inspect_image")
|
|
|
|
return f.Image, f.Err
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// CreateContainer is a test-spy implementation of DockerInterface.CreateContainer.
|
|
|
|
// It adds an entry "create" to the internal method call record.
|
2014-06-24 01:28:06 +00:00
|
|
|
func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*docker.Container, error) {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-07-23 16:53:31 +00:00
|
|
|
f.called = append(f.called, "create")
|
2014-06-24 01:28:06 +00:00
|
|
|
f.Created = append(f.Created, c.Name)
|
|
|
|
// This is not a very good fake. We'll just add this container's name to the list.
|
|
|
|
// Docker likes to add a '/', so copy that behavior.
|
2014-07-03 05:35:50 +00:00
|
|
|
name := "/" + c.Name
|
2014-10-02 18:58:58 +00:00
|
|
|
f.ContainerList = append(f.ContainerList, docker.APIContainers{ID: name, Names: []string{name}, Image: c.Config.Image})
|
2014-07-03 05:35:50 +00:00
|
|
|
return &docker.Container{ID: name}, nil
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// StartContainer is a test-spy implementation of DockerInterface.StartContainer.
|
|
|
|
// It adds an entry "start" to the internal method call record.
|
2014-06-24 01:28:06 +00:00
|
|
|
func (f *FakeDockerClient) StartContainer(id string, hostConfig *docker.HostConfig) error {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-07-23 16:53:31 +00:00
|
|
|
f.called = append(f.called, "start")
|
2014-11-07 06:41:16 +00:00
|
|
|
f.Container = &docker.Container{
|
|
|
|
ID: id,
|
|
|
|
Config: &docker.Config{Image: "testimage"},
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
}
|
2014-09-09 04:33:17 +00:00
|
|
|
return f.Err
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// StopContainer is a test-spy implementation of DockerInterface.StopContainer.
|
|
|
|
// It adds an entry "stop" to the internal method call record.
|
2014-06-24 01:28:06 +00:00
|
|
|
func (f *FakeDockerClient) StopContainer(id string, timeout uint) error {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-07-23 16:53:31 +00:00
|
|
|
f.called = append(f.called, "stop")
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Stopped = append(f.Stopped, id)
|
2014-07-03 05:35:50 +00:00
|
|
|
var newList []docker.APIContainers
|
2014-09-09 04:33:17 +00:00
|
|
|
for _, container := range f.ContainerList {
|
2014-07-03 05:35:50 +00:00
|
|
|
if container.ID != id {
|
|
|
|
newList = append(newList, container)
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 04:33:17 +00:00
|
|
|
f.ContainerList = newList
|
|
|
|
return f.Err
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
2014-06-24 23:31:33 +00:00
|
|
|
|
2014-10-28 00:29:55 +00:00
|
|
|
func (f *FakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) error {
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
f.called = append(f.called, "remove")
|
|
|
|
f.Removed = append(f.Removed, opts.ID)
|
|
|
|
return f.Err
|
|
|
|
}
|
|
|
|
|
2014-08-27 19:41:32 +00:00
|
|
|
// Logs is a test-spy implementation of DockerInterface.Logs.
|
|
|
|
// It adds an entry "logs" to the internal method call record.
|
|
|
|
func (f *FakeDockerClient) Logs(opts docker.LogsOptions) error {
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
f.called = append(f.called, "logs")
|
|
|
|
return f.Err
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// PullImage is a test-spy implementation of DockerInterface.StopContainer.
|
|
|
|
// It adds an entry "pull" to the internal method call record.
|
2014-06-19 12:29:42 +00:00
|
|
|
func (f *FakeDockerClient) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-07-23 16:53:31 +00:00
|
|
|
f.called = append(f.called, "pull")
|
2014-06-19 12:29:42 +00:00
|
|
|
f.pulled = append(f.pulled, fmt.Sprintf("%s/%s:%s", opts.Repository, opts.Registry, opts.Tag))
|
2014-09-09 04:33:17 +00:00
|
|
|
return f.Err
|
2014-06-19 12:29:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 10:08:48 +00:00
|
|
|
func (f *FakeDockerClient) Version() (*docker.Env, error) {
|
|
|
|
return &f.VersionInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeDockerClient) CreateExec(_ docker.CreateExecOptions) (*docker.Exec, error) {
|
|
|
|
return &docker.Exec{"12345678"}, nil
|
|
|
|
}
|
|
|
|
func (f *FakeDockerClient) StartExec(_ string, _ docker.StartExecOptions) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// FakeDockerPuller is a stub implementation of DockerPuller.
|
2014-06-24 23:31:33 +00:00
|
|
|
type FakeDockerPuller struct {
|
2014-09-09 04:33:17 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
2014-10-02 18:58:58 +00:00
|
|
|
HasImages []string
|
2014-06-24 23:31:33 +00:00
|
|
|
ImagesPulled []string
|
|
|
|
|
|
|
|
// Every pull will return the first error here, and then reslice
|
|
|
|
// to remove it. Will give nil errors if this slice is empty.
|
|
|
|
ErrorsToInject []error
|
|
|
|
}
|
|
|
|
|
2014-07-10 12:26:24 +00:00
|
|
|
// Pull records the image pull attempt, and optionally injects an error.
|
2014-07-23 16:53:31 +00:00
|
|
|
func (f *FakeDockerPuller) Pull(image string) (err error) {
|
2014-09-09 04:33:17 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2014-06-24 23:31:33 +00:00
|
|
|
f.ImagesPulled = append(f.ImagesPulled, image)
|
|
|
|
|
2014-07-23 16:53:31 +00:00
|
|
|
if len(f.ErrorsToInject) > 0 {
|
|
|
|
err = f.ErrorsToInject[0]
|
|
|
|
f.ErrorsToInject = f.ErrorsToInject[1:]
|
2014-06-24 23:31:33 +00:00
|
|
|
}
|
2014-07-23 16:53:31 +00:00
|
|
|
return err
|
2014-06-24 23:31:33 +00:00
|
|
|
}
|
2014-09-26 04:53:17 +00:00
|
|
|
|
|
|
|
func (f *FakeDockerPuller) IsImagePresent(name string) (bool, error) {
|
2014-10-02 18:58:58 +00:00
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
if f.HasImages == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
for _, s := range f.HasImages {
|
|
|
|
if s == name {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
2014-09-26 04:53:17 +00:00
|
|
|
}
|