2021-03-24 17:16:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadCommon(t *testing.T) {
|
2022-03-16 08:23:49 +00:00
|
|
|
pidDirPath := "./tests"
|
2021-03-24 17:16:17 +00:00
|
|
|
LogDirFullPath, err := filepath.Abs("./tests/log/")
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
t.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkingDirFullPath, err := filepath.Abs("./tests")
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
t.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
Input *Config
|
|
|
|
expectedResult *CommonCfg
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic valid configuration",
|
|
|
|
Input: &Config{
|
|
|
|
Common: &CommonCfg{
|
|
|
|
Daemonize: true,
|
|
|
|
PidDir: "./tests",
|
|
|
|
LogMedia: "file",
|
|
|
|
LogDir: "./tests/log/",
|
|
|
|
WorkingDir: "./tests/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedResult: &CommonCfg{
|
|
|
|
Daemonize: true,
|
2022-03-16 08:23:49 +00:00
|
|
|
PidDir: pidDirPath,
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "file",
|
|
|
|
LogDir: LogDirFullPath,
|
|
|
|
WorkingDir: WorkingDirFullPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty working dir",
|
|
|
|
Input: &Config{
|
|
|
|
Common: &CommonCfg{
|
|
|
|
Daemonize: true,
|
|
|
|
PidDir: "./tests",
|
|
|
|
LogMedia: "file",
|
|
|
|
LogDir: "./tests/log/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedResult: &CommonCfg{
|
|
|
|
Daemonize: true,
|
2022-03-16 08:23:49 +00:00
|
|
|
PidDir: pidDirPath,
|
2021-03-24 17:16:17 +00:00
|
|
|
LogMedia: "file",
|
|
|
|
LogDir: LogDirFullPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no common",
|
|
|
|
Input: &Config{},
|
|
|
|
expectedResult: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, test := range tests {
|
|
|
|
err := test.Input.LoadCommon()
|
|
|
|
if err == nil && test.err != "" {
|
|
|
|
fmt.Printf("TEST '%s': NOK\n", test.name)
|
|
|
|
t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
|
|
|
|
} else if test.err != "" {
|
|
|
|
if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
|
|
|
|
fmt.Printf("TEST '%s': NOK\n", test.name)
|
|
|
|
t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
|
|
|
|
test.err,
|
|
|
|
fmt.Sprintf("%s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isOk := assert.Equal(t, test.expectedResult, test.Input.Common)
|
|
|
|
if !isOk {
|
|
|
|
t.Fatalf("TEST '%s': NOK", test.name)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("TEST '%s': OK\n", test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|