-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2848 from oasislabs/matevz/feature/json_fixtures
oasis-net-runner: Add support for loading fixtures from JSON
- Loading branch information
Showing
9 changed files
with
194 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
`oasis-net-runner`: `--net.*` flags renamed to `--fixture.default.*` | ||
|
||
For example `--net.node.binary mynode/oasis-node` becomes | ||
`--fixture.default.node.binary mynode/oasis-node`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 `--fixture.file` allows user to load default fixture from JSON file. | ||
In addition `dump-fixture` command dumps configured JSON-encoded fixture to | ||
standard output which can serve as a template. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fixtures | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/spf13/viper" | ||
|
||
"github.com/oasislabs/oasis-core/go/oasis-test-runner/oasis" | ||
) | ||
|
||
const ( | ||
cfgFile = "fixture.file" | ||
) | ||
|
||
// newFixtureFromFile parses given JSON file and creates new fixture object from it. | ||
func newFixtureFromFile(path string) (*oasis.NetworkFixture, error) { | ||
f := oasis.NetworkFixture{} | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("newFixtureFromFile: failed to open fixture file: %w", err) | ||
} | ||
if err = json.Unmarshal(data, &f); err != nil { | ||
return nil, fmt.Errorf("newFixtureFromFile: failed to unmarshal JSON from fixture file: %w", err) | ||
} | ||
|
||
return &f, nil | ||
} | ||
|
||
func init() { | ||
FileFixtureFlags.String(cfgFile, "", "path to JSON-encoded fixture input file") | ||
_ = viper.BindPFlags(FileFixtureFlags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Package fixtures provides network configuration fixtures. | ||
package fixtures | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
flag "github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/oasislabs/oasis-core/go/oasis-test-runner/oasis" | ||
) | ||
|
||
var ( | ||
// FileFixtureFlags are command line flags for the fixtures. | ||
FileFixtureFlags = flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
// DefaultFixtureFlags are command line flags for the fixture.default.* flags. | ||
DefaultFixtureFlags = flag.NewFlagSet("", flag.ContinueOnError) | ||
) | ||
|
||
// GetFixture generates fixture object from given file or default fixture, if no fixtures file provided. | ||
func GetFixture() (f *oasis.NetworkFixture, err error) { | ||
if viper.IsSet(cfgFile) { | ||
f, err = newFixtureFromFile(viper.GetString(cfgFile)) | ||
} else { | ||
f, err = newDefaultFixture() | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
// DumpFixture dumps given fixture to JSON-encoded bytes. | ||
func DumpFixture(f *oasis.NetworkFixture) ([]byte, error) { | ||
data, err := json.Marshal(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package fixtures | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
defaultFixturePath = "../../../tests/fixture-data/net-runner/default.json" | ||
) | ||
|
||
func TestDefaultFixture(t *testing.T) { | ||
f, err := newDefaultFixture() | ||
require.Nil(t, err) | ||
require.NotNil(t, f) | ||
|
||
data, err := DumpFixture(f) | ||
require.Nil(t, err) | ||
require.NotNil(t, data) | ||
|
||
storedData, err := ioutil.ReadFile(defaultFixturePath) | ||
require.Nil(t, err) | ||
require.NotNil(t, storedData) | ||
|
||
require.EqualValues(t, storedData, data) | ||
} | ||
|
||
func TestCustomFixture(t *testing.T) { | ||
f, _ := newDefaultFixture() | ||
f.Network.NodeBinary = "myNodeBinary" | ||
f.Network.ConsensusBackend = "myConsensusBackend" | ||
f.Network.ConsensusGasCostsTxByte = 123456789 | ||
|
||
data, err := DumpFixture(f) | ||
require.Nil(t, err) | ||
tmpFile, _ := ioutil.TempFile("", "oasis-net-runner-customfixture.*.json") | ||
path := tmpFile.Name() | ||
_, _ = tmpFile.Write(data) | ||
tmpFile.Close() | ||
|
||
fs, err := newFixtureFromFile(path) | ||
require.Nil(t, err) | ||
require.EqualValues(t, f, fs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"tee":{"hardware":0,"mr_signer":null},"network":{"node_binary":"oasis-node","runtime_loader_binary":"oasis-core-runtime-loader","consensus_backend":"","consensus_timeout_commit":1000000000,"consensus_gas_costs_tx_byte":0,"halt_epoch":18446744073709551615,"epochtime_mock":false,"epochtime_tendermint_interval":0,"deterministic_identities":false,"staking_genesis":""},"entities":[{"IsDebugTestEntity":true,"Restore":false},{"IsDebugTestEntity":false,"Restore":false}],"runtimes":[{"id":"wAAAAAAAAAD///////////////////////////////8=","kind":2,"entity":0,"keymanager":-1,"binary":"simple-keymanager","genesis_state":"","genesis_round":0,"executor":{"group_size":0,"group_backup_size":0,"allowed_stragglers":0,"round_timeout":0},"merge":{"group_size":0,"group_backup_size":0,"allowed_stragglers":0,"round_timeout":0},"txn_scheduler":{"group_size":0,"algorithm":"","batch_flush_timeout":0,"max_batch_size":0,"max_batch_size_bytes":0},"storage":{"group_size":0,"max_apply_write_log_entries":0,"max_apply_ops":0,"max_merge_roots":0,"max_merge_ops":0,"checkpoint_interval":0,"checkpoint_num_kept":0,"checkpoint_chunk_size":0},"admission_policy":{"any_node":{}},"pruner":{"strategy":"","interval":0,"num_kept":0}},{"id":"gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","kind":1,"entity":0,"keymanager":0,"binary":"simple-keyvalue","genesis_state":"","genesis_round":0,"executor":{"group_size":2,"group_backup_size":1,"allowed_stragglers":0,"round_timeout":20000000000},"merge":{"group_size":2,"group_backup_size":1,"allowed_stragglers":0,"round_timeout":20000000000},"txn_scheduler":{"group_size":2,"algorithm":"batching","batch_flush_timeout":20000000000,"max_batch_size":1,"max_batch_size_bytes":16777216},"storage":{"group_size":1,"max_apply_write_log_entries":100000,"max_apply_ops":2,"max_merge_roots":8,"max_merge_ops":2,"checkpoint_interval":0,"checkpoint_num_kept":0,"checkpoint_chunk_size":0},"admission_policy":{"any_node":{}},"pruner":{"strategy":"","interval":0,"num_kept":0}}],"validators":[{"allow_early_termination":false,"allow_error_termination":false,"entity":1,"min_gas_price":0,"submission_gas_price":0,"tendermint_recover_corrupted_wal":false}],"keymanagers":[{"runtime":0,"entity":1,"allow_early_termination":false,"allow_error_termination":false,"submission_gas_price":0}],"storage_workers":[{"backend":"badger","entity":1,"allow_early_termination":false,"allow_error_termination":false,"submission_gas_price":0}],"compute_workers":[{"entity":1,"runtime_backend":"","allow_early_termination":false,"allow_error_termination":false,"submission_gas_price":0},{"entity":1,"runtime_backend":"","allow_early_termination":false,"allow_error_termination":false,"submission_gas_price":0},{"entity":1,"runtime_backend":"","allow_early_termination":false,"allow_error_termination":false,"submission_gas_price":0}],"clients":[{"consensus_disable_check_tx":false}]} |