Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: alpha test peers tests self #3052

Merged
merged 9 commits into from
May 8, 2024
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
23 changes: 17 additions & 6 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@
type testResultError struct{ error }

type testResult struct {
Name string
Verdict testVerdict
Measurement string
Suggestion string
Error testResultError
Name string
Verdict testVerdict
Measurement string
Suggestion string
Error testResultError
IsAcceptable bool
}

func (s *testResultError) UnmarshalText(data []byte) error {
Expand All @@ -101,6 +102,10 @@

// MarshalText implements encoding.TextMarshaler
func (s testResultError) MarshalText() ([]byte, error) {
if s.error == nil {
return []byte{}, nil
}

return []byte(s.Error()), nil
}

Expand Down Expand Up @@ -214,12 +219,18 @@
avg := 0
for _, t := range results {
switch t.Verdict {
case testVerdictBad, testVerdictFail:
case testVerdictBad:
return categoryScoreC
case testVerdictGood:
avg++
case testVerdictAvg:
avg--
case testVerdictFail:
if !t.IsAcceptable {
return categoryScoreC

Check warning on line 230 in cmd/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test.go#L229-L230

Added lines #L229 - L230 were not covered by tests
}

continue

Check warning on line 233 in cmd/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test.go#L233

Added line #L233 was not covered by tests
case testVerdictOk:
continue
}
Expand Down
57 changes: 46 additions & 11 deletions cmd/testpeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"io"
"math"
"math/rand"
"net"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -99,7 +100,7 @@

func supportedSelfTestCases() map[testCaseName]func(context.Context, *testPeersConfig) testResult {
return map[testCaseName]func(context.Context, *testPeersConfig) testResult{
{name: "natOpen", order: 1}: natOpenTest,
{name: "libp2pTCPPortOpenTest", order: 1}: libp2pTCPPortOpenTest,
}
}

Expand Down Expand Up @@ -569,15 +570,49 @@
return testRes
}

func natOpenTest(ctx context.Context, _ *testPeersConfig) testResult {
// TODO(kalo): implement real port check
select {
case <-ctx.Done():
return testResult{Verdict: testVerdictFail}
default:
return testResult{
Verdict: testVerdictFail,
Error: errNotImplemented,
}
func dialLibp2pTCPIP(ctx context.Context, address string) error {
d := net.Dialer{Timeout: time.Second}
conn, err := d.DialContext(ctx, "tcp", address)
if err != nil {
return errors.Wrap(err, "net dial")
}
defer conn.Close()
buf := new(strings.Builder)
KaloyanTanev marked this conversation as resolved.
Show resolved Hide resolved
_, err = io.CopyN(buf, conn, 19)
if err != nil {

Check warning on line 582 in cmd/testpeers.go

View check run for this annotation

Codecov / codecov/patch

cmd/testpeers.go#L573-L582

Added lines #L573 - L582 were not covered by tests
return errors.Wrap(err, "io copy")
}
if !strings.Contains(buf.String(), "/multistream/1.0.0") {
return errors.New("multistream not found", z.Any("found", buf.String()), z.Any("address", address))
}

err = conn.Close()
if err != nil {
return errors.Wrap(err, "close conn")

Check warning on line 591 in cmd/testpeers.go

View check run for this annotation

Codecov / codecov/patch

cmd/testpeers.go#L585-L591

Added lines #L585 - L591 were not covered by tests
}

return nil
}

func libp2pTCPPortOpenTest(ctx context.Context, cfg *testPeersConfig) testResult {
testRes := testResult{Name: "Libp2pTCPPortOpen"}

Check warning on line 599 in cmd/testpeers.go

View check run for this annotation

Codecov / codecov/patch

cmd/testpeers.go#L593-L599

Added lines #L593 - L599 were not covered by tests
group, _ := errgroup.WithContext(ctx)

for _, addr := range cfg.P2P.TCPAddrs {
KaloyanTanev marked this conversation as resolved.
Show resolved Hide resolved
addrVal := addr
group.Go(func() error { return dialLibp2pTCPIP(ctx, addrVal) })

Check warning on line 604 in cmd/testpeers.go

View check run for this annotation

Codecov / codecov/patch

cmd/testpeers.go#L601-L604

Added lines #L601 - L604 were not covered by tests
}

Check warning on line 606 in cmd/testpeers.go

View check run for this annotation

Codecov / codecov/patch

cmd/testpeers.go#L606

Added line #L606 was not covered by tests
err := group.Wait()
if err != nil {
testRes.Verdict = testVerdictFail
testRes.Error = testResultError{err}

return testRes
}

testRes.Verdict = testVerdictOk

Check warning on line 616 in cmd/testpeers.go

View check run for this annotation

Codecov / codecov/patch

cmd/testpeers.go#L615-L616

Added lines #L615 - L616 were not covered by tests
return testRes
}
6 changes: 3 additions & 3 deletions cmd/testpeers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestPeersTest(t *testing.T) {
CategoryName: "peers",
Targets: map[string][]testResult{
"self": {
{Name: "natOpen", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errNotImplemented},
{Name: "libp2pTCPPortOpenTest", Verdict: testVerdictOk, Measurement: "", Suggestion: "", Error: testResultError{nil}},
},
"frantic-colony - enr:-JG4QFI0llFYxSoTAHm24OrbgoVx77dL6Ehl1Ydys39JYoWcBhiHrRhtGXDTaygWNsEWFb1cL7a1Bk0klIdaNuXplKWGAYGv0Gt7gmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQL6bcis0tFXnbqG4KuywxT5BLhtmijPFApKCDJNl3mXFYN0Y3CCDhqDdWRwgg4u": {
{Name: "ping", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errTimeoutInterrupted},
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestPeersTest(t *testing.T) {
CategoryName: "peers",
Targets: map[string][]testResult{
"self": {
{Name: "natOpen", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errNotImplemented},
{Name: "libp2pTCPPortOpenTest", Verdict: testVerdictOk, Measurement: "", Suggestion: "", Error: testResultError{nil}},
},
"frantic-colony - enr:-JG4QFI0llFYxSoTAHm24OrbgoVx77dL6Ehl1Ydys39JYoWcBhiHrRhtGXDTaygWNsEWFb1cL7a1Bk0klIdaNuXplKWGAYGv0Gt7gmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQL6bcis0tFXnbqG4KuywxT5BLhtmijPFApKCDJNl3mXFYN0Y3CCDhqDdWRwgg4u": {
{Name: "ping", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errTimeoutInterrupted},
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestPeersTest(t *testing.T) {
CategoryName: "peers",
Targets: map[string][]testResult{
"self": {
{Name: "natOpen", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errNotImplemented},
{Name: "libp2pTCPPortOpenTest", Verdict: testVerdictOk, Measurement: "", Suggestion: "", Error: testResultError{nil}},
},
"frantic-colony - enr:-JG4QFI0llFYxSoTAHm24OrbgoVx77dL6Ehl1Ydys39JYoWcBhiHrRhtGXDTaygWNsEWFb1cL7a1Bk0klIdaNuXplKWGAYGv0Gt7gmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQL6bcis0tFXnbqG4KuywxT5BLhtmijPFApKCDJNl3mXFYN0Y3CCDhqDdWRwgg4u": {
{Name: "ping", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errTimeoutInterrupted},
Expand Down
Loading