talos/pkg/machinery/client/status_test.go
Andrey Smirnov 96aa9638f7
chore: rename talos-systems/talos to siderolabs/talos
There's a cyclic dependency on siderolink library which imports talos
machinery back. We will fix that after we get talos pushed under a new
name.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2022-11-03 16:50:32 +04: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/siderolabs/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)
})
}
}