Skip to content

Commit

Permalink
fix to match output containing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
torao committed May 24, 2022
1 parent c79145b commit ab25481
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
6 changes: 5 additions & 1 deletion cmd/ostracon/commands/kms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func WithMockKMS(t *testing.T, dir, chainID string, f func(string, crypto.PrivKe
privKey := ed25519.GenPrivKeyFromSecret([]byte("🏺"))
shutdown := make(chan string)
go func() {
logger := log.NewOCLogger(log.NewSyncWriter(os.Stdout))
stdoutMutex.Lock()
stdout := os.Stdout
stdoutMutex.Unlock()

logger := log.NewOCLogger(log.NewSyncWriter(stdout))
logger.Info(fmt.Sprintf("MockKMS starting: [%s] %s", chainID, addr))
pv := privval.NewFilePV(privKey, path.Join(dir, "keyfile"), path.Join(dir, "statefile"))
connTimeout := 5 * time.Second
Expand Down
21 changes: 14 additions & 7 deletions cmd/ostracon/commands/show_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io/ioutil"
"os"
"sync"
"testing"

cfg "github.com/line/ostracon/config"
Expand Down Expand Up @@ -61,10 +62,11 @@ func TestShowValidatorWithKMS(t *testing.T) {
})
require.NoError(t, err)

// output must match the KMS public key
// output must contains the KMS public key
bz, err := tmjson.Marshal(privKey.PubKey())
require.NoError(t, err)
require.Equal(t, string(bz), output)
expected := string(bz)
require.Contains(t, output, expected)
})
}

Expand All @@ -78,17 +80,22 @@ func loadFilePVKey(t *testing.T, file string) privval.FilePVKey {
return privKey
}

func captureStdout(f func()) (string, error) {
original := os.Stdout
defer func() {
os.Stdout = original
}()
var stdoutMutex sync.Mutex

func captureStdout(f func()) (string, error) {
r, w, err := os.Pipe()
if err != nil {
return "", err
}

stdoutMutex.Lock()
original := os.Stdout
defer func() {
os.Stdout = original
}()
os.Stdout = w
stdoutMutex.Unlock()

f()
_ = w.Close()
var buffer bytes.Buffer
Expand Down

0 comments on commit ab25481

Please sign in to comment.