1
18
19 package pemfile
20
21 import (
22 "encoding/json"
23 "testing"
24 )
25
26 func TestParseConfig(t *testing.T) {
27 tests := []struct {
28 desc string
29 input any
30 wantOutput string
31 wantErr bool
32 }{
33 {
34 desc: "non JSON input",
35 input: new(int),
36 wantErr: true,
37 },
38 {
39 desc: "invalid JSON",
40 input: json.RawMessage(`bad bad json`),
41 wantErr: true,
42 },
43 {
44 desc: "JSON input does not match expected",
45 input: json.RawMessage(`["foo": "bar"]`),
46 wantErr: true,
47 },
48 {
49 desc: "no credential files",
50 input: json.RawMessage(`{}`),
51 wantErr: true,
52 },
53 {
54 desc: "only cert file",
55 input: json.RawMessage(`
56 {
57 "certificate_file": "/a/b/cert.pem"
58 }`),
59 wantErr: true,
60 },
61 {
62 desc: "only key file",
63 input: json.RawMessage(`
64 {
65 "private_key_file": "/a/b/key.pem"
66 }`),
67 wantErr: true,
68 },
69 {
70 desc: "cert and key in different directories",
71 input: json.RawMessage(`
72 {
73 "certificate_file": "/b/a/cert.pem",
74 "private_key_file": "/a/b/key.pem"
75 }`),
76 wantErr: true,
77 },
78 {
79 desc: "bad refresh duration",
80 input: json.RawMessage(`
81 {
82 "certificate_file": "/a/b/cert.pem",
83 "private_key_file": "/a/b/key.pem",
84 "ca_certificate_file": "/a/b/ca.pem",
85 "refresh_interval": "duration"
86 }`),
87 wantErr: true,
88 },
89 {
90 desc: "good config with default refresh interval",
91 input: json.RawMessage(`
92 {
93 "certificate_file": "/a/b/cert.pem",
94 "private_key_file": "/a/b/key.pem",
95 "ca_certificate_file": "/a/b/ca.pem"
96 }`),
97 wantOutput: "file_watcher:/a/b/cert.pem:/a/b/key.pem:/a/b/ca.pem:10m0s",
98 },
99 {
100 desc: "good config",
101 input: json.RawMessage(`
102 {
103 "certificate_file": "/a/b/cert.pem",
104 "private_key_file": "/a/b/key.pem",
105 "ca_certificate_file": "/a/b/ca.pem",
106 "refresh_interval": "200s"
107 }`),
108 wantOutput: "file_watcher:/a/b/cert.pem:/a/b/key.pem:/a/b/ca.pem:3m20s",
109 },
110 }
111
112 for _, test := range tests {
113 t.Run(test.desc, func(t *testing.T) {
114 builder := &pluginBuilder{}
115
116 bc, err := builder.ParseConfig(test.input)
117 if (err != nil) != test.wantErr {
118 t.Fatalf("ParseConfig(%+v) failed: %v", test.input, err)
119 }
120 if test.wantErr {
121 return
122 }
123
124 gotConfig := bc.String()
125 if gotConfig != test.wantOutput {
126 t.Fatalf("ParseConfig(%v) = %s, want %s", test.input, gotConfig, test.wantOutput)
127 }
128 })
129 }
130 }
131
View as plain text