Skip to content

Commit f66e099

Browse files
committed
cmd/geth: test for logging-output
1 parent da55b23 commit f66e099

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

cmd/geth/logging_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"bufio"
21+
"bytes"
22+
"fmt"
23+
"io"
24+
"os"
25+
"os/exec"
26+
"strings"
27+
"testing"
28+
29+
"github.com/docker/docker/pkg/reexec"
30+
)
31+
32+
func runSelf(args ...string) ([]byte, error) {
33+
cmd := &exec.Cmd{
34+
Path: reexec.Self(),
35+
Args: append([]string{"geth-test"}, args...),
36+
}
37+
return cmd.CombinedOutput()
38+
}
39+
40+
func split(input io.Reader) []string {
41+
var output []string
42+
scanner := bufio.NewScanner(input)
43+
scanner.Split(bufio.ScanLines)
44+
for scanner.Scan() {
45+
output = append(output, scanner.Text())
46+
}
47+
return output
48+
}
49+
50+
func censor(input string, start, end int) string {
51+
if len(input) < end {
52+
return input
53+
}
54+
return input[:start] + strings.Repeat("X", end-start) + input[end:]
55+
}
56+
57+
func TestLogging(t *testing.T) {
58+
testConsoleLogging(t, "terminal", 6, 24)
59+
testConsoleLogging(t, "logfmt", 2, 26)
60+
}
61+
62+
func testConsoleLogging(t *testing.T, format string, tStart, tEnd int) {
63+
haveB, err := runSelf("--log.format", format, "logtest")
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
readFile, err := os.Open(fmt.Sprintf("testdata/logging/logtest-%v.txt", format))
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
wantLines := split(readFile)
72+
haveLines := split(bytes.NewBuffer(haveB))
73+
for i, want := range wantLines {
74+
if i > len(haveLines)-1 {
75+
t.Fatalf("format %v, line %d missing, want:%v", format, i, want)
76+
}
77+
have := haveLines[i]
78+
// Black out the timestamp
79+
have = censor(have, tStart, tEnd)
80+
want = censor(want, tStart, tEnd)
81+
if have != want {
82+
t.Fatalf("format %v, line %d\nhave %v\nwant %v", format, i, have, want)
83+
}
84+
}
85+
if len(haveLines) != len(wantLines) {
86+
t.Errorf("format %v, want %d lines, have %d", format, len(haveLines), len(wantLines))
87+
}
88+
}

cmd/geth/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"fmt"
22+
"math/big"
2223
"os"
2324
"sort"
2425
"strconv"
@@ -45,6 +46,7 @@ import (
4546
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
4647
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
4748

49+
"github.com/holiman/uint256"
4850
"github.com/urfave/cli/v2"
4951
)
5052

@@ -233,6 +235,7 @@ func init() {
233235
snapshotCommand,
234236
// See verkle.go
235237
verkleCommand,
238+
logTestCommand,
236239
}
237240
sort.Sort(cli.CommandsByName(app.Commands))
238241

@@ -470,3 +473,28 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) {
470473
unlockAccount(ks, account, i, passwords)
471474
}
472475
}
476+
477+
// logTest is an entry point which spits out some logs. This is used by testing
478+
// to verify expected outputs
479+
func logTest(ctx *cli.Context) error {
480+
ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999"
481+
bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999"
482+
bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900"
483+
bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900"
484+
485+
ua, _ := uint256.FromDecimal("111222333444555678999")
486+
ub, _ := uint256.FromDecimal("11122233344455567899900")
487+
488+
log.Info("Output testing started",
489+
"big.Int", ba,
490+
"-big.Int", bb,
491+
"big.Int", bc,
492+
"-big.Int", bd)
493+
log.Info("Testing uint256",
494+
"uint256.Int", ua,
495+
"uint256.Int", ub)
496+
log.Info("Special chars",
497+
"special \r\n\t chars", "special \r\n\t chars",
498+
)
499+
return nil
500+
}

cmd/geth/misccmd.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ and displays information about any security vulnerabilities that affect the curr
6767
Usage: "Display license information",
6868
ArgsUsage: " ",
6969
}
70+
logTestCommand = &cli.Command{
71+
Action: logTest,
72+
Name: "logtest",
73+
Usage: "Print some log messages",
74+
ArgsUsage: " ",
75+
Description: `
76+
This command is only meant for testing.
77+
`}
7078
)
7179

7280
func printVersion(ctx *cli.Context) error {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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
2+
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
3+
t=2023-10-18T11:29:46+0200 lvl=info msg="Special chars" "special \r\n\t chars"="special \r\n\t chars"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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
2+
INFO [XXXXXXXXXXXXXXXXXX] Testing uint256 uint256.Int=111,222,333,444,555,678,999 uint256.Int=11,122,233,344,455,567,899,900
3+
INFO [XXXXXXXXXXXXXXXXXX] Special chars "special \r\n\t chars"="special \r\n\t chars"

0 commit comments

Comments
 (0)