2019-10-16 09:16:59 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package versioncontrol_test
|
|
|
|
|
|
|
|
import (
|
2020-12-03 02:14:43 +00:00
|
|
|
"context"
|
2019-10-16 09:16:59 +01:00
|
|
|
"encoding/hex"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2020-12-03 02:14:43 +00:00
|
|
|
"net/http"
|
2019-10-16 09:16:59 +01:00
|
|
|
"reflect"
|
2020-12-03 02:14:43 +00:00
|
|
|
"strings"
|
2019-10-16 09:16:59 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
2020-12-03 02:14:43 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-10-16 09:16:59 +01:00
|
|
|
|
2020-12-03 02:14:43 +00:00
|
|
|
"storj.io/common/testcontext"
|
2023-02-17 12:58:26 +00:00
|
|
|
"storj.io/common/testrand"
|
2019-10-16 09:16:59 +01:00
|
|
|
"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:",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-12-03 02:14:43 +00:00
|
|
|
func TestPeerEndpoint(t *testing.T) {
|
|
|
|
minimumVersion := "v0.0.1"
|
|
|
|
suggestedVersion := "v0.0.2"
|
|
|
|
|
|
|
|
createURL := func(process, version string) string {
|
|
|
|
urlTmpl := "http://example.com/{version}/{process}_{os}_{arch}"
|
|
|
|
url := strings.Replace(urlTmpl, "{version}", version, 1)
|
|
|
|
url = strings.Replace(url, "{process}", process, 1)
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &versioncontrol.Config{
|
|
|
|
Address: "127.0.0.1:0",
|
|
|
|
Versions: versioncontrol.OldVersionConfig{
|
|
|
|
Satellite: minimumVersion,
|
|
|
|
Storagenode: minimumVersion,
|
|
|
|
Uplink: minimumVersion,
|
|
|
|
Gateway: minimumVersion,
|
|
|
|
Identity: minimumVersion,
|
|
|
|
},
|
|
|
|
Binary: versioncontrol.ProcessesConfig{
|
|
|
|
Storagenode: versioncontrol.ProcessConfig{
|
|
|
|
Minimum: versioncontrol.VersionConfig{
|
|
|
|
Version: minimumVersion,
|
|
|
|
URL: createURL("storagenode", minimumVersion),
|
|
|
|
},
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
|
|
|
Version: suggestedVersion,
|
|
|
|
URL: createURL("storagenode", suggestedVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StoragenodeUpdater: versioncontrol.ProcessConfig{
|
|
|
|
Minimum: versioncontrol.VersionConfig{
|
|
|
|
Version: minimumVersion,
|
|
|
|
URL: createURL("storagenode-updater", minimumVersion),
|
|
|
|
},
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
|
|
|
Version: suggestedVersion,
|
|
|
|
URL: createURL("storagenode-updater", suggestedVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Uplink: versioncontrol.ProcessConfig{
|
|
|
|
Minimum: versioncontrol.VersionConfig{
|
|
|
|
Version: minimumVersion,
|
|
|
|
URL: createURL("uplink", minimumVersion),
|
|
|
|
},
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
|
|
|
Version: suggestedVersion,
|
|
|
|
URL: createURL("uplink", suggestedVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Gateway: versioncontrol.ProcessConfig{
|
|
|
|
Minimum: versioncontrol.VersionConfig{
|
|
|
|
Version: minimumVersion,
|
|
|
|
URL: createURL("gateway", minimumVersion),
|
|
|
|
},
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
|
|
|
Version: suggestedVersion,
|
|
|
|
URL: createURL("gateway", suggestedVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Identity: versioncontrol.ProcessConfig{
|
|
|
|
Minimum: versioncontrol.VersionConfig{
|
|
|
|
Version: minimumVersion,
|
|
|
|
URL: createURL("identity", minimumVersion),
|
|
|
|
},
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
|
|
|
Version: suggestedVersion,
|
|
|
|
URL: createURL("identity", suggestedVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
|
|
|
|
peer, err := versioncontrol.New(log, config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, peer)
|
|
|
|
|
|
|
|
testCtx := testcontext.New(t)
|
|
|
|
ctx, cancel := context.WithCancel(testCtx)
|
|
|
|
|
|
|
|
var wg errgroup.Group
|
|
|
|
wg.Go(func() error {
|
|
|
|
return peer.Run(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
defer testCtx.Check(peer.Close)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
baseURL := "http://" + peer.Addr()
|
|
|
|
|
|
|
|
t.Run("resolve process url", func(t *testing.T) {
|
|
|
|
queryTmpl := "processes/{service}/{version}/url?os={os}&arch={arch}"
|
|
|
|
|
|
|
|
urls := make(map[string]string)
|
|
|
|
for _, supportedBinary := range versioncontrol.SupportedBinaries {
|
|
|
|
splitted := strings.SplitN(supportedBinary, "_", 3)
|
|
|
|
|
|
|
|
service := splitted[0]
|
|
|
|
os := splitted[1]
|
|
|
|
arch := splitted[2]
|
|
|
|
|
|
|
|
for _, versionType := range []string{"minimum", "suggested"} {
|
|
|
|
query := strings.Replace(queryTmpl, "{service}", service, 1)
|
|
|
|
query = strings.Replace(query, "{version}", versionType, 1)
|
|
|
|
query = strings.Replace(query, "{os}", os, 1)
|
|
|
|
query = strings.Replace(query, "{arch}", arch, 1)
|
|
|
|
|
|
|
|
var url string
|
|
|
|
switch versionType {
|
|
|
|
case "minimum":
|
|
|
|
url = createURL(service, minimumVersion)
|
|
|
|
case "suggested":
|
|
|
|
url = createURL(service, suggestedVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
url = strings.Replace(url, "{os}", os, 1)
|
|
|
|
url = strings.Replace(url, "{arch}", arch, 1)
|
|
|
|
urls[query] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for query, url := range urls {
|
|
|
|
query, url := query, url
|
|
|
|
|
|
|
|
t.Run(query, func(t *testing.T) {
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"/"+query, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2020-12-03 02:14:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2020-12-03 02:14:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
|
|
|
require.NoError(t, resp.Body.Close())
|
|
|
|
|
|
|
|
require.Equal(t, url, string(b))
|
|
|
|
log.Debug(string(b))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-16 09:16:59 +01:00
|
|
|
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),
|
2023-02-17 12:58:26 +00:00
|
|
|
Cursor: testrand.Intn(101),
|
2019-10-16 09:16:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randSeedString(t *testing.T) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
seed := make([]byte, 32)
|
2023-02-17 12:58:26 +00:00
|
|
|
testrand.Read(seed)
|
2019-10-16 09:16:59 +01:00
|
|
|
return hex.EncodeToString(seed)
|
|
|
|
}
|