Skip to content

Commit

Permalink
oasis-net-runner: Add support for dumping fixture to file
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Apr 20, 2020
1 parent 4b0ba75 commit 55ff06d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changelog/2848.feature.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -358,6 +357,7 @@ except the `oasis-net-runner` invocation:
<!-- markdownlint-disable line-length -->
```
./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 \
Expand Down Expand Up @@ -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

Expand Down
60 changes: 45 additions & 15 deletions go/oasis-net-runner/fixtures/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"math"
"os"
"time"

flag "github.com/spf13/pflag"
Expand All @@ -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 (
Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 55ff06d

Please sign in to comment.