Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests update: use require assertion #1257

Merged
merged 6 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions examples/functions/golang/soloduel/mmf/matchfunction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ import (

"open-match.dev/open-match/pkg/pb"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMakeMatchesDeduplicate(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

poolNameToTickets := map[string][]*pb.Ticket{
"pool1": {{Id: "1"}},
"pool2": {{Id: "1"}},
}

matches, err := makeMatches(poolNameToTickets)
assert.Nil(err)
assert.Equal(len(matches), 0)
require.Nil(err)
require.Equal(len(matches), 0)
}

func TestMakeMatches(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

poolNameToTickets := map[string][]*pb.Ticket{
"pool1": {{Id: "1"}, {Id: "2"}, {Id: "3"}},
Expand All @@ -45,11 +45,11 @@ func TestMakeMatches(t *testing.T) {
}

matches, err := makeMatches(poolNameToTickets)
assert.Nil(err)
assert.Equal(len(matches), 3)
require.Nil(err)
require.Equal(len(matches), 3)

for _, match := range matches {
assert.Equal(2, len(match.Tickets))
assert.Equal(matchName, match.MatchFunction)
require.Equal(2, len(match.Tickets))
require.Equal(matchName, match.MatchFunction)
}
}
25 changes: 15 additions & 10 deletions internal/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"open-match.dev/open-match/internal/filter/testcases"
Expand All @@ -31,9 +31,10 @@ func TestMeetsCriteria(t *testing.T) {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
pf, err := NewPoolFilter(tc.Pool)
if err != nil {
t.Error("pool should be valid")
}

require.NoError(t, err)
require.NotNil(t, pf)

tc.Ticket.CreateTime = ptypes.TimestampNow()
if !pf.In(tc.Ticket) {
t.Error("ticket should be included in the pool")
Expand All @@ -45,9 +46,10 @@ func TestMeetsCriteria(t *testing.T) {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
pf, err := NewPoolFilter(tc.Pool)
if err != nil {
t.Error("pool should be valid")
}

require.NoError(t, err)
require.NotNil(t, pf)

tc.Ticket.CreateTime = ptypes.TimestampNow()
if pf.In(tc.Ticket) {
t.Error("ticket should be excluded from the pool")
Expand Down Expand Up @@ -83,10 +85,13 @@ func TestValidPoolFilter(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
pf, err := NewPoolFilter(tc.pool)
assert.Nil(t, pf)

require.Error(t, err)
require.Nil(t, pf)

s := status.Convert(err)
assert.Equal(t, tc.code, s.Code())
assert.Equal(t, tc.msg, s.Message())
require.Equal(t, tc.code, s.Code())
require.Equal(t, tc.msg, s.Message())
})
}
}
14 changes: 7 additions & 7 deletions internal/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

stackdriver "github.com/TV4/logrus-stackdriver-formatter"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewFormatter(t *testing.T) {
Expand All @@ -37,9 +37,9 @@ func TestNewFormatter(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("newFormatter(%s) => %s", tc.in, tc.expected), func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
actual := newFormatter(tc.in)
assert.Equal(reflect.TypeOf(tc.expected), reflect.TypeOf(actual))
require.Equal(reflect.TypeOf(tc.expected), reflect.TypeOf(actual))
})
}
}
Expand All @@ -60,9 +60,9 @@ func TestIsDebugLevel(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("isDebugLevel(%s) => %t", tc.in, tc.expected), func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
actual := isDebugLevel(tc.in)
assert.Equal(tc.expected, actual)
require.Equal(tc.expected, actual)
})
}
}
Expand All @@ -87,9 +87,9 @@ func TestToLevel(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("toLevel(%s) => %s", tc.in, tc.expected), func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
actual := toLevel(tc.in)
assert.Equal(tc.expected, actual)
require.Equal(tc.expected, actual)
})
}
}
22 changes: 11 additions & 11 deletions internal/rpc/clientcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -27,31 +27,31 @@ const (
)

