From 235ed7ecb92527ce0029c2a6f5ace06707db9b28 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 17 Apr 2015 16:30:15 +0200 Subject: cmd/geth, tests: enable running multiple tests from a single file This commit also changes the block test loading so tests containing invalid RLP blocks can be loaded and return an error only when they are run. (cherry picked from commit 898ba87984791249586b97c9ce340dd087b79d67) --- cmd/geth/blocktest.go | 89 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 24 deletions(-) (limited to 'cmd/geth') diff --git a/cmd/geth/blocktest.go b/cmd/geth/blocktest.go index f0b6bb1a2..792981ec0 100644 --- a/cmd/geth/blocktest.go +++ b/cmd/geth/blocktest.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "github.com/codegangsta/cli" "github.com/ethereum/go-ethereum/cmd/utils" @@ -12,7 +13,7 @@ import ( ) var blocktestCmd = cli.Command{ - Action: runblocktest, + Action: runBlockTest, Name: "blocktest", Usage: `loads a block test file`, Description: ` @@ -25,27 +26,78 @@ be able to interact with the chain defined by the test. `, } -func runblocktest(ctx *cli.Context) { - if len(ctx.Args()) != 3 { - utils.Fatalf("Usage: ethereum blocktest {rpc, norpc}") +func runBlockTest(ctx *cli.Context) { + var ( + file, testname string + rpc bool + ) + args := ctx.Args() + switch { + case len(args) == 1: + file = args[0] + case len(args) == 2: + file, testname = args[0], args[1] + case len(args) == 3: + file, testname = args[0], args[1] + rpc = true + default: + utils.Fatalf(`Usage: ethereum blocktest [ [ "rpc" ] ]`) } - file, testname, startrpc := ctx.Args()[0], ctx.Args()[1], ctx.Args()[2] - bt, err := tests.LoadBlockTests(file) if err != nil { utils.Fatalf("%v", err) } + + // run all tests if no test name is specified + if testname == "" { + ecode := 0 + for name, test := range bt { + fmt.Printf("----------------- Running Block Test %q\n", name) + ethereum, err := runOneBlockTest(ctx, test) + if err != nil { + fmt.Println(err) + fmt.Println("FAIL") + ecode = 1 + } + if ethereum != nil { + ethereum.Stop() + ethereum.WaitForShutdown() + } + } + os.Exit(ecode) + return + } + // otherwise, run the given test test, ok := bt[testname] if !ok { utils.Fatalf("Test file does not contain test named %q", testname) } + ethereum, err := runOneBlockTest(ctx, test) + if err != nil { + utils.Fatalf("%v", err) + } + defer ethereum.Stop() + if rpc { + fmt.Println("Block Test post state validated, starting RPC interface.") + startEth(ctx, ethereum) + utils.StartRPC(ethereum, ctx) + ethereum.WaitForShutdown() + } +} +func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, error) { cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() } cfg.MaxPeers = 0 // disable network + cfg.Shh = false // disable whisper + cfg.NAT = nil // disable port mapping + ethereum, err := eth.New(cfg) if err != nil { - utils.Fatalf("%v", err) + return nil, err + } + if err := ethereum.Start(); err != nil { + return nil, err } // import the genesis block @@ -54,27 +106,16 @@ func runblocktest(ctx *cli.Context) { // import pre accounts statedb, err := test.InsertPreState(ethereum.StateDb()) if err != nil { - utils.Fatalf("could not insert genesis accounts: %v", err) + return ethereum, fmt.Errorf("InsertPreState: %v", err) } // insert the test blocks, which will execute all transactions - chain := ethereum.ChainManager() - if err := chain.InsertChain(test.Blocks); err != nil { - utils.Fatalf("Block Test load error: %v %T", err, err) - } else { - fmt.Println("Block Test chain loaded") + if err := test.InsertBlocks(ethereum.ChainManager()); err != nil { + return ethereum, fmt.Errorf("Block Test load error: %v %T", err, err) } - + fmt.Println("chain loaded") if err := test.ValidatePostState(statedb); err != nil { - utils.Fatalf("post state validation failed: %v", err) - } - fmt.Println("Block Test post state validated, starting ethereum.") - - if startrpc == "rpc" { - startEth(ctx, ethereum) - utils.StartRPC(ethereum, ctx) - ethereum.WaitForShutdown() - } else { - startEth(ctx, ethereum) + return ethereum, fmt.Errorf("post state validation failed: %v", err) } + return ethereum, nil } -- cgit v1.2.3 From 805345d13594b41e5dd2e4fed471c58c3775be9e Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Sun, 19 Apr 2015 00:35:48 +0200 Subject: Add block tests wrapper and fixes for tx tests * Add fixes to parsing and converting of fields in tx tests * Correct logic in tx tests; validation of fields and correct logic for when RLP decoding works/fails and when this is expected or not * Rename files for consistency * Add block tests wrapper to run block tests with go test --- cmd/geth/block_go_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 cmd/geth/block_go_test.go (limited to 'cmd/geth') diff --git a/cmd/geth/block_go_test.go b/cmd/geth/block_go_test.go new file mode 100644 index 000000000..1980e4798 --- /dev/null +++ b/cmd/geth/block_go_test.go @@ -0,0 +1,80 @@ +package main + +import ( + "path" + "testing" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/tests" +) + +// TODO: refactor test setup & execution to better align with vm and tx tests +// TODO: refactor to avoid duplication with cmd/geth/blocktest.go +func TestBcValidBlockTests(t *testing.T) { + runBlockTestsInFile("../../tests/files/BlockTests/bcValidBlockTest.json", t) +} + +/* +func TestBcUncleTests(t *testing.T) { + runBlockTestsInFile("../../tests/files/BlockTests/bcUncleTest.json", t) +} +*/ + +func runBlockTestsInFile(filepath string, t *testing.T) { + bt, err := tests.LoadBlockTests(filepath) + if err != nil { + t.Fatal(err) + } + for name, test := range bt { + runTest(name, test, t) + } +} + +func runTest(name string, test *tests.BlockTest, t *testing.T) { + t.Log("Running test: ", name) + cfg := testEthConfig() + ethereum, err := eth.New(cfg) + if err != nil { + t.Fatalf("%v", err) + } + + err = ethereum.Start() + if err != nil { + t.Fatalf("%v", err) + } + + // import the genesis block + ethereum.ResetWithGenesisBlock(test.Genesis) + + // import pre accounts + statedb, err := test.InsertPreState(ethereum.StateDb()) + if err != nil { + t.Fatalf("InsertPreState: %v", err) + } + + // insert the test blocks, which will execute all transactions + if err := test.InsertBlocks(ethereum.ChainManager()); err != nil { + t.Fatalf("Block Test load error: %v %T", err, err) + } + + if err := test.ValidatePostState(statedb); err != nil { + t.Fatal("post state validation failed: %v", err) + } + t.Log("Test passed: ", name) +} + +func testEthConfig() *eth.Config { + ks := crypto.NewKeyStorePassphrase(path.Join(common.DefaultDataDir(), "keys")) + + return ð.Config{ + DataDir: common.DefaultDataDir(), + LogLevel: 5, + Etherbase: "primary", + AccountManager: accounts.NewManager(ks), + NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() }, + } +} -- cgit v1.2.3