Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Jan 27, 2025
1 parent 2321eb7 commit 1f8a43e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 7 deletions.
21 changes: 18 additions & 3 deletions pp-mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package main

import (
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"strconv"
"syscall"

"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/accommodation/v1/accommodationv1grpc"
"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/accommodation/v2/accommodationv2grpc"
Expand All @@ -27,6 +30,9 @@ import (
)

func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

grpcServer := grpc.NewServer()

// Accommodation V1
Expand Down Expand Up @@ -58,17 +64,26 @@ func main() {
if found {
port, err = strconv.Atoi(p)
if err != nil {
panic(err)
log.Printf("failed to parse port: %v", err)
os.Exit(1)

Check failure on line 68 in pp-mock/server.go

View workflow job for this annotation

GitHub Actions / Static Analysis

exitAfterDefer: os.Exit will exit, and `defer stop()` will not run (gocritic)
}
}

log.SetOutput(os.Stdout)
log.Printf("Starting server on port: %d", port)
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
log.Printf("failed to listen: %v", err)
os.Exit(1)
}

go func() {
<-ctx.Done()
log.Printf("Shutting down server")
grpcServer.Stop()
}()

if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
log.Printf("grpc server stopped serving: %v", err)
}
}
4 changes: 4 additions & 0 deletions tests/e2e/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func StartNewBot(

cmd := exec.Command(botBinPath, "--config", configPath)
cmd.Stdout = w
cmd.Stderr = w

if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("failed to start bot (%d): %w", cmd.Process.Pid, err)
Expand Down Expand Up @@ -104,6 +105,9 @@ type Bot struct {
}

func (b *Bot) Stop(ctx context.Context) error {
if b == nil {
return nil
}
if err := process.StopProcess(ctx, b.pid); err != nil {
return fmt.Errorf("failed to stop cmb process with pid %d: %w", b.pid, err)
}
Expand Down
28 changes: 27 additions & 1 deletion tests/e2e/bot/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"math/big"
Expand All @@ -25,6 +26,7 @@ type Factory struct {
migrationsPath string
networkClient *node.Client
matrix *conduit.MatrixServer
bots []*Bot
}

// NewFactory creates a new bot factory.
Expand Down Expand Up @@ -68,7 +70,7 @@ func (f *Factory) CreateBot(ctx context.Context, port uint64, out io.Writer) (*B

botDir := path.Join(f.dir, strconv.FormatUint(port, 10)) // TODO@ add some more suffix to make bots form different tests unique 100%

return StartNewBot(
bot, errChan, err := StartNewBot(
ctx,
botDir,
f.binPath,
Expand Down Expand Up @@ -103,4 +105,28 @@ func (f *Factory) CreateBot(ctx context.Context, port uint64, out io.Writer) (*B
},
out,
)

if err != nil {
return nil, nil, fmt.Errorf("failed to start bot: %w", err)
}

f.bots = append(f.bots, bot)

return bot, errChan, nil
}

func (f *Factory) StopBots(ctx context.Context) error {
var errs []error

for _, bot := range f.bots {
if err := bot.Stop(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to stop bot (%d): %w", bot.pid, err))
}
}

if len(errs) > 0 {
return errors.Join(errs...)
}

return nil
}
1 change: 1 addition & 0 deletions tests/e2e/conduit/conduit.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func StartNewMatrixServer(
"CONDUIT_PORT="+portStr,
)
cmd.Stdout = w
cmd.Stderr = w

if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("failed to start matrix server (%d): %w", cmd.Process.Pid, err)
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ var _ = gk.Describe("E2E_TEST_1", func() {

g.Expect(matrix.Stop(ctx)).To(g.Succeed())
g.Expect(network.Stop(ctx)).To(g.Succeed())
g.Expect(botFactory.StopBots(ctx)).To(g.Succeed())
g.Expect(partnerPluginFactory.StopPartnerPlugins(ctx)).To(g.Succeed())
})

// gk.It("can start node and conduit", func() {})
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/node/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ func (n *Network) startNewNode(
fmt.Sprintf("--%s=%s", config.BootstrapIPsKey, bootstrapIPsArg),
fmt.Sprintf("--%s=%s", config.NetworkNameKey, n.networkName),
)

cmd.Stdout = w
cmd.Stderr = w

if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("failed to start node (%d): %w", cmd.Process.Pid, err)
Expand Down
30 changes: 28 additions & 2 deletions tests/e2e/partner-plugin/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package partnerplugin

import (
"context"
"errors"
"fmt"
"io"
"math/big"

Expand All @@ -11,7 +13,8 @@ import (
var DefaultCMAccountOwnerFunds = big.NewInt(0).Mul(node.CAM, big.NewInt(100))

type Factory struct {
binPath string
binPath string
partnerPlugins []*PartnerPlugin
}

// NewFactory creates a new partner-plugin factory.
Expand All @@ -21,5 +24,28 @@ func NewFactory(binPath string) *Factory {

// CreateBot creates a new bot.
func (f *Factory) CreatePartnerPlugin(ctx context.Context, port int, out io.Writer) (*PartnerPlugin, chan error, error) {
return StartNewPartnerPlugin(ctx, f.binPath, port, out)
plugin, errChan, err := StartNewPartnerPlugin(ctx, f.binPath, port, out)
if err != nil {
return nil, nil, fmt.Errorf("failed to create partner plugin: %w", err)
}

f.partnerPlugins = append(f.partnerPlugins, plugin)

return plugin, errChan, nil
}

func (f *Factory) StopPartnerPlugins(ctx context.Context) error {
var errs []error

for _, pp := range f.partnerPlugins {
if err := pp.Stop(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to stop partner plugin (%d): %w", pp.pid, err))
}
}

if len(errs) > 0 {
return errors.Join(errs...)
}

return nil
}
11 changes: 11 additions & 0 deletions tests/e2e/partner-plugin/partner_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func StartNewPartnerPlugin(
cmd := exec.Command(partnerPluginBinPath)
cmd.Env = append(cmd.Env, fmt.Sprintf("CMB_PARTNER_PLUGIN_MOCK_PORT=%d", port))
cmd.Stdout = w
cmd.Stderr = w

if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("failed to start partner-plugin (%d): %w", cmd.Process.Pid, err)
Expand Down Expand Up @@ -62,6 +63,16 @@ type PartnerPlugin struct {
pingClient pingv1grpc.PingServiceClient
}

func (pp *PartnerPlugin) Stop(ctx context.Context) error {
if pp == nil {
return nil
}
if err := process.StopProcess(ctx, pp.pid); err != nil {
return fmt.Errorf("failed to stop partner plugin process with pid %d: %w", pp.pid, err)
}
return nil
}

func (pp *PartnerPlugin) awaitReady(ctx context.Context) error {
ticker := time.NewTicker(requestTickerInterval)
defer ticker.Stop()
Expand Down

0 comments on commit 1f8a43e

Please sign in to comment.