frostfs-node/pkg/util/config/test/generate.go
Aleksey Savchuk 5fac4058e8 [#1364] cmd/common: Add tests for CreateViper and ReloadViper
Add tests for `CreateViper` and `ReloadViper` to ensure that no extra
files, except *.yaml, *.yml, *.json, are loaded from config directory.

Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2024-09-11 08:03:05 +00:00

58 lines
1.1 KiB
Go

package configtest
import (
"crypto/rand"
"os"
"path"
"testing"
"github.com/stretchr/testify/require"
)
type MarshalFunc = func(any) ([]byte, error)
type ConfigFile struct {
filename string
content map[string]any
marshal func(any) ([]byte, error)
}
type DummyFile struct {
filename string
size int
}
func NewConfigFile(filename string, content map[string]any, marshal MarshalFunc) ConfigFile {
return ConfigFile{
filename: filename,
content: content,
marshal: marshal,
}
}
func NewDummyFile(filename string, size int) DummyFile {
return DummyFile{
filename: filename,
size: size,
}
}
func PrepareConfigFiles(t *testing.T, dir string, files []ConfigFile) {
for _, file := range files {
data, err := file.marshal(file.content)
require.NoError(t, err)
err = os.WriteFile(path.Join(dir, file.filename), data, 0o600)
require.NoError(t, err)
}
}
func PrepareDummyFiles(t *testing.T, dir string, files []DummyFile) {
for _, file := range files {
data := make([]byte, file.size)
_, _ = rand.Read(data)
err := os.WriteFile(path.Join(dir, file.filename), data, 0o600)
require.NoError(t, err)
}
}