Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ then
fi

# Initialise arrays and script wide variables
IGNORE_DIRS=(".github" ".githooks" "docs" "bin")
IGNORE_DIRS=(".github" ".githooks" "docs" "bin" "internal")
MODULES_EDITED=() # Modules are considered top-level directories directly under root
MODULES_MISSING_CHANGELOG=()
CHANGELOG_FILES=() # Full paths to CHANGELOG.md files
Expand Down
2 changes: 1 addition & 1 deletion .githooks/pre-receive
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
BRANCH_CHANGED_FILES=($@)

# Initialise arrays and script wide variables
IGNORE_DIRS=(".github" ".githooks" "docs" "bin")
IGNORE_DIRS=(".github" ".githooks" "docs" "bin" "internal")
MODULES_EDITED=() # Modules are considered top-level directories directly under root
MODULES_MISSING_CHANGELOG=()
CHANGELOG_FILES=() # Full paths to CHANGELOG.md files
Expand Down
2 changes: 2 additions & 0 deletions docs/development/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ Pocket
│ ├── docs # Links to V1 Protocol implementation documentation (excluding the protocol specification)
├── consensus # Implementation of the Consensus module
├── docs # Links to V1 Protocol implementation documentation (excluding the protocol specification)
├── internal # Internal package following the convention established in Go 1.4: https://go.dev/doc/go1.4#internalpackages
│ └── testutil # Internal package for reusable and/or common test code
├── logger # Implementation of the Logger module
├── p2p # Implementation of the P2P module
├── persistence # Implementation of the Persistence module
Expand Down
51 changes: 51 additions & 0 deletions internal/testutil/mockdns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package testutil

import (
"fmt"
"net"
"net/url"
"testing"

"github.com/foxcpp/go-mockdns"
"github.com/stretchr/testify/require"
)

func PrepareDNSMockFromServiceURLs(t *testing.T, serviceURLs []string) (done func()) {
zones := make(map[string]mockdns.Zone)
for i, u := range serviceURLs {
// Perpend `scheme://` as serviceURLs are currently scheme-less.
// Required for parsing to produce useful results.
// (see: https://pkg.go.dev/net/[email protected]#URL)
serviceURL, err := url.Parse(fmt.Sprintf("scheme://%s", u))
require.NoError(t, err)

ipStr := fmt.Sprintf("10.0.0.%d", i+1)

if i >= 254 {
panic(fmt.Sprintf("would generate invalid IPv4 address: %s", ipStr))
}

zones[fmt.Sprintf("%s.", serviceURL.Hostname())] = mockdns.Zone{
A: []string{ipStr},
}
}

return PrepareDNSMock(zones)
}

func PrepareDNSMock(zones map[string]mockdns.Zone) (done func()) {
srv, _ := mockdns.NewServerWithLogger(zones, noopLogger{}, false)
srv.PatchNet(net.DefaultResolver)
return func() {
_ = srv.Close()
mockdns.UnpatchNet(net.DefaultResolver)
}
}

// NB: default logging behavior is too noisy.
// noopLogger implements go-mockdns's `mockdns.Logger` interface.
type noopLogger struct{}

func (nl noopLogger) Printf(format string, args ...interface{}) {
// noop
}
4 changes: 4 additions & 0 deletions p2p/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.0.44] - 2023-04-20

- Refactor `mockdns` test helpers

## [0.0.0.43] - 2023-04-17

- Add test to exercise `sortedPeersView#Add()` and `#Remove()`
Expand Down
3 changes: 2 additions & 1 deletion p2p/module_raintree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"

"github.com/pokt-network/pocket/internal/testutil"
"github.com/pokt-network/pocket/p2p/protocol"
)

Expand Down Expand Up @@ -233,7 +234,7 @@ func testRainTreeCalls(t *testing.T, origNode string, networkSimulationConfig Te
return iId < jId
})

