1 package clientv3
2
3 import (
4 "go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
5 "go.etcd.io/etcd/client/v3/credentials"
6 grpccredentials "google.golang.org/grpc/credentials"
7 "testing"
8 )
9
10 type dummyAuthTokenBundle struct{}
11
12 func (d dummyAuthTokenBundle) TransportCredentials() grpccredentials.TransportCredentials {
13 return nil
14 }
15
16 func (d dummyAuthTokenBundle) PerRPCCredentials() grpccredentials.PerRPCCredentials {
17 return nil
18 }
19
20 func (d dummyAuthTokenBundle) NewWithMode(mode string) (grpccredentials.Bundle, error) {
21 return nil, nil
22 }
23
24 func (d dummyAuthTokenBundle) UpdateAuthToken(token string) {
25 }
26
27 func TestClientShouldRefreshToken(t *testing.T) {
28 type fields struct {
29 authTokenBundle credentials.Bundle
30 }
31 type args struct {
32 err error
33 callOpts *options
34 }
35
36 optsWithTrue := &options{
37 retryAuth: true,
38 }
39 optsWithFalse := &options{
40 retryAuth: false,
41 }
42
43 tests := []struct {
44 name string
45 fields fields
46 args args
47 want bool
48 }{
49 {
50 name: "ErrUserEmpty and non nil authTokenBundle",
51 fields: fields{
52 authTokenBundle: &dummyAuthTokenBundle{},
53 },
54 args: args{rpctypes.ErrGRPCUserEmpty, optsWithTrue},
55 want: true,
56 },
57 {
58 name: "ErrUserEmpty and nil authTokenBundle",
59 fields: fields{
60 authTokenBundle: nil,
61 },
62 args: args{rpctypes.ErrGRPCUserEmpty, optsWithTrue},
63 want: false,
64 },
65 {
66 name: "ErrGRPCInvalidAuthToken and retryAuth",
67 fields: fields{
68 authTokenBundle: nil,
69 },
70 args: args{rpctypes.ErrGRPCInvalidAuthToken, optsWithTrue},
71 want: true,
72 },
73 {
74 name: "ErrGRPCInvalidAuthToken and !retryAuth",
75 fields: fields{
76 authTokenBundle: nil,
77 },
78 args: args{rpctypes.ErrGRPCInvalidAuthToken, optsWithFalse},
79 want: false,
80 },
81 {
82 name: "ErrGRPCAuthOldRevision and retryAuth",
83 fields: fields{
84 authTokenBundle: nil,
85 },
86 args: args{rpctypes.ErrGRPCAuthOldRevision, optsWithTrue},
87 want: true,
88 },
89 {
90 name: "ErrGRPCAuthOldRevision and !retryAuth",
91 fields: fields{
92 authTokenBundle: nil,
93 },
94 args: args{rpctypes.ErrGRPCAuthOldRevision, optsWithFalse},
95 want: false,
96 },
97 {
98 name: "Other error and retryAuth",
99 fields: fields{
100 authTokenBundle: nil,
101 },
102 args: args{rpctypes.ErrGRPCAuthFailed, optsWithTrue},
103 want: false,
104 },
105 {
106 name: "Other error and !retryAuth",
107 fields: fields{
108 authTokenBundle: nil,
109 },
110 args: args{rpctypes.ErrGRPCAuthFailed, optsWithFalse},
111 want: false,
112 },
113 }
114 for _, tt := range tests {
115 t.Run(tt.name, func(t *testing.T) {
116 c := &Client{
117 authTokenBundle: tt.fields.authTokenBundle,
118 }
119 if got := c.shouldRefreshToken(tt.args.err, tt.args.callOpts); got != tt.want {
120 t.Errorf("shouldRefreshToken() = %v, want %v", got, tt.want)
121 }
122 })
123 }
124 }
125
View as plain text