Skip to content

Commit

Permalink
Use testify
Browse files Browse the repository at this point in the history
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
  • Loading branch information
mxpv committed Apr 2, 2022
1 parent 9766107 commit 871b6b6
Show file tree
Hide file tree
Showing 30 changed files with 357 additions and 363 deletions.
59 changes: 28 additions & 31 deletions cio/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ import (
"testing"

"github.com/containerd/fifo"
"github.com/google/go-cmp/cmp/cmpopts"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"github.com/stretchr/testify/assert"
)

func assertHasPrefix(t *testing.T, s, prefix string) {
Expand All @@ -52,7 +50,7 @@ func TestNewFIFOSetInDir(t *testing.T) {
root := t.TempDir()

fifos, err := NewFIFOSetInDir(root, "theid", true)
assert.NilError(t, err)
assert.NoError(t, err)

dir := filepath.Dir(fifos.Stdin)
assertHasPrefix(t, dir, root)
Expand All @@ -64,20 +62,19 @@ func TestNewFIFOSetInDir(t *testing.T) {
Terminal: true,
},
}
assert.Assert(t, is.DeepEqual(fifos, expected, cmpFIFOSet))

assert.Equal(t, fifos.Config, expected.Config)

files, err := os.ReadDir(root)
assert.NilError(t, err)
assert.Check(t, is.Len(files, 1))
assert.NoError(t, err)
assert.Len(t, files, 1)

assert.NilError(t, fifos.Close())
assert.Nil(t, fifos.Close())
files, err = os.ReadDir(root)
assert.NilError(t, err)
assert.Check(t, is.Len(files, 0))
assert.NoError(t, err)
assert.Len(t, files, 0)
}

var cmpFIFOSet = cmpopts.IgnoreUnexported(FIFOSet{})

func TestNewAttach(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("setupFIFOProducers not yet implemented on windows")
Expand All @@ -97,25 +94,25 @@ func TestNewAttach(t *testing.T) {
attacher := NewAttach(withBytesBuffers)

fifos, err := NewFIFOSetInDir("", "theid", false)
assert.NilError(t, err)
assert.NoError(t, err)

attachedFifos, err := attacher(fifos)
assert.NilError(t, err)
assert.NoError(t, err)
defer attachedFifos.Close()

producers := setupFIFOProducers(t, attachedFifos.Config())
initProducers(t, producers, expectedStdout, expectedStderr)

actualStdin, err := io.ReadAll(producers.Stdin)
assert.NilError(t, err)
assert.NoError(t, err)

attachedFifos.Wait()
attachedFifos.Cancel()
assert.NilError(t, attachedFifos.Close())
assert.Nil(t, attachedFifos.Close())

assert.Check(t, is.Equal(expectedStdout, stdout.String()))
assert.Check(t, is.Equal(expectedStderr, stderr.String()))
assert.Check(t, is.Equal(expectedStdin, string(actualStdin)))
assert.Equal(t, expectedStdout, stdout.String())
assert.Equal(t, expectedStderr, stderr.String())
assert.Equal(t, expectedStdin, string(actualStdin))
}

type producers struct {
Expand All @@ -132,41 +129,41 @@ func setupFIFOProducers(t *testing.T, fifos Config) producers {
)

pipes.Stdin, err = fifo.OpenFifo(ctx, fifos.Stdin, syscall.O_RDONLY, 0)
assert.NilError(t, err)
assert.NoError(t, err)

pipes.Stdout, err = fifo.OpenFifo(ctx, fifos.Stdout, syscall.O_WRONLY, 0)
assert.NilError(t, err)
assert.NoError(t, err)

pipes.Stderr, err = fifo.OpenFifo(ctx, fifos.Stderr, syscall.O_WRONLY, 0)
assert.NilError(t, err)
assert.NoError(t, err)

return pipes
}

func initProducers(t *testing.T, producers producers, stdout, stderr string) {
_, err := producers.Stdout.Write([]byte(stdout))
assert.NilError(t, err)
assert.NilError(t, producers.Stdout.Close())
assert.NoError(t, err)
assert.Nil(t, producers.Stdout.Close())

_, err = producers.Stderr.Write([]byte(stderr))
assert.NilError(t, err)
assert.NilError(t, producers.Stderr.Close())
assert.NoError(t, err)
assert.Nil(t, producers.Stderr.Close())
}

func TestBinaryIOArgs(t *testing.T) {
res, err := BinaryIO("/file.bin", map[string]string{"id": "1"})("")
assert.NilError(t, err)
assert.NoError(t, err)
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stdout)
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stderr)
}

