Skip to content

Commit

Permalink
check for available ports in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Dec 29, 2024
1 parent 9cd6df3 commit 87b155a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
6 changes: 3 additions & 3 deletions cmd/mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func configFromEnv() (*mint.Config, error) {
return nil, fmt.Errorf("invalid DERIVATION_PATH_IDX: %v", err)
}

port := os.Getenv("MINT_PORT")
if len(port) == 0 {
port = "3338"
port, err := strconv.Atoi(os.Getenv("MINT_PORT"))
if err != nil {
port = 3338
}

mintPath := os.Getenv("MINT_DB_PATH")
Expand Down
2 changes: 1 addition & 1 deletion mint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

type Config struct {
DerivationPathIdx uint32
Port string
Port int
MintPath string
InputFeePpk uint
MintInfo MintInfo
Expand Down
8 changes: 3 additions & 5 deletions mint/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log/slog"
"net/http"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -62,7 +63,7 @@ func (ms *MintServer) Shutdown() {
ms.httpServer.Shutdown(context.Background())
}

func (ms *MintServer) setupHttpServer(port string) error {
func (ms *MintServer) setupHttpServer(port int) error {
r := mux.NewRouter()

r.HandleFunc("/v1/keys", ms.getActiveKeysets).Methods(http.MethodGet, http.MethodOptions)
Expand All @@ -81,11 +82,8 @@ func (ms *MintServer) setupHttpServer(port string) error {

r.Use(setupHeaders)

if len(port) == 0 {
return errors.New("port cannot be empty")
}
server := &http.Server{
Addr: ":" + port,
Addr: ":" + strconv.Itoa(port),
Handler: r,
}

Expand Down
16 changes: 13 additions & 3 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
mathrand "math/rand/v2"
"net"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -193,7 +194,7 @@ func FundCashuWallet(ctx context.Context, wallet *wallet.Wallet, lnd *btcdocker.

func MintConfig(
backend lightning.Client,
port string,
port int,
derivationPathIdx uint32,
dbpath string,
inputFeePpk uint,
Expand Down Expand Up @@ -276,7 +277,7 @@ func CreateTestMint(
if err != nil {
return nil, err
}
config, err := MintConfig(lndClient, "", 0, dbpath, inputFeePpk, limits)
config, err := MintConfig(lndClient, 0, 0, dbpath, inputFeePpk, limits)
if err != nil {
return nil, err
}
Expand All @@ -290,7 +291,7 @@ func CreateTestMint(

func CreateTestMintServer(
backend lightning.Client,
port string,
port int,
derivationPathIdx uint32,
dbpath string,
inputFeePpk uint,
Expand Down Expand Up @@ -753,6 +754,15 @@ func CreateNutshellMintContainer(ctx context.Context, inputFeePpk int, lnd *btcd
return nutshellContainer, nil
}

func GetAvailablePort() (int, error) {
l, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

func generateRandomString(length int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
Expand Down
42 changes: 26 additions & 16 deletions wallet/wallet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"slices"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -52,7 +53,7 @@ func testMain(m *testing.M) (int, error) {

testMintPath := filepath.Join(".", "testmint1")
fakeBackend := &lightning.FakeBackend{}
testMint, err := testutils.CreateTestMintServer(fakeBackend, "3338", 0, testMintPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, 3338, 0, testMintPath, 0)
if err != nil {
return 1, err
}
Expand All @@ -65,7 +66,7 @@ func testMain(m *testing.M) (int, error) {

testMintPath2 := filepath.Join(".", "testmint2")
fakeBackend2 := &lightning.FakeBackend{}
testMint2, err := testutils.CreateTestMintServer(fakeBackend2, "3339", 0, testMintPath2, 0)
testMint2, err := testutils.CreateTestMintServer(fakeBackend2, 3339, 0, testMintPath2, 0)
if err != nil {
return 1, err
}
Expand All @@ -78,7 +79,7 @@ func testMain(m *testing.M) (int, error) {

mintFeesPath := filepath.Join(".", "testmintwithfees")
fakeBackend3 := &lightning.FakeBackend{}
mintWithFees, err := testutils.CreateTestMintServer(fakeBackend3, "8080", 0, mintFeesPath, 100)
mintWithFees, err := testutils.CreateTestMintServer(fakeBackend3, 8080, 0, mintFeesPath, 100)
if err != nil {
return 1, err
}
Expand Down Expand Up @@ -566,11 +567,13 @@ func TestWalletBalanceFees(t *testing.T) {
}

func TestPendingProofs(t *testing.T) {
mintURL := "http://127.0.0.1:8081"
port, _ := testutils.GetAvailablePort()
mintURL := "http://127.0.0.1:" + strconv.Itoa(port)

testMintPath := filepath.Join(".", "testmint2")
// Setting delay so that it marks payments as pending
fakeBackend := &lightning.FakeBackend{PaymentDelay: int64(time.Minute) * 2}
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8081", 0, testMintPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, port, 0, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -760,11 +763,13 @@ func TestPendingProofs(t *testing.T) {

// Test wallet operations work after mint rotates to new keyset
func TestKeysetRotations(t *testing.T) {
mintURL := "http://127.0.0.1:8082"
port, _ := testutils.GetAvailablePort()
mintURL := "http://127.0.0.1:" + strconv.Itoa(port)

testMintPath := filepath.Join(".", "testmintkeysetrotation")
var keysetDerivationIdx uint32 = 0
fakeBackend := &lightning.FakeBackend{}
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8082", keysetDerivationIdx, testMintPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, port, keysetDerivationIdx, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -778,7 +783,7 @@ func TestKeysetRotations(t *testing.T) {
var bumpKeyset = func(mint *mint.MintServer) *mint.MintServer {
testMint.Shutdown()
keysetDerivationIdx++
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8082", keysetDerivationIdx, testMintPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, port, keysetDerivationIdx, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -986,29 +991,32 @@ func TestHTLC(t *testing.T) {
}

func TestSendToPubkey(t *testing.T) {
port, _ := testutils.GetAvailablePort()
p2pkMintURL := "http://127.0.0.1:" + strconv.Itoa(port)

p2pkMintPath := filepath.Join(".", "p2pkmint1")
fakeBackend := &lightning.FakeBackend{}
p2pkMint, err := testutils.CreateTestMintServer(fakeBackend, "8889", 0, p2pkMintPath, 0)
p2pkMint, err := testutils.CreateTestMintServer(fakeBackend, port, 0, p2pkMintPath, 0)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(p2pkMintPath)
go func() {
t.Fatal(p2pkMint.Start())
}()
p2pkMintURL := "http://127.0.0.1:8889"

port2, _ := testutils.GetAvailablePort()
p2pkMintURL2 := "http://127.0.0.1:" + strconv.Itoa(port2)
p2pkMintPath2 := filepath.Join(".", "p2pkmint2")
fakeBackend2 := &lightning.FakeBackend{}
p2pkMint2, err := testutils.CreateTestMintServer(fakeBackend2, "8890", 0, p2pkMintPath2, 0)
p2pkMint2, err := testutils.CreateTestMintServer(fakeBackend2, port2, 0, p2pkMintPath2, 0)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(p2pkMintPath2)
go func() {
t.Fatal(p2pkMint2.Start())
}()
p2pkMintURL2 := "http://127.0.0.1:8890"

testWalletPath := filepath.Join(".", "/testwalletp2pk")
testWallet, err := testutils.CreateTestWallet(testWalletPath, p2pkMintURL)
Expand Down Expand Up @@ -1190,25 +1198,29 @@ func TestMultimintPayment(t *testing.T) {
t.Fatal(err)
}

port, _ := testutils.GetAvailablePort()
mint1URL := "http://127.0.0.1:" + strconv.Itoa(port)
testMintPath := filepath.Join(".", "testmppmint1")
lndClient1, err := testutils.LndClient(lnd1, testMintPath)
if err != nil {
t.Fatal(err)
}
testMint, err := testutils.CreateTestMintServer(lndClient1, "8998", 0, testMintPath, 0)
testMint, err := testutils.CreateTestMintServer(lndClient1, port, 0, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
go func() {
log.Fatal(testMint.Start())
}()

port2, _ := testutils.GetAvailablePort()
mint2URL := "http://127.0.0.1:" + strconv.Itoa(port2)
testMintPath2 := filepath.Join(".", "testmppmint2")
lndClient2, err := testutils.LndClient(lnd2, testMintPath2)
if err != nil {
t.Fatal(err)
}
testMint2, err := testutils.CreateTestMintServer(lndClient2, "8999", 0, testMintPath2, 0)
testMint2, err := testutils.CreateTestMintServer(lndClient2, port2, 0, testMintPath2, 0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1219,8 +1231,6 @@ func TestMultimintPayment(t *testing.T) {
go func() {
log.Fatal(testMint2.Start())
}()
mint1URL := "http://127.0.0.1:8998"
mint2URL := "http://127.0.0.1:8999"

// add both mints to wallet
testWalletPath := filepath.Join(".", "/testwalletmultimintpayment")
Expand Down

0 comments on commit 87b155a

Please sign in to comment.