From bbf5da33a7f01e20a3b8a718843348dde6bfa6f8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 10 Dec 2022 15:16:08 +0100 Subject: [PATCH] remove use of deprecated github.com/bmizerany/assert package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The github.com/bmizerany/assert module has been deprecated and is no longer maintained. In addition, the dependency brings various indirect dependencies with it; module github.com/fluent/fluent-logger-golang go 1.19 require ( github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 github.com/tinylib/msgp v1.1.6 ) require ( github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/philhofer/fwd v1.1.1 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect ) The assertion package itself also looks to have some issues. For example, it's not marked as a `t.Helper()`, and while it includes the location of the error in the output, the failures are prefixed with the location of the assertion itself, which is somewhat confusing: === RUN Test_New_itShouldUseDefaultConfigValuesIfNoOtherProvided assert.go:15: /Users/thajeztah/go/src/github.com/fluent/fluent-logger-golang/fluent/fluent_test.go:275 assert.go:62: ! Unexpected: <24224> --- FAIL: Test_New_itShouldUseDefaultConfigValuesIfNoOtherProvided (0.00s) === RUN Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified assert.go:15: /Users/thajeztah/go/src/github.com/fluent/fluent-logger-golang/fluent/fluent_test.go:306 assert.go:62: ! Unexpected: <"unix"> --- FAIL: Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified (0.00s) === RUN Test_New_itShouldUseConfigValuesFromArguments assert.go:15: /Users/thajeztah/go/src/github.com/fluent/fluent-logger-golang/fluent/fluent_test.go:327 assert.go:62: ! Unexpected: <6666> --- FAIL: Test_New_itShouldUseConfigValuesFromArguments (0.00s) === RUN Test_New_itShouldUseConfigValuesFromMashalAsJSONArgument assert.go:15: /Users/thajeztah/go/src/github.com/fluent/fluent-logger-golang/fluent/fluent_test.go:333 assert.go:62: ! Unexpected: --- FAIL: Test_New_itShouldUseConfigValuesFromMashalAsJSONArgument (0.00s) === CONT TestNoPanicOnAsyncClose/Channel_closed_before_write assert.go:15: /Users/thajeztah/go/src/github.com/fluent/fluent-logger-golang/fluent/fluent_test.go:653 === CONT TestNoPanicOnAsyncClose/Channel_not_closed_at_all assert.go:15: /Users/thajeztah/go/src/github.com/fluent/fluent-logger-golang/fluent/fluent_test.go:655 === CONT TestNoPanicOnAsyncClose/Channel_closed_before_write assert.go:62: ! Unexpected: <&errors.errorString{s:"fluent#appendBuffer: Logger already closed"}> === CONT TestNoPanicOnAsyncClose/Channel_not_closed_at_all assert.go:62: ! Unexpected: <> While a good assertion library can help (for example by printing a rich diff output if a struct does not match), a look at how it's used shows that most cases are comparing primitive types (int, string, bool). This patch swaps the library for a local `assertEqual()` utility. With this patch, test-failures look like the example below: === RUN Test_New_itShouldUseDefaultConfigValuesIfNoOtherProvided fluent_test.go:281: got: '24224', expected: '24224' fluent_test.go:282: got: '127.0.0.1', expected: '127.0.0.1' fluent_test.go:283: got: '3s', expected: '3s' fluent_test.go:284: got: '0s', expected: '0s' fluent_test.go:285: got: '8192', expected: '8192' fluent_test.go:286: got: 'tcp', expected: 'tcp' fluent_test.go:287: got: '', expected: '' --- FAIL: Test_New_itShouldUseDefaultConfigValuesIfNoOtherProvided (0.00s) === RUN Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified fluent_test.go:312: got: 'unix', expected: 'unix' fluent_test.go:313: got: '/tmp/fluent-logger-golang.sock', expected: '/tmp/fluent-logger-golang.sock' --- FAIL: Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified (0.00s) === RUN Test_New_itShouldUseConfigValuesFromArguments fluent_test.go:333: got: '6666', expected: '6666' fluent_test.go:334: got: 'foobarhost', expected: 'foobarhost' --- FAIL: Test_New_itShouldUseConfigValuesFromArguments (0.00s) === RUN Test_New_itShouldUseConfigValuesFromMashalAsJSONArgument fluent_test.go:339: got: 'true', expected: 'true' --- FAIL: Test_New_itShouldUseConfigValuesFromMashalAsJSONArgument (0.00s) === RUN TestJsonConfig fluent_test.go:441: got: '{FluentPort:8888 FluentHost:localhost FluentNetwork:tcp FluentSocketPath:/var/tmp/fluent.sock Timeout:3µs WriteTimeout:6µs BufferLimit:10 RetryWait:5 MaxRetry:3 MaxRetryWait:0 TagPrefix:fluent Async:false ForceStopAsyncSend:false AsyncResultCallback: AsyncConnect:false MarshalAsJSON:true AsyncReconnectInterval:0 SubSecondPrecision:false RequestAck:false TlsInsecureSkipVerify:false}', expected: '{FluentPort:8888 FluentHost:localhost FluentNetwork:tcp FluentSocketPath:/var/tmp/fluent.sock Timeout:3µs WriteTimeout:6µs BufferLimit:10 RetryWait:5 MaxRetry:3 MaxRetryWait:0 TagPrefix:fluent Async:false ForceStopAsyncSend:false AsyncResultCallback: AsyncConnect:false MarshalAsJSON:true AsyncReconnectInterval:0 SubSecondPrecision:false RequestAck:false TlsInsecureSkipVerify:false}' --- FAIL: TestJsonConfig (0.00s) === CONT TestPostWithTime/without_Async fluent_test.go:177: got: '["acme.tag_name",1482493046,{"foo":"bar"},{}]', expected: '["acme.tag_name",1482493046,{"foo":"bar"},{}]' fluent_test.go:177: got: '["acme.tag_name",1482493050,{"fluentd":"is awesome"},{}]', expected: '["acme.tag_name",1482493050,{"fluentd":"is awesome"},{}]' fluent_test.go:177: got: '["acme.tag_name",1634263200,{"welcome":"to use"},{}]', expected: '["acme.tag_name",1634263200,{"welcome":"to use"},{}]' === CONT TestPostWithTime/with_Async fluent_test.go:177: got: '["acme.tag_name",1482493046,{"foo":"bar"},{}]', expected: '["acme.tag_name",1482493046,{"foo":"bar"},{}]' fluent_test.go:177: got: '["acme.tag_name",1482493050,{"fluentd":"is awesome"},{}]', expected: '["acme.tag_name",1482493050,{"fluentd":"is awesome"},{}]' fluent_test.go:177: got: '["acme.tag_name",1634263200,{"welcome":"to use"},{}]', expected: '["acme.tag_name",1634263200,{"welcome":"to use"},{}]' === CONT TestReconnectAndResendAfterTransientFailure/with_Async fluent_test.go:177: got: '["tag_name",1482493046,{"foo":"bar"},{}]', expected: '["tag_name",1482493046,{"foo":"bar"},{}]' === CONT TestReconnectAndResendAfterTransientFailure/without_Async fluent_test.go:177: got: '["tag_name",1482493046,{"foo":"bar"},{}]', expected: '["tag_name",1482493046,{"foo":"bar"},{}]' fluent_test.go:177: got: '["tag_name",1482493050,{"fluentd":"is awesome"},{}]', expected: '["tag_name",1482493050,{"fluentd":"is awesome"},{}]' === CONT TestReconnectAndResendAfterTransientFailure/with_Async fluent_test.go:177: got: '["tag_name",1482493050,{"fluentd":"is awesome"},{}]', expected: '["tag_name",1482493050,{"fluentd":"is awesome"},{}]' === CONT TestNoPanicOnAsyncClose/Channel_closed_before_write fluent_test.go:657: got: 'fluent#appendBuffer: Logger already closed', expected: 'fluent#appendBuffer: Logger already closed' === CONT TestNoPanicOnAsyncClose/Channel_not_closed_at_all fluent_test.go:659: got: '', expected: '' The list of dependencies have also been reduced with this patch: module github.com/fluent/fluent-logger-golang go 1.19 require github.com/tinylib/msgp v1.1.6 require github.com/philhofer/fwd v1.1.1 // indirect Signed-off-by: Sebastiaan van Stijn --- fluent/fluent_test.go | 57 +++++++++++++++++++++++++------------------ go.mod | 16 ++---------- go.sum | 19 --------------- 3 files changed, 35 insertions(+), 57 deletions(-) diff --git a/fluent/fluent_test.go b/fluent/fluent_test.go index 00a6086..cdd82f2 100644 --- a/fluent/fluent_test.go +++ b/fluent/fluent_test.go @@ -13,7 +13,6 @@ import ( "testing" "time" - "github.com/bmizerany/assert" "github.com/tinylib/msgp/msgp" ) @@ -162,13 +161,21 @@ func (d *testDialer) waitForNextDialing(accept bool, delayReads bool) *Conn { return conn } +// asserEqual asserts that actual and expected are equivalent, and otherwise +// marks the test as failed (t.Error). It uses reflect.DeepEqual internally. +func assertEqual(t *testing.T, actual, expected interface{}) { + t.Helper() + if !reflect.DeepEqual(actual, expected) { + t.Errorf("got: '%+v', expected: '%+v'", actual, expected) + } +} + // assertReceived is used below by test cases to assert the content written to a *Conn // matches an expected string. This is generally used in conjunction with // Conn.waitForNextWrite(). func assertReceived(t *testing.T, rcv []byte, expected string) { - if string(rcv) != expected { - t.Fatalf("got %s, expect %s", string(rcv), expected) - } + t.Helper() + assertEqual(t, string(rcv), expected) } // Conn extends net.Conn to add channels used to synchronise across goroutines, eg. @@ -272,13 +279,13 @@ func (c *Conn) Close() error { func Test_New_itShouldUseDefaultConfigValuesIfNoOtherProvided(t *testing.T) { f, _ := New(Config{}) - assert.Equal(t, f.Config.FluentPort, defaultPort) - assert.Equal(t, f.Config.FluentHost, defaultHost) - assert.Equal(t, f.Config.Timeout, defaultTimeout) - assert.Equal(t, f.Config.WriteTimeout, defaultWriteTimeout) - assert.Equal(t, f.Config.BufferLimit, defaultBufferLimit) - assert.Equal(t, f.Config.FluentNetwork, defaultNetwork) - assert.Equal(t, f.Config.FluentSocketPath, defaultSocketPath) + assertEqual(t, f.Config.FluentPort, defaultPort) + assertEqual(t, f.Config.FluentHost, defaultHost) + assertEqual(t, f.Config.Timeout, defaultTimeout) + assertEqual(t, f.Config.WriteTimeout, defaultWriteTimeout) + assertEqual(t, f.Config.BufferLimit, defaultBufferLimit) + assertEqual(t, f.Config.FluentNetwork, defaultNetwork) + assertEqual(t, f.Config.FluentSocketPath, defaultSocketPath) } func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) { @@ -302,8 +309,8 @@ func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) { return } defer f.Close() - assert.Equal(t, f.Config.FluentNetwork, network) - assert.Equal(t, f.Config.FluentSocketPath, socketFile) + assertEqual(t, f.Config.FluentNetwork, network) + assertEqual(t, f.Config.FluentSocketPath, socketFile) socketFile = "/tmp/fluent-logger-golang-xxx.sock" network = "unixxxx" @@ -322,13 +329,13 @@ func Test_New_itShouldUseUnixDomainSocketIfUnixSocketSpecified(t *testing.T) { func Test_New_itShouldUseConfigValuesFromArguments(t *testing.T) { f, _ := New(Config{FluentPort: 6666, FluentHost: "foobarhost"}) - assert.Equal(t, f.Config.FluentPort, 6666) - assert.Equal(t, f.Config.FluentHost, "foobarhost") + assertEqual(t, f.Config.FluentPort, 6666) + assertEqual(t, f.Config.FluentHost, "foobarhost") } func Test_New_itShouldUseConfigValuesFromMashalAsJSONArgument(t *testing.T) { f, _ := New(Config{MarshalAsJSON: true}) - assert.Equal(t, f.Config.MarshalAsJSON, true) + assertEqual(t, f.Config.MarshalAsJSON, true) } func Test_MarshalAsMsgpack(t *testing.T) { @@ -431,9 +438,7 @@ func TestJsonConfig(t *testing.T) { t.Error(err) } - if !reflect.DeepEqual(expect, got) { - t.Errorf("got %v, except %v", got, expect) - } + assertEqual(t, got, expect) } func TestPostWithTime(t *testing.T) { @@ -643,11 +648,11 @@ func TestNoPanicOnAsyncClose(t *testing.T) { if testcase.shouldError { f.Close() } - e := f.EncodeAndPostData("tag_name", time.Unix(1482493046, 0), map[string]string{"foo": "bar"}) + err = f.EncodeAndPostData("tag_name", time.Unix(1482493046, 0), map[string]string{"foo": "bar"}) if testcase.shouldError { - assert.Equal(t, fmt.Errorf("fluent#appendBuffer: Logger already closed"), e) + assertEqual(t, err, fmt.Errorf("fluent#appendBuffer: Logger already closed")) } else { - assert.Equal(t, nil, e) + assertEqual(t, err, nil) } }) } @@ -755,10 +760,14 @@ func TestSyncWriteAfterCloseFails(t *testing.T) { err = f.PostWithTime("tag_name", time.Unix(1482493050, 0), map[string]string{"foo": "buzz"}) // The event submission must fail, - assert.NotEqual(t, err, nil); + if err == nil { + t.Error("expected an error") + } // and also must keep Fluentd closed. - assert.NotEqual(t, f.closed, false); + if f.closed != true { + t.Error("expected Fluentd to be kept closed") + } }() conn := d.waitForNextDialing(true, false) diff --git a/go.mod b/go.mod index ad74e9c..53784cd 100644 --- a/go.mod +++ b/go.mod @@ -2,18 +2,6 @@ module github.com/fluent/fluent-logger-golang go 1.23.5 -require ( - github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 - github.com/tinylib/msgp v1.3.0 -) +require github.com/tinylib/msgp v1.3.0 -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/pretty v0.3.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.9.0 // indirect - github.com/stretchr/testify v1.10.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) +require github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect diff --git a/go.sum b/go.sum index 1e41ed8..eab54d2 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,4 @@ -github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= -github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1GshSTtih8C2gDs04w8dReiOGXrGLNoY= github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww= github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=