Skip to content

Commit

Permalink
Merge pull request moby#48915 from cyphar/tests-assert-modern
Browse files Browse the repository at this point in the history
tests: migrate assertions to be more modern
  • Loading branch information
thaJeztah authored Nov 22, 2024
2 parents 4def7cb + 557e4ed commit ca0910c
Show file tree
Hide file tree
Showing 73 changed files with 483 additions and 461 deletions.
2 changes: 1 addition & 1 deletion api/types/filters/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func TestGetBoolOrDefault(t *testing.T) {
assert.Check(t, is.DeepEqual(expected.Value, actual.Value))

wrappedErr := fmt.Errorf("something went wrong: %w", err)
assert.Check(t, errors.Is(wrappedErr, err), "Expected a wrapped error to be detected as invalidFilter")
assert.Check(t, is.ErrorIs(wrappedErr, err), "Expected a wrapped error to be detected as invalidFilter")
}

assert.Check(t, is.Equal(tc.expectedValue, value))
Expand Down
4 changes: 3 additions & 1 deletion builder/remotecontext/git/gitutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestCheckoutGit(t *testing.T) {
assert.Check(t, is.Equal("subcontents", string(b)))
} else {
_, err := os.Stat(filepath.Join(r, "sub/subfile"))
assert.Assert(t, is.ErrorContains(err, ""))
assert.ErrorContains(t, err, "")
assert.Assert(t, os.IsNotExist(err))
}

Expand Down Expand Up @@ -373,6 +373,8 @@ func TestGitInvalidRef(t *testing.T) {
for _, url := range gitUrls {
_, err := Clone(url)
assert.Assert(t, err != nil)
// On Windows, git has different case for the "invalid refspec" error,
// so we can't use ErrorContains.
assert.Check(t, is.Contains(strings.ToLower(err.Error()), "invalid refspec"))
}
}
5 changes: 2 additions & 3 deletions client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client"
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -125,7 +124,7 @@ func TestCanceledContext(t *testing.T) {

_, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, is.ErrorType(err, errdefs.IsCancelled))
assert.Check(t, errors.Is(err, context.Canceled))
assert.Check(t, is.ErrorIs(err, context.Canceled))
}

func TestDeadlineExceededContext(t *testing.T) {
Expand All @@ -145,5 +144,5 @@ func TestDeadlineExceededContext(t *testing.T) {

_, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, is.ErrorType(err, errdefs.IsDeadline))
assert.Check(t, errors.Is(err, context.DeadlineExceeded))
assert.Check(t, is.ErrorIs(err, context.DeadlineExceeded))
}
2 changes: 1 addition & 1 deletion cmd/dockerd/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
loadedConfig, err := loadDaemonCliConfig(opts)
assert.NilError(t, err)
assert.Assert(t, loadedConfig != nil)
assert.Check(t, loadedConfig.TLS == nil)
assert.Check(t, is.Nil(loadedConfig.TLS))
}

