2019-10-16 09:16:59 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package versioncontrol_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
|
|
|
"storj.io/storj/versioncontrol"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rolloutErrScenarios = []struct {
|
|
|
|
name string
|
2019-10-21 11:50:59 +01:00
|
|
|
rollout versioncontrol.RolloutConfig
|
2019-10-16 09:16:59 +01:00
|
|
|
errContains string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"short seed",
|
2019-10-21 11:50:59 +01:00
|
|
|
versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
// 31 byte seed
|
|
|
|
Seed: "00000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
Cursor: 0,
|
|
|
|
},
|
|
|
|
"invalid seed length:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"long seed",
|
2019-10-21 11:50:59 +01:00
|
|
|
versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
// 33 byte seed
|
|
|
|
Seed: "000000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
Cursor: 0,
|
|
|
|
},
|
|
|
|
"invalid seed length:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid seed",
|
2019-10-21 11:50:59 +01:00
|
|
|
versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
// non-hex seed
|
|
|
|
Seed: "G000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
Cursor: 0,
|
|
|
|
},
|
|
|
|
"invalid seed:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"negative cursor",
|
2019-10-21 11:50:59 +01:00
|
|
|
versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Seed: "0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
Cursor: -1,
|
|
|
|
},
|
|
|
|
"invalid cursor percentage:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cursor too big",
|
2019-10-21 11:50:59 +01:00
|
|
|
versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Seed: "0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
Cursor: 101,
|
|
|
|
},
|
|
|
|
"invalid cursor percentage:",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeer_Run(t *testing.T) {
|
|
|
|
testVersion := "v0.0.1"
|
2019-10-21 11:50:59 +01:00
|
|
|
testServiceVersions := versioncontrol.OldVersionConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Gateway: testVersion,
|
|
|
|
Identity: testVersion,
|
|
|
|
Satellite: testVersion,
|
|
|
|
Storagenode: testVersion,
|
|
|
|
Uplink: testVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("random rollouts", func(t *testing.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
config := versioncontrol.Config{
|
2019-11-13 15:32:44 +00:00
|
|
|
Address: "127.0.0.1:0",
|
2019-10-16 09:16:59 +01:00
|
|
|
Versions: testServiceVersions,
|
|
|
|
Binary: validRandVersions(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := versioncontrol.New(zaptest.NewLogger(t), &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, peer)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty rollout seed", func(t *testing.T) {
|
2019-10-21 11:50:59 +01:00
|
|
|
versionsType := reflect.TypeOf(versioncontrol.ProcessesConfig{})
|
2019-10-16 09:16:59 +01:00
|
|
|
fieldCount := versionsType.NumField()
|
|
|
|
|
|
|
|
// test invalid rollout for each binary
|
2019-10-20 08:56:23 +01:00
|
|
|
for i := 0; i < fieldCount; i++ {
|
2019-10-21 11:50:59 +01:00
|
|
|
versions := versioncontrol.ProcessesConfig{}
|
2019-10-16 09:16:59 +01:00
|
|
|
versionsValue := reflect.ValueOf(&versions)
|
2019-10-20 08:56:23 +01:00
|
|
|
field := versionsValue.Elem().Field(i)
|
2019-10-16 09:16:59 +01:00
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
binary := versioncontrol.ProcessConfig{
|
|
|
|
Rollout: versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Seed: "",
|
|
|
|
Cursor: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
field.Set(reflect.ValueOf(binary))
|
|
|
|
|
|
|
|
config := versioncontrol.Config{
|
2019-11-13 15:32:44 +00:00
|
|
|
Address: "127.0.0.1:0",
|
2019-10-16 09:16:59 +01:00
|
|
|
Versions: testServiceVersions,
|
|
|
|
Binary: versions,
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := versioncontrol.New(zaptest.NewLogger(t), &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, peer)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeer_Run_error(t *testing.T) {
|
|
|
|
for _, scenario := range rolloutErrScenarios {
|
|
|
|
scenario := scenario
|
|
|
|
t.Run(scenario.name, func(t *testing.T) {
|
2019-10-21 11:50:59 +01:00
|
|
|
versionsType := reflect.TypeOf(versioncontrol.ProcessesConfig{})
|
2019-10-16 09:16:59 +01:00
|
|
|
fieldCount := versionsType.NumField()
|
|
|
|
|
|
|
|
// test invalid rollout for each binary
|
2019-10-20 08:56:23 +01:00
|
|
|
for i := 0; i < fieldCount; i++ {
|
2019-10-21 11:50:59 +01:00
|
|
|
versions := versioncontrol.ProcessesConfig{}
|
2019-10-16 09:16:59 +01:00
|
|
|
versionsValue := reflect.ValueOf(&versions)
|
|
|
|
field := reflect.Indirect(versionsValue).Field(i)
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
binary := versioncontrol.ProcessConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Rollout: scenario.rollout,
|
|
|
|
}
|
|
|
|
|
|
|
|
field.Set(reflect.ValueOf(binary))
|
|
|
|
|
|
|
|
config := versioncontrol.Config{
|
2019-11-13 15:32:44 +00:00
|
|
|
Address: "127.0.0.1:0",
|
|
|
|
Binary: versions,
|
2019-10-16 09:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := versioncontrol.New(zaptest.NewLogger(t), &config)
|
|
|
|
require.Nil(t, peer)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), scenario.errContains)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersions_ValidateRollouts(t *testing.T) {
|
|
|
|
versions := validRandVersions(t)
|
|
|
|
err := versions.ValidateRollouts(zaptest.NewLogger(t))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRollout_Validate(t *testing.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
2019-10-21 11:50:59 +01:00
|
|
|
rollout := versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Seed: randSeedString(t),
|
|
|
|
Cursor: i,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := rollout.Validate()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRollout_Validate_error(t *testing.T) {
|
|
|
|
for _, scenario := range rolloutErrScenarios {
|
|
|
|
scenario := scenario
|
|
|
|
t.Run(scenario.name, func(t *testing.T) {
|
|
|
|
err := scenario.rollout.Validate()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, versioncontrol.RolloutErr.Has(err))
|
|
|
|
require.Contains(t, err.Error(), scenario.errContains)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
func validRandVersions(t *testing.T) versioncontrol.ProcessesConfig {
|
2019-10-16 09:16:59 +01:00
|
|
|
t.Helper()
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
return versioncontrol.ProcessesConfig{
|
|
|
|
Satellite: versioncontrol.ProcessConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Rollout: randRollout(t),
|
|
|
|
},
|
2019-10-21 11:50:59 +01:00
|
|
|
Storagenode: versioncontrol.ProcessConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Rollout: randRollout(t),
|
|
|
|
},
|
2019-10-21 11:50:59 +01:00
|
|
|
Uplink: versioncontrol.ProcessConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Rollout: randRollout(t),
|
|
|
|
},
|
2019-10-21 11:50:59 +01:00
|
|
|
Gateway: versioncontrol.ProcessConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Rollout: randRollout(t),
|
|
|
|
},
|
2019-10-21 11:50:59 +01:00
|
|
|
Identity: versioncontrol.ProcessConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Rollout: randRollout(t),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
func randRollout(t *testing.T) versioncontrol.RolloutConfig {
|
2019-10-16 09:16:59 +01:00
|
|
|
t.Helper()
|
|
|
|
|
2019-10-21 11:50:59 +01:00
|
|
|
return versioncontrol.RolloutConfig{
|
2019-10-16 09:16:59 +01:00
|
|
|
Seed: randSeedString(t),
|
|
|
|
Cursor: rand.Intn(101),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randSeedString(t *testing.T) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
seed := make([]byte, 32)
|
|
|
|
_, err := rand.Read(seed)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return hex.EncodeToString(seed)
|
|
|
|
}
|