WIP: unit test done, need further test
This commit is contained in:
@@ -249,6 +249,10 @@ func (c *CronTask) Environ() []string {
|
||||
return retval
|
||||
}
|
||||
|
||||
func (c *CronTask) PatchEnv(env []string) []string {
|
||||
return patchEnv(env, c.environ)
|
||||
}
|
||||
|
||||
func (c *CronTask) Setsid() bool {
|
||||
return c.setsid
|
||||
}
|
||||
@@ -264,7 +268,7 @@ func (c *CronTask) WorkingDir() string {
|
||||
func (c *CronTask) Status() wminit.TaskStatus {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return nil
|
||||
// return nil
|
||||
}
|
||||
|
||||
func (c *CronTask) TimeToRun(now time.Time) bool {
|
||||
|
||||
97
task/task.go
97
task/task.go
@@ -13,7 +13,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
envRef = regexp.MustCompile(`\$([a-zA-Z][a-zA-Z0-9]*)`)
|
||||
envCapture = regexp.MustCompile(`\$+[a-zA-Z_][a-zA-Z0-9_]*|\$+{[a-zA-Z_][a-zA-Z0-9_]*}`)
|
||||
envEsc = regexp.MustCompile(`^\$\$+[^\$]+$`) // escaped, starts with two or more $ character
|
||||
envRef = regexp.MustCompile(`^\$([^\$]+)$`) // capture the variable name
|
||||
envRefExplicit = regexp.MustCompile(`^\${([^\$]+)}$`) // capture the variable name - explicit
|
||||
)
|
||||
|
||||
type config interface {
|
||||
@@ -75,7 +78,7 @@ func (ts *Tasks) Crones() []wminit.CronTask {
|
||||
func (ts *Tasks) Get(name string) (wminit.Task, error) {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return nil, nil
|
||||
// return nil, nil
|
||||
}
|
||||
|
||||
type ServiceTask struct {
|
||||
@@ -311,32 +314,7 @@ func (t *ServiceTask) Environ() []string {
|
||||
}
|
||||
|
||||
func (t *ServiceTask) PatchEnv(env []string) []string {
|
||||
tMap := make(map[string]string)
|
||||
for _, e := range env {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
tMap[key] = value
|
||||
}
|
||||
|
||||
for _, e := range t.environ {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ContainsAny(key, "$") {
|
||||
continue
|
||||
}
|
||||
|
||||
//envRef.FindAllString()
|
||||
_ = value
|
||||
}
|
||||
|
||||
return nil
|
||||
return patchEnv(env, t.environ)
|
||||
}
|
||||
|
||||
func (t *ServiceTask) Setsid() bool {
|
||||
@@ -358,19 +336,19 @@ func (t *ServiceTask) WorkingDir() string {
|
||||
func (t *ServiceTask) Status() wminit.TaskStatus {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return nil
|
||||
// return nil
|
||||
}
|
||||
|
||||
func (t *ServiceTask) AutoStart() bool {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return false
|
||||
// return false
|
||||
}
|
||||
|
||||
func (t *ServiceTask) AutoRestart() bool {
|
||||
//TODO: implement me!
|
||||
panic("not implemented")
|
||||
return false
|
||||
// return false
|
||||
}
|
||||
|
||||
func (t *ServiceTask) StartSecs() uint {
|
||||
@@ -407,3 +385,60 @@ func validate(validators ...func() error) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func patchEnv(existing, new []string) []string {
|
||||
tMap := make(map[string]string)
|
||||
for _, e := range existing {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
tMap[key] = value
|
||||
}
|
||||
|
||||
for _, e := range new {
|
||||
key, value, ok := strings.Cut(e, "=")
|
||||
if !ok {
|
||||
wingmate.Log().Warn().Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ContainsAny(key, "$") {
|
||||
wingmate.Log().Error().Err(fmt.Errorf("variable name contains $")).Msgf("removing invalid environment:", e)
|
||||
continue
|
||||
}
|
||||
|
||||
value = envCapture.ReplaceAllStringFunc(value, func(rep string) string {
|
||||
if envEsc.MatchString(rep) {
|
||||
return rep
|
||||
}
|
||||
|
||||
if envName := envRefExplicit.FindStringSubmatch(rep); envName != nil && envName[1] != "" {
|
||||
exVal, ok := tMap[envName[1]]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return exVal
|
||||
}
|
||||
|
||||
if envName := envRef.FindStringSubmatch(rep); envName != nil && envName[1] != "" {
|
||||
exVal, ok := tMap[envName[1]]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return exVal
|
||||
}
|
||||
return rep
|
||||
})
|
||||
|
||||
tMap[key] = value
|
||||
}
|
||||
|
||||
outEnv := make([]string, 0, len(existing))
|
||||
for key, val := range tMap {
|
||||
outEnv = append(outEnv, fmt.Sprintf("%s=%s", key, val))
|
||||
}
|
||||
|
||||
return outEnv
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServicesV0(t *testing.T) {
|
||||
@@ -77,3 +80,49 @@ func TestTasks_List(t *testing.T) {
|
||||
|
||||
assert.ElementsMatch(t, testNames, tnames)
|
||||
}
|
||||
|
||||
func TestServiceTaskPatchEnv(t *testing.T) {
|
||||
type testEntry struct {
|
||||
name string
|
||||
systemEnv []string
|
||||
serviceEnv []string
|
||||
expected []string
|
||||
}
|
||||
_ = wingmate.NewLog(os.Stderr, wingmate.Caller|wingmate.Time)
|
||||
|
||||
tests := []testEntry{
|
||||
{
|
||||
name: "normal",
|
||||
systemEnv: []string{"PATH=/bin:/usr/bin:/usr/local/bin"},
|
||||
serviceEnv: []string{
|
||||
"SPARK_HOME=/opt/spark",
|
||||
"PATH=$SPARK_HOME/bin:$PATH",
|
||||
},
|
||||
expected: []string{
|
||||
"SPARK_HOME=/opt/spark",
|
||||
"PATH=/opt/spark/bin:/bin:/usr/bin:/usr/local/bin",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "explicit",
|
||||
systemEnv: []string{"PART=hello "},
|
||||
serviceEnv: []string{"GREET=${PART}world"},
|
||||
expected: []string{"PART=hello ", "GREET=hello world"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name+" - service", func(t *testing.T) {
|
||||
st := NewServiceTask(tt.name)
|
||||
st.SetEnv(tt.serviceEnv...)
|
||||
result := st.PatchEnv(tt.systemEnv)
|
||||
assert.ElementsMatch(t, result, tt.expected)
|
||||
})
|
||||
t.Run(tt.name+" - cron", func(t *testing.T) {
|
||||
st := NewCronTask(tt.name)
|
||||
st.SetEnv(tt.serviceEnv...)
|
||||
result := st.PatchEnv(tt.systemEnv)
|
||||
assert.ElementsMatch(t, result, tt.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user