This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #549 from BuxOrg/chore-351-pulse-req-logging
chore(BUX-351): improve logging when requesting Pulse
- Loading branch information
Showing
5 changed files
with
170 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package chainstate | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func initMockClient(ops ...ClientOps) (*Client, *buffLogger) { | ||
bLogger := newBuffLogger() | ||
ops = append(ops, WithLogger(bLogger.logger)) | ||
c, _ := NewClient( | ||
context.Background(), | ||
ops..., | ||
) | ||
return c.(*Client), bLogger | ||
} | ||
|
||
func TestVerifyMerkleRoots(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
mockURL := "http://pulse.test/api/v1/chain/merkleroot/verify" | ||
|
||
t.Run("no pulse client", func(t *testing.T) { | ||
c, _ := initMockClient() | ||
|
||
err := c.VerifyMerkleRoots(context.Background(), []MerkleRootConfirmationRequestItem{}) | ||
|
||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("pulse is not online", func(t *testing.T) { | ||
httpmock.Reset() | ||
httpmock.RegisterResponder("POST", mockURL, | ||
httpmock.NewStringResponder(500, `{"error":"Internal Server Error"}`), | ||
) | ||
c, bLogger := initMockClient(WithConnectionToPulse(mockURL, "")) | ||
|
||
err := c.VerifyMerkleRoots(context.Background(), []MerkleRootConfirmationRequestItem{}) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, 1, httpmock.GetTotalCallCount()) | ||
assert.True(t, bLogger.contains("pulse client returned status code 500")) | ||
}) | ||
|
||
t.Run("pulse wrong auth", func(t *testing.T) { | ||
httpmock.Reset() | ||
httpmock.RegisterResponder("POST", mockURL, | ||
httpmock.NewStringResponder(401, `Unauthorized`), | ||
) | ||
c, bLogger := initMockClient(WithConnectionToPulse(mockURL, "some-token")) | ||
|
||
err := c.VerifyMerkleRoots(context.Background(), []MerkleRootConfirmationRequestItem{}) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, 1, httpmock.GetTotalCallCount()) | ||
assert.True(t, bLogger.contains("401")) | ||
}) | ||
|
||
t.Run("pulse invalid state", func(t *testing.T) { | ||
httpmock.Reset() | ||
httpmock.RegisterResponder("POST", mockURL, | ||
httpmock.NewJsonResponderOrPanic(200, MerkleRootsConfirmationsResponse{ | ||
ConfirmationState: Invalid, | ||
Confirmations: []MerkleRootConfirmation{}, | ||
}), | ||
) | ||
c, bLogger := initMockClient(WithConnectionToPulse(mockURL, "some-token")) | ||
|
||
err := c.VerifyMerkleRoots(context.Background(), []MerkleRootConfirmationRequestItem{}) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, 1, httpmock.GetTotalCallCount()) | ||
assert.True(t, bLogger.contains("Not all merkle roots confirmed")) | ||
}) | ||
|
||
t.Run("pulse confirmedState", func(t *testing.T) { | ||
httpmock.Reset() | ||
httpmock.RegisterResponder("POST", mockURL, | ||
httpmock.NewJsonResponderOrPanic(200, MerkleRootsConfirmationsResponse{ | ||
ConfirmationState: Confirmed, | ||
Confirmations: []MerkleRootConfirmation{ | ||
{ | ||
Hash: "some-hash", | ||
BlockHeight: 1, | ||
MerkleRoot: "some-merkle-root", | ||
Confirmation: Confirmed, | ||
}, | ||
}, | ||
}), | ||
) | ||
c, bLogger := initMockClient(WithConnectionToPulse(mockURL, "some-token")) | ||
|
||
err := c.VerifyMerkleRoots(context.Background(), []MerkleRootConfirmationRequestItem{ | ||
{ | ||
MerkleRoot: "some-merkle-root", | ||
BlockHeight: 1, | ||
}, | ||
}) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, 1, httpmock.GetTotalCallCount()) | ||
assert.False(t, bLogger.contains("ERR")) | ||
assert.False(t, bLogger.contains("WARN")) | ||
}) | ||
} | ||
|
||
// buffLogger allows to check if a certain string was logged | ||
type buffLogger struct { | ||
logger *zerolog.Logger | ||
buf *bytes.Buffer | ||
} | ||
|
||
func newBuffLogger() *buffLogger { | ||
var buf bytes.Buffer | ||
logger := zerolog.New(&buf).Level(zerolog.DebugLevel).With().Logger() | ||
return &buffLogger{logger: &logger, buf: &buf} | ||
} | ||
|
||
func (l *buffLogger) contains(expected string) bool { | ||
return bytes.Contains(l.buf.Bytes(), []byte(expected)) | ||
} |