func TestBinaryIOAbsolutePath(t *testing.T) {
res, err := BinaryIO("/full/path/bin", nil)("!")
assert.NilError(t, err)
assert.NoError(t, err)

// Test parse back
parsed, err := url.Parse(res.Config().Stdout)
assert.NilError(t, err)
assert.NoError(t, err)
assert.Equal(t, "binary", parsed.Scheme)
assert.Equal(t, "/full/path/bin", parsed.Path)
}
Expand All @@ -178,13 +175,13 @@ func TestBinaryIOFailOnRelativePath(t *testing.T) {

func TestLogFileAbsolutePath(t *testing.T) {
res, err := LogFile("/full/path/file.txt")("!")
assert.NilError(t, err)
assert.NoError(t, err)
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stdout)
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stderr)

// Test parse back
parsed, err := url.Parse(res.Config().Stdout)
assert.NilError(t, err)
assert.NoError(t, err)
assert.Equal(t, "file", parsed.Scheme)
assert.Equal(t, "/full/path/file.txt", parsed.Path)
}
Expand Down
4 changes: 2 additions & 2 deletions cio/io_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"path/filepath"
"testing"

"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

func TestOpenFifos(t *testing.T) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestOpenFifos(t *testing.T) {
}
for _, scenario := range scenarios {
_, err := openFifos(context.Background(), scenario)
assert.Assert(t, err != nil, scenario)
assert.Error(t, err, scenario)
}
}

Expand Down
18 changes: 9 additions & 9 deletions cio/io_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package cio
import (
"testing"

"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

func TestNewFifoSetInDir_NoTerminal(t *testing.T) {
Expand All @@ -28,10 +28,10 @@ func TestNewFifoSetInDir_NoTerminal(t *testing.T) {
t.Fatalf("NewFifoSetInDir failed with: %v", err)
}

assert.Assert(t, !set.Terminal, "FIFOSet.Terminal should be false")
assert.Assert(t, set.Stdin != "", "FIFOSet.Stdin should be set")
assert.Assert(t, set.Stdout != "", "FIFOSet.Stdout should be set")
assert.Assert(t, set.Stderr != "", "FIFOSet.Stderr should be set")
assert.True(t, !set.Terminal, "FIFOSet.Terminal should be false")
assert.NotEmpty(t, set.Stdin, "FIFOSet.Stdin should be set")
assert.NotEmpty(t, set.Stdout, "FIFOSet.Stdout should be set")
assert.NotEmpty(t, set.Stderr, "FIFOSet.Stderr should be set")
}

func TestNewFifoSetInDir_Terminal(t *testing.T) {
Expand All @@ -40,8 +40,8 @@ func TestNewFifoSetInDir_Terminal(t *testing.T) {
t.Fatalf("NewFifoSetInDir failed with: %v", err)
}

assert.Assert(t, set.Terminal, "FIFOSet.Terminal should be false")
assert.Assert(t, set.Stdin != "", "FIFOSet.Stdin should be set")
assert.Assert(t, set.Stdout != "", "FIFOSet.Stdout should be set")
assert.Assert(t, set.Stderr == "", "FIFOSet.Stderr should not be set")
assert.True(t, set.Terminal, "FIFOSet.Terminal should be true")
assert.NotEmpty(t, set.Stdin, "FIFOSet.Stdin should be set")
assert.NotEmpty(t, set.Stdout, "FIFOSet.Stdout should be set")
assert.Empty(t, set.Stderr, "FIFOSet.Stderr should not be set")
}
9 changes: 4 additions & 5 deletions content/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (

"github.com/containerd/containerd/errdefs"
"github.com/opencontainers/go-digest"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"github.com/stretchr/testify/assert"
)

type copySource struct {
Expand Down Expand Up @@ -81,9 +80,9 @@ func TestCopy(t *testing.T) {
testcase.source.size,
testcase.source.digest)

assert.NilError(t, err)
assert.Check(t, is.Equal(testcase.source.digest, testcase.writer.committedDigest))
assert.Check(t, is.Equal(testcase.expected, testcase.writer.String()))
assert.NoError(t, err)
assert.Equal(t, testcase.source.digest, testcase.writer.committedDigest)
assert.Equal(t, testcase.expected, testcase.writer.String())
})
}
}
Expand Down
8 changes: 5 additions & 3 deletions content/local/locks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ package local
import (
"testing"

"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTryLock(t *testing.T) {
err := tryLock("testref")
assert.NilError(t, err)
assert.NoError(t, err)
defer unlock("testref")

err = tryLock("testref")
assert.ErrorContains(t, err, "ref testref locked for ")
require.NotNil(t, err)
assert.Contains(t, err.Error(), "ref testref locked for ")
}
18 changes: 9 additions & 9 deletions content/local/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

