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

Migrate system tests to use GRPC V2 API #6696

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions activation/e2e/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -64,7 +64,7 @@ func Test_BuilderWithMultipleClients(t *testing.T) {
db := statesql.InMemoryTest(t)
localDB := localsql.InMemoryTest(t)

svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)

grpcCfg, cleanup := launchServer(t, svc)
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/atx_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/spacemeshos/go-spacemesh/activation"
ae2e "github.com/spacemeshos/go-spacemesh/activation/e2e"
"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -214,7 +214,7 @@ func Test_MarryAndMerge(t *testing.T) {
t.Cleanup(func() { assert.NoError(t, cdb.Close()) })
localDB := localsql.InMemoryTest(t)

svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)

grpcCfg, cleanup := launchServer(t, svc)
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/builds_atx_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spacemeshos/go-spacemesh/activation"
ae2e "github.com/spacemeshos/go-spacemesh/activation/e2e"
"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestBuilder_SwitchesToBuildV2(t *testing.T) {
t.Cleanup(func() { assert.NoError(t, cdb.Close()) })

opts := testPostSetupOpts(t)
svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)

grpcCfg, cleanup := launchServer(t, svc)
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/certifier_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"golang.org/x/sync/errgroup"

"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql/localsql"
Expand All @@ -39,7 +39,7 @@ func TestCertification(t *testing.T) {

opts := testPostSetupOpts(t)
logger := zaptest.NewLogger(t)
svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)

