2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-29 08:28:37 -04:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-07-18 08:00:57 -04:00
|
|
|
"fmt"
|
2022-06-29 08:28:37 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceIsUnavailable(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
err error
|
|
|
|
wantUnavailable bool
|
|
|
|
}{
|
|
|
|
"nil": {},
|
|
|
|
"not status error": {
|
|
|
|
err: errors.New("error"),
|
|
|
|
},
|
|
|
|
"not unavailable": {
|
|
|
|
err: status.Error(codes.Internal, "error"),
|
|
|
|
},
|
|
|
|
"unavailable error with authentication handshake failure": {
|
2022-09-26 03:57:40 -04:00
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: bad certificate"`),
|
2022-06-29 08:28:37 -04:00
|
|
|
},
|
|
|
|
"normal unavailable error": {
|
|
|
|
err: status.Error(codes.Unavailable, "error"),
|
|
|
|
wantUnavailable: true,
|
|
|
|
},
|
2022-09-26 03:57:40 -04:00
|
|
|
"handshake EOF error": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: EOF"`),
|
|
|
|
wantUnavailable: true,
|
|
|
|
},
|
|
|
|
"handshake read tcp error": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: read tcp error"`),
|
|
|
|
wantUnavailable: true,
|
|
|
|
},
|
2024-03-08 07:15:06 -05:00
|
|
|
"handshake deadline exceeded error": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: context deadline exceeded"`),
|
|
|
|
wantUnavailable: true,
|
|
|
|
},
|
2022-07-18 08:00:57 -04:00
|
|
|
"wrapped error": {
|
|
|
|
err: fmt.Errorf("some wrapping: %w", status.Error(codes.Unavailable, "error")),
|
|
|
|
wantUnavailable: true,
|
|
|
|
},
|
|
|
|
"code unknown": {
|
|
|
|
err: status.Error(codes.Unknown, "unknown"),
|
|
|
|
},
|
2022-06-29 08:28:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-07-21 09:20:12 -04:00
|
|
|
assert.Equal(tc.wantUnavailable, ServiceIsUnavailable(tc.err))
|
2022-06-29 08:28:37 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 03:57:40 -04:00
|
|
|
|
|
|
|
func TestLoadbalancerIsNotReady(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
err error
|
|
|
|
wantNotReady bool
|
|
|
|
}{
|
|
|
|
"nil": {},
|
|
|
|
"not status error": {
|
|
|
|
err: errors.New("error"),
|
|
|
|
},
|
|
|
|
"not unavailable": {
|
|
|
|
err: status.Error(codes.Internal, "error"),
|
|
|
|
},
|
|
|
|
"unavailable error with authentication handshake failure": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: bad certificate"`),
|
|
|
|
},
|
|
|
|
"handshake EOF error": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: EOF"`),
|
|
|
|
},
|
|
|
|
"handshake read tcp error": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: read tcp error"`),
|
|
|
|
wantNotReady: true,
|
|
|
|
},
|
2024-03-08 07:15:06 -05:00
|
|
|
"handshake deadline exceeded error": {
|
|
|
|
err: status.Error(codes.Unavailable, `connection error: desc = "transport: authentication handshake failed: context deadline exceeded"`),
|
|
|
|
wantNotReady: true,
|
|
|
|
},
|
2022-09-26 03:57:40 -04:00
|
|
|
"normal unavailable error": {
|
|
|
|
err: status.Error(codes.Unavailable, "error"),
|
|
|
|
},
|
|
|
|
"wrapped error": {
|
|
|
|
err: fmt.Errorf("some wrapping: %w", status.Error(codes.Unavailable, "error")),
|
|
|
|
},
|
|
|
|
"code unknown": {
|
|
|
|
err: status.Error(codes.Unknown, "unknown"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.Equal(tc.wantNotReady, LoadbalancerIsNotReady(tc.err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|