From 55ff06dbbb4d98ca94a6c9deb47250cb678e6276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matev=C5=BE=20Jekovec?= Date: Fri, 17 Apr 2020 10:41:49 +0200 Subject: [PATCH] oasis-net-runner: Add support for dumping fixture to file --- .changelog/2848.feature.md | 5 +++ README.md | 10 +++-- go/oasis-net-runner/fixtures/default.go | 60 ++++++++++++++++++------- 3 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 .changelog/2848.feature.md diff --git a/.changelog/2848.feature.md b/.changelog/2848.feature.md new file mode 100644 index 00000000000..4c680e72c62 --- /dev/null +++ b/.changelog/2848.feature.md @@ -0,0 +1,5 @@ +`oasis-net-runner`: Add support for fixtures in JSON file + +New flag `--net.fixture.file` allows user to load network fixture from JSON +file. In addition `--net.fixture.dump` dumps configured network fixture to +JSON file which can serve as a template. diff --git a/README.md b/README.md index d90bd995ee6..5d0445e83fe 100644 --- a/README.md +++ b/README.md @@ -319,7 +319,6 @@ slightly different environmental variables set: export OASIS_UNSAFE_SKIP_AVR_VERIFY="1" export OASIS_UNSAFE_KM_POLICY_KEYS="1" export OASIS_UNSAFE_ALLOW_DEBUG_ENCLAVES="1" -export OASIS_TEE_HARDWARE=intel-sgx make ``` @@ -358,6 +357,7 @@ except the `oasis-net-runner` invocation: ``` ./go/oasis-net-runner/oasis-net-runner \ + --net.tee_hardware intel-sgx \ --net.node.binary go/oasis-node/oasis-node \ --net.runtime.binary target/sgx/x86_64-fortanix-unknown-sgx/debug/simple-keyvalue.sgxs \ --net.runtime.loader target/default/debug/oasis-core-runtime-loader \ @@ -387,8 +387,12 @@ To run all tests: make test ``` -Do not forget to set `OASIS_TEE_HARDWARE` flag (see above), if you want to -execute tests under SGX. +To execute tests using SGX set the following environmental variable before +running the tests: + +``` +export OASIS_TEE_HARDWARE=intel-sgx +``` ### Troubleshooting diff --git a/go/oasis-net-runner/fixtures/default.go b/go/oasis-net-runner/fixtures/default.go index 48d7384cb68..d00763864d1 100644 --- a/go/oasis-net-runner/fixtures/default.go +++ b/go/oasis-net-runner/fixtures/default.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "math" + "os" "time" flag "github.com/spf13/pflag" @@ -20,15 +21,16 @@ import ( ) const ( + cfgEpochtimeMock = "net.epochtime_mock" + cfgFixtureFile = "net.fixture.file" + cfgFixtureDump = "net.fixture.dump" + cfgHaltEpoch = "net.halt_epoch" + cfgKeymanagerBinary = "net.keymanager.binary" cfgNodeBinary = "net.node.binary" cfgRuntimeBinary = "net.runtime.binary" cfgRuntimeGenesisState = "net.runtime.genesis_state" cfgRuntimeLoader = "net.runtime.loader" - cfgKeymanagerBinary = "net.keymanager.binary" cfgTEEHardware = "net.tee_hardware" - cfgEpochtimeMock = "net.epochtime_mock" - cfgHaltEpoch = "net.halt_epoch" - cfgFixturesFile = "net.fixtures.file" ) var ( @@ -40,12 +42,25 @@ var ( ) // GetFixture generates a fixture object from given file or default fixture, if no fixtures file provided. -func GetFixture() (*oasis.NetworkFixture, error) { - if viper.IsSet(cfgFixturesFile) { - return NewFixtureFromFile(viper.GetString(cfgFixturesFile)) +func GetFixture() (f *oasis.NetworkFixture, err error) { + if viper.IsSet(cfgFixtureFile) { + f, err = NewFixtureFromFile(viper.GetString(cfgFixtureFile)) + } else { + f, err = NewDefaultFixture() + } + if err != nil { + return + } + + // If requested, dump fixture to file and exit. + if viper.IsSet(cfgFixtureDump) { + if err = DumpFixtureToFile(f, viper.GetString(cfgFixtureDump)); err != nil { + return + } + os.Exit(0) } - return NewDefaultFixture() + return } // NewDefaultFixture returns a default network fixture. @@ -149,27 +164,42 @@ func NewDefaultFixture() (*oasis.NetworkFixture, error) { // NewFixtureFromFile parses given JSON file and creates a new fixture object from it. func NewFixtureFromFile(path string) (*oasis.NetworkFixture, error) { f := oasis.NetworkFixture{} - fBytes, err := ioutil.ReadFile(viper.GetString(cfgFixturesFile)) + fBytes, err := ioutil.ReadFile(viper.GetString(cfgFixtureFile)) if err != nil { - return nil, fmt.Errorf("NewFixtureFromFile: failed to open fixtures file: %w", err) + return nil, fmt.Errorf("NewFixtureFromFile: failed to open fixture file: %w", err) } if err = json.Unmarshal(fBytes, &f); err != nil { - return nil, fmt.Errorf("NewFixtureFromFile: failed to unmarshal JSON from fixtures file: %w", err) + return nil, fmt.Errorf("NewFixtureFromFile: failed to unmarshal JSON from fixture file: %w", err) } return &f, nil } +// DumpFixtureToFile dumps settings as JSON-encoded fixture to file. +func DumpFixtureToFile(f *oasis.NetworkFixture, path string) error { + fBytes, err := json.Marshal(f) + if err != nil { + return fmt.Errorf("DumpFixtureToFile: failed to marshal fixture: %w", err) + } + + if err = ioutil.WriteFile(path, fBytes, 0600); err != nil { + return fmt.Errorf("DumpFixtureToFile: failed to write fixture file: %w", err) + } + + return nil +} + func init() { + Flags.Bool(cfgEpochtimeMock, false, "use mock epochtime") + Flags.String(cfgFixtureFile, "", "path to JSON-encoded fixture input file") + Flags.String(cfgFixtureDump, "", "dump network fixture to JSON-encoded file and exit") + Flags.Uint64(cfgHaltEpoch, math.MaxUint64, "halt epoch height") + Flags.String(cfgKeymanagerBinary, "simple-keymanager", "path to the keymanager runtime") Flags.String(cfgNodeBinary, "oasis-node", "path to the oasis-node binary") Flags.String(cfgRuntimeBinary, "simple-keyvalue", "path to the runtime binary") Flags.String(cfgRuntimeGenesisState, "", "path to the runtime genesis state") Flags.String(cfgRuntimeLoader, "oasis-core-runtime-loader", "path to the runtime loader") - Flags.String(cfgKeymanagerBinary, "simple-keymanager", "path to the keymanager runtime") Flags.String(cfgTEEHardware, "", "TEE hardware to use") - Flags.Bool(cfgEpochtimeMock, false, "use mock epochtime") - Flags.Uint64(cfgHaltEpoch, math.MaxUint64, "halt epoch height") - Flags.String(cfgFixturesFile, "", "path to JSON-encoded fixtures file") _ = viper.BindPFlags(Flags) _ = runtimeID.UnmarshalHex("8000000000000000000000000000000000000000000000000000000000000000")