From 0eccd068e531de89df6d2b3b736dd2642d6a3d59 Mon Sep 17 00:00:00 2001 From: "Quentin McGaw (desktop)" Date: Sat, 11 Sep 2021 21:04:21 +0000 Subject: [PATCH] Maint: rename health OpenVPN names to VPN - `HEALTH_OPENVPN_DURATION_INITIAL` renamed to `HEALTH_VPN_DURATION_INITIAL` with retro-compatiblity - `HEALTH_OPENVPN_DURATION_ADDITION` renamed to `HEALTH_VPN_DURATION_ADDITION` with retro-compatiblity --- internal/configuration/health.go | 16 +++--- internal/configuration/health_test.go | 74 ++++++++++++------------- internal/configuration/settings_test.go | 2 +- internal/healthcheck/health.go | 4 +- internal/healthcheck/openvpn.go | 6 +- internal/healthcheck/server.go | 2 +- 6 files changed, 53 insertions(+), 51 deletions(-) diff --git a/internal/configuration/health.go b/internal/configuration/health.go index 4d0747c3..b6c22bf9 100644 --- a/internal/configuration/health.go +++ b/internal/configuration/health.go @@ -12,7 +12,7 @@ import ( // Health contains settings for the healthcheck and health server. type Health struct { ServerAddress string - OpenVPN HealthyWait + VPN HealthyWait } func (settings *Health) String() string { @@ -24,8 +24,8 @@ func (settings *Health) lines() (lines []string) { lines = append(lines, indent+lastIndent+"Server address: "+settings.ServerAddress) - lines = append(lines, indent+lastIndent+"OpenVPN:") - for _, line := range settings.OpenVPN.lines() { + lines = append(lines, indent+lastIndent+"VPN:") + for _, line := range settings.VPN.lines() { lines = append(lines, indent+indent+line) } @@ -49,14 +49,16 @@ func (settings *Health) read(r reader) (err error) { return fmt.Errorf("environment variable HEALTH_SERVER_ADDRESS: %w", err) } - settings.OpenVPN.Initial, err = r.env.Duration("HEALTH_OPENVPN_DURATION_INITIAL", params.Default("6s")) + 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 { - return fmt.Errorf("environment variable HEALTH_OPENVPN_DURATION_INITIAL: %w", err) + return fmt.Errorf("environment variable HEALTH_VPN_DURATION_INITIAL: %w", err) } - settings.OpenVPN.Addition, err = r.env.Duration("HEALTH_OPENVPN_DURATION_ADDITION", params.Default("5s")) + retroKeyOption = params.RetroKeys([]string{"HEALTH_OPENVPN_DURATION_ADDITION"}, r.onRetroActive) + settings.VPN.Addition, err = r.env.Duration("HEALTH_VPN_DURATION_ADDITION", params.Default("5s"), retroKeyOption) if err != nil { - return fmt.Errorf("environment variable HEALTH_OPENVPN_DURATION_ADDITION: %w", err) + return fmt.Errorf("environment variable HEALTH_VPN_DURATION_ADDITION: %w", err) } return nil diff --git a/internal/configuration/health_test.go b/internal/configuration/health_test.go index d988ddbe..6680ab7f 100644 --- a/internal/configuration/health_test.go +++ b/internal/configuration/health_test.go @@ -16,7 +16,7 @@ func Test_Health_String(t *testing.T) { t.Parallel() var health Health - const expected = "|--Health:\n |--Server address: \n |--OpenVPN:\n |--Initial duration: 0s" + const expected = "|--Health:\n |--Server address: \n |--VPN:\n |--Initial duration: 0s" s := health.String() @@ -34,14 +34,14 @@ func Test_Health_lines(t *testing.T) { lines: []string{ "|--Health:", " |--Server address: ", - " |--OpenVPN:", + " |--VPN:", " |--Initial duration: 0s", }, }, "filled settings": { settings: Health{ ServerAddress: "address:9999", - OpenVPN: HealthyWait{ + VPN: HealthyWait{ Initial: time.Second, Addition: time.Minute, }, @@ -49,7 +49,7 @@ func Test_Health_lines(t *testing.T) { lines: []string{ "|--Health:", " |--Server address: address:9999", - " |--OpenVPN:", + " |--VPN:", " |--Initial duration: 1s", " |--Addition duration: 1m0s", }, @@ -74,61 +74,61 @@ func Test_Health_read(t *testing.T) { errDummy := errors.New("dummy") testCases := map[string]struct { - openvpnInitialDuration time.Duration - openvpnInitialErr error - openvpnAdditionDuration time.Duration - openvpnAdditionErr error - serverAddress string - serverAddressWarning string - serverAddressErr error - expected Health - err error + vpnInitialDuration time.Duration + vpnInitialErr error + vpnAdditionDuration time.Duration + vpnAdditionErr error + serverAddress string + serverAddressWarning string + serverAddressErr error + expected Health + err error }{ "success": { - openvpnInitialDuration: time.Second, - openvpnAdditionDuration: time.Minute, - serverAddress: "127.0.0.1:9999", + vpnInitialDuration: time.Second, + vpnAdditionDuration: time.Minute, + serverAddress: "127.0.0.1:9999", expected: Health{ ServerAddress: "127.0.0.1:9999", - OpenVPN: HealthyWait{ + VPN: HealthyWait{ Initial: time.Second, Addition: time.Minute, }, }, }, "listening address error": { - openvpnInitialDuration: time.Second, - openvpnAdditionDuration: time.Minute, - serverAddress: "127.0.0.1:9999", - serverAddressWarning: "warning", - serverAddressErr: errDummy, + vpnInitialDuration: time.Second, + vpnAdditionDuration: time.Minute, + serverAddress: "127.0.0.1:9999", + serverAddressWarning: "warning", + serverAddressErr: errDummy, expected: Health{ ServerAddress: "127.0.0.1:9999", }, err: errors.New("environment variable HEALTH_SERVER_ADDRESS: dummy"), }, "initial error": { - openvpnInitialDuration: time.Second, - openvpnInitialErr: errDummy, - openvpnAdditionDuration: time.Minute, + vpnInitialDuration: time.Second, + vpnInitialErr: errDummy, + vpnAdditionDuration: time.Minute, expected: Health{ - OpenVPN: HealthyWait{ + VPN: HealthyWait{ Initial: time.Second, }, }, - err: errors.New("environment variable HEALTH_OPENVPN_DURATION_INITIAL: dummy"), + err: errors.New("environment variable HEALTH_VPN_DURATION_INITIAL: dummy"), }, "addition error": { - openvpnInitialDuration: time.Second, - openvpnAdditionDuration: time.Minute, - openvpnAdditionErr: errDummy, + vpnInitialDuration: time.Second, + vpnAdditionDuration: time.Minute, + vpnAdditionErr: errDummy, expected: Health{ - OpenVPN: HealthyWait{ + VPN: HealthyWait{ Initial: time.Second, Addition: time.Minute, }, }, - err: errors.New("environment variable HEALTH_OPENVPN_DURATION_ADDITION: dummy"), + err: errors.New("environment variable HEALTH_VPN_DURATION_ADDITION: dummy"), }, } @@ -151,12 +151,12 @@ func Test_Health_read(t *testing.T) { if testCase.serverAddressErr == nil { env.EXPECT(). - Duration("HEALTH_OPENVPN_DURATION_INITIAL", gomock.Any()). - Return(testCase.openvpnInitialDuration, testCase.openvpnInitialErr) - if testCase.openvpnInitialErr == nil { + Duration("HEALTH_VPN_DURATION_INITIAL", gomock.Any()). + Return(testCase.vpnInitialDuration, testCase.vpnInitialErr) + if testCase.vpnInitialErr == nil { env.EXPECT(). - Duration("HEALTH_OPENVPN_DURATION_ADDITION", gomock.Any()). - Return(testCase.openvpnAdditionDuration, testCase.openvpnAdditionErr) + Duration("HEALTH_VPN_DURATION_ADDITION", gomock.Any()). + Return(testCase.vpnAdditionDuration, testCase.vpnAdditionErr) } } diff --git a/internal/configuration/settings_test.go b/internal/configuration/settings_test.go index 4f43d6e7..358736a8 100644 --- a/internal/configuration/settings_test.go +++ b/internal/configuration/settings_test.go @@ -51,7 +51,7 @@ func Test_Settings_lines(t *testing.T) { " |--Timezone: NOT SET ⚠️ - it can cause time related issues", "|--Health:", " |--Server address: ", - " |--OpenVPN:", + " |--VPN:", " |--Initial duration: 0s", "|--HTTP control server:", " |--Listening port: 0", diff --git a/internal/healthcheck/health.go b/internal/healthcheck/health.go index c34e3ea1..f7ddd5fc 100644 --- a/internal/healthcheck/health.go +++ b/internal/healthcheck/health.go @@ -23,7 +23,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { if previousErr != nil && err == nil { s.logger.Info("healthy!") s.vpn.healthyTimer.Stop() - s.vpn.healthyWait = s.config.OpenVPN.Initial + s.vpn.healthyWait = s.config.VPN.Initial } else if previousErr == nil && err != nil { s.logger.Info("unhealthy: " + err.Error()) s.vpn.healthyTimer.Stop() @@ -40,7 +40,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { return case <-timer.C: case <-s.vpn.healthyTimer.C: - s.onUnhealthyOpenvpn(ctx) + s.onUnhealthyVPN(ctx) } continue } diff --git a/internal/healthcheck/openvpn.go b/internal/healthcheck/openvpn.go index a86f11c8..8acf95e9 100644 --- a/internal/healthcheck/openvpn.go +++ b/internal/healthcheck/openvpn.go @@ -14,11 +14,11 @@ type vpnHealth struct { healthyTimer *time.Timer } -func (s *Server) onUnhealthyOpenvpn(ctx context.Context) { +func (s *Server) onUnhealthyVPN(ctx context.Context) { s.logger.Info("program has been unhealthy for " + - s.vpn.healthyWait.String() + ": restarting OpenVPN") + s.vpn.healthyWait.String() + ": restarting VPN") _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Stopped) _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Running) - s.vpn.healthyWait += s.config.OpenVPN.Addition + s.vpn.healthyWait += s.config.VPN.Addition s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait) } diff --git a/internal/healthcheck/server.go b/internal/healthcheck/server.go index 22f5dda0..0a37bddf 100644 --- a/internal/healthcheck/server.go +++ b/internal/healthcheck/server.go @@ -32,7 +32,7 @@ func NewServer(config configuration.Health, config: config, vpn: vpnHealth{ looper: vpnLooper, - healthyWait: config.OpenVPN.Initial, + healthyWait: config.VPN.Initial, }, } }