talos

Форк
0
/
status_test.go 
77 строк · 2.0 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package client_test
6

7
import (
8
	"errors"
9
	"fmt"
10
	"testing"
11

12
	"github.com/hashicorp/go-multierror"
13
	"github.com/stretchr/testify/assert"
14
	"google.golang.org/grpc/codes"
15
	"google.golang.org/grpc/status"
16

17
	"github.com/siderolabs/talos/pkg/machinery/client"
18
)
19

20
func TestStatus(t *testing.T) {
21
	for _, tt := range []struct {
22
		name      string
23
		err       error
24
		nilStatus bool
25
		message   string
26
		code      codes.Code
27
	}{
28
		{
29
			name:      "nil",
30
			err:       nil,
31
			nilStatus: true,
32
			code:      codes.OK,
33
		},
34
		{
35
			name:      "not status",
36
			err:       errors.New("some error"),
37
			nilStatus: true,
38
			code:      codes.Unknown,
39
		},
40
		{
41
			name:    "status",
42
			err:     status.Error(codes.AlreadyExists, "file already exists"),
43
			message: "file already exists",
44
			code:    codes.AlreadyExists,
45
		},
46
		{
47
			name:    "status wrapped",
48
			err:     multierror.Append(nil, status.Error(codes.AlreadyExists, "file already exists")).ErrorOrNil(),
49
			message: "file already exists",
50
			code:    codes.AlreadyExists,
51
		},
52
		{
53
			name:    "multiple wrapped",
54
			err:     multierror.Append(nil, status.Error(codes.FailedPrecondition, "can't be zero"), status.Error(codes.AlreadyExists, "file already exists")).ErrorOrNil(),
55
			message: "can't be zero",
56
			code:    codes.FailedPrecondition,
57
		},
58
		{
59
			name:    "double wrapped",
60
			err:     multierror.Append(nil, fmt.Errorf("127.0.0.1: %w", status.Error(codes.AlreadyExists, "file already exists"))).ErrorOrNil(),
61
			message: "file already exists",
62
			code:    codes.AlreadyExists,
63
		},
64
	} {
65
		t.Run(tt.name, func(t *testing.T) {
66
			st := client.Status(tt.err)
67
			if tt.nilStatus {
68
				assert.Nil(t, st)
69
			} else {
70
				assert.Equal(t, st.Message(), tt.message)
71
				assert.Equal(t, st.Code(), tt.code)
72
			}
73

74
			assert.Equal(t, client.StatusCode(tt.err), tt.code)
75
		})
76
	}
77
}
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.