Skip to content

Commit

Permalink
Add tcpdump option
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 14, 2023
1 parent 8225472 commit 42df410
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"testing"
"time"

Expand All @@ -19,6 +22,7 @@ type SlidingSyncDeployment struct {
postgres testcontainers.Container
slidingSync testcontainers.Container
slidingSyncURL string
tcpdump *exec.Cmd
}

func (d *SlidingSyncDeployment) SlidingSyncURL(t *testing.T) string {
Expand All @@ -41,9 +45,12 @@ func (d *SlidingSyncDeployment) Teardown() {
log.Fatalf("failed to stop postgres: %s", err)
}
}
if d.tcpdump != nil {
d.tcpdump.Process.Signal(os.Interrupt)
}
}

func RunNewDeployment(t *testing.T) *SlidingSyncDeployment {
func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment {
// allow 30s for everything to deploy
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
Expand Down Expand Up @@ -107,11 +114,26 @@ func RunNewDeployment(t *testing.T) *SlidingSyncDeployment {
t.Logf(" sliding sync: ssproxy %s", ssURL)
t.Logf(" synapse: hs1 %s", csapi.BaseURL)
t.Logf(" postgres: postgres")
var cmd *exec.Cmd
if shouldTCPDump {
t.Log("Running tcpdump...")
su, _ := url.Parse(ssURL)
cu, _ := url.Parse(csapi.BaseURL)
filter := fmt.Sprintf("tcp port %s or port %s", su.Port(), cu.Port())
cmd = exec.Command("tcpdump", "-i", "any", "-s", "0", filter, "-w", "test.pcap")
t.Log(cmd.String())
if err := cmd.Start(); err != nil {
t.Fatalf("tcpdump failed: %v", err)
}
// TODO needs sudo
t.Logf("Started tcpdumping: PID %d", cmd.Process.Pid)
}
return &SlidingSyncDeployment{
Deployment: deployment,
slidingSync: ssContainer,
postgres: postgresContainer,
slidingSyncURL: ssURL,
tcpdump: cmd,
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Deploy(t *testing.T) *deploy.SlidingSyncDeployment {
if ssDeployment != nil {
return ssDeployment
}
ssDeployment = deploy.RunNewDeployment(t)
ssDeployment = deploy.RunNewDeployment(t, os.Getenv("COMPLEMENT_CRYPTO_TCPDUMP") == "1")
return ssDeployment
}

Expand Down

0 comments on commit 42df410

Please sign in to comment.