2019-09-20 15:22:40 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main_test
|
|
|
|
|
|
|
|
import (
|
2019-10-31 12:27:53 +00:00
|
|
|
"archive/zip"
|
2019-12-21 00:44:03 +00:00
|
|
|
"compress/flate"
|
2019-10-31 12:27:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-09-20 15:22:40 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-10-21 11:50:59 +01:00
|
|
|
"os"
|
2019-09-20 15:22:40 +01:00
|
|
|
"os/exec"
|
2019-10-31 12:27:53 +00:00
|
|
|
"path/filepath"
|
2019-12-18 14:31:59 +00:00
|
|
|
"strconv"
|
2019-09-20 15:22:40 +01:00
|
|
|
"testing"
|
2019-10-31 12:27:53 +00:00
|
|
|
"time"
|
2019-09-20 15:22:40 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/identity/testidentity"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/version"
|
2019-09-20 15:22:40 +01:00
|
|
|
"storj.io/storj/versioncontrol"
|
|
|
|
)
|
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
const (
|
|
|
|
oldVersion = "v0.19.0"
|
|
|
|
newVersion = "v0.19.5"
|
|
|
|
)
|
|
|
|
|
2019-11-14 14:03:49 +00:00
|
|
|
func TestAutoUpdater(t *testing.T) {
|
2019-10-31 12:27:53 +00:00
|
|
|
// TODO cleanup `.exe` extension for different OS
|
|
|
|
|
2019-09-20 15:22:40 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
oldSemVer, err := version.NewSemVer(oldVersion)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newSemVer, err := version.NewSemVer(newVersion)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
oldInfo := version.Info{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
CommitHash: "",
|
|
|
|
Version: oldSemVer,
|
|
|
|
Release: false,
|
2019-10-21 11:50:59 +01:00
|
|
|
}
|
2019-09-20 15:22:40 +01:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
// build real bin with old version, will be used for both storagenode and updater
|
2019-12-18 14:31:59 +00:00
|
|
|
oldBin := CompileWithVersion(ctx, "storj.io/storj/cmd/storagenode-updater", oldInfo)
|
2019-10-31 12:27:53 +00:00
|
|
|
storagenodePath := ctx.File("fake", "storagenode.exe")
|
2019-11-14 14:03:49 +00:00
|
|
|
copyBin(ctx, t, oldBin, storagenodePath)
|
2019-10-21 11:50:59 +01:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
updaterPath := ctx.File("fake", "storagenode-updater.exe")
|
|
|
|
move(t, oldBin, updaterPath)
|
|
|
|
|
|
|
|
// build real storagenode and updater with new version
|
|
|
|
newInfo := version.Info{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
CommitHash: "",
|
|
|
|
Version: newSemVer,
|
|
|
|
Release: false,
|
|
|
|
}
|
2019-12-18 14:31:59 +00:00
|
|
|
newBin := CompileWithVersion(ctx, "storj.io/storj/cmd/storagenode-updater", newInfo)
|
2019-11-14 14:03:49 +00:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
updateBins := map[string]string{
|
|
|
|
"storagenode": newBin,
|
|
|
|
"storagenode-updater": newBin,
|
2019-10-21 11:50:59 +01:00
|
|
|
}
|
2019-09-20 15:22:40 +01:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
// run versioncontrol and update zips http servers
|
|
|
|
versionControlPeer, cleanupVersionControl := testVersionControlWithUpdates(ctx, t, updateBins)
|
|
|
|
defer cleanupVersionControl()
|
|
|
|
|
|
|
|
logPath := ctx.File("storagenode-updater.log")
|
|
|
|
|
|
|
|
// write identity files to disk for use in rollout calculation
|
|
|
|
identConfig := testIdentityFiles(ctx, t)
|
|
|
|
|
|
|
|
// run updater (update)
|
2019-11-14 14:03:49 +00:00
|
|
|
args := []string{"run",
|
|
|
|
"--config-dir", ctx.Dir(),
|
|
|
|
"--server-address", "http://" + versionControlPeer.Addr(),
|
|
|
|
"--binary-location", storagenodePath,
|
|
|
|
"--check-interval", "0s",
|
|
|
|
"--identity.cert-path", identConfig.CertPath,
|
|
|
|
"--identity.key-path", identConfig.KeyPath,
|
|
|
|
"--log", logPath,
|
|
|
|
}
|
2019-10-31 12:27:53 +00:00
|
|
|
|
|
|
|
// NB: updater currently uses `log.SetOutput` so all output after that call
|
|
|
|
// only goes to the log file.
|
|
|
|
out, err := exec.Command(updaterPath, args...).CombinedOutput()
|
|
|
|
logData, logErr := ioutil.ReadFile(logPath)
|
|
|
|
if assert.NoError(t, logErr) {
|
|
|
|
logStr := string(logData)
|
|
|
|
t.Log(logStr)
|
|
|
|
if !assert.Contains(t, logStr, "storagenode restarted successfully") {
|
|
|
|
t.Log(logStr)
|
|
|
|
}
|
2019-11-18 07:26:50 +00:00
|
|
|
if !assert.Contains(t, logStr, "storagenode-updater restarted successfully") {
|
|
|
|
t.Log(logStr)
|
|
|
|
}
|
2019-10-31 12:27:53 +00:00
|
|
|
} else {
|
|
|
|
t.Log(string(out))
|
|
|
|
}
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
oldStoragenode := ctx.File("fake", "storagenode"+".old."+oldVersion+".exe")
|
|
|
|
oldStoragenodeInfo, err := os.Stat(oldStoragenode)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, oldStoragenodeInfo)
|
|
|
|
require.NotZero(t, oldStoragenodeInfo.Size())
|
|
|
|
|
2019-11-18 07:26:50 +00:00
|
|
|
backupUpdater := ctx.File("fake", "storagenode-updater.old.exe")
|
|
|
|
backupUpdaterInfo, err := os.Stat(backupUpdater)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, backupUpdaterInfo)
|
|
|
|
require.NotZero(t, backupUpdaterInfo.Size())
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 14:31:59 +00:00
|
|
|
// CompileWithVersion compiles the specified package with the version variables set
|
|
|
|
// to the passed version info values and returns the executable name.
|
|
|
|
func CompileWithVersion(ctx *testcontext.Context, pkg string, info version.Info) string {
|
|
|
|
ldFlagsX := map[string]string{
|
|
|
|
"storj.io/storj/private/version.buildTimestamp": strconv.Itoa(int(info.Timestamp.Unix())),
|
|
|
|
"storj.io/storj/private/version.buildCommitHash": info.CommitHash,
|
|
|
|
"storj.io/storj/private/version.buildVersion": info.Version.String(),
|
|
|
|
"storj.io/storj/private/version.buildRelease": strconv.FormatBool(info.Release),
|
|
|
|
}
|
|
|
|
return ctx.CompileWithLDFlagsX(pkg, ldFlagsX)
|
|
|
|
}
|
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
func move(t *testing.T, src, dst string) {
|
|
|
|
err := os.Rename(src, dst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:03:49 +00:00
|
|
|
func copyBin(ctx *testcontext.Context, t *testing.T, src, dst string) {
|
2019-10-31 12:27:53 +00:00
|
|
|
s, err := os.Open(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(s.Close)
|
|
|
|
|
|
|
|
d, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(d.Close)
|
|
|
|
|
|
|
|
_, err = io.Copy(d, s)
|
2019-09-20 15:22:40 +01:00
|
|
|
require.NoError(t, err)
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testIdentityFiles(ctx *testcontext.Context, t *testing.T) identity.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
ident, err := testidentity.PregeneratedIdentity(0, storj.LatestIDVersion())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
identConfig := identity.Config{
|
|
|
|
CertPath: ctx.File("identity", "identity.cert"),
|
|
|
|
KeyPath: ctx.File("identity", "identity.Key"),
|
|
|
|
}
|
|
|
|
err = identConfig.Save(ident)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
configData := fmt.Sprintf(
|
|
|
|
"identity.cert-path: %s\nidentity.key-path: %s",
|
|
|
|
identConfig.CertPath,
|
|
|
|
identConfig.KeyPath,
|
|
|
|
)
|
|
|
|
err = ioutil.WriteFile(ctx.File("config.yaml"), []byte(configData), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return identConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func testVersionControlWithUpdates(ctx *testcontext.Context, t *testing.T, updateBins map[string]string) (peer *versioncontrol.Peer, cleanup func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var mux http.ServeMux
|
|
|
|
for name, src := range updateBins {
|
|
|
|
dst := ctx.File("updates", name+".zip")
|
|
|
|
zipBin(ctx, t, dst, src)
|
|
|
|
zipData, err := ioutil.ReadFile(dst)
|
2019-09-20 15:22:40 +01:00
|
|
|
require.NoError(t, err)
|
2019-10-31 12:27:53 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/"+name, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := w.Write(zipData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}))
|
|
|
|
}
|
2019-09-20 15:22:40 +01:00
|
|
|
|
|
|
|
ts := httptest.NewServer(&mux)
|
|
|
|
|
2019-11-07 08:42:07 +00:00
|
|
|
var randSeed version.RolloutBytes
|
|
|
|
testrand.Read(randSeed[:])
|
|
|
|
storagenodeSeed := fmt.Sprintf("%x", randSeed)
|
|
|
|
|
|
|
|
testrand.Read(randSeed[:])
|
|
|
|
updaterSeed := fmt.Sprintf("%x", randSeed)
|
|
|
|
|
2019-09-20 15:22:40 +01:00
|
|
|
config := &versioncontrol.Config{
|
2019-11-07 08:42:07 +00:00
|
|
|
Address: "127.0.0.1:0",
|
2019-10-31 12:27:53 +00:00
|
|
|
// NB: this config field is required for versioncontrol to run.
|
2019-10-21 11:50:59 +01:00
|
|
|
Versions: versioncontrol.OldVersionConfig{
|
2019-09-20 15:22:40 +01:00
|
|
|
Satellite: "v0.0.1",
|
|
|
|
Storagenode: "v0.0.1",
|
|
|
|
Uplink: "v0.0.1",
|
|
|
|
Gateway: "v0.0.1",
|
|
|
|
Identity: "v0.0.1",
|
|
|
|
},
|
2019-10-21 11:50:59 +01:00
|
|
|
Binary: versioncontrol.ProcessesConfig{
|
|
|
|
Storagenode: versioncontrol.ProcessConfig{
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
2019-10-31 12:27:53 +00:00
|
|
|
Version: newVersion,
|
|
|
|
URL: ts.URL + "/storagenode",
|
|
|
|
},
|
|
|
|
Rollout: versioncontrol.RolloutConfig{
|
2019-11-07 08:42:07 +00:00
|
|
|
Seed: storagenodeSeed,
|
2019-10-31 12:27:53 +00:00
|
|
|
Cursor: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StoragenodeUpdater: versioncontrol.ProcessConfig{
|
|
|
|
Suggested: versioncontrol.VersionConfig{
|
|
|
|
Version: newVersion,
|
|
|
|
URL: ts.URL + "/storagenode-updater",
|
2019-09-20 15:22:40 +01:00
|
|
|
},
|
2019-10-21 11:50:59 +01:00
|
|
|
Rollout: versioncontrol.RolloutConfig{
|
2019-11-07 08:42:07 +00:00
|
|
|
Seed: updaterSeed,
|
2019-10-21 11:50:59 +01:00
|
|
|
Cursor: 100,
|
|
|
|
},
|
2019-09-20 15:22:40 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
peer, err := versioncontrol.New(zaptest.NewLogger(t), config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ctx.Go(func() error {
|
|
|
|
return peer.Run(ctx)
|
|
|
|
})
|
2019-10-31 12:27:53 +00:00
|
|
|
return peer, func() {
|
|
|
|
ts.Close()
|
|
|
|
ctx.Check(peer.Close)
|
2019-10-21 11:50:59 +01:00
|
|
|
}
|
2019-10-31 12:27:53 +00:00
|
|
|
}
|
2019-10-21 11:50:59 +01:00
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
func zipBin(ctx *testcontext.Context, t *testing.T, dst, src string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
zipFile, err := os.Create(dst)
|
|
|
|
require.NoError(t, err)
|
2019-11-14 14:03:49 +00:00
|
|
|
defer ctx.Check(zipFile.Close)
|
2019-10-31 12:27:53 +00:00
|
|
|
|
|
|
|
base := filepath.Base(dst)
|
|
|
|
base = base[:len(base)-len(".zip")]
|
|
|
|
|
|
|
|
writer := zip.NewWriter(zipFile)
|
|
|
|
defer ctx.Check(writer.Close)
|
|
|
|
|
2019-12-21 00:44:03 +00:00
|
|
|
writer.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
|
|
|
|
return flate.NewWriter(out, flate.NoCompression)
|
|
|
|
})
|
|
|
|
|
2019-10-31 12:27:53 +00:00
|
|
|
contents, err := writer.Create(base)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = contents.Write(data)
|
|
|
|
require.NoError(t, err)
|
2019-09-20 15:22:40 +01:00
|
|
|
}
|