• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

golang-migrate / migrate / 24243069390

10 Apr 2026 12:30PM UTC coverage: 54.419% (-0.01%) from 54.432%
24243069390

Pull #1387

github

dbaxa
Migrate from github.com/docker/docker to github.com/moby/moby due to the docker namespace being deprecated

Signed-off-by: David Black <dblack@atlassian.com>
Pull Request #1387: Migrate from github.com/docker/docker to github.com/moby/moby due to the docker namespace being deprecated

0 of 27 new or added lines in 2 files covered. (0.0%)

4378 of 8045 relevant lines covered (54.42%)

48.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/testing/docker.go
1
// Package testing is used in driver tests and should only be used by migrate tests.
2
//
3
// Deprecated: If you'd like to test using Docker images, use package github.com/dhui/dktest instead
4
package testing
5

6
import (
7
        "bufio"
8
        "context"
9
        "encoding/json"
10
        "errors"
11
        "fmt"
12
        "io"
13
        "math/rand/v2"
14
        "strconv"
15
        "strings"
16
        "testing"
17

18
        dockercontainer "github.com/moby/moby/api/types/container"
19
        dockernetwork "github.com/moby/moby/api/types/network"
20
        dockerclient "github.com/moby/moby/client"
21
)
22

23
func NewDockerContainer(t testing.TB, image string, env []string, cmd []string) (*DockerContainer, error) {
×
NEW
24
        c, err := dockerclient.New(
×
25
                dockerclient.FromEnv,
×
26
        )
×
27
        if err != nil {
×
28
                return nil, err
×
29
        }
×
30

31
        if cmd == nil {
×
32
                cmd = make([]string, 0)
×
33
        }
×
34

35
        contr := &DockerContainer{
×
36
                t:         t,
×
37
                client:    c,
×
38
                ImageName: image,
×
39
                ENV:       env,
×
40
                Cmd:       cmd,
×
41
        }
×
42

×
43
        if err := contr.PullImage(); err != nil {
×
44
                return nil, err
×
45
        }
×
46

47
        if err := contr.Start(); err != nil {
×
48
                return nil, err
×
49
        }
×
50

51
        return contr, nil
×
52
}
53

54
// DockerContainer implements Instance interface
55
type DockerContainer struct {
56
        t                  testing.TB
57
        client             *dockerclient.Client
58
        ImageName          string
59
        ENV                []string
60
        Cmd                []string
61
        ContainerId        string
62
        ContainerName      string
63
        ContainerJSON      dockercontainer.InspectResponse
64
        containerInspected bool
65
        keepForDebugging   bool
66
}
67

68
func (d *DockerContainer) PullImage() (err error) {
×
69
        if d == nil {
×
70
                return errors.New("cannot pull image on a nil *DockerContainer")
×
71
        }
×
72
        d.t.Logf("Docker: Pull image %v", d.ImageName)
×
NEW
73
        r, err := d.client.ImagePull(context.Background(), d.ImageName, dockerclient.ImagePullOptions{})
×
74
        if err != nil {
×
75
                return err
×
76
        }
×
77
        defer func() {
×
78
                if errClose := r.Close(); errClose != nil {
×
79
                        err = errors.Join(err, errClose)
×
80
                }
×
81
        }()
82

83
        // read output and log relevant lines
84
        bf := bufio.NewScanner(r)
×
85
        for bf.Scan() {
×
86
                var resp dockerImagePullOutput
×
87
                if err := json.Unmarshal(bf.Bytes(), &resp); err != nil {
×
88
                        return err
×
89
                }
×
90
                if strings.HasPrefix(resp.Status, "Status: ") {
×
91
                        d.t.Logf("Docker: %v", resp.Status)
×
92
                }
×
93
        }
94
        return bf.Err()
×
95
}
96

97
func (d *DockerContainer) Start() error {
×
98
        if d == nil {
×
99
                return errors.New("cannot start a nil *DockerContainer")
×
100
        }
×
101

102
        containerName := fmt.Sprintf("migrate_test_%s", pseudoRandStr(10))
×
103

×
104
        // create container first
×
105
        resp, err := d.client.ContainerCreate(context.Background(),
×
NEW
106
                dockerclient.ContainerCreateOptions{
×
NEW
107
                        Config: &dockercontainer.Config{
×
NEW
108
                                Image:  d.ImageName,
×
NEW
109
                                Labels: map[string]string{"migrate_test": "true"},
×
NEW
110
                                Env:    d.ENV,
×
NEW
111
                                Cmd:    d.Cmd,
×
NEW
112
                        },
×
NEW
113
                        HostConfig: &dockercontainer.HostConfig{
×
NEW
114
                                PublishAllPorts: true,
×
NEW
115
                        },
×
NEW
116
                        NetworkingConfig: &dockernetwork.NetworkingConfig{},
×
NEW
117
                        Platform:         nil,
×
NEW
118
                        Name:             containerName,
×
NEW
119
                        Image:            "",
×
NEW
120
                })
×
121
        if err != nil {
×
122
                return err
×
123
        }
×
124

125
        d.ContainerId = resp.ID
×
126
        d.ContainerName = containerName
×
127

×
128
        // then start it
×
NEW
129
        if _, err := d.client.ContainerStart(context.Background(), resp.ID, dockerclient.ContainerStartOptions{}); err != nil {
×
130
                return err
×
131
        }
×
132

133
        d.t.Logf("Docker: Started container %v (%v) for image %v listening at %v:%v", resp.ID[0:12], containerName, d.ImageName, d.Host(), d.Port())
×
134
        for _, v := range resp.Warnings {
×
135
                d.t.Logf("Docker: Warning: %v", v)
×
136
        }
×
137
        return nil
×
138
}
139

