Skip to content

Commit a7d4b91

Browse files
authored
Merge pull request moby#50524 from thaJeztah/cleanup_testutils
integration, integration-cli: remove various deprecated test-utilities, and some minor (linting) fixes
2 parents 79dd3b0 + 617326a commit a7d4b91

File tree

11 files changed

+98
-128
lines changed

11 files changed

+98
-128
lines changed

integration-cli/check_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,18 @@ func (s *DockerSuite) OnTimeout(t *testing.T) {
422422
if testEnv.IsRemoteDaemon() {
423423
return
424424
}
425-
path := filepath.Join(os.Getenv("DEST"), "docker.pid")
426-
b, err := os.ReadFile(path)
425+
pidFile := filepath.Join(os.Getenv("DEST"), "docker.pid")
426+
b, err := os.ReadFile(pidFile)
427427
if err != nil {
428-
t.Fatalf("Failed to get daemon PID from %s\n", path)
428+
t.Fatalf("Failed to get daemon PID from %s\n", pidFile)
429429
}
430430

431431
rawPid, err := strconv.ParseInt(string(b), 10, 32)
432432
if err != nil {
433-
t.Fatalf("Failed to parse pid from %s: %s\n", path, err)
433+
t.Fatalf("Failed to parse pid from %s: %s\n", pidFile, err)
434434
}
435435

436-
daemonPid := int(rawPid)
437-
if daemonPid > 0 {
436+
if daemonPid := int(rawPid); daemonPid > 0 {
438437
testdaemon.SignalDaemonDump(daemonPid)
439438
}
440439
}

integration-cli/daemon/daemon.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"time"
99

1010
"github.com/docker/docker/testutil/daemon"
11+
"github.com/moby/moby/api/types/container"
12+
"github.com/moby/moby/client"
13+
"github.com/moby/moby/client/pkg/stringid"
1114
"github.com/pkg/errors"
1215
"gotest.tools/v3/assert"
1316
"gotest.tools/v3/icmd"
@@ -83,12 +86,17 @@ func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
8386
func (d *Daemon) CheckActiveContainerCount(ctx context.Context) func(t *testing.T) (interface{}, string) {
8487
return func(t *testing.T) (interface{}, string) {
8588
t.Helper()
86-
out, err := d.Cmd("ps", "-q")
89+
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(d.Sock()))
8790
assert.NilError(t, err)
88-
if strings.TrimSpace(out) == "" {
89-
return 0, ""
91+
92+
ctrs, err := apiClient.ContainerList(ctx, container.ListOptions{})
93+
_ = apiClient.Close()
94+
assert.NilError(t, err)
95+
var out strings.Builder
96+
for _, ctr := range ctrs {
97+
out.WriteString(stringid.TruncateID(ctr.ID) + "\n")
9098
}
91-
return len(strings.Split(strings.TrimSpace(out), "\n")), fmt.Sprintf("output: %q", out)
99+
return len(ctrs), out.String()
92100
}
93101
}
94102

integration-cli/docker_api_containers_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,13 @@ func (s *DockerAPISuite) TestContainerAPICommitWithLabelInConfig(c *testing.T) {
484484
img, err := apiClient.ContainerCommit(testutil.GetContext(c), cName, options)
485485
assert.NilError(c, err)
486486

487-
label1 := inspectFieldMap(c, img.ID, "Config.Labels", "key1")
488-
assert.Equal(c, label1, "value1")
489-
490-
label2 := inspectFieldMap(c, img.ID, "Config.Labels", "key2")
491-
assert.Equal(c, label2, "value2")
487+
imgInspect, err := apiClient.ImageInspect(testutil.GetContext(c), img.ID)
488+
assert.NilError(c, err)
489+
assert.Check(c, is.Equal(imgInspect.Config.Labels["key1"], "value1"))
490+
assert.Check(c, is.Equal(imgInspect.Config.Labels["key2"], "value2"))
492491

493-
cmd := inspectField(c, img.ID, "Config.Cmd")
494-
assert.Equal(c, cmd, "[/bin/sh -c touch /test]", fmt.Sprintf("got wrong Cmd from commit: %q", cmd))
492+
expected := []string{"/bin/sh", "-c", "touch /test"}
493+
assert.Check(c, is.DeepEqual(imgInspect.Config.Cmd, expected))
495494

496495
// sanity check, make sure the image is what we think it is
497496
cli.DockerCmd(c, "run", img.ID, "ls", "/test")
@@ -1015,32 +1014,36 @@ func (s *DockerAPISuite) TestContainerAPIDeleteRemoveLinks(c *testing.T) {
10151014
func (s *DockerAPISuite) TestContainerAPIDeleteRemoveVolume(c *testing.T) {
10161015
testRequires(c, testEnv.IsLocalDaemon)
10171016

1018-
vol := "/testvolume"
1017+
testVol := "/testvolume"
10191018
if testEnv.DaemonInfo.OSType == "windows" {
1020-
vol = `c:\testvolume`
1019+
testVol = `c:\testvolume`
10211020
}
10221021

1023-
id := runSleepingContainer(c, "-v", vol)
1022+
id := runSleepingContainer(c, "-v", testVol)
10241023
cli.WaitRun(c, id)
10251024

1026-
source, err := inspectMountSourceField(id, vol)
1025+
apiClient, err := client.NewClientWithOpts(client.FromEnv)
10271026
assert.NilError(c, err)
1028-
_, err = os.Stat(source)
1027+
defer apiClient.Close()
1028+
1029+
ctrInspect, err := apiClient.ContainerInspect(testutil.GetContext(c), id)
1030+
assert.NilError(c, err)
1031+
assert.Assert(c, is.Len(ctrInspect.Mounts, 1), "expected to have 1 mount")
1032+
mnt := ctrInspect.Mounts[0]
1033+
assert.Equal(c, mnt.Destination, testVol)
1034+
1035+
_, err = os.Stat(mnt.Source)
10291036
assert.NilError(c, err)
10301037

10311038
removeOptions := container.RemoveOptions{
10321039
Force: true,
10331040
RemoveVolumes: true,
10341041
}
10351042

1036-
apiClient, err := client.NewClientWithOpts(client.FromEnv)
1037-
assert.NilError(c, err)
1038-
defer apiClient.Close()
1039-
10401043
err = apiClient.ContainerRemove(testutil.GetContext(c), id, removeOptions)
10411044
assert.NilError(c, err)
10421045

1043-
_, err = os.Stat(source)
1046+
_, err = os.Stat(mnt.Source)
10441047
assert.Assert(c, os.IsNotExist(err), "expected to get ErrNotExist error, got %v", err)
10451048
}
10461049

integration-cli/docker_cli_by_digest_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *DockerRegistrySuite) TestRemoveImageByDigest(t *testing.T) {
159159
assert.NilError(t, err, "unexpected error deleting image")
160160

161161
// try to inspect again - it should error this time
162-
_, err = inspectFieldWithError(imageReference, "Id")
162+
_, err = inspectFilter(imageReference, ".Id")
163163
// unexpected nil err trying to inspect what should be a non-existent image
164164
assert.ErrorContains(t, err, "No such object")
165165
}
@@ -441,7 +441,7 @@ func (s *DockerRegistrySuite) TestDeleteImageByIDOnlyPulledByDigest(t *testing.T
441441

442442
cli.DockerCmd(t, "rmi", imageID)
443443

444-
_, err = inspectFieldWithError(imageID, "Id")
444+
_, err = inspectFilter(imageID, ".Id")
445445
assert.ErrorContains(t, err, "", "image should have been deleted")
446446
}
447447

@@ -468,7 +468,7 @@ func (s *DockerRegistrySuite) TestDeleteImageWithDigestAndTag(t *testing.T) {
468468
cli.DockerCmd(t, "rmi", repoTag)
469469

470470
// rmi should have deleted the tag, the digest reference, and the image itself
471-
_, err = inspectFieldWithError(imageID, "Id")
471+
_, err = inspectFilter(imageID, ".Id")
472472
assert.ErrorContains(t, err, "", "image should have been deleted")
473473
}
474474

@@ -493,16 +493,16 @@ func (s *DockerRegistrySuite) TestDeleteImageWithDigestAndMultiRepoTag(t *testin
493493

494494
// rmi should have deleted repoTag and image reference, but left repoTag2
495495
inspectField(t, repoTag2, "Id")
496-
_, err = inspectFieldWithError(imageReference, "Id")
496+
_, err = inspectFilter(imageReference, ".Id")
497497
assert.ErrorContains(t, err, "", "image digest reference should have been removed")
498498

499-
_, err = inspectFieldWithError(repoTag, "Id")
499+
_, err = inspectFilter(repoTag, ".Id")
500500
assert.ErrorContains(t, err, "", "image tag reference should have been removed")
501501

502502
cli.DockerCmd(t, "rmi", repoTag2)
503503

504504
// rmi should have deleted the tag, the digest reference, and the image itself
505-
_, err = inspectFieldWithError(imageID, "Id")
505+
_, err = inspectFilter(imageID, ".Id")
506506
assert.ErrorContains(t, err, "", "image should have been deleted")
507507
}
508508

integration-cli/docker_cli_create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ func (s *DockerCLICreateSuite) TestCreateVolumesCreated(c *testing.T) {
152152
const name = "test_create_volume"
153153
cli.DockerCmd(c, "create", "--name", name, "-v", prefix+slash+"foo", "busybox")
154154

155-
dir, err := inspectMountSourceField(name, prefix+slash+"foo")
155+
mnt, err := inspectMountPoint(name, prefix+slash+"foo")
156156
assert.Assert(c, err == nil, "Error getting volume host path: %q", err)
157157

158-
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
158+
if _, err := os.Stat(mnt.Source); err != nil && os.IsNotExist(err) {
159159
c.Fatalf("Volume was not created")
160160
}
161161
if err != nil {

integration-cli/docker_cli_events_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,11 @@ func (s *DockerCLIEventSuite) TestEventsSinceInTheFuture(c *testing.T) {
690690

691691
since := daemonTime(c)
692692
until := since.Add(time.Duration(-24) * time.Hour)
693-
out, _, err := dockerCmdWithError("events", "--filter", "image=busybox", "--since", parseEventTime(since), "--until", parseEventTime(until))
693+
out, _, err := dockerCmdWithError("events",
694+
"--filter", "image=busybox",
695+
"--since", fmt.Sprintf("%d.%09d", since.Unix(), int64(since.Nanosecond())),
696+
"--until", fmt.Sprintf("%d.%09d", until.Unix(), int64(until.Nanosecond())),
697+
)
694698

695699
assert.ErrorContains(c, err, "")
696700
assert.Assert(c, is.Contains(out, "cannot be after `until`"))

integration-cli/docker_cli_restart_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *DockerCLIRestartSuite) TestRestartWithVolumes(c *testing.T) {
7676
out = strings.Trim(out, " \n\r")
7777
assert.Equal(c, out, "1")
7878

79-
source, err := inspectMountSourceField(cID, prefix+slash+"test")
79+
mnt, err := inspectMountPoint(cID, prefix+slash+"test")
8080
assert.NilError(c, err)
8181

8282
cli.DockerCmd(c, "restart", cID)
@@ -86,9 +86,9 @@ func (s *DockerCLIRestartSuite) TestRestartWithVolumes(c *testing.T) {
8686
out = strings.Trim(out, " \n\r")
8787
assert.Equal(c, out, "1")
8888

89-
sourceAfterRestart, err := inspectMountSourceField(cID, prefix+slash+"test")
89+
mountAfterRestart, err := inspectMountPoint(cID, prefix+slash+"test")
9090
assert.NilError(c, err)
91-
assert.Equal(c, source, sourceAfterRestart)
91+
assert.Equal(c, mnt.Source, mountAfterRestart.Source)
9292
}
9393

9494
func (s *DockerCLIRestartSuite) TestRestartDisconnectedContainer(c *testing.T) {

integration-cli/docker_cli_run_test.go

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -639,17 +639,17 @@ func (s *DockerCLIRunSuite) TestRunCreateVolumeWithSymlink(c *testing.T) {
639639
c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
640640
}
641641

642-
volPath, err := inspectMountSourceField("test-createvolumewithsymlink", "/bar/foo")
642+
mnt, err := inspectMountPoint("test-createvolumewithsymlink", "/bar/foo")
643643
assert.NilError(c, err)
644644

645645
_, exitCode, err = dockerCmdWithError("rm", "-v", "test-createvolumewithsymlink")
646646
if err != nil || exitCode != 0 {
647647
c.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
648648
}
649649

650-
_, err = os.Stat(volPath)
650+
_, err = os.Stat(mnt.Source)
651651
if !os.IsNotExist(err) {
652-
c.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
652+
c.Fatalf("[open] (expecting 'file does not exist' error) err: %v, mnt.Source: %s", err, mnt.Source)
653653
}
654654
}
655655

@@ -2109,26 +2109,26 @@ func (s *DockerCLIRunSuite) TestRunVolumesCleanPaths(c *testing.T) {
21092109
VOLUME `+prefix+`/foo/`))
21102110
cli.DockerCmd(c, "run", "-v", prefix+"/foo", "-v", prefix+"/bar/", "--name", "dark_helmet", "run_volumes_clean_paths")
21112111

2112-
out, err := inspectMountSourceField("dark_helmet", prefix+slash+"foo"+slash)
2112+
mnt, err := inspectMountPoint("dark_helmet", prefix+slash+"foo"+slash)
21132113
if !errors.Is(err, errMountNotFound) {
2114-
c.Fatalf("Found unexpected volume entry for '%s/foo/' in volumes\n%q", prefix, out)
2114+
c.Fatalf("Found unexpected volume entry for '%s/foo/' in volumes\n%q", prefix, mnt.Source)
21152115
}
21162116

2117-
out, err = inspectMountSourceField("dark_helmet", prefix+slash+`foo`)
2117+
mnt, err = inspectMountPoint("dark_helmet", prefix+slash+`foo`)
21182118
assert.NilError(c, err)
2119-
if !strings.Contains(strings.ToLower(out), strings.ToLower(testEnv.PlatformDefaults.VolumesConfigPath)) {
2120-
c.Fatalf("Volume was not defined for %s/foo\n%q", prefix, out)
2119+
if !strings.Contains(strings.ToLower(mnt.Source), strings.ToLower(testEnv.PlatformDefaults.VolumesConfigPath)) {
2120+
c.Fatalf("Volume was not defined for %s/foo\n%q", prefix, mnt.Source)
21212121
}
21222122

2123-
out, err = inspectMountSourceField("dark_helmet", prefix+slash+"bar"+slash)
2123+
mnt, err = inspectMountPoint("dark_helmet", prefix+slash+"bar"+slash)
21242124
if !errors.Is(err, errMountNotFound) {
2125-
c.Fatalf("Found unexpected volume entry for '%s/bar/' in volumes\n%q", prefix, out)
2125+
c.Fatalf("Found unexpected volume entry for '%s/bar/' in volumes\n%q", prefix, mnt.Source)
21262126
}
21272127

2128-
out, err = inspectMountSourceField("dark_helmet", prefix+slash+"bar")
2128+
mnt, err = inspectMountPoint("dark_helmet", prefix+slash+"bar")
21292129
assert.NilError(c, err)
2130-
if !strings.Contains(strings.ToLower(out), strings.ToLower(testEnv.PlatformDefaults.VolumesConfigPath)) {
2131-
c.Fatalf("Volume was not defined for %s/bar\n%q", prefix, out)
2130+
if !strings.Contains(strings.ToLower(mnt.Source), strings.ToLower(testEnv.PlatformDefaults.VolumesConfigPath)) {
2131+
c.Fatalf("Volume was not defined for %s/bar\n%q", prefix, mnt.Source)
21322132
}
21332133
}
21342134

@@ -2286,23 +2286,22 @@ func (s *DockerCLIRunSuite) TestRunMountShmMqueueFromHost(c *testing.T) {
22862286
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, NotUserNamespace)
22872287

22882288
cli.DockerCmd(c, "run", "-d", "--name", "shmfromhost", "-v", "/dev/shm:/dev/shm", "-v", "/dev/mqueue:/dev/mqueue", "busybox", "sh", "-c", "echo -n test > /dev/shm/test && touch /dev/mqueue/toto && top")
2289-
defer os.Remove("/dev/mqueue/toto")
2290-
defer os.Remove("/dev/shm/test")
2291-
volPath, err := inspectMountSourceField("shmfromhost", "/dev/shm")
2289+
c.Cleanup(func() {
2290+
err := os.Remove("/dev/shm/test")
2291+
assert.Check(c, err == nil || errors.Is(err, os.ErrNotExist))
2292+
err = os.Remove("/dev/mqueue/toto")
2293+
assert.Check(c, err == nil || errors.Is(err, os.ErrNotExist))
2294+
})
2295+
mnt, err := inspectMountPoint("shmfromhost", "/dev/shm")
22922296
assert.NilError(c, err)
2293-
if volPath != "/dev/shm" {
2294-
c.Fatalf("volumePath should have been /dev/shm, was %s", volPath)
2295-
}
2297+
assert.Equal(c, mnt.Source, "/dev/shm")
22962298

22972299
out := cli.DockerCmd(c, "run", "--name", "ipchost", "--ipc", "host", "busybox", "cat", "/dev/shm/test").Combined()
2298-
if out != "test" {
2299-
c.Fatalf("Output of /dev/shm/test expected test but found: %s", out)
2300-
}
2300+
assert.Equal(c, out, "test", "unexpected content for /dev/shm/test")
23012301

23022302
// Check that the mq was created
2303-
if _, err := os.Stat("/dev/mqueue/toto"); err != nil {
2304-
c.Fatalf("Failed to confirm '/dev/mqueue/toto' presence on host: %s", err.Error())
2305-
}
2303+
_, err = os.Stat("/dev/mqueue/toto")
2304+
assert.NilError(c, err, "failed to confirm '/dev/mqueue/toto' presence on host")
23062305
}
23072306

23082307
func (s *DockerCLIRunSuite) TestContainerNetworkMode(c *testing.T) {
@@ -2941,10 +2940,11 @@ func (s *DockerCLIRunSuite) TestRunNetworkFilesBindMount(c *testing.T) {
29412940
// Not applicable on Windows as uses Unix specific functionality
29422941
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
29432942

2944-
expected := "test123"
2943+
tmpDir := c.TempDir()
2944+
filename := filepath.Join(tmpDir, "testfile")
29452945

2946-
filename := createTmpFile(c, expected)
2947-
defer os.Remove(filename)
2946+
const expected = "test123"
2947+
assert.NilError(c, os.WriteFile(filename, []byte(expected), 0o644))
29482948

29492949
// #nosec G302 -- for user namespaced test runs, the temp file must be accessible to unprivileged root
29502950
if err := os.Chmod(filename, 0o646); err != nil {
@@ -2965,8 +2965,9 @@ func (s *DockerCLIRunSuite) TestRunNetworkFilesBindMountRO(c *testing.T) {
29652965
// Not applicable on Windows as uses Unix specific functionality
29662966
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
29672967

2968-
filename := createTmpFile(c, "test123")
2969-
defer os.Remove(filename)
2968+
tmpDir := c.TempDir()
2969+
filename := filepath.Join(tmpDir, "testfile")
2970+
assert.NilError(c, os.WriteFile(filename, []byte("test123"), 0o644))
29702971

29712972
// #nosec G302 -- for user namespaced test runs, the temp file must be accessible to unprivileged root
29722973
if err := os.Chmod(filename, 0o646); err != nil {
@@ -2987,8 +2988,9 @@ func (s *DockerCLIRunSuite) TestRunNetworkFilesBindMountROFilesystem(c *testing.
29872988
// Not applicable on Windows as uses Unix specific functionality
29882989
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, UserNamespaceROMount)
29892990

2990-
filename := createTmpFile(c, "test123")
2991-
defer os.Remove(filename)
2991+
tmpDir := c.TempDir()
2992+
filename := filepath.Join(tmpDir, "testfile")
2993+
assert.NilError(c, os.WriteFile(filename, []byte("test123"), 0o644))
29922994

29932995
// #nosec G302 -- for user namespaced test runs, the temp file must be accessible to unprivileged root
29942996
if err := os.Chmod(filename, 0o646); err != nil {
@@ -3077,7 +3079,7 @@ func (s *DockerCLIRunSuite) TestRunCreateContainerFailedCleanUp(c *testing.T) {
30773079
_, _, err := dockerCmdWithError("run", "--name", name, "--link", "nothing:nothing", "busybox")
30783080
assert.Assert(c, err != nil, "Expected docker run to fail!")
30793081

3080-
containerID, err := inspectFieldWithError(name, "Id")
3082+
containerID, err := inspectFilter(name, ".Id")
30813083
assert.Assert(c, err != nil, "Expected not to have this container: %s!", containerID)
30823084
assert.Equal(c, containerID, "", fmt.Sprintf("Expected not to have this container: %s!", containerID))
30833085
}

0 commit comments

Comments
 (0)