Skip to content

Add unit tests for modules/ping and fix Windows ping failures - #2141

Merged
Seanstoppable merged 3 commits into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-ping-unit-tests
Jul 26, 2026
Merged

Seanstoppable merged 3 commits into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-ping-unit-tests

Conversation

@Seanstoppable

Copy link
Copy Markdown
Collaborator

Summary

  • Adds unit tests for modules/ping, taking coverage from 0% to 100%.
  • Refactors Widget to inject a mockable pinger interface (pingerFactory field, defaulting to newRealPinger), so doPings/Refresh can be tested without sending real ICMP packets.
  • Fixes a real bug found while testing manually: on Windows, pro-bing's default unprivileged (UDP) ICMP mode isn't supported at all (socket: the requested protocol has not been configured into the system), so every host was silently reported as DOWN. Now uses privileged (raw ICMP) mode on Windows; other platforms keep the unprivileged default so they don't need root/CAP_NET_RAW.
  • Also changed the ping-error log call from log.Fatalf to log.Printf, so a single failed ping no longer terminates the whole app.

Testing

  • go test ./modules/ping/... -cover → 100.0% coverage, all tests pass.
  • golangci-lint run ./modules/ping/... → 0 issues.
  • go build ./... succeeds.
  • Manually verified pinging 8.8.8.8, 1.1.1.1, and github.com now correctly report Up on Windows (previously all showed DOWN).

Cover buildhosts YAML parsing (labels, defaults, invalid entries),
NewSettingsFromYAML, ConfigText, and Widget.content status/color
formatting via table-driven tests. doPings is left untested since it
performs real ICMP network calls.
Introduce a pinger interface (Run/Statistics) and a pingerFactory field
on Widget, defaulting to newRealPinger which wraps probing.NewPinger.
This lets tests substitute a fake pinger instead of sending real ICMP
packets, enabling coverage of doPings and Refresh.

Also extract hostUpFromStatistics as a small pure function, and change
the ping-error path from log.Fatalf to log.Printf so a single failed
ping no longer kills the whole app.

Coverage: 62.7% -> 100%.
pro-bing defaults to unprivileged (UDP) ICMP mode, which Windows does
not support at all: Run() immediately fails with 'socket: the
requested protocol has not been configured into the system', leaving
every host marked as down.

Use privileged (raw ICMP) mode on Windows, where sending raw ICMP
echo requests works without elevation in testing. Other platforms are
left on the unprivileged default so they don't require root/CAP_NET_RAW.

Verified against 8.8.8.8, 1.1.1.1, and github.com - all now report Up.
@Seanstoppable
Seanstoppable merged commit 3eb3143 into wtfutil:trunk Jul 26, 2026
4 checks passed
@Seanstoppable
Seanstoppable deleted the seanstoppable-ping-unit-tests branch July 26, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址