Skip to content

Commit

Permalink
test(mexec): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jul 30, 2024
1 parent 8b2abdf commit 7d1f940
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"cSpell.words": [
"charsets",
"menv",
"MINIT",
"mlog",
"mtmpl",
"munit"
"munit",
"shellquote",
"simplifiedchinese"
]
}
8 changes: 4 additions & 4 deletions pkg/mexec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (m *manager) Execute(opts ExecuteOptions) (err error) {
}
defer done()

opts.Logger.Print("minit: " + opts.Name + ": process started")
opts.Logger.Printf("minit: %s: process started", opts.Name)

// streaming
go opts.Logger.Out().ReadFrom(outPipe)
Expand All @@ -170,23 +170,23 @@ func (m *manager) Execute(opts ExecuteOptions) (err error) {
var code int

if err != nil {
opts.Logger.Errorf("minit: %s: process exited with error: %s", opts.Name, err.Error())
if ee, ok := err.(*exec.ExitError); ok {
code = ee.ExitCode()
} else {
opts.Logger.Error("minit: " + opts.Name + ": process exited with error: " + err.Error())
return
}
}

if checkSuccessCode(opts.SuccessCodes, code) {
err = nil
opts.Logger.Print("minit: " + opts.Name + ": process exited successfully")
opts.Logger.Printf("minit: %s: exit code %d is in success_codes", opts.Name, code)
return
}

err = fmt.Errorf("exit code: %d is not in success_codes", code)

opts.Logger.Error("minit: " + opts.Name + ": process exited with error: " + err.Error())
opts.Logger.Errorf("minit: %s: process exited with error: %s", opts.Name, err.Error())

return
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/mexec/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestNewManager(t *testing.T) {

buf, err := os.ReadFile(filepath.Join("testdata", "test.out.log"))
require.Contains(t, string(buf), "BBB")
require.NoError(t, err)

go func() {
time.Sleep(time.Second)
Expand All @@ -55,9 +56,9 @@ func TestNewManager(t *testing.T) {
Command: []string{
"sleep", "$AAA",
},
Logger: logger,
Logger: logger,
SuccessCodes: []int{0, 130, -1},
})
require.NoError(t, err)

require.True(t, time.Now().Sub(t1) < time.Second*2)
require.True(t, time.Since(t1) < time.Second*2)
}

0 comments on commit 7d1f940

Please sign in to comment.