-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdocker_test.go
152 lines (148 loc) · 3.3 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"strings"
"testing"
)
func TestDockerTag(t *testing.T) {
if !integration {
t.Skip("Skipping git integration test")
}
tests := []struct {
tagTemplate string
}{
{
tagTemplate: "{{ .GitBranch }}",
},
{
tagTemplate: "{{ .GitRevCount }}",
},
{
tagTemplate: "{{ .GitRevShort }}",
},
}
for i, test := range tests {
tag, err := dockerTag(test.tagTemplate)
if err != nil {
t.Errorf("(%d) Unexpected error: %s", i, err)
}
if tag == "" {
t.Errorf("(%d) Tag should not be empty", i)
}
if strings.Contains(tag, "\n") {
t.Errorf("(%d) Tag should not contain newline characters", i)
}
}
}
func TestDockerImageList(t *testing.T) {
if !integration {
t.Skip("Skipping docker registry integration test")
}
c := config{
Image: configImage{
Repository: "index.docker.io",
TagTemplate: "latest",
},
Environments: map[string]configEnvironment{
"prod": configEnvironment{
Images: map[string]configImage{
"hello": configImage{
Name: "library/hello-world",
},
},
},
},
}
images, err := dockerImageList(c, "prod")
if err != nil {
t.Errorf("Unexpected error getting Docker image list: %s", err)
}
if len(images) != 1 {
t.Fatalf("Expected image list length = 1, got: %d", len(images))
}
if _, ok := images["hello"]; !ok {
t.Fatalf(
"Expected 1st image key = '%s'",
"hello",
)
}
if images["hello"].Repository != "index.docker.io" {
t.Errorf(
"Expected 1st image repository = '%s', got: '%s",
"index.docker.io",
images["hello"].Repository,
)
}
if images["hello"].Name != "library/hello-world" {
t.Errorf(
"Expected 1st image name = '%s', got: '%s",
"library/hello-world",
images["hello"].Name,
)
}
if images["hello"].Tag != "latest" {
t.Errorf(
"Expected 1st image tag = '%s', got: '%s",
"latest",
images["hello"].Tag,
)
}
}
func TestDockerCheckImage(t *testing.T) {
tests := []struct {
image dockerImage
err string
}{
// TODO: Find a secure docker registry that supports anonymous tokens
// {
// image: dockerImage{
// Repository: "index.docker.io",
// Name: "secure-image",
// Tag: "b65349dad81",
// },
// err: "",
// },
{
image: dockerImage{
Repository: "index.docker.io",
Name: "library/hello-world",
Tag: "latest",
},
err: "",
},
{
image: dockerImage{
Repository: "index.docker.io",
Name: "library/hello-world",
Tag: "this-tag-does-not-exist",
},
err: "Docker image/tag (index.docker.io/library/hello-world:this-tag-does-not-exist) not found",
},
{
image: dockerImage{},
err: "Image repository cannot be blank",
},
{
image: dockerImage{
Repository: "index.docker.io",
},
err: "Image name cannot be blank",
},
{
image: dockerImage{
Repository: "index.docker.io",
Name: "library/hello-world",
},
err: "Image tag cannot be blank",
},
}
for i, test := range tests {
e := dockerCheckImage(test.image)
if e != nil && test.err == "" {
t.Errorf("(%d) Unexpected error: %s", i, e)
} else if e == nil && test.err != "" {
t.Errorf("(%d) Expected error '%s' but no error occurred", i, test.err)
} else if e != nil && e.Error() != test.err {
t.Errorf("(%d) Expected error '%s' but got '%s'", i, test.err, e)
}
}
}