-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddy_test.go
132 lines (102 loc) · 3.65 KB
/
caddy_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package caddy_gcp_secret_manager_test
import (
"context"
"errors"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
caddy_gcp_secret_manager "github.com/trea/caddy-gcp-secret-manager"
"testing"
"testing/fstest"
)
func TestUnmarshalBadCaddyfile(t *testing.T) {
cf := `gcp-secret-manager`
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{}
d := caddyfile.NewTestDispenser(cf)
err := s.UnmarshalCaddyfile(d)
if err == nil {
t.Fatalf("Unmarshaling Caddyfile should have failed since there's no project ID")
}
}
func TestUnmarshalBasicCaddyfile(t *testing.T) {
cf := `gcp-secret-manager my-project-id`
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{}
d := caddyfile.NewTestDispenser(cf)
err := s.UnmarshalCaddyfile(d)
if err != nil {
t.Fatalf("Unmarshal should have worked, got err: %+v", err)
}
if s.ProjectID != "my-project-id" {
t.Fatalf("Expected ProjectID to be '%s', is '%s'", "my-project-id", s.ProjectID)
}
}
func TestUnmarshalCaddyfileWithCredentialsFile(t *testing.T) {
cf := `
gcp-secret-manager test-project-id {
credentials_file /app/gcp-credentials.json
}`
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{}
d := caddyfile.NewTestDispenser(cf)
err := s.UnmarshalCaddyfile(d)
if err != nil {
t.Fatalf("Unmarshal should have worked, got err: %+v", err)
}
if s.ProjectID != "test-project-id" {
t.Errorf("Expected ProjectID to be '%s', got '%s' instead", "test-project-id", s.ProjectID)
}
if s.CredentialsFile != "/app/gcp-credentials.json" {
t.Errorf("Expected CredentialsFile to be '%s', got '%s' instead", "/app/gcp-credentials.json", s.CredentialsFile)
}
}
func TestUnmarshalCaddyfileWithProjectAndCredentialsNested(t *testing.T) {
cf := `
gcp-secret-manager {
project_id test-project-id
credentials_file /app/gcp-credentials.json
}`
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{}
d := caddyfile.NewTestDispenser(cf)
err := s.UnmarshalCaddyfile(d)
if err != nil {
t.Fatalf("Unmarshal should have worked, got err: %+v", err)
}
if s.ProjectID != "test-project-id" {
t.Errorf("Expected ProjectID to be '%s', got '%s' instead", "test-project-id", s.ProjectID)
}
if s.CredentialsFile != "/app/gcp-credentials.json" {
t.Errorf("Expected CredentialsFile to be '%s', got '%s' instead", "/app/gcp-credentials.json", s.CredentialsFile)
}
}
func TestProvision(t *testing.T) {
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
if err := s.Provision(ctx); err != nil {
t.Fatalf("Expected provision to pass, got err: %+v", err)
}
}
func TestValidationFailsWhenGivenCredentialsFileThatDoesntExist(t *testing.T) {
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{
CredentialsFile: "some-garbage-credentials-file.json",
}
if err := s.Validate(); err == nil {
t.Fatalf("Expected Validate to fail because the file didn't exist")
} else if errors.Is(err, caddy_gcp_secret_manager.ErrCredentialsFile) == false {
t.Errorf("Expected error to be ErrCredentialsFile")
}
}
func TestValidationFailsWhenGivenCredentialsFileThatExistsButContainsInvalidJson(t *testing.T) {
fs := fstest.MapFS{
"credentials.json": {
Data: []byte("{this-isnt-json}"),
},
}
s := caddy_gcp_secret_manager.CaddyGcpSecretManagerStorage{
CredentialsFile: "credentials.json",
Fs: fs,
}
if err := s.Validate(); err == nil {
t.Fatalf("Expected Validate to fail because the file contains invalid json")
} else if errors.Is(err, caddy_gcp_secret_manager.ErrInvalidJSON) == false {
t.Errorf("Expected error returned to be ErrInvalidJSON")
}
}