testsuite/storjscan: add testsuite to test storjscan integration

Create Run function which spins up storjscan, eth test network and
testplanet configured with storjscan endpoint and credentials.

Change-Id: I5bafd07b6b2e4f39228faf76af7dd7135bed5f77
This commit is contained in:
Yaroslav Vorobiov 2022-07-06 00:54:03 +01:00 committed by Jennifer Li Johnson
parent 0f3da7f895
commit 53db7a897f
4 changed files with 1679 additions and 0 deletions

143
testsuite/storjscan/go.mod Normal file
View File

@ -0,0 +1,143 @@
module storj.io/storj/testsuite/storjscan
go 1.17
replace storj.io/storj => ../../
require (
github.com/spacemonkeygo/monkit/v3 v3.0.19
github.com/stretchr/testify v1.8.0
github.com/zeebo/errs v1.3.0
go.uber.org/zap v1.21.0
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
storj.io/common v0.0.0-20220719163320-cd2ef8e1b9b0
storj.io/private v0.0.0-20220725143645-8515ce4e6b70
storj.io/storj v1.59.1
storj.io/storjscan v0.0.0-20220616081657-7b8cf8dca8b0
storj.io/uplink v1.9.1-0.20220623122820-25d79c31a876
)
require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/alicebob/miniredis/v2 v2.13.3 // indirect
github.com/apache/thrift v0.12.0 // indirect
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/btcsuite/btcd v0.21.0-beta // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/calebcase/tmpfile v1.0.3 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/cloudfoundry/gosigar v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.10.16 // indirect
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/go-oauth2/oauth2/v4 v4.4.2 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-redis/redis/v8 v8.7.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/pprof v0.0.0-20211108044417-e9b028704de0 // indirect
github.com/google/uuid v1.1.5 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/schema v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/graph-gophers/graphql-go v1.3.0 // indirect
github.com/graphql-go/graphql v0.7.9 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/huin/goupnp v1.0.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/influxdata/influxdb v1.8.3 // indirect
github.com/influxdata/influxdb-client-go/v2 v2.4.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0 // indirect
github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.10.0 // indirect
github.com/jackc/pgx/v4 v4.15.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jtolds/monkit-hw/v2 v2.0.0-20191108235325-141a0da276b3 // indirect
github.com/jtolds/tracetagger/v2 v2.0.0-rc5 // indirect
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
github.com/lucas-clemente/quic-go v0.27.1 // indirect
github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect
github.com/marten-seemann/qtls-go1-17 v0.1.1 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/miguelmota/go-ethereum-hdwallet v0.1.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/pointerstructure v1.2.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/oschwald/maxminddb-golang v1.8.0 // indirect
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/otp v1.3.0 // indirect
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spf13/cobra v1.1.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 // indirect
github.com/stripe/stripe-go/v72 v72.51.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef // indirect
github.com/vivint/infectious v0.0.0-20200605153912-25a574ae18a3 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
github.com/yuin/gopher-lua v0.0.0-20191220021717-ab39c6098bdb // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
github.com/zeebo/mwc v0.0.4 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
go.opentelemetry.io/otel v0.18.0 // indirect
go.opentelemetry.io/otel/metric v0.18.0 // indirect
go.opentelemetry.io/otel/trace v0.18.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.1.10 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/segmentio/analytics-go.v3 v3.1.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/urfave/cli.v1 v1.20.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
storj.io/drpc v0.0.32 // indirect
storj.io/monkit-jaeger v0.0.0-20220726162929-c3a9898b5bca // indirect
)

