1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-17 04:30:03 +02:00

Adding app/log/config.go

This commit is contained in:
Henrik Hautakoski 2023-05-03 13:14:27 +02:00
parent 12f78d23ce
commit 19a84b77fb
2 changed files with 111 additions and 0 deletions

27
app/log/config.go Normal file
View file

@ -0,0 +1,27 @@
package log
import (
"path"
"time"
"github.com/eosswedenorg/thalos/app/types"
)
type Config struct {
Filename string `yaml:"filename"`
Directory string `yaml:"directory"`
MaxFileSize types.Size `yaml:"maxfilesize"`
MaxTime time.Duration `yaml:"maxtime"`
}
func (c Config) GetFilename() string {
return path.Base(c.Filename)
}
func (c Config) GetDirectory() string {
return path.Clean(c.Directory)
}
func (c Config) GetFilePath() string {
return path.Join(c.GetDirectory(), c.GetFilename())
}

84
app/log/config_test.go Normal file
View file

@ -0,0 +1,84 @@
package log
import (
"testing"
)
func TestConfig_GetDirectory(t *testing.T) {
tests := []struct {
name string
directory string
want string
}{
{"empty", "", "."},
{"root", "/", "/"},
{"one", "dir", "dir"},
{"path", "/path/to/some/directory", "/path/to/some/directory"},
{"relative", "relative/directory", "relative/directory"},
{"backtrace", "/path/./to/some/../directory", "/path/to/directory"},
{"multislash", "//path/to///directory//", "/path/to/directory"},
{"everything", "path/to/..//./from/directory//", "path/from/directory"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := Config{
Directory: tt.directory,
}
if got := c.GetDirectory(); got != tt.want {
t.Errorf("Config.GetDirectory() = %v, want %v", got, tt.want)
}
})
}
}
func TestConfig_GetFilename(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{"empty", "", "."},
{"name", "some_file.txt", "some_file.txt"},
{"path", "/path/to/my.log", "my.log"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := Config{
Filename: tt.filename,
}
if got := c.GetFilename(); got != tt.want {
t.Errorf("Config.GetFilename() = %v, want %v", got, tt.want)
}
})
}
}
func TestConfig_GetFilePath(t *testing.T) {
tests := []struct {
name string
filename string
directory string
want string
}{
{"empty", "", "", "."},
{"directory", "", "dir", "dir"},
{"filename", "filename", "", "filename"},
{"both", "filename", "dir", "dir/filename"},
{"root", "filename", "/", "/filename"},
{"abs", "filename", "/path/to/logs", "/path/to/logs/filename"},
{"relative", "filename", "/srv/../log", "/log/filename"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := Config{
Filename: tt.filename,
Directory: tt.directory,
}
if got := c.GetFilePath(); got != tt.want {
t.Errorf("Config.GetFilePath() = %v, want %v", got, tt.want)
}
})
}
}