Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Improve test config handling #6699

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions activation/e2e/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func launchPostSupervisor(
postCfg activation.PostConfig,
postOpts activation.PostSetupOpts,
) func() {
cmdCfg := activation.DefaultTestPostServiceConfig()
cmdCfg := activation.DefaultTestPostServiceConfig(tb)
cmdCfg.NodeAddress = fmt.Sprintf("http://%s", cfg.PublicListener)
provingOpts := activation.DefaultPostProvingOpts()
provingOpts.RandomXMode = activation.PostRandomXModeLight
Expand All @@ -105,7 +105,7 @@ func launchPostSupervisor(
}

func launchServer(tb testing.TB, services ...grpcserver.ServiceAPI) (grpcserver.Config, func()) {
cfg := grpcserver.DefaultTestConfig()
cfg := grpcserver.DefaultTestConfig(tb)

// run on random ports
server := grpcserver.New("127.0.0.1:0", zaptest.NewLogger(tb).Named("grpc"), cfg)
Expand Down
8 changes: 4 additions & 4 deletions activation/post_supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"strings"
"sync"
"sync/atomic"
"testing"

"github.com/spacemeshos/post/initialization"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

Expand All @@ -39,11 +41,9 @@ func DefaultPostServiceConfig() PostSupervisorConfig {
}

// DefaultTestPostServiceConfig returns the default config for post service in tests.
func DefaultTestPostServiceConfig() PostSupervisorConfig {
func DefaultTestPostServiceConfig(tb testing.TB) PostSupervisorConfig {
path, err := exec.Command("go", "env", "GOMOD").Output()
if err != nil {
panic(err)
}
require.NoError(tb, err)

return PostSupervisorConfig{
PostServiceCmd: filepath.Join(filepath.Dir(string(path)), "build", DefaultPostServiceName),
Expand Down
16 changes: 8 additions & 8 deletions activation/post_supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func newPostManager(tb testing.TB, cfg PostConfig, opts PostSetupOpts) *PostSetu
func Test_PostSupervisor_ErrorOnMissingBinary(t *testing.T) {
log := zaptest.NewLogger(t)

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
cmdCfg.PostServiceCmd = "missing"
postCfg := DefaultPostConfig()
postOpts := DefaultPostSetupOpts()
Expand All @@ -93,7 +93,7 @@ func Test_PostSupervisor_StopWithoutStart(t *testing.T) {
func Test_PostSupervisor_Start_FailPrepare(t *testing.T) {
log := zaptest.NewLogger(t).WithOptions(zap.WithFatalHook(calledFatal(t)))

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
postCfg := DefaultPostConfig()
postOpts := DefaultPostSetupOpts()
postOpts.DataDir = t.TempDir()
Expand Down Expand Up @@ -129,7 +129,7 @@ func calledFatal(tb testing.TB) zapcore.CheckWriteHook {
func Test_PostSupervisor_Start_FailStartSession(t *testing.T) {
log := zaptest.NewLogger(t, zaptest.WrapOptions(zap.WithFatalHook(calledFatal(t))))

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
postCfg := DefaultPostConfig()
postOpts := DefaultPostSetupOpts()
postOpts.DataDir = t.TempDir()
Expand All @@ -151,7 +151,7 @@ func Test_PostSupervisor_Start_FailStartSession(t *testing.T) {
func Test_PostSupervisor_StartsServiceCmd(t *testing.T) {
log := zaptest.NewLogger(t)

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
postCfg := DefaultPostConfig()
postOpts := testSetupOpts(t)
provingOpts := DefaultPostProvingOpts()
Expand Down Expand Up @@ -188,7 +188,7 @@ func Test_PostSupervisor_StartsServiceCmd(t *testing.T) {
func Test_PostSupervisor_Restart_Possible(t *testing.T) {
log := zaptest.NewLogger(t)

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
postCfg := DefaultPostConfig()
postOpts := testSetupOpts(t)
provingOpts := DefaultPostProvingOpts()
Expand Down Expand Up @@ -219,7 +219,7 @@ func Test_PostSupervisor_Restart_Possible(t *testing.T) {
func Test_PostSupervisor_LogFatalOnCrash(t *testing.T) {
log := zaptest.NewLogger(t, zaptest.WrapOptions(zap.WithFatalHook(calledFatal(t))))

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
postCfg := DefaultPostConfig()
postOpts := testSetupOpts(t)
provingOpts := DefaultPostProvingOpts()
Expand Down Expand Up @@ -250,7 +250,7 @@ func Test_PostSupervisor_LogFatalOnCrash(t *testing.T) {
func Test_PostSupervisor_LogFatalOnInvalidConfig(t *testing.T) {
log := zaptest.NewLogger(t, zaptest.WrapOptions(zap.WithFatalHook(calledFatal(t))))

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
cmdCfg.NodeAddress = "http://127.0.0.1:9099" // wrong port
cmdCfg.MaxRetries = 1 // speedup test, will fail on 2nd retry (~ 5s)
postCfg := DefaultPostConfig()
Expand Down Expand Up @@ -282,7 +282,7 @@ func Test_PostSupervisor_LogFatalOnInvalidConfig(t *testing.T) {
func Test_PostSupervisor_StopOnError(t *testing.T) {
log := zaptest.NewLogger(t)

cmdCfg := DefaultTestPostServiceConfig()
cmdCfg := DefaultTestPostServiceConfig(t)
postCfg := DefaultPostConfig()
postOpts := testSetupOpts(t)
provingOpts := DefaultPostProvingOpts()
Expand Down
21 changes: 14 additions & 7 deletions api/grpcserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package grpcserver

import (
"testing"
"time"
)

Expand Down Expand Up @@ -74,19 +75,25 @@ const (
func DefaultConfig() Config {
return Config{
PublicServices: []Service{
GlobalState, Mesh, Transaction, Node, Activation, ActivationV2Alpha1,
RewardV2Alpha1, NetworkV2Alpha1, NodeV2Alpha1, LayerV2Alpha1, TransactionV2Alpha1,
AccountV2Alpha1, MalfeasanceV2Alpha1,
// v1
GlobalState, Mesh, Transaction, Node, Activation,

// v2alpha1
ActivationV2Alpha1, RewardV2Alpha1, NetworkV2Alpha1, NodeV2Alpha1,
LayerV2Alpha1, TransactionV2Alpha1, AccountV2Alpha1, MalfeasanceV2Alpha1,

// v2beta1
ActivationV2Beta1, RewardV2Beta1, NetworkV2Beta1, NodeV2Beta1,
LayerV2Beta1, TransactionV2Beta1, AccountV2Beta1, MalfeasanceV2Beta1,
},
PublicListener: "0.0.0.0:9092",
PrivateServices: []Service{
Admin, Smesher, Debug, ActivationStreamV2Alpha1,
RewardStreamV2Alpha1, LayerStreamV2Alpha1, TransactionStreamV2Alpha1,
MalfeasanceStreamV2Alpha1,
// v1
Admin, Smesher, Debug,

// v2alpha1
ActivationStreamV2Alpha1, RewardStreamV2Alpha1, LayerStreamV2Alpha1,
TransactionStreamV2Alpha1, MalfeasanceStreamV2Alpha1,

// v2beta1
ActivationStreamV2Beta1, RewardStreamV2Beta1, LayerStreamV2Beta1,
Expand All @@ -107,7 +114,7 @@ func DefaultConfig() Config {
}

// DefaultTestConfig returns the default config for tests.
func DefaultTestConfig() Config {
func DefaultTestConfig(tb testing.TB) Config {
conf := DefaultConfig()
conf.PublicListener = "127.0.0.1:0"
conf.PrivateListener = "127.0.0.1:0"
Expand Down
8 changes: 4 additions & 4 deletions api/grpcserver/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func NewTx(nonce uint64, recipient types.Address, signer *signing.EdSigner) *typ
}

func launchServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
cfg := DefaultTestConfig()
cfg := DefaultTestConfig(tb)
grpcService, err := NewWithServices(cfg.PublicListener, zaptest.NewLogger(tb).Named("grpc"), cfg, services)
require.NoError(tb, err)

Expand Down Expand Up @@ -432,7 +432,7 @@ func TestNewServersConfig(t *testing.T) {
port2, err := getFreePort(0)
require.NoError(t, err, "Should be able to establish a connection on a port")

grpcService := New(fmt.Sprintf(":%d", port1), zaptest.NewLogger(t).Named("grpc"), DefaultTestConfig())
grpcService := New(fmt.Sprintf(":%d", port1), zaptest.NewLogger(t).Named("grpc"), DefaultTestConfig(t))
jsonService := NewJSONHTTPServer(zaptest.NewLogger(t).Named("grpc.JSON"), fmt.Sprintf(":%d", port2),
[]string{}, false)

Expand Down Expand Up @@ -483,7 +483,7 @@ func TestNewLocalServer(t *testing.T) {
genTime := NewMockgenesisTimeAPI(ctrl)
syncer := NewMocksyncer(ctrl)

cfg := DefaultTestConfig()
cfg := DefaultTestConfig(t)
cfg.PostListener = tc.listener
svc := NewNodeService(peerCounter, meshApi, genTime, syncer, "v0.0.0", "cafebabe")
grpcService, err := NewWithServices(cfg.PostListener, logger, cfg, []ServiceAPI{svc})
Expand Down Expand Up @@ -523,7 +523,7 @@ func setupSmesherService(tb testing.TB, sig *signing.EdSigner) (*smesherServiceC
activation.DefaultPostSetupOpts(),
sig,
)
svc.SetPostServiceConfig(activation.DefaultTestPostServiceConfig())
svc.SetPostServiceConfig(activation.DefaultTestPostServiceConfig(tb))
cfg, cleanup := launchServer(tb, svc)
tb.Cleanup(cleanup)

Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/grpcserver_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func launchTLSServer(tb testing.TB, certDir string, services ...ServiceAPI) (Con
serverCert := filepath.Join(certDir, serverCertName)
serverKey := filepath.Join(certDir, serverKeyName)

cfg := DefaultTestConfig()
cfg := DefaultTestConfig(tb)
cfg.TLSListener = "127.0.0.1:0"
cfg.TLSCACert = caCert
cfg.TLSCert = serverCert
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func launchJsonServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
cfg := DefaultTestConfig()
cfg := DefaultTestConfig(tb)

// run on random port
jsonService := NewJSONHTTPServer(zaptest.NewLogger(tb).Named("grpc.JSON"), "127.0.0.1:0",
Expand Down
10 changes: 5 additions & 5 deletions api/grpcserver/post_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func Test_GenerateProof(t *testing.T) {
opts.ProviderID.SetUint32(initialization.CPUProviderID())
opts.Scrypt.N = 2 // Speedup initialization in tests.

serviceCfg := activation.DefaultTestPostServiceConfig()
serviceCfg := activation.DefaultTestPostServiceConfig(t)
serviceCfg.NodeAddress = fmt.Sprintf("http://%s", cfg.PublicListener)

id, postCleanup := launchPostSupervisor(t, log.Named("supervisor"), cfg, serviceCfg, opts)
Expand Down Expand Up @@ -175,7 +175,7 @@ func Test_GenerateProof_TLS(t *testing.T) {
opts.ProviderID.SetUint32(initialization.CPUProviderID())
opts.Scrypt.N = 2 // Speedup initialization in tests.

serviceCfg := activation.DefaultTestPostServiceConfig()
serviceCfg := activation.DefaultTestPostServiceConfig(t)
serviceCfg.NodeAddress = fmt.Sprintf("https://%s", cfg.TLSListener)
serviceCfg.CACert = filepath.Join(certDir, caCertName)
serviceCfg.Cert = filepath.Join(certDir, clientCertName)
Expand Down Expand Up @@ -225,7 +225,7 @@ func Test_GenerateProof_Cancel(t *testing.T) {
opts.ProviderID.SetUint32(initialization.CPUProviderID())
opts.Scrypt.N = 2 // Speedup initialization in tests.

serviceCfg := activation.DefaultTestPostServiceConfig()
serviceCfg := activation.DefaultTestPostServiceConfig(t)
serviceCfg.NodeAddress = fmt.Sprintf("http://%s", cfg.PublicListener)

id, postCleanup := launchPostSupervisor(t, log.Named("supervisor"), cfg, serviceCfg, opts)
Expand Down Expand Up @@ -265,7 +265,7 @@ func Test_Metadata(t *testing.T) {
opts.ProviderID.SetUint32(initialization.CPUProviderID())
opts.Scrypt.N = 2 // Speedup initialization in tests.

serviceCfg := activation.DefaultTestPostServiceConfig()
serviceCfg := activation.DefaultTestPostServiceConfig(t)
serviceCfg.NodeAddress = fmt.Sprintf("http://%s", cfg.PublicListener)

id, postCleanup := launchPostSupervisor(t, log.Named("supervisor"), cfg, serviceCfg, opts)
Expand Down Expand Up @@ -309,7 +309,7 @@ func Test_GenerateProof_MultipleServices(t *testing.T) {
opts.ProviderID.SetUint32(initialization.CPUProviderID())
opts.Scrypt.N = 2 // Speedup initialization in tests.

serviceCfg := activation.DefaultTestPostServiceConfig()
serviceCfg := activation.DefaultTestPostServiceConfig(t)
serviceCfg.NodeAddress = fmt.Sprintf("http://%s", cfg.PublicListener)

// all but one should not be able to register to the node (i.e. open a stream to it).
Expand Down
4 changes: 2 additions & 2 deletions api/grpcserver/smesher_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestStartSmeshingPassesCorrectSmeshingOpts(t *testing.T) {
grpcPostService := grpcserver.NewMockgrpcPostService(ctrl)
sig, err := signing.NewEdSigner()
require.NoError(t, err)
cmdCfg := activation.DefaultTestPostServiceConfig()
cmdCfg := activation.DefaultTestPostServiceConfig(t)
svc := grpcserver.NewSmesherService(
smeshingProvider,
postSupervisor,
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestStartSmeshing_ErrorOnMultiSmeshingSetup(t *testing.T) {
activation.DefaultPostSetupOpts(),
nil, // no nodeID in multi smesher setup
)
svc.SetPostServiceConfig(activation.DefaultTestPostServiceConfig())
svc.SetPostServiceConfig(activation.DefaultTestPostServiceConfig(t))

types.SetNetworkHRP("stest")
providerID := uint32(7)
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2alpha1/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestNetworkService_Info(t *testing.T) {
ctx := context.Background()
genesis := time.Unix(genTimeUnix, 0)
c := config.DefaultTestConfig()
c := config.DefaultTestConfig(t)

svc := NewNetworkService(genesis, &c)
cfg, cleanup := launchServer(t, svc)
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2alpha1/v2alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
)

func launchServer(tb testing.TB, services ...grpcserver.ServiceAPI) (grpcserver.Config, func()) {
cfg := grpcserver.DefaultTestConfig()
cfg := grpcserver.DefaultTestConfig(tb)
grpc, err := grpcserver.NewWithServices(cfg.PublicListener, zaptest.NewLogger(tb).Named("grpc"), cfg, services)
require.NoError(tb, err)

Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2beta1/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestNetworkService_Info(t *testing.T) {
ctx := context.Background()
genesis := time.Unix(genTimeUnix, 0)
c := config.DefaultTestConfig()
c := config.DefaultTestConfig(t)

svc := NewNetworkService(genesis, &c)
cfg, cleanup := launchServer(t, svc)
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2beta1/v2alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
)

func launchServer(tb testing.TB, services ...grpcserver.ServiceAPI) (grpcserver.Config, func()) {
cfg := grpcserver.DefaultTestConfig()
cfg := grpcserver.DefaultTestConfig(tb)
grpc, err := grpcserver.NewWithServices(cfg.PublicListener, zaptest.NewLogger(tb).Named("grpc"), cfg, services)
require.NoError(tb, err)

Expand Down
6 changes: 3 additions & 3 deletions beacon/beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestBeacon_MultipleNodes(t *testing.T) {
atxPublishLid := types.LayerID(types.GetLayersPerEpoch()*2 - 1)
current := atxPublishLid.Add(1)
dbs := make([]*datastore.CachedDB, 0, numNodes)
cfg := NodeSimUnitTestConfig()
cfg := NodeSimUnitTestConfig(t)
bootstrap := types.Beacon{1, 2, 3, 4}
now := time.Now()
for i := 0; i < numNodes; i++ {
Expand Down Expand Up @@ -246,7 +246,7 @@ func TestBeacon_MultipleNodes_OnlyOneHonest(t *testing.T) {
atxPublishLid := types.LayerID(types.GetLayersPerEpoch()*2 - 1)
current := atxPublishLid.Add(1)
dbs := make([]*datastore.CachedDB, 0, numNodes)
cfg := NodeSimUnitTestConfig()
cfg := NodeSimUnitTestConfig(t)
bootstrap := types.Beacon{1, 2, 3, 4}
now := time.Now()
for i := 0; i < numNodes; i++ {
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestBeacon_NoProposals(t *testing.T) {
atxPublishLid := types.LayerID(types.GetLayersPerEpoch()*2 - 1)
current := atxPublishLid.Add(1)
dbs := make([]*datastore.CachedDB, 0, numNodes)
cfg := NodeSimUnitTestConfig()
cfg := NodeSimUnitTestConfig(t)
now := time.Now()
bootstrap := types.Beacon{1, 2, 3, 4}
for i := 0; i < numNodes; i++ {
Expand Down
3 changes: 2 additions & 1 deletion beacon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package beacon

import (
"math/big"
"testing"
"time"

"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -69,7 +70,7 @@ func UnitTestConfig() Config {
}

// NodeSimUnitTestConfig returns configuration for the beacon the unit tests with node simulation .
func NodeSimUnitTestConfig() Config {
func NodeSimUnitTestConfig(tb testing.TB) Config {
return Config{
Kappa: 40,
Q: *big.NewRat(1, 3),
Expand Down
8 changes: 1 addition & 7 deletions cmd/bootstrapper/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand All @@ -28,11 +27,6 @@ import (
"github.com/spacemeshos/go-spacemesh/sql/statesql"
)

func TestMain(m *testing.M) {
types.SetLayersPerEpoch(epochLayers)
os.Exit(m.Run())
}

const (
epochLayers = 3
activeSetSize = 11
Expand Down Expand Up @@ -61,7 +55,7 @@ func createAtxs(tb testing.TB, db sql.Executor, epoch types.EpochID, atxids []ty
}

func launchServer(tb testing.TB, db sql.StateDatabase) (grpcserver.Config, func()) {
cfg := grpcserver.DefaultTestConfig()
cfg := grpcserver.DefaultTestConfig(tb)
grpcService := grpcserver.New("127.0.0.1:0", zaptest.NewLogger(tb).Named("grpc"), cfg)
jsonService := grpcserver.NewJSONHTTPServer(zaptest.NewLogger(tb).Named("grpc.JSON"), "127.0.0.1:0",
[]string{}, false)
Expand Down
Loading
Loading