core/config: add databroker_storage_connection_string_file (#5242)

* core/config: add databroker_storage_connection_string_file

* add file to file list
This commit is contained in:
Caleb Doxsey 2024-08-27 09:42:14 -06:00 committed by GitHub
parent d062f9d68d
commit f3620cf6e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 110 additions and 15 deletions

View file

@ -1300,6 +1300,66 @@ func TestOptions_RuntimeFlags(t *testing.T) {
}
}
func TestOptions_GetDataBrokerStorageConnectionString(t *testing.T) {
t.Parallel()
t.Run("validate", func(t *testing.T) {
t.Parallel()
o := NewDefaultOptions()
o.Services = "databroker"
o.DataBrokerStorageType = "postgres"
o.SharedKey = cryptutil.NewBase64Key()
assert.ErrorContains(t, o.Validate(), "missing databroker storage backend dsn",
"should validate DSN")
o.DataBrokerStorageConnectionString = "DSN"
assert.NoError(t, o.Validate(),
"should have no error when the dsn is set")
o.DataBrokerStorageConnectionString = ""
o.DataBrokerStorageConnectionStringFile = "DSN_FILE"
assert.NoError(t, o.Validate(),
"should have no error when the dsn file is set")
})
t.Run("literal", func(t *testing.T) {
t.Parallel()
o := NewDefaultOptions()
o.DataBrokerStorageConnectionString = "DSN"
dsn, err := o.GetDataBrokerStorageConnectionString()
assert.NoError(t, err)
assert.Equal(t, "DSN", dsn)
})
t.Run("file", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
fp := filepath.Join(dir, "DSN_FILE")
o := NewDefaultOptions()
o.DataBrokerStorageConnectionStringFile = fp
o.DataBrokerStorageConnectionString = "IGNORED"
dsn, err := o.GetDataBrokerStorageConnectionString()
assert.Error(t, err,
"should return an error when the file doesn't exist")
assert.Empty(t, dsn)
os.WriteFile(fp, []byte(`
DSN
`), 0o644)
dsn, err = o.GetDataBrokerStorageConnectionString()
assert.NoError(t, err,
"should not return an error when the file exists")
assert.Equal(t, "DSN", dsn,
"should return the trimmed contents of the file")
})
}
func encodeCert(cert *tls.Certificate) []byte {
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Certificate[0]})
}