Merge pull request #410 from docker/server-tests

Move server package to gotest.tools
This commit is contained in:
Chris Crone 2020-08-04 10:26:47 +02:00 committed by GitHub
commit 22af875171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 65 deletions

View File

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

View File

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

View File

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