From f668a0f821b498285483056cdc22f212faa18ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matev=C5=BE=20Jekovec?= Date: Fri, 12 Nov 2021 09:52:28 +0100 Subject: [PATCH] test-runner: Read runtime version from fixture --- .changelog/4343.trivial.md | 0 go/common/version/version.go | 63 +++++++++++++++++---------- go/common/version/version_test.go | 34 ++++++++++++--- go/oasis-test-runner/oasis/fixture.go | 3 ++ go/oasis-test-runner/oasis/runtime.go | 3 ++ 5 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 .changelog/4343.trivial.md diff --git a/.changelog/4343.trivial.md b/.changelog/4343.trivial.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/go/common/version/version.go b/go/common/version/version.go index 0b29a85d614..99898cd54b9 100644 --- a/go/common/version/version.go +++ b/go/common/version/version.go @@ -47,9 +47,9 @@ func (v Version) ToU64() uint64 { // FromU64 returns the version from platform-dependent uint64. func FromU64(v uint64) Version { return Version{ - Major: uint16((v >> 32) & 0xff), - Minor: uint16((v >> 16) & 0xff), - Patch: uint16(v & 0xff), + Major: uint16((v >> 32) & 0xffff), + Minor: uint16((v >> 16) & 0xffff), + Patch: uint16(v & 0xffff), } } @@ -58,6 +58,41 @@ func (v Version) String() string { return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) } +// FromString parses version in semver format. e.g. "1.0.0" +func FromString(s string) (Version, error) { + // Trim potential pre-release suffix. + s = strings.Split(s, "-")[0] + // Trim potential git commit. + s = strings.Split(s, "+")[0] + split := strings.SplitN(s, ".", 4) + if len(split) != 3 { + return Version{}, fmt.Errorf("version: failed to parse SemVer '%s': exactly three components are required", s) + } + + var semVers []uint16 = []uint16{0, 0, 0} + for i, v := range split { + if i >= 3 { + break + } + ver, err := strconv.ParseUint(v, 10, 16) + if err != nil { + return Version{}, fmt.Errorf("version: failed to parse SemVer '%s': %w", s, err) + } + semVers[i] = uint16(ver) + } + + return Version{Major: semVers[0], Minor: semVers[1], Patch: semVers[2]}, nil +} + +// MustFromString parses version in semver format and panics, if there is an error. +func MustFromString(s string) Version { + ver, err := FromString(s) + if err != nil { + panic(err) + } + return ver +} + // MaskNonMajor masks all non-major version segments to 0 and returns a new // protocol version. // @@ -111,7 +146,7 @@ var ( TendermintAppVersion = ConsensusProtocol.MaskNonMajor().ToU64() // Toolchain is the version of the Go compiler/standard library. - Toolchain = parseSemVerStr(strings.TrimPrefix(runtime.Version(), "go")) + Toolchain = MustFromString(strings.TrimPrefix(runtime.Version(), "go")) ) // ProtocolVersions are the protocol versions. @@ -178,26 +213,6 @@ var Versions = ProtocolVersions{ RuntimeCommitteeProtocol, } -func parseSemVerStr(s string) Version { - // Trim potential pre-release suffix. - s = strings.Split(s, "-")[0] - split := strings.SplitN(s, ".", 4) - - var semVers []uint16 = []uint16{0, 0, 0} - for i, v := range split { - if i >= 3 { - break - } - ver, err := strconv.ParseUint(v, 10, 16) - if err != nil { - panic(fmt.Errorf("version: failed to parse SemVer '%s': %w", s, err)) - } - semVers[i] = uint16(ver) - } - - return Version{Major: semVers[0], Minor: semVers[1], Patch: semVers[2]} -} - var goModulesVersionRegex = regexp.MustCompile(`v0.(?P[0-9]{2})(?P[0-9]{2}).(?P[0-9]+)`) // Convert Go Modules compatible version to Oasis Core's canonical version. diff --git a/go/common/version/version_test.go b/go/common/version/version_test.go index e864d1a62d0..435ea62b3b7 100644 --- a/go/common/version/version_test.go +++ b/go/common/version/version_test.go @@ -61,20 +61,38 @@ func TestMaskNonMajor(t *testing.T) { require.NotEqual(v1.MaskNonMajor(), v4.MaskNonMajor(), "version.MaskNonMajor() should not match") } -func TestParseSemVer(t *testing.T) { +func TestFromString(t *testing.T) { require := require.New(t) for _, v := range []struct { semver string expected Version }{ - {"1.0.0", Version{1, 0, 0}}, - {"2.1.3", Version{2, 1, 3}}, - {"1.0.0-alpha", Version{1, 0, 0}}, - {"1.0.0-alpha+1.2", Version{1, 0, 0}}, - {"1.8.2-beta.1.13", Version{1, 8, 2}}, + {"0.0.1", Version{0, 0, 1}}, + {"0.1.2", Version{0, 1, 2}}, + {"1.2.3", Version{1, 2, 3}}, + {"1.2.3-alpha", Version{1, 2, 3}}, + {"1.2.3-alpha+git0253df22", Version{1, 2, 3}}, + {"1.2.3+git0253df22", Version{1, 2, 3}}, + {"1.2.3-beta.1", Version{1, 2, 3}}, + {"300.400.500", Version{300, 400, 500}}, + {"30000.40000.50000", Version{30000, 40000, 50000}}, } { - require.Equal(parseSemVerStr(v.semver), v.expected, "parseSemVerStr()") + version, err := FromString(v.semver) + require.NoError(err) + require.Equal(v.expected, version, "FromString()") + } + + // Invalid versions. + for _, v := range []string{ + "", + "100000.0.0", "0.100000.0", "0.0.100000", + "-1.0.0", "0.-1.0", "0.0.-1", + "1.0", "1", + "a.b.c", + } { + _, err := FromString(v) + require.Error(err, v) } } @@ -86,6 +104,8 @@ func TestFromToU64(t *testing.T) { {0, 0, 0}, {1, 1, 1}, {10, 20, 30}, + {300, 400, 500}, + {30000, 40000, 50000}, } { require.Equal(FromU64(v.ToU64()), v, "FromU64(version.ToU64())") } diff --git a/go/oasis-test-runner/oasis/fixture.go b/go/oasis-test-runner/oasis/fixture.go index a50909cd0d3..e362e0a7aaa 100644 --- a/go/oasis-test-runner/oasis/fixture.go +++ b/go/oasis-test-runner/oasis/fixture.go @@ -8,6 +8,7 @@ import ( "github.com/oasisprotocol/oasis-core/go/common" "github.com/oasisprotocol/oasis-core/go/common/node" "github.com/oasisprotocol/oasis-core/go/common/sgx" + "github.com/oasisprotocol/oasis-core/go/common/version" "github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env" "github.com/oasisprotocol/oasis-core/go/oasis-test-runner/log" registry "github.com/oasisprotocol/oasis-core/go/registry/api" @@ -225,6 +226,7 @@ type RuntimeFixture struct { // nolint: maligned Kind registry.RuntimeKind `json:"kind"` Entity int `json:"entity"` Keymanager int `json:"keymanager"` + Version version.Version `json:"version"` Binaries map[node.TEEHardware][]string `json:"binaries"` GenesisState storage.WriteLog `json:"genesis_state,omitempty"` @@ -272,6 +274,7 @@ func (f *RuntimeFixture) Create(netFixture *NetworkFixture, net *Network) (*Runt Keymanager: km, TEEHardware: netFixture.TEE.Hardware, MrSigner: netFixture.TEE.MrSigner, + Version: f.Version, Executor: f.Executor, TxnScheduler: f.TxnScheduler, Storage: f.Storage, diff --git a/go/oasis-test-runner/oasis/runtime.go b/go/oasis-test-runner/oasis/runtime.go index 32aa754edf2..2ed316384a2 100644 --- a/go/oasis-test-runner/oasis/runtime.go +++ b/go/oasis-test-runner/oasis/runtime.go @@ -14,6 +14,7 @@ import ( "github.com/oasisprotocol/oasis-core/go/common/crypto/hash" "github.com/oasisprotocol/oasis-core/go/common/node" "github.com/oasisprotocol/oasis-core/go/common/sgx" + "github.com/oasisprotocol/oasis-core/go/common/version" "github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env" registry "github.com/oasisprotocol/oasis-core/go/registry/api" scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api" @@ -52,6 +53,7 @@ type RuntimeCfg struct { // nolint: maligned Keymanager *Runtime TEEHardware node.TEEHardware MrSigner *sgx.MrSigner + Version version.Version Binaries map[node.TEEHardware][]string GenesisState storage.WriteLog @@ -152,6 +154,7 @@ func (net *Network) NewRuntime(cfg *RuntimeCfg) (*Runtime, error) { EntityID: cfg.Entity.entity.ID, Kind: cfg.Kind, TEEHardware: cfg.TEEHardware, + Version: registry.VersionInfo{Version: cfg.Version}, Executor: cfg.Executor, TxnScheduler: cfg.TxnScheduler, Storage: cfg.Storage,