talos/pkg/machinery/client/status_test.go
Andrey Smirnov 10c28758a4 fix: ignore DeadlineExceeded error correctly on bootstrap
The problem was that gRPC method `status.Code(err)` doesn't unwrap
errors, while Talos client returns errors wrapped with
`multierror.Error` and `fmt.Errrorf`, so `status.Code` doesn't return
error code correctly.

Fix that by introducing our own client method which correctly goes over
the chain of wrapped errors.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2021-07-07 12:02:26 -07:00

64 lines
1.5 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package client_test
import (
"errors"
"fmt"
"testing"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/talos-systems/talos/pkg/machinery/client"
)
func TestStatusCode(t *testing.T) {
for _, tt := range []struct {
name string
err error
code codes.Code
}{
{
name: "nil",
err: nil,
code: codes.OK,
},
{
name: "not status",
err: errors.New("some error"),
code: codes.Unknown,
},
{
name: "status",
err: status.Error(codes.AlreadyExists, "file already exists"),
code: codes.AlreadyExists,
},
{
name: "status wrapped",
err: multierror.Append(nil, status.Error(codes.AlreadyExists, "file already exists")).ErrorOrNil(),
code: codes.AlreadyExists,
},
{
name: "multiple wrapped",
err: multierror.Append(nil, status.Error(codes.FailedPrecondition, "can't be zero"), status.Error(codes.AlreadyExists, "file already exists")).ErrorOrNil(),
code: codes.FailedPrecondition,
},
{
name: "double wrapped",
err: multierror.Append(nil, fmt.Errorf("127.0.0.1: %w", status.Error(codes.AlreadyExists, "file already exists"))).ErrorOrNil(),
code: codes.AlreadyExists,
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, client.StatusCode(tt.err), tt.code)
})
}
}