1303
testsuite/storjscan/go.sum Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,200 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package storjscantest
import (
"context"
"runtime/pprof"
"testing"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"storj.io/common/grant"
"storj.io/common/storj"
"storj.io/common/testcontext"
"storj.io/private/dbutil/pgtest"
"storj.io/storj/private/blockchain"
"storj.io/storj/private/testmonkit"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
"storj.io/storjscan"
"storj.io/storjscan/private/testeth"
"storj.io/storjscan/storjscandb/storjscandbtest"
"storj.io/storjscan/tokenprice/coinmarketcap"
"storj.io/uplink"
)
var mon = monkit.Package()
// Stack contains references to storjscan app and eth test network.
type Stack struct {
Log *zap.Logger
App *storjscan.App
Network *testeth.Network
Token blockchain.Address
}
// Test defines common services for storjscan tests.
type Test func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, stack *Stack)
// Run runs testplanet and storjscan and executes test function.
func Run(t *testing.T, test Test) {
databases := satellitedbtest.Databases()
if len(databases) == 0 {
t.Fatal("Databases flag missing, set at least one:\n" +
"-postgres-test-db=" + pgtest.DefaultPostgres + "\n" +
"-cockroach-test-db=" + pgtest.DefaultCockroach)
}
config := testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
NonParallel: true,
}
for _, satelliteDB := range databases {
satelliteDB := satelliteDB
t.Run(satelliteDB.Name, func(t *testing.T) {
parallel := !config.NonParallel
if parallel {
t.Parallel()
}
if satelliteDB.MasterDB.URL == "" {
t.Skipf("Database %s connection string not provided. %s", satelliteDB.MasterDB.Name, satelliteDB.MasterDB.Message)
}
planetConfig := config
if planetConfig.Name == "" {
planetConfig.Name = t.Name()
}
log := testplanet.NewLogger(t)
testmonkit.Run(context.Background(), t, func(parent context.Context) {
defer pprof.SetGoroutineLabels(parent)
parent = pprof.WithLabels(parent, pprof.Labels("test", t.Name()))
timeout := config.Timeout
if timeout == 0 {
timeout = testcontext.DefaultTimeout
}
ctx := testcontext.NewWithContextAndTimeout(parent, t, timeout)
defer ctx.Cleanup()
// storjscan ---------
stack := Stack{
Log: log.Named("storjscan"),
}
storjscanDB, err := storjscandbtest.OpenDB(ctx, stack.Log.Named("db"), satelliteDB.MasterDB.URL, "storjscandb-"+t.Name(), "S")
if err != nil {
t.Fatalf("%+v", err)
}
defer ctx.Check(storjscanDB.Close)
if err = storjscanDB.MigrateToLatest(ctx); err != nil {
t.Fatalf("%+v", err)
}
stack.Network, err = testeth.NewNetwork()
if err != nil {
t.Fatalf("%+v", err)
}
if err = stack.Network.Start(); err != nil {
t.Fatalf("%+v", err)
}
defer ctx.Check(stack.Network.Close)
token, err := testeth.DeployToken(ctx, stack.Network, 1000000)
if err != nil {
t.Fatalf("%+v", err)
}
stack.Token = blockchain.Address(token)
var storjscanConfig storjscan.Config
storjscanConfig.API.Address = "127.0.0.1:0"
storjscanConfig.API.Keys = []string{"eu:eusecret"}
storjscanConfig.Tokens.Endpoint = stack.Network.HTTPEndpoint()
storjscanConfig.Tokens.Contract = stack.Token.Hex()
storjscanConfig.TokenPrice.PriceWindow = time.Minute
storjscanConfig.TokenPrice.Interval = time.Minute
storjscanConfig.TokenPrice.CoinmarketcapConfig = coinmarketcap.Config{} // TODO: disable
stack.App, err = storjscan.NewApp(stack.Log.Named("app"), storjscanConfig, storjscanDB)
if err != nil {
t.Fatalf("%+v", err)
}
var run errgroup.Group
runCtx, runCancel := context.WithCancel(ctx)
run.Go(func() error {
err := stack.App.Run(runCtx)
return err
})
defer ctx.Check(func() error {
runCancel()
var errlist errs.Group
errlist.Add(run.Wait())
errlist.Add(stack.App.Close())
return errlist.Err()
})
// ------------
planetConfig.Reconfigure = testplanet.Reconfigure{Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.Payments.Storjscan.Auth.Identifier = "eu"
config.Payments.Storjscan.Auth.Secret = "eusecret"
config.Payments.Storjscan.Endpoint = "http://" + stack.App.API.Listener.Addr().String()
config.Payments.Storjscan.Confirmations = 1
}}
planet, err := testplanet.NewCustom(ctx, log, planetConfig, satelliteDB)
if err != nil {
t.Fatalf("%+v", err)
}
defer ctx.Check(planet.Shutdown)
planet.Start(ctx)
provisionUplinks(ctx, t, planet)
test(t, ctx, planet, &stack)
})
})
}
}
func provisionUplinks(ctx context.Context, t *testing.T, planet *testplanet.Planet) {
for _, planetUplink := range planet.Uplinks {
for _, satellite := range planet.Satellites {
apiKey := planetUplink.APIKey[satellite.ID()]
// create access grant manually to avoid dialing satellite for
// project id and deriving key with argon2.IDKey method
encAccess := grant.NewEncryptionAccessWithDefaultKey(&storj.Key{})
encAccess.SetDefaultPathCipher(storj.EncAESGCM)
grantAccess := grant.Access{
SatelliteAddress: satellite.URL(),
APIKey: apiKey,
EncAccess: encAccess,
}
serializedAccess, err := grantAccess.Serialize()
if err != nil {
t.Fatalf("%+v", err)
}
access, err := uplink.ParseAccess(serializedAccess)
if err != nil {
t.Fatalf("%+v", err)
}
planetUplink.Access[satellite.ID()] = access
}
}
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package storjscantest_test
import (
"testing"
"github.com/stretchr/testify/require"
"storj.io/common/testcontext"
"storj.io/storj/private/testplanet"
"storj.io/storj/testsuite/storjscan/storjscantest"
"storj.io/storjscan/blockchain"
)
func TestRun(t *testing.T) {
storjscantest.Run(t, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, stack *storjscantest.Stack) {
client := stack.Network.Dial()
block, err := client.BlockNumber(ctx)
require.NoError(t, err)
require.EqualValues(t, 1, block)
err = stack.App.API.Server.LogRoutes()
require.NoError(t, err)
pmnts, err := stack.App.Tokens.Service.Payments(ctx, blockchain.Address{}, 0)
require.NoError(t, err)
require.Len(t, pmnts, 0)
// TODO: add satellite whoami test call
})
}