/
docker_test.go
93 lines (79 loc) · 2.06 KB
/
docker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"errors"
"testing"
"github.com/dchest/uniuri"
"github.com/fsouza/go-dockerclient"
)
type dockerMock struct {
host string
pulledImages []docker.PullImageOptions
containers []*docker.Container
}
func (d *dockerMock) ListContainers(options docker.ListContainersOptions) ([]docker.APIContainers, error) {
panic("")
}
func (d *dockerMock) InspectContainer(id string) (*docker.Container, error) {
panic("")
}
func (d *dockerMock) CreateContainer(docker.CreateContainerOptions) (*docker.Container, error) {
container := &docker.Container{
ID: uniuri.New(),
}
d.containers = append(d.containers, container)
return container, nil
}
func (d *dockerMock) StartContainer(id string, hostConfig *docker.HostConfig) error {
for _, c := range d.containers {
if c.ID == id {
return nil
}
}
return errors.New("No Such Container")
}
func (d *dockerMock) StopContainer(id string, timeout uint) error {
panic("")
}
func (d *dockerMock) RemoveContainer(opts docker.RemoveContainerOptions) error {
panic("")
}
func (d *dockerMock) WaitContainer(id string) (int, error) {
panic("")
}
func (d *dockerMock) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
d.pulledImages = append(d.pulledImages, opts)
return nil
}
func (d *dockerMock) AddEventListener(listener chan<- *docker.APIEvents) error {
panic("")
}
func TestRun(t *testing.T) {
runner := NewDockerRunner(&dockerMock{})
options := DockerRunOptions{
ContainerName: "test-container",
ContainerConfig: &docker.Config{},
HostConfig: &docker.HostConfig{},
}
_, err := runner.Run("ubuntu:14.04", options)
if err != nil {
t.Error(err)
}
}
func TestPull(t *testing.T) {
expects := map[string][2]string{
"ubuntu:14.04": [2]string{"ubuntu", "14.04"},
"ubuntu": [2]string{"ubuntu", "latest"},
}
for input, result := range expects {
d := dockerMock{}
puller := NewDockerPuller(&d)
puller.Pull(input)
image := d.pulledImages[0]
if image.Repository != result[0] {
t.Error(image)
}
if image.Tag != result[1] {
t.Error(image)
}
}
}