-
Notifications
You must be signed in to change notification settings - Fork 4
/
secrets_test.go
104 lines (79 loc) · 2.06 KB
/
secrets_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
package sdk
import (
"fmt"
"os"
"testing"
cmp "github.com/google/go-cmp/cmp"
)
func Test_ReadSecret_FromTemporaryDirectory(t *testing.T) {
tmpDir := t.TempDir()
os.Setenv("secret_mount_path", tmpDir)
value := "test1234"
if err := os.WriteFile(tmpDir+"/api-key", []byte(value), 0644); err != nil {
t.Fatal(err)
}
secret, err := ReadSecret("api-key")
if err != nil {
t.Fatal(err)
}
if secret != value {
t.Fatalf("want %s, but got %s", value, secret)
}
}
func Test_ReadSecrets_FromTemporaryDirectory(t *testing.T) {
tmpDir := t.TempDir()
os.Setenv("secret_mount_path", tmpDir)
value1 := "test1234"
if err := os.WriteFile(tmpDir+"/api-key", []byte(value1), 0644); err != nil {
t.Fatal(err)
}
value2 := "1234test"
if err := os.WriteFile(tmpDir+"/secret-key", []byte(value2), 0644); err != nil {
t.Fatal(err)
}
secrets, err := ReadSecrets()
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"api-key": value1,
"secret-key": value2,
}
if len(secrets.values) != len(want) {
t.Fatalf("want %d secrets, but got %d", len(want), len(secrets.values))
}
if !cmp.Equal(want, secrets.values) {
t.Fatalf("want %v, but got %v", want, secrets)
}
}
func Test_SecretMap_Exists_Present(t *testing.T) {
m := newSecretMap(map[string]string{"key1": "value1"})
if !m.Exists("key1") {
t.Fatalf("key1 should exist")
}
}
func Test_SecretMap_Exists_NotPresent(t *testing.T) {
m := newSecretMap(map[string]string{"key2": "value1"})
if m.Exists("key1") {
t.Fatalf("key1 should not exist")
}
}
func Test_SecretMap_GetValue_Exists(t *testing.T) {
m := newSecretMap(map[string]string{"key1": "value1"})
v, err := m.Get("key1")
if err != nil {
t.Fatalf("key1 should exist")
}
if v != m.values["key1"] {
t.Fatalf("want %s, but got %s", m.values["key1"], v)
}
}
func Test_SecretMap_GetValue_NotFound(t *testing.T) {
m := newSecretMap(map[string]string{})
secret := "key1"
_, err := m.Get(secret)
wantErr := fmt.Errorf("secret %s not found", secret)
if err.Error() != wantErr.Error() {
t.Fatalf("want %q, but got %q", wantErr, err)
}
}