Maint: improve internal/configuration/health_test.go unit test

This commit is contained in:
Quentin McGaw (desktop)
2021-09-11 22:14:37 +00:00
parent 541a4a3271
commit cade2732b0

View File

@@ -73,21 +73,39 @@ func Test_Health_read(t *testing.T) {
errDummy := errors.New("dummy") errDummy := errors.New("dummy")
type stringCallWithWarning struct {
call bool
s string
warning string
err error
}
type durationCall struct {
call bool
duration time.Duration
err error
}
testCases := map[string]struct { testCases := map[string]struct {
vpnInitialDuration time.Duration serverAddress stringCallWithWarning
vpnInitialErr error vpnInitial durationCall
vpnAdditionDuration time.Duration vpnAddition durationCall
vpnAdditionErr error expected Health
serverAddress string err error
serverAddressWarning string
serverAddressErr error
expected Health
err error
}{ }{
"success": { "success": {
vpnInitialDuration: time.Second, serverAddress: stringCallWithWarning{
vpnAdditionDuration: time.Minute, call: true,
serverAddress: "127.0.0.1:9999", s: "127.0.0.1:9999",
},
vpnInitial: durationCall{
call: true,
duration: time.Second,
},
vpnAddition: durationCall{
call: true,
duration: time.Minute,
},
expected: Health{ expected: Health{
ServerAddress: "127.0.0.1:9999", ServerAddress: "127.0.0.1:9999",
VPN: HealthyWait{ VPN: HealthyWait{
@@ -97,20 +115,26 @@ func Test_Health_read(t *testing.T) {
}, },
}, },
"listening address error": { "listening address error": {
vpnInitialDuration: time.Second, serverAddress: stringCallWithWarning{
vpnAdditionDuration: time.Minute, call: true,
serverAddress: "127.0.0.1:9999", s: "127.0.0.1:9999",
serverAddressWarning: "warning", warning: "warning",
serverAddressErr: errDummy, err: errDummy,
},
expected: Health{ expected: Health{
ServerAddress: "127.0.0.1:9999", ServerAddress: "127.0.0.1:9999",
}, },
err: errors.New("environment variable HEALTH_SERVER_ADDRESS: dummy"), err: errors.New("environment variable HEALTH_SERVER_ADDRESS: dummy"),
}, },
"initial error": { "initial error": {
vpnInitialDuration: time.Second, serverAddress: stringCallWithWarning{
vpnInitialErr: errDummy, call: true,
vpnAdditionDuration: time.Minute, },
vpnInitial: durationCall{
call: true,
duration: time.Second,
err: errDummy,
},
expected: Health{ expected: Health{
VPN: HealthyWait{ VPN: HealthyWait{
Initial: time.Second, Initial: time.Second,
@@ -119,9 +143,18 @@ func Test_Health_read(t *testing.T) {
err: errors.New("environment variable HEALTH_VPN_DURATION_INITIAL: dummy"), err: errors.New("environment variable HEALTH_VPN_DURATION_INITIAL: dummy"),
}, },
"addition error": { "addition error": {
vpnInitialDuration: time.Second, serverAddress: stringCallWithWarning{
vpnAdditionDuration: time.Minute, call: true,
vpnAdditionErr: errDummy, },
vpnInitial: durationCall{
call: true,
duration: time.Second,
},
vpnAddition: durationCall{
call: true,
duration: time.Minute,
err: errDummy,
},
expected: Health{ expected: Health{
VPN: HealthyWait{ VPN: HealthyWait{
Initial: time.Second, Initial: time.Second,
@@ -142,22 +175,31 @@ func Test_Health_read(t *testing.T) {
env := mock_params.NewMockInterface(ctrl) env := mock_params.NewMockInterface(ctrl)
logger := mock_logging.NewMockLogger(ctrl) logger := mock_logging.NewMockLogger(ctrl)
env.EXPECT().ListeningAddress("HEALTH_SERVER_ADDRESS", gomock.Any()). if testCase.serverAddress.call {
Return(testCase.serverAddress, testCase.serverAddressWarning, value := testCase.serverAddress.s
testCase.serverAddressErr) warning := testCase.serverAddress.warning
if testCase.serverAddressWarning != "" { err := testCase.serverAddress.err
logger.EXPECT().Warn("environment variable HEALTH_SERVER_ADDRESS: " + testCase.serverAddressWarning) env.EXPECT().ListeningAddress("HEALTH_SERVER_ADDRESS", gomock.Any()).
Return(value, warning, err)
if warning != "" {
logger.EXPECT().Warn("environment variable HEALTH_SERVER_ADDRESS: " + warning)
}
} }
if testCase.serverAddressErr == nil { if testCase.vpnInitial.call {
value := testCase.vpnInitial.duration
err := testCase.vpnInitial.err
env.EXPECT(). env.EXPECT().
Duration("HEALTH_VPN_DURATION_INITIAL", gomock.Any()). Duration("HEALTH_VPN_DURATION_INITIAL", gomock.Any()).
Return(testCase.vpnInitialDuration, testCase.vpnInitialErr) Return(value, err)
if testCase.vpnInitialErr == nil { }
env.EXPECT().
Duration("HEALTH_VPN_DURATION_ADDITION", gomock.Any()). if testCase.vpnAddition.call {
Return(testCase.vpnAdditionDuration, testCase.vpnAdditionErr) value := testCase.vpnAddition.duration
} err := testCase.vpnAddition.err
env.EXPECT().
Duration("HEALTH_VPN_DURATION_ADDITION", gomock.Any()).
Return(value, err)
} }
r := reader{ r := reader{