mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
cmd/geth: test for logging-output
This commit is contained in:
parent
da55b23d21
commit
f66e0999c4
5 changed files with 130 additions and 0 deletions
88
cmd/geth/logging_test.go
Normal file
88
cmd/geth/logging_test.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// Copyright 2023 The go-ethereum Authors
|
||||
// This file is part of go-ethereum.
|
||||
//
|
||||
// go-ethereum is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// go-ethereum is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/pkg/reexec"
|
||||
)
|
||||
|
||||
func runSelf(args ...string) ([]byte, error) {
|
||||
cmd := &exec.Cmd{
|
||||
Path: reexec.Self(),
|
||||
Args: append([]string{"geth-test"}, args...),
|
||||
}
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
||||
func split(input io.Reader) []string {
|
||||
var output []string
|
||||
scanner := bufio.NewScanner(input)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
output = append(output, scanner.Text())
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func censor(input string, start, end int) string {
|
||||
if len(input) < end {
|
||||
return input
|
||||
}
|
||||
return input[:start] + strings.Repeat("X", end-start) + input[end:]
|
||||
}
|
||||
|
||||
func TestLogging(t *testing.T) {
|
||||
testConsoleLogging(t, "terminal", 6, 24)
|
||||
testConsoleLogging(t, "logfmt", 2, 26)
|
||||
}
|
||||
|
||||
func testConsoleLogging(t *testing.T, format string, tStart, tEnd int) {
|
||||
haveB, err := runSelf("--log.format", format, "logtest")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readFile, err := os.Open(fmt.Sprintf("testdata/logging/logtest-%v.txt", format))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantLines := split(readFile)
|
||||
haveLines := split(bytes.NewBuffer(haveB))
|
||||
for i, want := range wantLines {
|
||||
if i > len(haveLines)-1 {
|
||||
t.Fatalf("format %v, line %d missing, want:%v", format, i, want)
|
||||
}
|
||||
have := haveLines[i]
|
||||
// Black out the timestamp
|
||||
have = censor(have, tStart, tEnd)
|
||||
want = censor(want, tStart, tEnd)
|
||||
if have != want {
|
||||
t.Fatalf("format %v, line %d\nhave %v\nwant %v", format, i, have, want)
|
||||
}
|
||||
}
|
||||
if len(haveLines) != len(wantLines) {
|
||||
t.Errorf("format %v, want %d lines, have %d", format, len(haveLines), len(wantLines))
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -45,6 +46,7 @@ import (
|
|||
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
|
||||
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
|
||||
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
|
|
@ -233,6 +235,7 @@ func init() {
|
|||
snapshotCommand,
|
||||
// See verkle.go
|
||||
verkleCommand,
|
||||
logTestCommand,
|
||||
}
|
||||
sort.Sort(cli.CommandsByName(app.Commands))
|
||||
|
||||
|
|
@ -470,3 +473,28 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) {
|
|||
unlockAccount(ks, account, i, passwords)
|
||||
}
|
||||
}
|
||||
|
||||
// logTest is an entry point which spits out some logs. This is used by testing
|
||||
// to verify expected outputs
|
||||
func logTest(ctx *cli.Context) error {
|
||||
ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999"
|
||||
bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999"
|
||||
bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900"
|
||||
bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900"
|
||||
|
||||
ua, _ := uint256.FromDecimal("111222333444555678999")
|
||||
ub, _ := uint256.FromDecimal("11122233344455567899900")
|
||||
|
||||
log.Info("Output testing started",
|
||||
"big.Int", ba,
|
||||
"-big.Int", bb,
|
||||
"big.Int", bc,
|
||||
"-big.Int", bd)
|
||||
log.Info("Testing uint256",
|
||||
"uint256.Int", ua,
|
||||
"uint256.Int", ub)
|
||||
log.Info("Special chars",
|
||||
"special \r\n\t chars", "special \r\n\t chars",
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,6 +67,14 @@ and displays information about any security vulnerabilities that affect the curr
|
|||
Usage: "Display license information",
|
||||
ArgsUsage: " ",
|
||||
}
|
||||
logTestCommand = &cli.Command{
|
||||
Action: logTest,
|
||||
Name: "logtest",
|
||||
Usage: "Print some log messages",
|
||||
ArgsUsage: " ",
|
||||
Description: `
|
||||
This command is only meant for testing.
|
||||
`}
|
||||
)
|
||||
|
||||
func printVersion(ctx *cli.Context) error {
|
||||
|
|
|
|||
3
cmd/geth/testdata/logging/logtest-logfmt.txt
vendored
Normal file
3
cmd/geth/testdata/logging/logtest-logfmt.txt
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
t=2023-10-18T11:29:46+0200 lvl=info msg="Output testing started" big.Int=111,222,333,444,555,678,999 -big.Int=-111,222,333,444,555,678,999 big.Int=11,122,233,344,455,567,899,900 -big.Int=-11,122,233,344,455,567,899,900
|
||||
t=2023-10-18T11:29:46+0200 lvl=info msg="Testing uint256" uint256.Int=111,222,333,444,555,678,999 uint256.Int=11,122,233,344,455,567,899,900
|
||||
t=2023-10-18T11:29:46+0200 lvl=info msg="Special chars" "special \r\n\t chars"="special \r\n\t chars"
|
||||
3
cmd/geth/testdata/logging/logtest-terminal.txt
vendored
Normal file
3
cmd/geth/testdata/logging/logtest-terminal.txt
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
INFO [XXXXXXXXXXXXXXXXXX] Output testing started big.Int=111,222,333,444,555,678,999 -big.Int=-111,222,333,444,555,678,999 big.Int=11,122,233,344,455,567,899,900 -big.Int=-11,122,233,344,455,567,899,900
|
||||
INFO [XXXXXXXXXXXXXXXXXX] Testing uint256 uint256.Int=111,222,333,444,555,678,999 uint256.Int=11,122,233,344,455,567,899,900
|
||||
INFO [XXXXXXXXXXXXXXXXXX] Special chars "special \r\n\t chars"="special \r\n\t chars"
|
||||
Loading…
Reference in a new issue