Skip to content

Commit

Permalink
cmd/geth: test for logging-output
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Oct 18, 2023
1 parent da55b23 commit f66e099
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cmd/geth/logging_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
28 changes: 28 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"fmt"
"math/big"
"os"
"sort"
"strconv"
Expand All @@ -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"
)

Expand Down Expand Up @@ -233,6 +235,7 @@ func init() {
snapshotCommand,
// See verkle.go
verkleCommand,
logTestCommand,
}
sort.Sort(cli.CommandsByName(app.Commands))

Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions cmd/geth/misccmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions cmd/geth/testdata/logging/logtest-logfmt.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions cmd/geth/testdata/logging/logtest-terminal.txt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit f66e099

Please sign in to comment.