Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/s3: improve patch number capacity in version comparison #5224

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/updater/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func TestGetVersion(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

testValidVersion(t, "algonode_update_0.1.0.log", uint64(0x00010000))
testValidVersion(t, "algo_update_0.1.0", uint64(0x00010000))
testValidVersion(t, "algo_update_65535.1.0", uint64(0xFFFF00010000))
testValidVersion(t, "algo_update_65535.65535.65535", uint64(0xFFFFFFFFFFFF))
testValidVersion(t, "algonode_update_0.1.0.log", uint64(0x01000000))
testValidVersion(t, "algo_update_0.1.0", uint64(0x01000000))
testValidVersion(t, "algo_update_65535.1.0", uint64(0x00FFFF0001000000))
testValidVersion(t, "algo_update_65535.65535.65535", uint64(0xFFFFFFFF00FFFF))
testValidVersion(t, "algo_update_65535.65535.16777215", uint64(0xFFFFFFFFFFFFFF))

testInvalidVersion(t, "algo_update_0.-1.0")
testInvalidVersion(t, "algo_update_1e5.0.0")
Expand Down
22 changes: 11 additions & 11 deletions util/s3/s3Helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ func GetVersionFromName(name string) (version uint64, err error) {
return
}
var val uint64
for index, match := range submatchAll[0] {
if index > 0 {
version <<= 16
val, err = strconv.ParseUint(match, 10, 0)
if err != nil {
return
}
version += val
submatch := submatchAll[0][1:] // skip the first match which is the whole string
offsets := []int{0, 16, 24} // some bits for major (not really restricted), 16 bits for minor, 24 bits for patch
for index, match := range submatch {
version <<= offsets[index]
val, err = strconv.ParseUint(match, 10, 0)
if err != nil {
return
}
version += val
}
return
}
Expand All @@ -262,13 +262,13 @@ func GetVersionFromName(name string) (version uint64, err error) {
func GetVersionPartsFromVersion(version uint64) (major uint64, minor uint64, patch uint64, err error) {
val := version

if val < 1<<32 {
if val < 1<<40 {
err = errors.New("versions below 1.0.0 not supported")
return
}

patch = val & 0xffff
val >>= 16
patch = val & 0xffffff
val >>= 24
minor = val & 0xffff
val >>= 16
major = val
Expand Down
73 changes: 60 additions & 13 deletions util/s3/s3Helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package s3

import (
"fmt"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -174,19 +175,20 @@ func TestMakeS3SessionForDownloadWithBucket(t *testing.T) {

func TestGetVersionFromName(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

type args struct {
name string
version string
expected uint64
}
tests := []args{
{name: "test 1 (major)", version: "_1.0.0", expected: 1 * 1 << 32},
{name: "test 2 (major)", version: "_2.0.0", expected: 2 * 1 << 32},
{name: "test 3 (minor)", version: "_1.1.0", expected: 1*1<<32 + 1*1<<16},
{name: "test 4 (minor)", version: "_1.2.0", expected: 1*1<<32 + 2*1<<16},
{name: "test 5 (patch)", version: "_1.0.1", expected: 1*1<<32 + 1},
{name: "test 6 (patch)", version: "_1.0.2", expected: 1*1<<32 + 2},
{name: "test 1 (major)", version: "_1.0.0", expected: 1 * 1 << 40},
{name: "test 2 (major)", version: "_2.0.0", expected: 2 * 1 << 40},
{name: "test 3 (minor)", version: "_1.1.0", expected: 1*1<<40 + 1*1<<24},
{name: "test 4 (minor)", version: "_1.2.0", expected: 1*1<<40 + 2*1<<24},
{name: "test 5 (patch)", version: "_1.0.1", expected: 1*1<<40 + 1},
{name: "test 6 (patch)", version: "_1.0.2", expected: 1*1<<40 + 2},
}

for _, test := range tests {
Expand All @@ -196,8 +198,24 @@ func TestGetVersionFromName(t *testing.T) {
}
}

func TestGetVersionFromNameCompare(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

name1 := "config_3.13.170018.tar.gz"
name2 := "config_3.15.157.tar.gz"

ver1, err := GetVersionFromName(name1)
require.NoError(t, err)
ver2, err := GetVersionFromName(name2)
require.NoError(t, err)

require.Less(t, ver1, ver2)
}

func TestGetPartsFromVersion(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

type args struct {
name string
Expand All @@ -207,12 +225,12 @@ func TestGetPartsFromVersion(t *testing.T) {
expPatch uint64
}
tests := []args{
{name: "test 1 (major)", version: 1 * 1 << 32, expMajor: 1, expMinor: 0, expPatch: 0},
{name: "test 2 (major)", version: 2 * 1 << 32, expMajor: 2, expMinor: 0, expPatch: 0},
{name: "test 3 (minor)", version: 1*1<<32 + 1*1<<16, expMajor: 1, expMinor: 1, expPatch: 0},
{name: "test 4 (minor)", version: 1*1<<32 + 2*1<<16, expMajor: 1, expMinor: 2, expPatch: 0},
{name: "test 5 (patch)", version: 1*1<<32 + 1, expMajor: 1, expMinor: 0, expPatch: 1},
{name: "test 6 (patch)", version: 1*1<<32 + 2, expMajor: 1, expMinor: 0, expPatch: 2},
{name: "test 1 (major)", version: 1 * 1 << 40, expMajor: 1, expMinor: 0, expPatch: 0},
{name: "test 2 (major)", version: 2 * 1 << 40, expMajor: 2, expMinor: 0, expPatch: 0},
{name: "test 3 (minor)", version: 1*1<<40 + 1*1<<24, expMajor: 1, expMinor: 1, expPatch: 0},
{name: "test 4 (minor)", version: 1*1<<40 + 2*1<<24, expMajor: 1, expMinor: 2, expPatch: 0},
{name: "test 5 (patch)", version: 1*1<<40 + 1, expMajor: 1, expMinor: 0, expPatch: 1},
{name: "test 6 (patch)", version: 1*1<<40 + 2, expMajor: 1, expMinor: 0, expPatch: 2},
}

for _, test := range tests {
Expand All @@ -223,6 +241,35 @@ func TestGetPartsFromVersion(t *testing.T) {
require.Equal(t, test.expPatch, actualPatch, test.name)
}

_, _, _, err := GetVersionPartsFromVersion(1<<32 - 1)
_, _, _, err := GetVersionPartsFromVersion(1<<40 - 1)
require.Error(t, err, "Versions less than 1.0.0 should not be parsed.")
}

func TestGetPartsFromVersionEndToEnd(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

type args struct {
major uint64
minor uint64
patch uint64
}
tests := []args{
{major: 1, minor: 0, patch: 0},
{major: 3, minor: 13, patch: 170018},
{major: 3, minor: 15, patch: 157},
}

for _, test := range tests {
name := fmt.Sprintf("config_%d.%d.%d.tar.gz", test.major, test.minor, test.patch)
t.Run(name, func(t *testing.T) {
ver, err := GetVersionFromName(name)
require.NoError(t, err)
actualMajor, actualMinor, actualPatch, err := GetVersionPartsFromVersion(ver)
require.NoError(t, err)
require.Equal(t, test.major, actualMajor)
require.Equal(t, test.minor, actualMinor)
require.Equal(t, test.patch, actualPatch)
})
}
}