prepareDNSResolverMock(t, valIds)
testutil.PrepareDNSMockFromServiceURLs(t, valIds)

// Create connection and bus mocks along with a shared WaitGroup to track the number of expected
// reads and writes throughout the mocked local network
Expand Down
20 changes: 2 additions & 18 deletions p2p/raintree/peers_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package raintree
import (
"encoding/hex"
"fmt"
"net"
"net/url"
"strings"
"testing"
Expand All @@ -14,6 +13,7 @@ import (
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"

"github.com/pokt-network/pocket/internal/testutil"
typesP2P "github.com/pokt-network/pocket/p2p/types"
mocksP2P "github.com/pokt-network/pocket/p2p/types/mocks"
"github.com/pokt-network/pocket/runtime/configs"
Expand Down Expand Up @@ -354,22 +354,6 @@ func mockAlphabetValidatorServiceURLsDNS(t *testing.T) (done func()) {
A: []string{fmt.Sprintf("10.0.0.%d", i+1)},
}
}
return prepareDNSMock(zones)
}

// TECHDEBT(#609): de-duplicate / refactor `prepaand reDNSMock` & `noopLogger`.
func prepareDNSMock(zones map[string]mockdns.Zone) (done func()) {
srv, _ := mockdns.NewServerWithLogger(zones, noopLogger{}, false)
srv.PatchNet(net.DefaultResolver)
return func() {
_ = srv.Close()
mockdns.UnpatchNet(net.DefaultResolver)
}
}

// noopLogger implements go-mockdns's `mockdns.Logger` interface.
type noopLogger struct{}

func (nl noopLogger) Printf(format string, args ...interface{}) {
// noop
return testutil.PrepareDNSMock(zones)
}
41 changes: 0 additions & 41 deletions p2p/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"fmt"
"log"
"net"
"net/url"
"sort"
"strconv"
"sync"
"testing"
"time"

"github.com/foxcpp/go-mockdns"
"github.com/golang/mock/gomock"
libp2pCrypto "github.com/libp2p/go-libp2p/core/crypto"
libp2pPeer "github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -144,45 +142,6 @@ func setupMockNetPeers(t *testing.T, netMock mocknet.Mocknet, numPeers int) (pee
return peerIDs
}

// TECHDEBT(#609): this is one of a few places where we could de-duplicate test
// code if we had a conventional place to store packages intended for import
// only into tests.
func prepareDNSResolverMock(t *testing.T, serviceURLs []string) (done func()) {
zones := make(map[string]mockdns.Zone)
for i, u := range serviceURLs {
// Perpend `scheme://` as serviceURLs are currently scheme-less.
// Required for parsing to produce useful results.
// (see: https://pkg.go.dev/net/[email protected]#URL)
serviceURL, err := url.Parse(fmt.Sprintf("scheme://%s", u))
require.NoError(t, err)

ipStr := fmt.Sprintf("10.0.0.%d", i+1)

if i >= 254 {
panic(fmt.Sprintf("would generate invalid IPv4 address: %s", ipStr))
}

zones[fmt.Sprintf("%s.", serviceURL.Hostname())] = mockdns.Zone{
A: []string{ipStr},
}
}

srv, _ := mockdns.NewServerWithLogger(zones, noopLogger{}, false)
srv.PatchNet(net.DefaultResolver)
return func() {
_ = srv.Close()
mockdns.UnpatchNet(net.DefaultResolver)
}
}

// NB: default logging behavior is too noisy.
// noopLogger implements go-mockdns's `mockdns.Logger` interface.
type noopLogger struct{}

func (nl noopLogger) Printf(format string, args ...interface{}) {
// noop
}

// createMockRuntimeMgrs creates `numValidators` instances of mocked `RuntimeMgr` that are essentially
// representing the runtime environments of the validators that we will use in our tests
func createMockRuntimeMgrs(t *testing.T, numValidators int) []modules.RuntimeMgr {
Expand Down