type memoryLabelStore struct {
Expand Down Expand Up @@ -351,7 +351,7 @@ func checkWrite(ctx context.Context, t checker, cs content.Store, dgst digest.Di

func TestWriterTruncateRecoversFromIncompleteWrite(t *testing.T) {
cs, err := NewStore(t.TempDir())
assert.NilError(t, err)
assert.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -362,26 +362,26 @@ func TestWriterTruncateRecoversFromIncompleteWrite(t *testing.T) {
setupIncompleteWrite(ctx, t, cs, ref, total)

writer, err := cs.Writer(ctx, content.WithRef(ref), content.WithDescriptor(ocispec.Descriptor{Size: total}))
assert.NilError(t, err)
assert.NoError(t, err)

assert.NilError(t, writer.Truncate(0))
assert.Nil(t, writer.Truncate(0))

_, err = writer.Write(contentB)
assert.NilError(t, err)
assert.NoError(t, err)

dgst := digest.FromBytes(contentB)
err = writer.Commit(ctx, total, dgst)
assert.NilError(t, err)
assert.NoError(t, err)
}

func setupIncompleteWrite(ctx context.Context, t *testing.T, cs content.Store, ref string, total int64) {
writer, err := cs.Writer(ctx, content.WithRef(ref), content.WithDescriptor(ocispec.Descriptor{Size: total}))
assert.NilError(t, err)
assert.NoError(t, err)

_, err = writer.Write([]byte("bad data"))
assert.NilError(t, err)
assert.NoError(t, err)

assert.NilError(t, writer.Close())
assert.Nil(t, writer.Close())
}

func TestWriteReadEmptyFileTimestamp(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions content/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log/logtest"
"github.com/containerd/containerd/pkg/testutil"
digest "github.com/opencontainers/go-digest"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -271,7 +271,7 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
}

checkStatus(t, w1, expected, dgstFirst, preStart, postStart, preUpdate, postUpdate)
assert.NilError(t, w1.Close(), "close first writer")
assert.Nil(t, w1.Close(), "close first writer")

w2, err := cs.Writer(ctx, content.WithRef(ref), content.WithDescriptor(ocispec.Descriptor{Size: 256, Digest: dgst}))
if err != nil {
Expand All @@ -295,7 +295,7 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
}
postCommit := time.Now()

assert.NilError(t, w2.Close(), "close second writer")
assert.Nil(t, w2.Close(), "close second writer")
info := content.Info{
Digest: dgst,
Size: 256,
Expand Down
2 changes: 1 addition & 1 deletion contrib/apparmor/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package apparmor
import (
"testing"

"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

func TestCleanProfileName(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion gc/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"time"

"github.com/containerd/containerd/gc"
"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

func TestPauseThreshold(t *testing.T) {
Expand Down
Loading

0 comments on commit 871b6b6

Please sign in to comment.