func TestGetGRPC(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

cc := NewClientCache(viper.New())
client, err := cc.GetGRPC(fakeGRPCAddress)
assert.Nil(err)
require.Nil(err)

cachedClient, err := cc.GetGRPC(fakeGRPCAddress)
assert.Nil(err)
require.Nil(err)

// Test caching by comparing pointer value
assert.EqualValues(client, cachedClient)
require.EqualValues(client, cachedClient)
}

func TestGetHTTP(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

cc := NewClientCache(viper.New())
client, address, err := cc.GetHTTP(fakeHTTPAddress)
assert.Nil(err)
assert.Equal(fakeHTTPAddress, address)
require.Nil(err)
require.Equal(fakeHTTPAddress, address)

cachedClient, address, err := cc.GetHTTP(fakeHTTPAddress)
assert.Nil(err)
assert.Equal(fakeHTTPAddress, address)
require.Nil(err)
require.Equal(fakeHTTPAddress, address)

// Test caching by comparing pointer value
assert.EqualValues(client, cachedClient)
require.EqualValues(client, cachedClient)
}
80 changes: 41 additions & 39 deletions internal/rpc/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"open-match.dev/open-match/internal/config"
"open-match.dev/open-match/internal/telemetry"
Expand All @@ -34,39 +34,39 @@ import (
)

func TestSecureGRPCFromConfig(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

cfg, rpcParams, closer := configureConfigAndKeysForTesting(assert, true)
cfg, rpcParams, closer := configureConfigAndKeysForTesting(t, require, true)
defer closer()

runGrpcClientTests(t, assert, cfg, rpcParams)
runGrpcClientTests(t, require, cfg, rpcParams)
}

func TestInsecureGRPCFromConfig(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

cfg, rpcParams, closer := configureConfigAndKeysForTesting(assert, false)
cfg, rpcParams, closer := configureConfigAndKeysForTesting(t, require, false)
defer closer()

runGrpcClientTests(t, assert, cfg, rpcParams)
runGrpcClientTests(t, require, cfg, rpcParams)
}

func TestHTTPSFromConfig(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

cfg, rpcParams, closer := configureConfigAndKeysForTesting(assert, true)
cfg, rpcParams, closer := configureConfigAndKeysForTesting(t, require, true)
defer closer()

runHTTPClientTests(assert, cfg, rpcParams)
runHTTPClientTests(require, cfg, rpcParams)
}

func TestInsecureHTTPFromConfig(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

cfg, rpcParams, closer := configureConfigAndKeysForTesting(assert, false)
cfg, rpcParams, closer := configureConfigAndKeysForTesting(t, require, false)
defer closer()

runHTTPClientTests(assert, cfg, rpcParams)
runHTTPClientTests(require, cfg, rpcParams)
}

func TestSanitizeHTTPAddress(t *testing.T) {
Expand All @@ -88,15 +88,15 @@ func TestSanitizeHTTPAddress(t *testing.T) {
tc := testCase
description := fmt.Sprintf("sanitizeHTTPAddress(%s, %t) => (%s, %v)", tc.address, tc.preferHTTPS, tc.expected, tc.err)
t.Run(description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
actual, err := sanitizeHTTPAddress(tc.address, tc.preferHTTPS)
assert.Equal(tc.expected, actual)
assert.Equal(tc.err, err)
require.Equal(tc.expected, actual)
require.Equal(tc.err, err)
})
}
}