func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion daemon/cluster/convert/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
swarmapi "github.com/moby/swarmkit/v2/api"

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestTopologyFromGRPC(t *testing.T) {
Expand All @@ -23,7 +24,7 @@ func TestTopologyFromGRPC(t *testing.T) {

func TestCapacityRangeFromGRPC(t *testing.T) {
nilCapacity := capacityRangeFromGRPC(nil)
assert.Assert(t, nilCapacity == nil)
assert.Assert(t, is.Nil(nilCapacity))

swarmZeroCapacity := &swarmapi.CapacityRange{}
zeroCapacity := capacityRangeFromGRPC(swarmZeroCapacity)
Expand Down
4 changes: 2 additions & 2 deletions daemon/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func containerListContainsName(containers []*containertypes.Summary, name string

func TestListInvalidFilter(t *testing.T) {
db, err := container.NewViewDB()
assert.Assert(t, err == nil)
assert.NilError(t, err)
d := &Daemon{
containersReplica: db,
}
Expand All @@ -94,7 +94,7 @@ func TestListInvalidFilter(t *testing.T) {

func TestNameFilter(t *testing.T) {
db, err := container.NewViewDB()
assert.Assert(t, err == nil)
assert.NilError(t, err)
d := &Daemon{
containersReplica: db,
}
Expand Down
3 changes: 1 addition & 2 deletions daemon/logger/local/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/docker/docker/daemon/logger"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -36,7 +35,7 @@ func testDecode(t *testing.T, buf []byte, split int) {
assert.NilError(t, err)

_, err = d.Decode()
assert.Assert(t, errors.Is(err, io.EOF))
assert.ErrorIs(t, err, io.EOF)

_, err = fw.Write(buf[split:])
assert.NilError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions daemon/logger/loggerutils/sharedtemp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestSharedTempFileConverter(t *testing.T) {
defer t.Logf("goroutine %v: exit", i)
start.Done()
_, err := uut.Do(src)
assert.Check(t, errors.Is(err, fakeErr), "in goroutine %v", i)
assert.Check(t, is.ErrorIs(err, fakeErr), "in goroutine %v", i)
}()
}
done.Wait()
Expand All @@ -190,7 +190,7 @@ func TestSharedTempFileConverter(t *testing.T) {
// request should retry from scratch.
fakeErr = errors.New("another fake error")
_, err = uut.Do(src)
assert.Check(t, errors.Is(err, fakeErr))
assert.Check(t, is.ErrorIs(err, fakeErr))

fakeErr = nil
f, err := uut.Do(src)
Expand Down
22 changes: 11 additions & 11 deletions daemon/oci_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {

err := daemon.setWindowsCredentialSpec(&container.Container{}, spec)
assert.NilError(t, err)
assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))

err = daemon.setWindowsCredentialSpec(&container.Container{HostConfig: &containertypes.HostConfig{}}, spec)
assert.NilError(t, err)
assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))

err = daemon.setWindowsCredentialSpec(&container.Container{HostConfig: &containertypes.HostConfig{SecurityOpt: []string{}}}, spec)
assert.NilError(t, err)
assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

dummyContainerID := "dummy-container-ID"
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory(`file://C:\path\to\my\credspec.json`), spec)
assert.ErrorContains(t, err, "invalid credential spec: file:// path cannot be absolute")

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

t.Run("it's not allowed to use a 'file://' option breaking out of the cred specs' directory", func(t *testing.T) {
Expand All @@ -99,7 +99,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory(`file://..\credspec.json`), spec)
assert.ErrorContains(t, err, fmt.Sprintf("invalid credential spec: file:// path must be under %s", credSpecsDir))

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

t.Run("when using a 'file://' option pointing to a file that doesn't exist, it fails gracefully", func(t *testing.T) {
Expand All @@ -108,7 +108,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory("file://i-dont-exist.json"), spec)
assert.Check(t, is.ErrorContains(err, fmt.Sprintf("failed to load credential spec for container %s", dummyContainerID)))
assert.Check(t, is.ErrorIs(err, os.ErrNotExist))
assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

t.Run("happy path with a 'registry://' option", func(t *testing.T) {
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory("registry://my-cred-spec"), spec)
assert.ErrorContains(t, err, fmt.Sprintf("registry key %s could not be opened: %v", credentialSpecRegistryLocation, dummyError))

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

t.Run("when using a 'registry://' option pointing to a value that doesn't exist, it fails gracefully", func(t *testing.T) {
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory("config://whatever"), spec)
assert.Equal(t, errInvalidCredentialSpecSecOpt, err)

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

t.Run("happy path with a 'raw://' option", func(t *testing.T) {
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory("credentialspe=config://whatever"), spec)
assert.ErrorContains(t, err, "security option not supported: credentialspe")

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

t.Run("it rejects unsupported credentialspec options", func(t *testing.T) {
Expand All @@ -259,7 +259,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory("idontexist://whatever"), spec)
assert.Equal(t, errInvalidCredentialSpecSecOpt, err)

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})

for _, option := range []string{"file", "registry", "config", "raw"} {
Expand All @@ -269,7 +269,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
err := daemon.setWindowsCredentialSpec(containerFactory(option+"://"), spec)
assert.Equal(t, errInvalidCredentialSpecSecOpt, err)

assert.Check(t, spec.Windows == nil)
assert.Check(t, is.Nil(spec.Windows))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion integration-cli/docker_api_attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *DockerAPISuite) TestGetContainersWsAttachContainerNotFound(c *testing.T
b, err := request.ReadBody(body)
assert.NilError(c, err)
expected := "No such container: doesnotexist"
assert.Assert(c, strings.Contains(getErrorMessage(c, b), expected))
assert.Assert(c, is.Contains(getErrorMessage(c, b), expected))
}

func (s *DockerAPISuite) TestPostContainersAttach(c *testing.T) {
Expand Down
32 changes: 16 additions & 16 deletions integration-cli/docker_api_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (s *DockerAPISuite) TestGetContainerStatsRmRunning(c *testing.T) {
assert.NilError(c, err)

cli.DockerCmd(c, "rm", "-f", id)
assert.Assert(c, <-chErr == nil)
assert.Assert(c, is.Nil(<-chErr))
}

// ChannelBuffer holds a chan of byte array that can be populate in a goroutine.
Expand Down Expand Up @@ -624,7 +624,7 @@ func (s *DockerAPISuite) TestContainerAPIVerifyHeader(c *testing.T) {

create := func(ct string) (*http.Response, io.ReadCloser, error) {
jsonData := bytes.NewBuffer(nil)
assert.Assert(c, json.NewEncoder(jsonData).Encode(config) == nil)
assert.NilError(c, json.NewEncoder(jsonData).Encode(config))
return request.Post(testutil.GetContext(c), "/containers/create", request.RawContent(io.NopCloser(jsonData)), request.ContentType(ct))
}

Expand Down Expand Up @@ -667,7 +667,7 @@ func (s *DockerAPISuite) TestContainerAPIInvalidPortSyntax(c *testing.T) {

b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, strings.Contains(string(b[:]), "invalid port"))
assert.Assert(c, is.Contains(string(b[:]), "invalid port"))
}

func (s *DockerAPISuite) TestContainerAPIRestartPolicyInvalidPolicyName(c *testing.T) {
Expand All @@ -687,7 +687,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartPolicyInvalidPolicyName(c *testi

b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, strings.Contains(string(b[:]), "invalid restart policy"))
assert.Assert(c, is.Contains(string(b[:]), "invalid restart policy"))
}

func (s *DockerAPISuite) TestContainerAPIRestartPolicyRetryMismatch(c *testing.T) {
Expand All @@ -707,7 +707,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartPolicyRetryMismatch(c *testing.T

b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, strings.Contains(string(b[:]), "invalid restart policy: maximum retry count can only be used with 'on-failure'"))
assert.Assert(c, is.Contains(string(b[:]), "invalid restart policy: maximum retry count can only be used with 'on-failure'"))
}

func (s *DockerAPISuite) TestContainerAPIRestartPolicyNegativeRetryCount(c *testing.T) {
Expand All @@ -727,7 +727,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartPolicyNegativeRetryCount(c *test

b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, strings.Contains(string(b[:]), "maximum retry count cannot be negative"))
assert.Assert(c, is.Contains(string(b[:]), "maximum retry count cannot be negative"))
}

func (s *DockerAPISuite) TestContainerAPIRestartPolicyDefaultRetryCount(c *testing.T) {
Expand Down Expand Up @@ -782,7 +782,7 @@ func (s *DockerAPISuite) TestContainerAPIPostCreateNull(c *testing.T) {
ID string
}
var ctr createResp
assert.Assert(c, json.Unmarshal(b, &ctr) == nil)
assert.NilError(c, json.Unmarshal(b, &ctr))
out := inspectField(c, ctr.ID, "HostConfig.CpusetCpus")
assert.Equal(c, out, "")

Expand All @@ -808,10 +808,10 @@ func (s *DockerAPISuite) TestCreateWithTooLowMemoryLimit(c *testing.T) {
res, body, err := request.Post(testutil.GetContext(c), "/containers/create", request.RawString(config), request.JSON)
assert.NilError(c, err)
b, err2 := request.ReadBody(body)
assert.Assert(c, err2 == nil)
assert.NilError(c, err2)

assert.Equal(c, res.StatusCode, http.StatusBadRequest)
assert.Assert(c, strings.Contains(string(b), "Minimum memory limit allowed is 6MB"))
assert.Assert(c, is.Contains(string(b), "Minimum memory limit allowed is 6MB"))
}

func (s *DockerAPISuite) TestContainerAPIRename(c *testing.T) {
Expand Down Expand Up @@ -856,7 +856,7 @@ func (s *DockerAPISuite) TestContainerAPIRestart(c *testing.T) {
err = apiClient.ContainerRestart(testutil.GetContext(c), name, container.StopOptions{Timeout: &timeout})
assert.NilError(c, err)

assert.Assert(c, waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second) == nil)
assert.NilError(c, waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second))
}

func (s *DockerAPISuite) TestContainerAPIRestartNotimeoutParam(c *testing.T) {
Expand All @@ -871,7 +871,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartNotimeoutParam(c *testing.T) {
err = apiClient.ContainerRestart(testutil.GetContext(c), name, container.StopOptions{})
assert.NilError(c, err)

assert.Assert(c, waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second) == nil)
assert.NilError(c, waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second))
}

func (s *DockerAPISuite) TestContainerAPIStart(c *testing.T) {
Expand Down Expand Up @@ -913,7 +913,7 @@ func (s *DockerAPISuite) TestContainerAPIStop(c *testing.T) {
Timeout: &timeout,
})
assert.NilError(c, err)
assert.Assert(c, waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second) == nil)
assert.NilError(c, waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second))

// second call to start should give 304
// maybe add ContainerStartWithRaw to test it
Expand Down Expand Up @@ -1075,7 +1075,7 @@ func (s *DockerAPISuite) TestContainerAPIPostContainerStop(c *testing.T) {

err = apiClient.ContainerStop(testutil.GetContext(c), containerID, container.StopOptions{})
assert.NilError(c, err)
assert.Assert(c, waitInspect(containerID, "{{ .State.Running }}", "false", 60*time.Second) == nil)
assert.NilError(c, waitInspect(containerID, "{{ .State.Running }}", "false", 60*time.Second))
}

// #14170
Expand Down Expand Up @@ -1347,7 +1347,7 @@ func (s *DockerAPISuite) TestPostContainersCreateMemorySwappinessHostConfigOmitt
containerJSON, err := apiClient.ContainerInspect(testutil.GetContext(c), ctr.ID)
assert.NilError(c, err)

assert.Assert(c, containerJSON.HostConfig.MemorySwappiness == nil)
assert.Assert(c, is.Nil(containerJSON.HostConfig.MemorySwappiness))
}

// check validation is done daemon side and not only in cli
Expand Down Expand Up @@ -1437,7 +1437,7 @@ func (s *DockerAPISuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T)
case <-time.After(2 * time.Second):
c.Fatal("stream was not closed after container was removed")
case sr := <-bc:
assert.Assert(c, sr.err == nil)
assert.NilError(c, sr.err)
sr.stats.Body.Close()
}
}
Expand Down Expand Up @@ -2032,7 +2032,7 @@ func (s *DockerAPISuite) TestContainersAPICreateMountsTmpfs(c *testing.T) {
assert.NilError(c, err)
out := cli.DockerCmd(c, "start", "-a", cName).Combined()
for _, option := range x.expectedOptions {
assert.Assert(c, strings.Contains(out, option))
assert.Assert(c, is.Contains(out, option))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions integration-cli/docker_api_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (s *DockerAPISuite) TestExecAPIStartValidCommand(c *testing.T) {
var inspectJSON struct{ ExecIDs []string }
inspectContainer(ctx, c, name, &inspectJSON)

assert.Assert(c, inspectJSON.ExecIDs == nil)
assert.Assert(c, is.Nil(inspectJSON.ExecIDs))
}

// #30311
Expand All @@ -179,7 +179,7 @@ func (s *DockerAPISuite) TestExecAPIStartInvalidCommand(c *testing.T) {
var inspectJSON struct{ ExecIDs []string }
inspectContainer(ctx, c, name, &inspectJSON)

assert.Assert(c, inspectJSON.ExecIDs == nil)
assert.Assert(c, is.Nil(inspectJSON.ExecIDs))
}

func (s *DockerAPISuite) TestExecStateCleanup(c *testing.T) {
Expand Down
Loading

0 comments on commit ca0910c

Please sign in to comment.