2018-11-07 19:07:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2018-11-19 06:54:04 +00:00
|
|
|
"fmt"
|
2018-11-07 19:07:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-yaml/yaml"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/chaintest/backend"
|
2018-11-19 01:59:11 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
prefixed "github.com/x-cray/logrus-prefixed-formatter"
|
2018-11-07 19:07:41 +00:00
|
|
|
)
|
|
|
|
|
2018-11-19 06:54:04 +00:00
|
|
|
func readTestsFromYaml(yamlDir string) ([]interface{}, error) {
|
2019-01-11 10:55:01 +00:00
|
|
|
const forkChoiceTestsFolderName = "fork-choice-tests"
|
2018-11-19 06:54:04 +00:00
|
|
|
const shuffleTestsFolderName = "shuffle-tests"
|
2019-01-06 15:25:43 +00:00
|
|
|
const stateTestsFolderName = "state-tests"
|
2018-11-19 06:54:04 +00:00
|
|
|
|
|
|
|
var tests []interface{}
|
|
|
|
|
|
|
|
dirs, err := ioutil.ReadDir(yamlDir)
|
|
|
|
if err != nil {
|
2019-01-30 10:11:13 +00:00
|
|
|
return nil, fmt.Errorf("could not read YAML tests directory: %v", err)
|
2018-11-19 06:54:04 +00:00
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
files, err := ioutil.ReadDir(path.Join(yamlDir, dir.Name()))
|
|
|
|
if err != nil {
|
2019-01-30 10:11:13 +00:00
|
|
|
return nil, fmt.Errorf("could not read YAML tests directory: %v", err)
|
2018-11-19 06:54:04 +00:00
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
filePath := path.Join(yamlDir, dir.Name(), file.Name())
|
2019-01-06 15:25:43 +00:00
|
|
|
// #nosec G304
|
|
|
|
data, err := ioutil.ReadFile(filePath)
|
2018-11-19 06:54:04 +00:00
|
|
|
if err != nil {
|
2019-01-30 10:11:13 +00:00
|
|
|
return nil, fmt.Errorf("could not read YAML file: %v", err)
|
2018-11-19 06:54:04 +00:00
|
|
|
}
|
|
|
|
switch dir.Name() {
|
2019-01-11 10:55:01 +00:00
|
|
|
case forkChoiceTestsFolderName:
|
|
|
|
decoded := &backend.ForkChoiceTest{}
|
2018-11-19 06:54:04 +00:00
|
|
|
if err := yaml.Unmarshal(data, decoded); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not unmarshal YAML file into test struct: %v", err)
|
|
|
|
}
|
|
|
|
tests = append(tests, decoded)
|
|
|
|
case shuffleTestsFolderName:
|
|
|
|
decoded := &backend.ShuffleTest{}
|
|
|
|
if err := yaml.Unmarshal(data, decoded); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not unmarshal YAML file into test struct: %v", err)
|
|
|
|
}
|
|
|
|
tests = append(tests, decoded)
|
2019-01-06 15:25:43 +00:00
|
|
|
case stateTestsFolderName:
|
|
|
|
decoded := &backend.StateTest{}
|
|
|
|
if err := yaml.Unmarshal(data, decoded); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not unmarshal YAML file into test struct: %v", err)
|
|
|
|
}
|
|
|
|
tests = append(tests, decoded)
|
2018-11-19 06:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tests, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTests(tests []interface{}, sb *backend.SimulatedBackend) error {
|
|
|
|
for _, tt := range tests {
|
|
|
|
switch typedTest := tt.(type) {
|
2019-01-11 10:55:01 +00:00
|
|
|
case *backend.ForkChoiceTest:
|
2018-11-19 06:54:04 +00:00
|
|
|
log.Infof("Title: %v", typedTest.Title)
|
|
|
|
log.Infof("Summary: %v", typedTest.Summary)
|
|
|
|
log.Infof("Test Suite: %v", typedTest.TestSuite)
|
|
|
|
for _, testCase := range typedTest.TestCases {
|
2019-01-11 10:55:01 +00:00
|
|
|
if err := sb.RunForkChoiceTest(testCase); err != nil {
|
2018-11-19 06:54:04 +00:00
|
|
|
return fmt.Errorf("chain test failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 15:25:43 +00:00
|
|
|
log.Info("Test PASSED")
|
2018-11-19 06:54:04 +00:00
|
|
|
case *backend.ShuffleTest:
|
|
|
|
log.Infof("Title: %v", typedTest.Title)
|
|
|
|
log.Infof("Summary: %v", typedTest.Summary)
|
|
|
|
log.Infof("Test Suite: %v", typedTest.TestSuite)
|
|
|
|
log.Infof("Fork: %v", typedTest.Fork)
|
|
|
|
log.Infof("Version: %v", typedTest.Version)
|
|
|
|
for _, testCase := range typedTest.TestCases {
|
|
|
|
if err := sb.RunShuffleTest(testCase); err != nil {
|
|
|
|
return fmt.Errorf("chain test failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 15:25:43 +00:00
|
|
|
log.Info("Test PASSED")
|
|
|
|
case *backend.StateTest:
|
|
|
|
log.Infof("Title: %v", typedTest.Title)
|
|
|
|
log.Infof("Summary: %v", typedTest.Summary)
|
|
|
|
log.Infof("Test Suite: %v", typedTest.TestSuite)
|
|
|
|
log.Infof("Fork: %v", typedTest.Fork)
|
|
|
|
log.Infof("Version: %v", typedTest.Version)
|
|
|
|
for _, testCase := range typedTest.TestCases {
|
|
|
|
if err := sb.RunStateTransitionTest(testCase); err != nil {
|
|
|
|
return fmt.Errorf("chain test failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("Test PASSED")
|
2018-11-19 06:54:04 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("receive unknown test type: %T", typedTest)
|
|
|
|
}
|
2019-01-06 15:25:43 +00:00
|
|
|
log.Info("-----------------------------")
|
2018-11-19 06:54:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
func main() {
|
|
|
|
var yamlDir = flag.String("tests-dir", "", "path to directory of yaml tests")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
customFormatter := new(prefixed.TextFormatter)
|
|
|
|
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
|
|
|
|
customFormatter.FullTimestamp = true
|
|
|
|
log.SetFormatter(customFormatter)
|
|
|
|
|
2018-11-19 06:54:04 +00:00
|
|
|
tests, err := readTestsFromYaml(*yamlDir)
|
2018-11-07 19:07:41 +00:00
|
|
|
if err != nil {
|
2018-11-19 06:54:04 +00:00
|
|
|
log.Fatalf("Fail to load tests from yaml: %v", err)
|
2018-11-07 19:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := backend.NewSimulatedBackend()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not create backend: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-19 06:54:04 +00:00
|
|
|
log.Info("----Running Tests----")
|
2018-11-07 19:07:41 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2018-11-19 06:54:04 +00:00
|
|
|
err = runTests(tests, sb)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Test failed %v", err)
|
2018-11-07 19:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endTime := time.Now()
|
2019-01-06 15:25:43 +00:00
|
|
|
log.Infof("Test Runs Finished In: %v", endTime.Sub(startTime))
|
2018-11-07 19:07:41 +00:00
|
|
|
}
|