forked from TrueCloudLab/frostfs-node
[#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>
This commit is contained in:
parent
2220f6a809
commit
5fac4058e8
2 changed files with 165 additions and 0 deletions
58
pkg/util/config/test/generate.go
Normal file
58
pkg/util/config/test/generate.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue