Feat: HEALTH_ADDRESS_TO_PING variable
- Defaults to `1.1.1.1` - Add more Ping integration tests with different addresses - Add unit test pinging 127.0.0.1 - Add comment explaining why we need to use ICMP instead of UDP
This commit is contained in:
@@ -5,6 +5,7 @@ package healthcheck
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -12,9 +13,34 @@ import (
|
||||
func Test_healthCheck_ping(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pinger := newPinger()
|
||||
const timeout = time.Second
|
||||
|
||||
err := healthCheck(context.Background(), pinger)
|
||||
testCases := map[string]struct {
|
||||
address string
|
||||
err error
|
||||
}{
|
||||
"1.1.1.1": {
|
||||
address: "1.1.1.1",
|
||||
},
|
||||
"99.99.99.99": {
|
||||
address: "99.99.99.99",
|
||||
err: context.DeadlineExceeded,
|
||||
},
|
||||
}
|
||||
|
||||
assert.NoError(t, err)
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
pinger := newPinger(testCase.address)
|
||||
|
||||
err := healthCheck(ctx, pinger)
|
||||
|
||||
assert.ErrorIs(t, testCase.err, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -69,7 +70,7 @@ func Test_healthCheck(t *testing.T) {
|
||||
t.Run("canceled real pinger", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pinger := newPinger()
|
||||
pinger := newPinger("1.1.1.1")
|
||||
|
||||
canceledCtx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
@@ -78,4 +79,18 @@ func Test_healthCheck(t *testing.T) {
|
||||
|
||||
assert.ErrorIs(t, context.Canceled, err)
|
||||
})
|
||||
|
||||
t.Run("ping 127.0.0.1", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pinger := newPinger("127.0.0.1")
|
||||
|
||||
const timeout = 100 * time.Millisecond
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err := healthCheck(ctx, pinger)
|
||||
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,11 +9,10 @@ type Pinger interface {
|
||||
Stop()
|
||||
}
|
||||
|
||||
func newPinger() (pinger *ping.Pinger) {
|
||||
const addrToPing = "1.1.1.1"
|
||||
func newPinger(addrToPing string) (pinger *ping.Pinger) {
|
||||
const count = 1
|
||||
pinger = ping.New(addrToPing)
|
||||
pinger.Count = count
|
||||
pinger.SetPrivileged(true)
|
||||
pinger.SetPrivileged(true) // see https://github.com/go-ping/ping#linux
|
||||
return pinger
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func NewServer(config configuration.Health,
|
||||
return &Server{
|
||||
logger: logger,
|
||||
handler: newHandler(logger),
|
||||
pinger: newPinger(),
|
||||
pinger: newPinger(config.AddressToPing),
|
||||
config: config,
|
||||
vpn: vpnHealth{
|
||||
looper: vpnLooper,
|
||||
|
||||
Reference in New Issue
Block a user