grpcCfg, cleanup := launchServer(t, svc)
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/checkpoint_merged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spacemeshos/go-spacemesh/activation"
ae2e "github.com/spacemeshos/go-spacemesh/activation/e2e"
"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/checkpoint"
"github.com/spacemeshos/go-spacemesh/codec"
Expand Down Expand Up @@ -48,7 +48,7 @@ func Test_CheckpointAfterMerge(t *testing.T) {
t.Cleanup(func() { assert.NoError(t, cdb.Close()) })
localDB := localsql.InMemoryTest(t)

svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/spacemeshos/go-spacemesh/activation"
ae2e "github.com/spacemeshos/go-spacemesh/activation/e2e"
"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/checkpoint"
"github.com/spacemeshos/go-spacemesh/codec"
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestCheckpoint_PublishingSoloATXs(t *testing.T) {
t.Cleanup(func() { assert.NoError(t, cdb.Close()) })

opts := testPostSetupOpts(t)
svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)
Expand Down
7 changes: 4 additions & 3 deletions activation/e2e/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spacemeshos/go-spacemesh/activation"
ae2e "github.com/spacemeshos/go-spacemesh/activation/e2e"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/signing"
Expand Down Expand Up @@ -130,7 +131,7 @@ func initPost(
sig *signing.EdSigner,
golden types.ATXID,
grpcCfg grpcserver.Config,
svc *grpcserver.PostService,
svc *v1.PostService,
) {
tb.Helper()

Expand Down Expand Up @@ -161,7 +162,7 @@ func TestNIPostBuilderWithClients(t *testing.T) {
localDb := localsql.InMemoryTest(t)

opts := testPostSetupOpts(t)
svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)
Expand Down Expand Up @@ -247,7 +248,7 @@ func Test_NIPostBuilderWithMultipleClients(t *testing.T) {
db := statesql.InMemoryTest(t)

opts := testPostSetupOpts(t)
svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/spacemeshos/go-spacemesh/activation"
ae2e "github.com/spacemeshos/go-spacemesh/activation/e2e"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql/localsql"
Expand All @@ -34,7 +34,7 @@ func TestValidator_Validate(t *testing.T) {
validator := activation.NewMocknipostValidator(gomock.NewController(t))

opts := testPostSetupOpts(t)
svc := grpcserver.NewPostService(logger, grpcserver.PostServiceQueryInterval(100*time.Millisecond))
svc := v1.NewPostService(logger, v1.PostServiceQueryInterval(100*time.Millisecond))
svc.AllowConnections(true)
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)
Expand Down
15 changes: 8 additions & 7 deletions api/grpcserver/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.uber.org/zap/zaptest"
"google.golang.org/protobuf/encoding/protojson"

v1 "github.com/spacemeshos/go-spacemesh/api/grpcserver/v1"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/sql/statesql"
Expand Down Expand Up @@ -59,16 +60,16 @@ func TestJsonApi(t *testing.T) {
const build = "cafebabe"

ctrl, ctx := gomock.WithContext(context.Background(), t)
peerCounter := NewMockpeerCounter(ctrl)
meshAPIMock := NewMockmeshAPI(ctrl)
genTime := NewMockgenesisTimeAPI(ctrl)
syncer := NewMocksyncer(ctrl)
conStateAPI := NewMockconservativeState(ctrl)
peerCounter := v1.NewMockpeerCounter(ctrl)
meshAPIMock := v1.NewMockmeshAPI(ctrl)
genTime := v1.NewMockgenesisTimeAPI(ctrl)
syncer := v1.NewMocksyncer(ctrl)
conStateAPI := v1.NewMockconservativeState(ctrl)

cdb := datastore.NewCachedDB(statesql.InMemoryTest(t), zaptest.NewLogger(t))
t.Cleanup(func() { assert.NoError(t, cdb.Close()) })
svc1 := NewNodeService(peerCounter, meshAPIMock, genTime, syncer, version, build)
svc2 := NewMeshService(
svc1 := v1.NewNodeService(peerCounter, meshAPIMock, genTime, syncer, version, build)
svc2 := v1.NewMeshService(
cdb,
meshAPIMock,
conStateAPI,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/events.go → api/grpcserver/v1/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package grpcserver
package v1

import (
"context"
"errors"
"fmt"
"io"
"log"
"math"
"math/big"
"net"
"os"
"path/filepath"
"strconv"
"testing"
"time"

Expand All @@ -28,13 +25,12 @@ import (
"go.uber.org/zap/zaptest/observer"
"golang.org/x/sync/errgroup"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
Expand Down Expand Up @@ -122,17 +118,6 @@ func genLayerBlock(layerID types.LayerID, txs []types.TransactionID) *types.Bloc
return b
}

func dialGrpc(tb testing.TB, cfg Config) *grpc.ClientConn {
tb.Helper()
conn, err := grpc.NewClient(
cfg.PublicListener,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
require.NoError(tb, err)
tb.Cleanup(func() { require.NoError(tb, conn.Close()) })
return conn
}

func TestMain(m *testing.M) {
types.SetLayersPerEpoch(layersPerEpoch)

Expand Down Expand Up @@ -399,47 +384,6 @@ func NewTx(nonce uint64, recipient types.Address, signer *signing.EdSigner) *typ
return &tx
}

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

// start gRPC server
require.NoError(tb, grpcService.Start())

// update config with bound addresses
cfg.PublicListener = grpcService.BoundAddress

return cfg, func() { assert.NoError(tb, grpcService.Close()) }
}

func getFreePort(optionalPort int) (int, error) {
l, e := net.Listen("tcp", fmt.Sprintf(":%v", optionalPort))
if e != nil {
l, e = net.Listen("tcp", ":0")
if e != nil {
return 0, fmt.Errorf("listen TCP: %w", e)
}
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

func TestNewServersConfig(t *testing.T) {
port1, err := getFreePort(0)
require.NoError(t, err, "Should be able to establish a connection on a port")

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(t))
jsonService := NewJSONHTTPServer(zaptest.NewLogger(t).Named("grpc.JSON"), fmt.Sprintf(":%d", port2),
[]string{}, false)

require.Contains(t, grpcService.listener, strconv.Itoa(port1), "Expected same port")
require.Contains(t, jsonService.listener, strconv.Itoa(port2), "Expected same port")
}

func TestNewLocalServer(t *testing.T) {
tt := []struct {
name string
Expand Down Expand Up @@ -483,10 +427,10 @@ func TestNewLocalServer(t *testing.T) {
genTime := NewMockgenesisTimeAPI(ctrl)
syncer := NewMocksyncer(ctrl)

cfg := DefaultTestConfig(t)
cfg := grpcserver.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})
_, err := grpcserver.NewWithServices(cfg.PostListener, logger, cfg, []grpcserver.ServiceAPI{svc})
if tc.warn {
require.Equal(t, 1, observedLogs.Len(), "Expected a warning log")
require.Equal(t, "unsecured grpc server is listening on a public IP address",
Expand All @@ -495,9 +439,7 @@ func TestNewLocalServer(t *testing.T) {
require.Equal(t, tc.listener, observedLogs.All()[0].ContextMap()["address"])
return
}

require.NoError(t, err)
require.Equal(t, grpcService.listener, tc.listener, "expected same listener")
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand All @@ -15,7 +15,7 @@ import (
"github.com/spacemeshos/go-spacemesh/system"
)

//go:generate mockgen -typed -package=grpcserver -destination=./mocks.go -source=./interface.go
//go:generate mockgen -typed -package=v1 -destination=./mocks.go -source=./interface.go

// networkInfo interface.
type networkInfo interface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpcserver
package v1

import (
"context"
Expand Down
Loading
Loading