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:
Quentin McGaw (desktop)
2021-09-11 22:22:55 +00:00
parent cade2732b0
commit 6627cda96c
8 changed files with 109 additions and 10 deletions

View File

@@ -12,6 +12,7 @@ import (
// Health contains settings for the healthcheck and health server.
type Health struct {
ServerAddress string
AddressToPing string
VPN HealthyWait
}
@@ -24,6 +25,8 @@ func (settings *Health) lines() (lines []string) {
lines = append(lines, indent+lastIndent+"Server address: "+settings.ServerAddress)
lines = append(lines, indent+lastIndent+"Address to ping: "+settings.AddressToPing)
lines = append(lines, indent+lastIndent+"VPN:")
for _, line := range settings.VPN.lines() {
lines = append(lines, indent+indent+line)
@@ -49,6 +52,11 @@ func (settings *Health) read(r reader) (err error) {
return fmt.Errorf("environment variable HEALTH_SERVER_ADDRESS: %w", err)
}
settings.AddressToPing, err = r.env.Get("HEALTH_ADDRESS_TO_PING", params.Default("1.1.1.1"))
if err != nil {
return fmt.Errorf("environment variable HEALTH_ADDRESS_TO_PING: %w", err)
}
retroKeyOption := params.RetroKeys([]string{"HEALTH_OPENVPN_DURATION_INITIAL"}, r.onRetroActive)
settings.VPN.Initial, err = r.env.Duration("HEALTH_VPN_DURATION_INITIAL", params.Default("6s"), retroKeyOption)
if err != nil {

View File

@@ -15,8 +15,15 @@ import (
func Test_Health_String(t *testing.T) {
t.Parallel()
var health Health
const expected = "|--Health:\n |--Server address: \n |--VPN:\n |--Initial duration: 0s"
health := Health{
ServerAddress: "a",
AddressToPing: "b",
}
const expected = `|--Health:
|--Server address: a
|--Address to ping: b
|--VPN:
|--Initial duration: 0s`
s := health.String()
@@ -34,6 +41,7 @@ func Test_Health_lines(t *testing.T) {
lines: []string{
"|--Health:",
" |--Server address: ",
" |--Address to ping: ",
" |--VPN:",
" |--Initial duration: 0s",
},
@@ -41,6 +49,7 @@ func Test_Health_lines(t *testing.T) {
"filled settings": {
settings: Health{
ServerAddress: "address:9999",
AddressToPing: "1.1.1.1",
VPN: HealthyWait{
Initial: time.Second,
Addition: time.Minute,
@@ -49,6 +58,7 @@ func Test_Health_lines(t *testing.T) {
lines: []string{
"|--Health:",
" |--Server address: address:9999",
" |--Address to ping: 1.1.1.1",
" |--VPN:",
" |--Initial duration: 1s",
" |--Addition duration: 1m0s",
@@ -73,6 +83,12 @@ func Test_Health_read(t *testing.T) {
errDummy := errors.New("dummy")
type stringCall struct {
call bool
s string
err error
}
type stringCallWithWarning struct {
call bool
s string
@@ -88,6 +104,7 @@ func Test_Health_read(t *testing.T) {
testCases := map[string]struct {
serverAddress stringCallWithWarning
addressToPing stringCall
vpnInitial durationCall
vpnAddition durationCall
expected Health
@@ -98,6 +115,10 @@ func Test_Health_read(t *testing.T) {
call: true,
s: "127.0.0.1:9999",
},
addressToPing: stringCall{
call: true,
s: "1.2.3.4",
},
vpnInitial: durationCall{
call: true,
duration: time.Second,
@@ -108,6 +129,7 @@ func Test_Health_read(t *testing.T) {
},
expected: Health{
ServerAddress: "127.0.0.1:9999",
AddressToPing: "1.2.3.4",
VPN: HealthyWait{
Initial: time.Second,
Addition: time.Minute,
@@ -126,10 +148,27 @@ func Test_Health_read(t *testing.T) {
},
err: errors.New("environment variable HEALTH_SERVER_ADDRESS: dummy"),
},
"address to ping error": {
serverAddress: stringCallWithWarning{
call: true,
},
addressToPing: stringCall{
call: true,
s: "address",
err: errDummy,
},
expected: Health{
AddressToPing: "address",
},
err: errors.New("environment variable HEALTH_ADDRESS_TO_PING: dummy"),
},
"initial error": {
serverAddress: stringCallWithWarning{
call: true,
},
addressToPing: stringCall{
call: true,
},
vpnInitial: durationCall{
call: true,
duration: time.Second,
@@ -146,6 +185,9 @@ func Test_Health_read(t *testing.T) {
serverAddress: stringCallWithWarning{
call: true,
},
addressToPing: stringCall{
call: true,
},
vpnInitial: durationCall{
call: true,
duration: time.Second,
@@ -186,6 +228,13 @@ func Test_Health_read(t *testing.T) {
}
}
if testCase.addressToPing.call {
value := testCase.addressToPing.s
err := testCase.addressToPing.err
env.EXPECT().Get("HEALTH_ADDRESS_TO_PING", gomock.Any()).
Return(value, err)
}
if testCase.vpnInitial.call {
value := testCase.vpnInitial.duration
err := testCase.vpnInitial.err

View File

@@ -51,6 +51,7 @@ func Test_Settings_lines(t *testing.T) {
" |--Timezone: NOT SET ⚠️ - it can cause time related issues",
"|--Health:",
" |--Server address: ",
" |--Address to ping: ",
" |--VPN:",
" |--Initial duration: 0s",
"|--HTTP control server:",