@@ -19,9 +19,11 @@ package encryptionconfig
19
19
import (
20
20
"bytes"
21
21
"encoding/base64"
22
+ "errors"
22
23
"io"
23
24
"io/ioutil"
24
25
"os"
26
+ "sync"
25
27
"testing"
26
28
"time"
27
29
@@ -61,19 +63,31 @@ func mustConfigReader(t *testing.T, path string) io.Reader {
61
63
// testEnvelopeService is a mock envelope service which can be used to simulate remote Envelope services
62
64
// for testing of the envelope transformer with other transformers.
63
65
type testEnvelopeService struct {
66
+ err error
64
67
}
65
68
66
69
func (t * testEnvelopeService ) Decrypt (data []byte ) ([]byte , error ) {
70
+ if t .err != nil {
71
+ return nil , t .err
72
+ }
67
73
return base64 .StdEncoding .DecodeString (string (data ))
68
74
}
69
75
70
76
func (t * testEnvelopeService ) Encrypt (data []byte ) ([]byte , error ) {
77
+ if t .err != nil {
78
+ return nil , t .err
79
+ }
71
80
return []byte (base64 .StdEncoding .EncodeToString (data )), nil
72
81
}
73
82
74
83
// The factory method to create mock envelope service.
75
84
func newMockEnvelopeService (endpoint string , timeout time.Duration ) (envelope.Service , error ) {
76
- return & testEnvelopeService {}, nil
85
+ return & testEnvelopeService {nil }, nil
86
+ }
87
+
88
+ // The factory method to create mock envelope service which always returns error.
89
+ func newMockErrorEnvelopeService (endpoint string , timeout time.Duration ) (envelope.Service , error ) {
90
+ return & testEnvelopeService {errors .New ("test" )}, nil
77
91
}
78
92
79
93
func TestLegacyConfig (t * testing.T ) {
@@ -261,6 +275,49 @@ func TestKMSPluginHealthz(t *testing.T) {
261
275
}
262
276
}
263
277
278
+ func TestKMSPluginHealthzTTL (t * testing.T ) {
279
+ service , _ := newMockEnvelopeService ("unix:///tmp/testprovider.sock" , 3 * time .Second )
280
+ errService , _ := newMockErrorEnvelopeService ("unix:///tmp/testprovider.sock" , 3 * time .Second )
281
+
282
+ testCases := []struct {
283
+ desc string
284
+ probe * kmsPluginProbe
285
+ wantTTL time.Duration
286
+ }{
287
+ {
288
+ desc : "kms provider in good state" ,
289
+ probe : & kmsPluginProbe {
290
+ name : "test" ,
291
+ ttl : kmsPluginHealthzNegativeTTL ,
292
+ Service : service ,
293
+ l : & sync.Mutex {},
294
+ lastResponse : & kmsPluginHealthzResponse {},
295
+ },
296
+ wantTTL : kmsPluginHealthzPositiveTTL ,
297
+ },
298
+ {
299
+ desc : "kms provider in bad state" ,
300
+ probe : & kmsPluginProbe {
301
+ name : "test" ,
302
+ ttl : kmsPluginHealthzPositiveTTL ,
303
+ Service : errService ,
304
+ l : & sync.Mutex {},
305
+ lastResponse : & kmsPluginHealthzResponse {},
306
+ },
307
+ wantTTL : kmsPluginHealthzNegativeTTL ,
308
+ },
309
+ }
310
+
311
+ for _ , tt := range testCases {
312
+ t .Run (tt .desc , func (t * testing.T ) {
313
+ tt .probe .Check ()
314
+ if tt .probe .ttl != tt .wantTTL {
315
+ t .Fatalf ("want ttl %v, got ttl %v" , tt .wantTTL , tt .probe .ttl )
316
+ }
317
+ })
318
+ }
319
+ }
320
+
264
321
// As long as got and want contain envelope.Service we will return true.
265
322
// If got has an envelope.Service and want does note (or vice versa) this will return false.
266
323
func serviceComparer (_ , _ envelope.Service ) bool {
0 commit comments