wip: daemon
This commit is contained in:
@@ -16,8 +16,8 @@ const (
|
||||
defaultPath = "/etc/wingmate/"
|
||||
defaultName = "config"
|
||||
|
||||
serviceKey = "service"
|
||||
cronKey = "cron"
|
||||
ServiceKey = "service"
|
||||
CronKey = "cron"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -1,39 +1,92 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"gitea.suyono.dev/suyono/wingmate/files/testconfig"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testReadArgs struct {
|
||||
cmd *cobra.Command
|
||||
args []string
|
||||
}
|
||||
|
||||
type testRead struct {
|
||||
name string
|
||||
env map[string]string
|
||||
args testReadArgs
|
||||
wantErr bool
|
||||
pre func(t *testing.T, tc *testRead)
|
||||
post func(t *testing.T, tc *testRead)
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
type args struct {
|
||||
cmd *cobra.Command
|
||||
args []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
env map[string]string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
tests := []testRead{
|
||||
{
|
||||
name: "env",
|
||||
env: map[string]string{
|
||||
strings.ToUpper(envPrefix + "_" + configPathKey): "/path/to/config",
|
||||
strings.ToUpper(envPrefix + "_" + configSearchPathKey): "/path/one,/path/two",
|
||||
},
|
||||
args: args{
|
||||
args: testReadArgs{
|
||||
nil,
|
||||
[]string{},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "env exist",
|
||||
args: testReadArgs{
|
||||
nil,
|
||||
[]string{},
|
||||
},
|
||||
wantErr: false,
|
||||
pre: func(t *testing.T, tc *testRead) {
|
||||
var (
|
||||
f *os.File
|
||||
err error
|
||||
fname string
|
||||
)
|
||||
if f, err = os.CreateTemp("", "config-*.yml"); err != nil {
|
||||
t.Fatal("create temp:", err)
|
||||
}
|
||||
fname = f.Name()
|
||||
if _, err = f.WriteString(testconfig.One); err != nil {
|
||||
t.Fatal("writing temp:", err)
|
||||
}
|
||||
if err = f.Close(); err != nil {
|
||||
t.Fatal("closing temp:", err)
|
||||
}
|
||||
|
||||
tc.env = map[string]string{
|
||||
strings.ToUpper(envPrefix + "_" + configPathKey): fname,
|
||||
}
|
||||
|
||||
tc.post = func(t *testing.T, tc *testRead) {
|
||||
_ = os.Remove(fname)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var err error
|
||||
|
||||
if tt.pre != nil {
|
||||
tt.pre(t, &tt)
|
||||
}
|
||||
|
||||
if tt.post != nil {
|
||||
defer tt.post(t, &tt)
|
||||
}
|
||||
|
||||
for k, v := range tt.env {
|
||||
if err = os.Setenv(k, v); err != nil {
|
||||
t.Fatal("failed", err)
|
||||
@@ -57,3 +110,82 @@ func TestRead(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
type args struct {
|
||||
cmd *cobra.Command
|
||||
args []string
|
||||
}
|
||||
tests := []testRead{
|
||||
{
|
||||
name: "env exist",
|
||||
args: testReadArgs{
|
||||
nil,
|
||||
[]string{},
|
||||
},
|
||||
wantErr: false,
|
||||
pre: func(t *testing.T, tc *testRead) {
|
||||
var (
|
||||
f *os.File
|
||||
err error
|
||||
fname string
|
||||
)
|
||||
if f, err = os.CreateTemp("", "config-*.yml"); err != nil {
|
||||
t.Fatal("create temp:", err)
|
||||
}
|
||||
fname = f.Name()
|
||||
if _, err = f.WriteString(testconfig.One); err != nil {
|
||||
t.Fatal("writing temp:", err)
|
||||
}
|
||||
if err = f.Close(); err != nil {
|
||||
t.Fatal("closing temp:", err)
|
||||
}
|
||||
|
||||
tc.env = map[string]string{
|
||||
strings.ToUpper(envPrefix + "_" + configPathKey): fname,
|
||||
}
|
||||
|
||||
tc.post = func(t *testing.T, tc *testRead) {
|
||||
_ = os.Remove(fname)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var err error
|
||||
|
||||
if tt.pre != nil {
|
||||
tt.pre(t, &tt)
|
||||
}
|
||||
|
||||
if tt.post != nil {
|
||||
defer tt.post(t, &tt)
|
||||
}
|
||||
|
||||
for k, v := range tt.env {
|
||||
if err = os.Setenv(k, v); err != nil {
|
||||
t.Fatal("failed", err)
|
||||
}
|
||||
}
|
||||
defer func() {
|
||||
for k := range tt.env {
|
||||
if err = os.Unsetenv(k); err != nil {
|
||||
t.Fatal("failed", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if err = Read(tt.args.cmd, tt.args.args); err != nil {
|
||||
t.Fatal("fail to read config:", err)
|
||||
}
|
||||
|
||||
t.Log(viper.AllKeys())
|
||||
m := viper.GetStringMap(ServiceKey)
|
||||
for s := range m {
|
||||
t.Log(s)
|
||||
t.Log(viper.GetStringMap(ServiceKey + "." + s))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user