-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsecrets_test.go
67 lines (52 loc) · 1.42 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
package run
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/kelseyhightower/run/internal/gcptest"
)
var accessSecretTests = []struct {
name string
want []byte
err error
}{
{"foo", []byte("Test"), nil},
{"bar", nil, ErrSecretNotFound},
{"denied", nil, ErrSecretPermissionDenied},
{"unauthorized", nil, ErrSecretUnauthorized},
{"unexpected", nil, ErrSecretUnknownError},
}
func TestAccessSecret(t *testing.T) {
ms := httptest.NewServer(http.HandlerFunc(gcptest.MetadataHandler))
defer ms.Close()
metadataEndpoint = ms.URL
ss := httptest.NewServer(http.HandlerFunc(gcptest.SecretsHandler))
defer ss.Close()
secretmanagerEndpoint = ss.URL
for _, tt := range accessSecretTests {
secret, err := AccessSecret(tt.name)
if !errors.Is(err, tt.err) {
t.Errorf("unexpected error, want %q, got %q", tt.err, err)
}
if !bytes.Equal(secret, tt.want) {
t.Errorf("want %v, got %v", tt.want, secret)
}
}
}
func TestAccessSecretVersion(t *testing.T) {
ms := httptest.NewServer(http.HandlerFunc(gcptest.MetadataHandler))
defer ms.Close()
metadataEndpoint = ms.URL
ss := httptest.NewServer(http.HandlerFunc(gcptest.SecretsHandler))
defer ss.Close()
secretmanagerEndpoint = ss.URL
secret, err := AccessSecretVersion("foo", "1")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !bytes.Equal(secret, []byte("Test")) {
t.Errorf("want %v, got %v", "Test", secret)
}
}