-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgrpc_test.go
More file actions
236 lines (217 loc) · 6.07 KB
/
grpc_test.go
File metadata and controls
236 lines (217 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package bootstrap
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/log"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/testutil"
"github.com/stretchr/testify/assert"
)
func TestGetServiceClient(t *testing.T) {
clusterCACertPEM, _, err := testutil.GenerateCertPEM(testutil.CertTemplate{
CommonName: "hcp",
Organization: "aks",
IsCA: true,
Expiration: time.Now().Add(time.Hour),
})
assert.NoError(t, err)
cases := []struct {
name string
setupFunc func(*testing.T) *Config
errorSubstr []string
}{
{
name: "cluster ca data cannot be read",
setupFunc: func(t *testing.T) *Config {
return &Config{
ClusterCAFilePath: "does/not/exist.crt",
NextProto: "nextProto",
APIServerFQDN: "fqdn",
}
},
errorSubstr: []string{"reading cluster CA data from does/not/exist.crt"},
},
{
name: "cluster ca data is invalid",
setupFunc: func(t *testing.T) *Config {
tempDir := t.TempDir()
caFilePath := filepath.Join(tempDir, "ca.crt")
err := os.WriteFile(caFilePath, []byte("SGVsbG8gV29ybGQh"), os.ModePerm)
assert.NoError(t, err)
return &Config{
ClusterCAFilePath: caFilePath,
NextProto: "nextProto",
APIServerFQDN: "fqdn",
}
},
errorSubstr: []string{
"failed to get TLS config",
"unable to construct new cert pool using cluster CA data",
},
},
{
name: "client connection can be created with provided auth token",
setupFunc: func(t *testing.T) *Config {
lis := bufconn.Listen(1024)
defer func() {
assert.NoError(t, lis.Close())
}()
tempDir := t.TempDir()
caFilePath := filepath.Join(tempDir, "ca.crt")
err := os.WriteFile(caFilePath, clusterCACertPEM, os.ModePerm)
assert.NoError(t, err)
return &Config{
ClusterCAFilePath: caFilePath,
NextProto: "nextProto",
APIServerFQDN: lis.Addr().String(),
}
},
errorSubstr: nil,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cfg := c.setupFunc(t)
client, closeFn, err := getServiceClient("token", cfg)
if len(c.errorSubstr) > 0 {
assert.Error(t, err)
for _, substr := range c.errorSubstr {
assert.Contains(t, err.Error(), substr)
}
assert.Nil(t, client)
assert.Nil(t, closeFn)
} else {
assert.NoError(t, err)
assert.NotNil(t, client)
assert.NotNil(t, closeFn)
closeFn := closeFn()
assert.NoError(t, closeFn)
}
})
}
}
func TestGetTLSConfig(t *testing.T) {
clusterCACertPEM, _, err := testutil.GenerateCertPEM(testutil.CertTemplate{
CommonName: "hcp",
Organization: "aks",
IsCA: true,
Expiration: time.Now().Add(time.Hour),
})
assert.NoError(t, err)
rootPool := x509.NewCertPool()
ok := rootPool.AppendCertsFromPEM(clusterCACertPEM)
assert.True(t, ok)
cases := []struct {
name string
nextProto string
expectedNextProtos []string
}{
{
name: "without nextProto",
nextProto: "",
expectedNextProtos: nil,
},
{
name: "with nextProto",
nextProto: "bootstrap",
expectedNextProtos: []string{"bootstrap", "h2"},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
config, err := getTLSConfig(clusterCACertPEM, c.nextProto, tls.VersionTLS13)
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, c.expectedNextProtos, config.NextProtos)
assert.False(t, config.InsecureSkipVerify)
assert.Equal(t, uint16(tls.VersionTLS13), config.MinVersion)
assert.True(t, config.RootCAs.Equal(rootPool))
})
}
}
func TestGetTLSMinVersion(t *testing.T) {
cases := []struct {
name string
cfg *Config
expected uint16
}{
{
name: "TLSv1.3 is specified",
cfg: &Config{TLSMinVersion: "1.3"},
expected: tls.VersionTLS13,
},
{
name: "TLSv1.2 is specified",
cfg: &Config{TLSMinVersion: "1.2"},
expected: tls.VersionTLS12,
},
{
name: "no minimum TLS version is specified",
cfg: &Config{},
expected: tls.VersionTLS13,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
assert.Equal(t, c.expected, getTLSMinVersion(c.cfg))
})
}
}
func TestGetGRPCOnRetryCallbackFunc(t *testing.T) {
t.Cleanup(func() {
lastGRPCRetryError = nil
})
ctx := log.NewTestContext()
errs := []error{errors.New("e0"), errors.New("e1"), errors.New("e2")}
fn := getGRPCOnRetryCallbackFunc()
for idx, err := range errs {
fn(ctx, uint(idx+1), err)
}
assert.Equal(t, errs[len(errs)-1], lastGRPCRetryError)
}
func TestWithLastGRPCRetryErrorIfDeadlineExceeded(t *testing.T) {
cases := []struct {
name string
err error
lastGRPCRetryError error
expectedErr error
}{
{
name: "last GRPC retry error is nil",
err: errors.New("non-retryable error"),
lastGRPCRetryError: nil,
expectedErr: errors.New("non-retryable error"),
},
{
name: "err is not a context.DeadlineExceeded",
err: errors.New("an error"),
lastGRPCRetryError: errors.New("service unavailable"),
expectedErr: errors.New("an error"),
},
{
name: "err is a context.DeadlineExceeded and last GRPC retry error is non-nil",
err: status.Error(codes.DeadlineExceeded, "context deadline exceeded"),
lastGRPCRetryError: errors.New("service unavailable"),
expectedErr: fmt.Errorf("%w: last error: %s", status.Error(codes.DeadlineExceeded, "context deadline exceeded"), errors.New("service unavailable")),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Cleanup(func() {
lastGRPCRetryError = nil
})
lastGRPCRetryError = c.lastGRPCRetryError
assert.Equal(t, c.expectedErr, withLastGRPCRetryErrorIfDeadlineExceeded(c.err))
})
}
}