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"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/version"
|
2019-10-21 11:50:59 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
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 {
|
|
|
|
var updates int
|
|
|
|
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) {
|
|
|
|
updates++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Condition(t, func() bool {
|
|
|
|
diff := updates - (total * percentage / 100)
|
|
|
|
return int(math.Abs(float64(diff))) < tolerance
|
|
|
|
})
|
|
|
|
}
|
2019-10-16 09:16:59 +01:00
|
|
|
}
|