2019-09-05 22:10:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package version_test
|
|
|
|
|
|
|
|
import (
|
2019-10-16 09:16:59 +01:00
|
|
|
"encoding/json"
|
2019-10-21 11:50:59 +01:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2019-09-05 22:10:05 +01:00
|
|
|
"testing"
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-09-05 22:10:05 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/version"
|
2019-09-05 22:10:05 +01:00
|
|
|
)
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
func TestInfo_IsZero(t *testing.T) {
|
|
|
|
zeroInfo := version.Info{}
|
|
|
|
require.True(t, zeroInfo.IsZero())
|
|
|
|
|
|
|
|
ver, err := version.NewSemVer("1.2.3")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info := version.Info{
|
|
|
|
Version: ver,
|
|
|
|
}
|
|
|
|
require.False(t, info.IsZero())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSemVer_IsZero(t *testing.T) {
|
|
|
|
zeroVer := version.SemVer{}
|
|
|
|
require.True(t, zeroVer.IsZero())
|
|
|
|
|
|
|
|
ver, err := version.NewSemVer("1.2.3")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, ver.IsZero())
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:10:05 +01:00
|
|
|
func TestSemVer_Compare(t *testing.T) {
|
|
|
|
version001, err := version.NewSemVer("v0.0.1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
version002, err := version.NewSemVer("v0.0.2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
version030, err := version.NewSemVer("v0.3.0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
version040, err := version.NewSemVer("v0.4.0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
version500, err := version.NewSemVer("v5.0.0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
version600, err := version.NewSemVer("v6.0.0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// compare the same values
|
|
|
|
require.True(t, version001.Compare(version001) == 0)
|
|
|
|
require.True(t, version030.Compare(version030) == 0)
|
|
|
|
require.True(t, version500.Compare(version500) == 0)
|
|
|
|
|
|
|
|
require.True(t, version001.Compare(version002) < 0)
|
|
|
|
require.True(t, version030.Compare(version040) < 0)
|
|
|
|
require.True(t, version500.Compare(version600) < 0)
|
|
|
|
require.True(t, version001.Compare(version030) < 0)
|
|
|
|
require.True(t, version030.Compare(version500) < 0)
|
|
|
|
|
|
|
|
require.True(t, version002.Compare(version001) > 0)
|
|
|
|
require.True(t, version040.Compare(version030) > 0)
|
|
|
|
require.True(t, version600.Compare(version500) > 0)
|
|
|
|
require.True(t, version030.Compare(version002) > 0)
|
|
|
|
require.True(t, version600.Compare(version040) > 0)
|
|
|
|
}
|
2019-10-16 09:16:59 +01:00
|
|
|
|
|
|
|
func TestRollout_MarshalJSON_UnmarshalJSON(t *testing.T) {
|
2019-10-21 11:50:59 +01:00
|
|
|
var arbitraryRollout version.Rollout
|
2019-10-16 09:16:59 +01:00
|
|
|
for i := 0; i < len(version.RolloutBytes{}); i++ {
|
2019-10-21 11:50:59 +01:00
|
|
|
arbitraryRollout.Seed[i] = byte(i)
|
|
|
|
arbitraryRollout.Cursor[i] = byte(i * 2)
|
2019-10-16 09:16:59 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
rollout version.Rollout
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"arbitrary rollout",
|
|
|
|
arbitraryRollout,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty rollout",
|
|
|
|
version.Rollout{},
|
|
|
|
},
|
|
|
|
}
|
2019-10-16 09:16:59 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
for _, scenario := range scenarios {
|
|
|
|
scenario := scenario
|
|
|
|
t.Run(scenario.name, func(t *testing.T) {
|
|
|
|
var actualRollout version.Rollout
|
2019-10-16 09:16:59 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
_, err := json.Marshal(actualRollout.Seed)
|
|
|
|
require.NoError(t, err)
|
2019-10-16 09:16:59 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
jsonRollout, err := json.Marshal(scenario.rollout)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonRollout, &actualRollout)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, scenario.rollout, actualRollout)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldUpdate(t *testing.T) {
|
|
|
|
// NB: total and acceptable tolerance are negatively correlated.
|
|
|
|
total := 10000
|
|
|
|
tolerance := total * 2 / 100 // 2%
|
|
|
|
|
|
|
|
for p := 10; p < 100; p += 10 {
|
2019-10-31 12:27:53 +00:00
|
|
|
var rollouts int
|
2019-10-21 11:50:59 +01:00
|
|
|
percentage := p
|
|
|
|
cursor := version.PercentageToCursor(percentage)
|
|
|
|
|
|
|
|
rollout := version.Rollout{
|
|
|
|
Seed: version.RolloutBytes{},
|
|
|
|
Cursor: cursor,
|
|
|
|
}
|
|
|
|
rand.Read(rollout.Seed[:])
|
|
|
|
|
|
|
|
for i := 0; i < total; i++ {
|
|
|
|
var nodeID storj.NodeID
|
|
|
|
_, err := rand.Read(nodeID[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if version.ShouldUpdate(rollout, nodeID) {
|
2019-10-31 12:27:53 +00:00
|
|
|
rollouts++
|
2019-10-21 11:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Condition(t, func() bool {
|
2019-10-31 12:27:53 +00:00
|
|
|
diff := rollouts - (total * percentage / 100)
|
2019-10-21 11:50:59 +01:00
|
|
|
return int(math.Abs(float64(diff))) < tolerance
|
|
|
|
})
|
|
|
|
}
|
2019-10-16 09:16:59 +01:00
|
|
|
}
|