-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathcontroller_test.go
More file actions
209 lines (179 loc) · 4.05 KB
/
controller_test.go
File metadata and controls
209 lines (179 loc) · 4.05 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package container
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestController_ListTags(t *testing.T) {
const registry = "mcr.microsoft.com"
const repo = "mssql/server"
ListTags(repo, "https://"+registry)
}
func TestController_EnsureImage(t *testing.T) {
const registry = "docker.io"
const repo = "library/alpine"
const tag = "latest"
const port = 0
imageName := fmt.Sprintf(
"%s/%s:%s",
registry,
repo,
tag)
c := NewController()
err := c.EnsureImage(imageName)
checkErr(err)
id := c.ContainerRun(
imageName,
[]string{},
port,
"",
"",
"amd64",
"linux",
[]string{"ash", "-c", "echo 'Hello World'; sleep 3"},
false,
)
c.ContainerRunning(id)
c.ContainerWaitForLogEntry(id, "Hello World")
c.ContainerExists(id)
c.ContainerFiles(id, "*.mdf")
// Bind to 0.0.0.0 so the container can reach the server via the
// Docker bridge network (host.docker.internal resolves to 172.17.0.1).
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("test"))
}))
l, err := net.Listen("tcp4", "0.0.0.0:0")
checkErr(err)
ts.Listener = l
ts.Start()
defer ts.Close()
// Build URL from listener port so it works regardless of whether
// the OS returns 127.0.0.1, localhost, or [::] in ts.URL.
_, tsPort, _ := net.SplitHostPort(ts.Listener.Addr().String())
tsURL := fmt.Sprintf("http://host.docker.internal:%s", tsPort)
c.DownloadFile(id, tsURL+"/test.bak", "/tmp")
err = c.ContainerStop(id)
checkErr(err)
err = c.ContainerStart(id)
checkErr(err)
err = c.ContainerStop(id)
checkErr(err)
err = c.ContainerRemove(id)
checkErr(err)
}
func TestController_ContainerRunFailure(t *testing.T) {
const registry = "docker.io"
const repo = "does-not-exist"
const tag = "latest"
imageName := fmt.Sprintf(
"%s/%s:%s",
registry,
repo,
tag)
c := NewController()
assert.Panics(t, func() {
c.ContainerRun(
imageName,
[]string{},
0,
"",
"",
"amd64",
"linux",
[]string{"ash", "-c", "echo 'Hello World'; sleep 1"},
false,
)
})
}
func TestController_ContainerRunFailureCleanup(t *testing.T) {
const registry = "docker.io"
const repo = "library/alpine"
const tag = "latest"
imageName := fmt.Sprintf(
"%s/%s:%s",
registry,
repo,
tag)
c := NewController()
assert.Panics(t, func() {
c.ContainerRun(
imageName,
[]string{},
0,
"",
"",
"amd64",
"linux",
[]string{"ash", "-c", "echo 'Hello World'; sleep 1"},
true,
)
})
}
func TestController_ContainerStopNeg2(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
err := c.ContainerStop("")
checkErr(err)
})
}
func TestController_ContainerRemoveNeg(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
err := c.ContainerRemove("")
checkErr(err)
})
}
func TestController_ContainerFilesNeg(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.ContainerFiles("", "")
})
}
func TestController_ContainerFilesNeg2(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.ContainerFiles("id", "")
})
}
func TestController_ContainerRunningNeg(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.ContainerRunning("")
})
}
func TestController_ContainerStartNeg(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
err := c.ContainerStart("")
checkErr(err)
})
}
func TestController_DownloadFileNeg(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.DownloadFile("", "", "")
})
}
func TestController_DownloadFileNeg2(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.DownloadFile("not_blank", "", "")
})
}
func TestController_DownloadFileNeg3(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.DownloadFile("not_blank", "not_blank", "")
})
}
func TestController_DownloadFileNoFilename(t *testing.T) {
c := NewController()
assert.Panics(t, func() {
c.DownloadFile("not_blank", "http://host:9999/", "/tmp")
})
}