func runGrpcClientTests(t *testing.T, assert *assert.Assertions, cfg config.View, rpcParams *ServerParams) {
func runGrpcClientTests(t *testing.T, require *require.Assertions, cfg config.View, rpcParams *ServerParams) {
// Serve a fake frontend server and wait for its full start up
ff := &shellTesting.FakeFrontend{}
rpcParams.AddHandleFunc(func(s *grpc.Server) {
Expand All @@ -106,22 +106,22 @@ func runGrpcClientTests(t *testing.T, assert *assert.Assertions, cfg config.View
s := &Server{}
defer s.Stop()
err := s.Start(rpcParams)
assert.Nil(err)
require.Nil(err)

// Acquire grpc client
grpcConn, err := GRPCClientFromConfig(cfg, "test")
assert.Nil(err)
assert.NotNil(grpcConn)
require.Nil(err)
require.NotNil(grpcConn)

// Confirm the client works as expected
ctx := utilTesting.NewContext(t)
feClient := pb.NewFrontendServiceClient(grpcConn)
grpcResp, err := feClient.CreateTicket(ctx, &pb.CreateTicketRequest{})
assert.Nil(err)
assert.NotNil(grpcResp)
require.Nil(err)
require.NotNil(grpcResp)
}

func runHTTPClientTests(assert *assert.Assertions, cfg config.View, rpcParams *ServerParams) {
func runHTTPClientTests(require *require.Assertions, cfg config.View, rpcParams *ServerParams) {
// Serve a fake frontend server and wait for its full start up
ff := &shellTesting.FakeFrontend{}
rpcParams.AddHandleFunc(func(s *grpc.Server) {
Expand All @@ -130,34 +130,34 @@ func runHTTPClientTests(assert *assert.Assertions, cfg config.View, rpcParams *S
s := &Server{}
defer s.Stop()
err := s.Start(rpcParams)
assert.Nil(err)
require.Nil(err)

// Acquire http client
httpClient, baseURL, err := HTTPClientFromConfig(cfg, "test")
assert.Nil(err)
require.Nil(err)

// Confirm the client works as expected
httpReq, err := http.NewRequest(http.MethodGet, baseURL+telemetry.HealthCheckEndpoint, nil)
assert.Nil(err)
assert.NotNil(httpReq)
require.Nil(err)
require.NotNil(httpReq)

httpResp, err := httpClient.Do(httpReq)
assert.Nil(err)
assert.NotNil(httpResp)
require.Nil(err)
require.NotNil(httpResp)
defer func() {
if httpResp != nil {
httpResp.Body.Close()
}
}()

body, err := ioutil.ReadAll(httpResp.Body)
assert.Nil(err)
assert.Equal(200, httpResp.StatusCode)
assert.Equal("ok", string(body))
require.Nil(err)
require.Equal(200, httpResp.StatusCode)
require.Equal("ok", string(body))
}

// Generate a config view and optional TLS key manifests (optional) for testing
func configureConfigAndKeysForTesting(assert *assert.Assertions, tlsEnabled bool) (config.View, *ServerParams, func()) {
func configureConfigAndKeysForTesting(t *testing.T, require *require.Assertions, tlsEnabled bool) (config.View, *ServerParams, func()) {
// Create netlisteners on random ports used for rpc serving
grpcL := MustListen()
httpL := MustListen()
Expand All @@ -171,27 +171,27 @@ func configureConfigAndKeysForTesting(assert *assert.Assertions, tlsEnabled bool

// Create temporary TLS key files for testing
pubFile, err := ioutil.TempFile("", "pub*")
assert.Nil(err)
require.Nil(err)

if tlsEnabled {
// Generate public and private key bytes
pubBytes, priBytes, err := certgenTesting.CreateCertificateAndPrivateKeyForTesting([]string{
fmt.Sprintf("localhost:%s", MustGetPortNumber(grpcL)),
fmt.Sprintf("localhost:%s", MustGetPortNumber(httpL)),
})
assert.Nil(err)
require.Nil(err)

// Write certgen key bytes to the temp files
err = ioutil.WriteFile(pubFile.Name(), pubBytes, 0400)
assert.Nil(err)
require.Nil(err)

// Generate a config view with paths to the manifests
cfg.Set(configNameClientTrustedCertificatePath, pubFile.Name())

rpcParams.SetTLSConfiguration(pubBytes, pubBytes, priBytes)
}

return cfg, rpcParams, func() { removeTempFile(assert, pubFile.Name()) }
return cfg, rpcParams, func() { removeTempFile(t, pubFile.Name()) }
}

func MustListen() net.Listener {
Expand All @@ -210,9 +210,11 @@ func MustGetPortNumber(l net.Listener) string {
return port
}

func removeTempFile(assert *assert.Assertions, paths ...string) {
func removeTempFile(t *testing.T, paths ...string) {
for _, path := range paths {
err := os.Remove(path)
assert.Nil(err)
if err != nil {
t.Errorf("Can not remove the temporary file: %s, err: %s", path, err.Error())
}
}
}