-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
397 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
{ | ||
"cSpell.words": [ | ||
"charsets", | ||
"exem", | ||
"menv", | ||
"merrs", | ||
"mexec", | ||
"MINIT", | ||
"mlog", | ||
"mrunners", | ||
"mtmpl", | ||
"munit", | ||
"shellquote", | ||
"simplifiedchinese", | ||
"stretchr" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package mrunners | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yankeguo/minit/pkg/mexec" | ||
"github.com/yankeguo/minit/pkg/mlog" | ||
"github.com/yankeguo/minit/pkg/munit" | ||
"github.com/yankeguo/rg" | ||
) | ||
|
||
func TestRunnerCron(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
r := &runnerCron{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindCron, | ||
Name: "test", | ||
Cron: "@every 1s", | ||
Immediate: true, | ||
Command: []string{ | ||
"echo", "hhhlll", | ||
}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
ctx, ctxCancel := context.WithCancel(context.Background()) | ||
defer ctxCancel() | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := r.Do(ctx) | ||
require.NoError(t, err) | ||
}() | ||
|
||
time.Sleep(time.Millisecond * 2500) | ||
|
||
ctxCancel() | ||
|
||
wg.Wait() | ||
|
||
require.Equal(t, 3, strings.Count(buf.String(), "hhhlll\n")) | ||
} | ||
|
||
func TestRunnerCronCritical(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
r := &runnerCron{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindCron, | ||
Name: "test", | ||
Cron: "@every 1s", | ||
Shell: "/bin/bash", | ||
Critical: true, | ||
Command: []string{ | ||
"exit 2", | ||
}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
ctx, ctxCancel := context.WithCancel(context.Background()) | ||
defer ctxCancel() | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := r.Do(ctx) | ||
require.Error(t, err) | ||
}() | ||
|
||
time.Sleep(time.Millisecond * 2500) | ||
|
||
ctxCancel() | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package mrunners | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yankeguo/minit/pkg/mexec" | ||
"github.com/yankeguo/minit/pkg/mlog" | ||
"github.com/yankeguo/minit/pkg/munit" | ||
"github.com/yankeguo/rg" | ||
) | ||
|
||
func TestRunnerDaemon(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
r := &runnerDaemon{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindDaemon, | ||
Name: "test", | ||
Shell: "/bin/bash", | ||
Command: []string{ | ||
"sleep 1 && echo hello && exit 2", | ||
}, | ||
Critical: true, | ||
SuccessCodes: []int{0, 2}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
ctx, ctxCancel := context.WithCancel(context.Background()) | ||
defer ctxCancel() | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := r.Do(ctx) | ||
require.NoError(t, err) | ||
}() | ||
|
||
time.Sleep(time.Millisecond * 2500) | ||
|
||
ctxCancel() | ||
|
||
wg.Wait() | ||
|
||
require.Equal(t, 1, bytes.Count(buf.Bytes(), []byte("hello\n"))) | ||
} | ||
|
||
func TestRunnerDaemonCritical(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
r := &runnerDaemon{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindDaemon, | ||
Name: "test", | ||
Shell: "/bin/bash", | ||
Command: []string{ | ||
"sleep 1 && echo hello && exit 2", | ||
}, | ||
Critical: true, | ||
SuccessCodes: []int{1}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
ctx, ctxCancel := context.WithCancel(context.Background()) | ||
defer ctxCancel() | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := r.Do(ctx) | ||
require.Error(t, err) | ||
}() | ||
|
||
time.Sleep(time.Millisecond * 2500) | ||
|
||
ctxCancel() | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package mrunners | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yankeguo/minit/pkg/mexec" | ||
"github.com/yankeguo/minit/pkg/mlog" | ||
"github.com/yankeguo/minit/pkg/munit" | ||
"github.com/yankeguo/rg" | ||
) | ||
|
||
func TestRunnerOnce(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
r := &runnerOnce{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindOnce, | ||
Name: "test", | ||
Shell: "/bin/bash", | ||
Command: []string{ | ||
"sleep 1 && echo hello && exit 2", | ||
}, | ||
Critical: true, | ||
SuccessCodes: []int{0, 2}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
err := r.Do(context.Background()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestRunnerOnceCritical(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
r := &runnerOnce{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindOnce, | ||
Name: "test", | ||
Shell: "/bin/bash", | ||
Command: []string{ | ||
"sleep 1 && echo hello && exit 2", | ||
}, | ||
Critical: true, | ||
SuccessCodes: []int{1}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
err := r.Do(context.Background()) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestRunnerOnceCriticalNonBlocking(t *testing.T) { | ||
exem := mexec.NewManager() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
blocking := false | ||
|
||
r := &runnerOnce{ | ||
RunnerOptions: RunnerOptions{ | ||
Unit: munit.Unit{ | ||
Kind: munit.KindOnce, | ||
Name: "test", | ||
Shell: "/bin/bash", | ||
Blocking: &blocking, | ||
Command: []string{ | ||
"sleep 1 && echo hello && exit 2", | ||
}, | ||
Critical: true, | ||
SuccessCodes: []int{1}, | ||
}, | ||
Exec: exem, | ||
Logger: rg.Must(mlog.NewProcLogger(mlog.ProcLoggerOptions{ | ||
ConsoleOut: buf, | ||
ConsoleErr: buf, | ||
})), | ||
}, | ||
} | ||
|
||
start := time.Now() | ||
|
||
err := r.Do(context.Background()) | ||
require.NoError(t, err) | ||
require.True(t, time.Since(start) < time.Millisecond*100) | ||
} |
Oops, something went wrong.