140
func (d *DockerContainer) KeepForDebugging() {
×
141
        if d == nil {
×
142
                return
×
143
        }
×
144

145
        d.keepForDebugging = true
×
146
}
147

148
func (d *DockerContainer) Remove() error {
×
149
        if d == nil {
×
150
                return errors.New("cannot remove a nil *DockerContainer")
×
151
        }
×
152

153
        if d.keepForDebugging {
×
154
                return nil
×
155
        }
×
156

157
        if len(d.ContainerId) == 0 {
×
158
                return errors.New("missing containerId")
×
159
        }
×
NEW
160
        if _, err := d.client.ContainerRemove(context.Background(), d.ContainerId,
×
NEW
161
                dockerclient.ContainerRemoveOptions{
×
162
                        Force: true,
×
163
                }); err != nil {
×
164
                d.t.Log(err)
×
165
                return err
×
166
        }
×
167
        d.t.Logf("Docker: Removed %v", d.ContainerName)
×
168
        return nil
×
169
}
170

171
func (d *DockerContainer) Inspect() error {
×
172
        if d == nil {
×
173
                return errors.New("cannot inspect a nil *DockerContainer")
×
174
        }
×
175

176
        if len(d.ContainerId) == 0 {
×
177
                return errors.New("missing containerId")
×
178
        }
×
NEW
179
        resp, err := d.client.ContainerInspect(context.Background(), d.ContainerId, dockerclient.ContainerInspectOptions{})
×
180
        if err != nil {
×
181
                return err
×
182
        }
×
183

NEW
184
        d.ContainerJSON = resp.Container
×
185
        d.containerInspected = true
×
186
        return nil
×
187
}
188

189
func (d *DockerContainer) Logs() (io.ReadCloser, error) {
×
190
        if d == nil {
×
191
                return nil, errors.New("cannot view logs for a nil *DockerContainer")
×
192
        }
×
193
        if len(d.ContainerId) == 0 {
×
194
                return nil, errors.New("missing containerId")
×
195
        }
×
196

NEW
197
        return d.client.ContainerLogs(context.Background(), d.ContainerId, dockerclient.ContainerLogsOptions{
×
198
                ShowStdout: true,
×
199
                ShowStderr: true,
×
200
        })
×
201
}
202

203
func (d *DockerContainer) portMapping(selectFirst bool, cPort int) (hostIP string, hostPort uint, err error) {
×
204
        if !d.containerInspected {
×
205
                if err := d.Inspect(); err != nil {
×
206
                        d.t.Fatal(err)
×
207
                }
×
208
        }
209

210
        for port, bindings := range d.ContainerJSON.NetworkSettings.Ports {
×
NEW
211
                if !selectFirst && int(port.Num()) != cPort {
×
212
                        // Skip ahead until we find the port we want
×
213
                        continue
×
214
                }
215
                if len(bindings) > 0 {
×
216
                        binding := bindings[0]
×
217
                        hostPortUint, err := strconv.ParseUint(binding.HostPort, 10, 64)
×
218
                        if err != nil {
×
219
                                return "", 0, err
×
220
                        }
×
NEW
221
                        return bindings[0].HostIP.String(), uint(hostPortUint), nil
×
222
                }
223
        }
224

225
        if selectFirst {
×
226
                return "", 0, errors.New("no port binding")
×
227
        } else {
×
228
                return "", 0, errors.New("specified port not bound")
×
229
        }
×
230
}
231

232
func (d *DockerContainer) Host() string {
×
233
        if d == nil {
×
234
                panic("Cannot get host for a nil *DockerContainer")
×
235
        }
236
        hostIP, _, err := d.portMapping(true, -1)
×
237
        if err != nil {
×
238
                d.t.Fatal(err)
×
239
        }
×
240

241
        if hostIP == "0.0.0.0" {
×
242
                return "127.0.0.1"
×
243
        } else {
×
244
                return hostIP
×
245
        }
×
246
}
247

248
func (d *DockerContainer) Port() uint {
×
249
        if d == nil {
×
250
                panic("Cannot get port for a nil *DockerContainer")
×
251
        }
252
        _, port, err := d.portMapping(true, -1)
×
253
        if err != nil {
×
254
                d.t.Fatal(err)
×
255
        }
×
256
        return port
×
257
}
258

259
func (d *DockerContainer) PortFor(cPort int) uint {
×
260
        if d == nil {
×
261
                panic("Cannot get port for a nil *DockerContainer")
×
262
        }
263
        _, port, err := d.portMapping(false, cPort)
×
264
        if err != nil {
×
265
                d.t.Fatal(err)
×
266
        }
×
267
        return port
×
268
}
269

270
func (d *DockerContainer) NetworkSettings() dockercontainer.NetworkSettings {
×
271
        if d == nil {
×
272
                panic("Cannot get network settings for a nil *DockerContainer")
×
273
        }
274
        netSettings := d.ContainerJSON.NetworkSettings
×
275
        return *netSettings
×
276
}
277

278
type dockerImagePullOutput struct {
279
        Status          string `json:"status"`
280
        ProgressDetails struct {
281
                Current int `json:"current"`
282
                Total   int `json:"total"`
283
        } `json:"progressDetail"`
284
        Id       string `json:"id"`
285
        Progress string `json:"progress"`
286
}
287

288
func pseudoRandStr(n int) string {
×
289
        var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
×
290
        b := make([]rune, n)
×
291
        for i := range b {
×
292
                b[i] = letterRunes[rand.IntN(len(letterRunes))]
×
293
        }
×
294
        return string(b)
×
295
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc