server: Move to gotest.tools

Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
Christopher Crone 2020-08-03 09:54:18 +02:00
parent a46ada6c47
commit 5ae94aceb6
4 changed files with 59 additions and 65 deletions

View File

@ -23,85 +23,77 @@ import (
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"github.com/docker/api/config"
apicontext "github.com/docker/api/context"
)
type interceptorSuite struct {
suite.Suite
dir string
ctx context.Context
}
func (is *interceptorSuite) BeforeTest(suiteName, testName string) {
func testContext(t *testing.T) context.Context {
dir, err := ioutil.TempDir("", "example")
require.Nil(is.T(), err)
assert.NilError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
ctx := context.Background()
ctx = config.WithDir(ctx, dir)
err = ioutil.WriteFile(path.Join(dir, "config.json"), []byte(`{"currentContext": "default"}`), 0644)
require.Nil(is.T(), err)
assert.NilError(t, err)
is.dir = dir
is.ctx = ctx
return ctx
}
func (is *interceptorSuite) AfterTest(suiteName, tesName string) {
err := os.RemoveAll(is.dir)
require.Nil(is.T(), err)
func TestUnaryGetCurrentContext(t *testing.T) {
ctx := testContext(t)
interceptor := unaryServerInterceptor(ctx)
currentContext := callUnary(context.Background(), t, interceptor)
assert.Equal(t, currentContext, "default")
}
func (is *interceptorSuite) TestUnaryGetCurrentContext() {
interceptor := unaryServerInterceptor(is.ctx)
currentContext := is.callUnary(context.Background(), interceptor)
assert.Equal(is.T(), "default", currentContext)
}
func (is *interceptorSuite) TestUnaryContextFromMetadata() {
func TestUnaryContextFromMetadata(t *testing.T) {
ctx := testContext(t)
contextName := "test"
interceptor := unaryServerInterceptor(is.ctx)
interceptor := unaryServerInterceptor(ctx)
reqCtx := context.Background()
reqCtx = metadata.NewIncomingContext(reqCtx, metadata.MD{
(key): []string{contextName},
})
currentContext := is.callUnary(reqCtx, interceptor)
assert.Equal(is.T(), contextName, currentContext)
currentContext := callUnary(reqCtx, t, interceptor)
assert.Equal(t, contextName, currentContext)
}
func (is *interceptorSuite) TestStreamGetCurrentContext() {
interceptor := streamServerInterceptor(is.ctx)
func TestStreamGetCurrentContext(t *testing.T) {
ctx := testContext(t)
interceptor := streamServerInterceptor(ctx)
currentContext := is.callStream(context.Background(), interceptor)
currentContext := callStream(context.Background(), t, interceptor)
assert.Equal(is.T(), "default", currentContext)
assert.Equal(t, currentContext, "default")
}
func (is *interceptorSuite) TestStreamContextFromMetadata() {
func TestStreamContextFromMetadata(t *testing.T) {
ctx := testContext(t)
contextName := "test"
interceptor := streamServerInterceptor(is.ctx)
interceptor := streamServerInterceptor(ctx)
reqCtx := context.Background()
reqCtx = metadata.NewIncomingContext(reqCtx, metadata.MD{
(key): []string{contextName},
})
currentContext := is.callStream(reqCtx, interceptor)
assert.Equal(is.T(), contextName, currentContext)
currentContext := callStream(reqCtx, t, interceptor)
assert.Equal(t, currentContext, contextName)
}
func (is *interceptorSuite) callStream(ctx context.Context, interceptor grpc.StreamServerInterceptor) string {
func callStream(ctx context.Context, t *testing.T, interceptor grpc.StreamServerInterceptor) string {
currentContext := ""
err := interceptor(nil, &contextServerStream{
ctx: ctx,
@ -112,12 +104,12 @@ func (is *interceptorSuite) callStream(ctx context.Context, interceptor grpc.Str
return nil
})
require.Nil(is.T(), err)
assert.NilError(t, err)
return currentContext
}
func (is *interceptorSuite) callUnary(ctx context.Context, interceptor grpc.UnaryServerInterceptor) string {
func callUnary(ctx context.Context, t *testing.T, interceptor grpc.UnaryServerInterceptor) string {
currentContext := ""
resp, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
FullMethod: "/com.docker.api.protos.context.v1.Contexts/test",
@ -126,12 +118,8 @@ func (is *interceptorSuite) callUnary(ctx context.Context, interceptor grpc.Unar
return nil, nil
})
require.Nil(is.T(), err)
require.Nil(is.T(), resp)
assert.NilError(t, err)
assert.Assert(t, cmp.Nil(resp))
return currentContext
}
func TestInterceptor(t *testing.T) {
suite.Run(t, new(interceptorSuite))
}

View File

@ -3,7 +3,7 @@ package proxy
import (
"testing"
"gotest.tools/assert"
"gotest.tools/v3/assert"
"github.com/docker/api/containers"
"github.com/docker/api/formatter"

View File

@ -20,8 +20,9 @@ import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
v1 "github.com/docker/api/protos/containers/v1"
)
@ -68,7 +69,9 @@ func TestLogStreamWriter(t *testing.T) {
l, err := sw.Write(in)
assert.Nil(t, err)
assert.Equal(t, len(in), l)
assert.Equal(t, expected, ls.logs)
assert.NilError(t, err)
assert.Assert(t, cmp.Len(in, l))
logs, ok := (ls.logs).(*v1.LogsResponse)
assert.Assert(t, ok)
assert.DeepEqual(t, logs.Value, expected.Value)
}

View File

@ -22,11 +22,12 @@ import (
"testing"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/anypb"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
streamsv1 "github.com/docker/api/protos/streams/v1"
)
@ -76,7 +77,7 @@ func getReader(t *testing.T, in []byte, errResult error) IO {
Value: in,
}
m, err := ptypes.MarshalAny(&message)
require.Nil(t, err)
assert.NilError(t, err)
return IO{
Stream: &Stream{
@ -93,7 +94,7 @@ func getAny(t *testing.T, in []byte) *any.Any {
Type: streamsv1.IOStream_STDOUT,
Value: in,
})
require.Nil(t, err)
assert.NilError(t, err)
return value
}
@ -104,9 +105,9 @@ func TestStreamReader(t *testing.T) {
n, err := r.Read(buffer)
assert.Nil(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, in, buffer)
assert.NilError(t, err)
assert.Equal(t, n, 5)
assert.DeepEqual(t, buffer, in)
}
func TestStreamReaderError(t *testing.T) {
@ -116,8 +117,8 @@ func TestStreamReaderError(t *testing.T) {
n, err := r.Read(buffer)
assert.Equal(t, 0, n)
assert.Equal(t, err, errResult)
assert.Equal(t, n, 0)
assert.Error(t, err, errResult.Error())
}
func TestStreamWriter(t *testing.T) {
@ -132,7 +133,9 @@ func TestStreamWriter(t *testing.T) {
}
n, err := w.Write(in)
assert.Nil(t, err)
assert.Equal(t, len(in), n)
assert.Equal(t, expected, bs.sendResult)
assert.NilError(t, err)
assert.Assert(t, cmp.Len(in, n))
sendResult, ok := (bs.sendResult).(*anypb.Any)
assert.Assert(t, ok)
assert.DeepEqual(t, sendResult.Value, expected.Value)
}