-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecretManager_test.go
108 lines (81 loc) · 2.44 KB
/
SecretManager_test.go
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
package secman
import (
"bytes"
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const VALID_PROJECT_ID = "<project-id>"
func TestNewSetGetDeleteClose(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s, err := New(ctx, VALID_PROJECT_ID, "europe-west1", "europe-west6")
assert.NoError(t, err)
assert.NotNil(t, s)
defer s.Close()
secretName := fmt.Sprintf("test-secret-%d", time.Now().UnixNano())
secret := &bytes.Buffer{}
secret.WriteString(fmt.Sprintf("secret-%d", time.Now().UnixNano()))
err = s.Set(secretName, secret.Bytes())
assert.NoError(t, err)
got, err := s.Get(secretName)
assert.NoError(t, err)
assert.EqualValues(t, secret.Bytes(), got)
err = s.Delete(secretName)
assert.NoError(t, err)
}
func TestNewInvalidContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
s, err := New(ctx, "")
assert.Error(t, err)
assert.Nil(t, s)
}
func TestGetInvalid(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s, err := New(ctx, "non-existing-test-project-12342")
assert.NoError(t, err)
assert.NotNil(t, s)
defer s.Close()
sec, err := s.Get("non-existing-secret")
assert.Error(t, err)
assert.Nil(t, sec)
}
func TestSetTwice(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
secretName := fmt.Sprintf("test-secret-%d", time.Now().UnixNano())
s, err := New(ctx, VALID_PROJECT_ID, "europe-west1", "europe-west6")
assert.NoError(t, err)
assert.NotNil(t, s)
defer s.Close()
secret := &bytes.Buffer{}
secret.WriteString(fmt.Sprintf("secret-%d", 1))
err = s.Set(secretName, secret.Bytes())
assert.NoError(t, err)
got1, err := s.Get(secretName)
assert.NoError(t, err)
assert.EqualValues(t, secret.Bytes(), got1)
secret.Reset()
secret.WriteString(fmt.Sprintf("secret-%d", 2))
err = s.Set(secretName, secret.Bytes())
assert.NoError(t, err)
got2, err := s.Get(secretName)
assert.NoError(t, err)
assert.EqualValues(t, secret.Bytes(), got2)
err = s.Delete(secretName)
assert.NoError(t, err)
}
func TestCreateInvalid(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s, err := New(ctx, "non-existing-test-project-12342")
assert.NoError(t, err)
assert.NotNil(t, s)
defer s.Close()
err = s.Set("non-existing-secret", []byte("secret"))
assert.Error(t, err)
}