mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fix bug not creating filepath for silences/nflog if it does not exist (#39174)
We created this filepath just as we're about persist the templates - with the latest change, we now need to create it sooner.
This commit is contained in:
@@ -93,6 +93,12 @@ func (fs *FileStore) IsExists(fn string) bool {
|
|||||||
|
|
||||||
// WriteFileToDisk writes a file with the provided name and contents to the Alertmanager working directory with the default grafana permission.
|
// WriteFileToDisk writes a file with the provided name and contents to the Alertmanager working directory with the default grafana permission.
|
||||||
func (fs *FileStore) WriteFileToDisk(fn string, content []byte) error {
|
func (fs *FileStore) WriteFileToDisk(fn string, content []byte) error {
|
||||||
|
// Ensure the working directory is created
|
||||||
|
err := os.MkdirAll(fs.workingDirPath, 0750)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create the working directory %q: %s", fs.workingDirPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
return os.WriteFile(fs.pathFor(fn), content, 0644)
|
return os.WriteFile(fs.pathFor(fn), content, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,27 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestFileStore_FilepathFor_DirectoryNotExist(t *testing.T) {
|
||||||
|
store := newFakeKVStore(t)
|
||||||
|
workingDir := filepath.Join(t.TempDir(), "notexistdir")
|
||||||
|
fs := NewFileStore(1, store, workingDir)
|
||||||
|
filekey := "silences"
|
||||||
|
filePath := filepath.Join(workingDir, filekey)
|
||||||
|
|
||||||
|
// With a file already on the database and the path does not exist yet, it creates the path,
|
||||||
|
// writes the file to disk, then returns the filepath.
|
||||||
|
{
|
||||||
|
require.NoError(t, store.Set(context.Background(), 1, KVNamespace, filekey, encode([]byte("silence1,silence3"))))
|
||||||
|
r, err := fs.FilepathFor(context.Background(), filekey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, filePath, r)
|
||||||
|
f, err := ioutil.ReadFile(filepath.Clean(filePath))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "silence1,silence3", string(f))
|
||||||
|
require.NoError(t, os.Remove(filePath))
|
||||||
|
require.NoError(t, store.Del(context.Background(), 1, KVNamespace, filekey))
|
||||||
|
}
|
||||||
|
}
|
||||||
func TestFileStore_FilepathFor(t *testing.T) {
|
func TestFileStore_FilepathFor(t *testing.T) {
|
||||||
store := newFakeKVStore(t)
|
store := newFakeKVStore(t)
|
||||||
workingDir := t.TempDir()
|
workingDir := t.TempDir()
|
||||||
|
|||||||
Reference in New Issue
Block a user