Skip to content

Commit 42db852

Browse files
test: refactor common mock DNS test code
1 parent b99e81c commit 42db852

4 files changed

Lines changed: 55 additions & 60 deletions

File tree

internal/testutil/mockdns.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package testutil
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/url"
7+
"testing"
8+
9+
"github.com/foxcpp/go-mockdns"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func PrepareDNSMockFromServiceURLs(t *testing.T, serviceURLs []string) (done func()) {
14+
zones := make(map[string]mockdns.Zone)
15+
for i, u := range serviceURLs {
16+
// Perpend `scheme://` as serviceURLs are currently scheme-less.
17+
// Required for parsing to produce useful results.
18+
// (see: https://pkg.go.dev/net/url@go1.20.2#URL)
19+
serviceURL, err := url.Parse(fmt.Sprintf("scheme://%s", u))
20+
require.NoError(t, err)
21+
22+
ipStr := fmt.Sprintf("10.0.0.%d", i+1)
23+
24+
if i >= 254 {
25+
panic(fmt.Sprintf("would generate invalid IPv4 address: %s", ipStr))
26+
}
27+
28+
zones[fmt.Sprintf("%s.", serviceURL.Hostname())] = mockdns.Zone{
29+
A: []string{ipStr},
30+
}
31+
}
32+
33+
return PrepareDNSMock(zones)
34+
}
35+
36+
func PrepareDNSMock(zones map[string]mockdns.Zone) (done func()) {
37+
srv, _ := mockdns.NewServerWithLogger(zones, noopLogger{}, false)
38+
srv.PatchNet(net.DefaultResolver)
39+
return func() {
40+
_ = srv.Close()
41+
mockdns.UnpatchNet(net.DefaultResolver)
42+
}
43+
}
44+
45+
// NB: default logging behavior is too noisy.
46+
// noopLogger implements go-mockdns's `mockdns.Logger` interface.
47+
type noopLogger struct{}
48+
49+
func (nl noopLogger) Printf(format string, args ...interface{}) {
50+
// noop
51+
}

p2p/module_raintree_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/stretchr/testify/require"
1717
"google.golang.org/protobuf/types/known/anypb"
1818

19+
"github.com/pokt-network/pocket/internal/testutil"
1920
"github.com/pokt-network/pocket/p2p/protocol"
2021
)
2122

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

236-
prepareDNSResolverMock(t, valIds)
237+
testutil.PrepareDNSMockFromServiceURLs(t, valIds)
237238

238239
// Create connection and bus mocks along with a shared WaitGroup to track the number of expected
239240
// reads and writes throughout the mocked local network

p2p/raintree/peers_manager_test.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package raintree
33
import (
44
"encoding/hex"
55
"fmt"
6-
"net"
76
"net/url"
87
"strings"
98
"testing"
@@ -14,6 +13,7 @@ import (
1413
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
1514
"github.com/stretchr/testify/require"
1615

16+
"github.com/pokt-network/pocket/internal/testutil"
1717
typesP2P "github.com/pokt-network/pocket/p2p/types"
1818
mocksP2P "github.com/pokt-network/pocket/p2p/types/mocks"
1919
"github.com/pokt-network/pocket/runtime/configs"
@@ -340,22 +340,6 @@ func mockAlphabetValidatorServiceURLsDNS(t *testing.T) (done func()) {
340340
A: []string{fmt.Sprintf("10.0.0.%d", i+1)},
341341
}
342342
}
343-
return prepareDNSMock(zones)
344-
}
345-
346-
// TECHDEBT(#609): de-duplicate / refactor `prepaand reDNSMock` & `noopLogger`.
347-
func prepareDNSMock(zones map[string]mockdns.Zone) (done func()) {
348-
srv, _ := mockdns.NewServerWithLogger(zones, noopLogger{}, false)
349-
srv.PatchNet(net.DefaultResolver)
350-
return func() {
351-
_ = srv.Close()
352-
mockdns.UnpatchNet(net.DefaultResolver)
353-
}
354-
}
355-
356-
// noopLogger implements go-mockdns's `mockdns.Logger` interface.
357-
type noopLogger struct{}
358343

359-
func (nl noopLogger) Printf(format string, args ...interface{}) {
360-
// noop
344+
return testutil.PrepareDNSMock(zones)
361345
}

p2p/utils_test.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import (
44
"fmt"
55
"log"
66
"net"
7-
"net/url"
87
"sort"
98
"strconv"
109
"sync"
1110
"testing"
1211
"time"
1312

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

147-
// TECHDEBT(#609): this is one of a few places where we could de-duplicate test
148-
// code if we had a conventional place to store packages intended for import
149-
// only into tests.
150-
func prepareDNSResolverMock(t *testing.T, serviceURLs []string) (done func()) {
151-
zones := make(map[string]mockdns.Zone)
152-
for i, u := range serviceURLs {
153-
// Perpend `scheme://` as serviceURLs are currently scheme-less.
154-
// Required for parsing to produce useful results.
155-
// (see: https://pkg.go.dev/net/url@go1.20.2#URL)
156-
serviceURL, err := url.Parse(fmt.Sprintf("scheme://%s", u))
157-
require.NoError(t, err)
158-
159-
ipStr := fmt.Sprintf("10.0.0.%d", i+1)
160-
161-
if i >= 254 {
162-
panic(fmt.Sprintf("would generate invalid IPv4 address: %s", ipStr))
163-
}
164-
165-
zones[fmt.Sprintf("%s.", serviceURL.Hostname())] = mockdns.Zone{
166-
A: []string{ipStr},
167-
}
168-
}
169-
170-
srv, _ := mockdns.NewServerWithLogger(zones, noopLogger{}, false)
171-
srv.PatchNet(net.DefaultResolver)
172-
return func() {
173-
_ = srv.Close()
174-
mockdns.UnpatchNet(net.DefaultResolver)
175-
}
176-
}
177-
178-
// NB: default logging behavior is too noisy.
179-
// noopLogger implements go-mockdns's `mockdns.Logger` interface.
180-
type noopLogger struct{}
181-
182-
func (nl noopLogger) Printf(format string, args ...interface{}) {
183-
// noop
184-
}
185-
186145
// createMockRuntimeMgrs creates `numValidators` instances of mocked `RuntimeMgr` that are essentially
187146
// representing the runtime environments of the validators that we will use in our tests
188147
func createMockRuntimeMgrs(t *testing.T, numValidators int) []modules.RuntimeMgr {

0 commit comments

Comments
 (0)