Skip to content

Commit

Permalink
test: add tests for mrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jul 30, 2024
1 parent 9b6b453 commit 55ef932
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
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"
]
}
}
5 changes: 3 additions & 2 deletions pkg/mrunners/runner_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (r *runnerCron) Do(ctx context.Context) (err error) {
cr := cron.New(cron.WithLogger(cron.PrintfLogger(r.Logger)))

var chErr chan error

if r.Unit.Critical {
chErr = make(chan error, 1)
}
Expand All @@ -56,7 +57,7 @@ func (r *runnerCron) Do(ctx context.Context) (err error) {
r.Print("triggered")
if err := r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
r.Error("failed executing: " + err.Error())
if r.Unit.Critical {
if chErr != nil {
select {
case chErr <- err:
default:
Expand All @@ -72,7 +73,7 @@ func (r *runnerCron) Do(ctx context.Context) (err error) {

cr.Start()

if r.Unit.Critical {
if chErr != nil {
select {
case <-ctx.Done():
case err = <-chErr:
Expand Down
103 changes: 103 additions & 0 deletions pkg/mrunners/runner_cron_test.go
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()
}
103 changes: 103 additions & 0 deletions pkg/mrunners/runner_daemon_test.go
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()
}
107 changes: 107 additions & 0 deletions pkg/mrunners/runner_once_test.go
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)
}
Loading

0 comments on commit 55ef932

Please sign in to comment.