container: organize test cases

This commit is contained in:
criyle 2025-02-22 21:07:11 +00:00
parent 396ac9723e
commit 10235abbf2
3 changed files with 90 additions and 47 deletions

View File

@ -51,16 +51,74 @@ func BenchmarkContainer(b *testing.B) {
}) })
} }
func TestContainerSuccess(t *testing.T) { type testCase struct {
t.Parallel() name string
m := getEnv(t, nil) param ExecveParam
r := m.Execve(context.TODO(), ExecveParam{ expected runner.Status
Args: []string{"/bin/true"}, }
Env: []string{"PATH=/bin"},
}) var err error = errors.New("test error")
if r.Status != runner.StatusNormal {
t.Fatal(r.Status, r.Error, r) var successParam = ExecveParam{
} Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
}
var tests []testCase = []testCase{
{
name: "Success",
param: successParam,
expected: runner.StatusNormal,
},
{
name: "SuccessWithSync",
param: ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
SyncFunc: func(p int) error { return nil },
},
expected: runner.StatusNormal,
},
{
name: "NotExists",
param: ExecveParam{
Args: []string{"not_exists"},
Env: []string{"PATH=/bin"},
},
expected: runner.StatusRunnerError,
},
{
name: "NotExistsWithSync",
param: ExecveParam{
Args: []string{"not_exists"},
Env: []string{"PATH=/bin"},
SyncFunc: func(p int) error { return nil },
},
expected: runner.StatusRunnerError,
},
{
name: "SyncFuncFail",
param: ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
SyncFunc: func(pid int) error {
return err
},
},
expected: runner.StatusRunnerError,
},
{
name: "SyncFuncFailAfterExec",
param: ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
SyncFunc: func(pid int) error {
return err
},
SyncAfterExec: true,
},
expected: runner.StatusRunnerError,
},
} }
type credgen struct{} type credgen struct{}
@ -77,41 +135,31 @@ func TestContainerSetCred(t *testing.T) {
if os.Getpid() != 1 { if os.Getpid() != 1 {
t.Skip("root required for this test") t.Skip("root required for this test")
} }
m := getEnv(t, credgen{}) runTest(t, successParam, runner.StatusNormal, credgen{})
r := m.Execve(context.TODO(), ExecveParam{ }
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"}, func runTest(t *testing.T, param ExecveParam, expected runner.Status, credGen CredGenerator) {
}) t.Parallel()
m := getEnv(t, credGen)
r := m.Execve(context.TODO(), param)
if r.Status != expected {
t.Fatal(r.Status, r.Error, r)
}
if err := m.Ping(); err != nil {
t.Fatal(err)
}
// can also success once more (no protocol mismatch)
r = m.Execve(context.TODO(), successParam)
if r.Status != runner.StatusNormal { if r.Status != runner.StatusNormal {
t.Fatal(r.Status, r.Error) t.Fatal(r.Status, r.Error, r)
} }
} }
func TestContainerNotExists(t *testing.T) { func TestCases(t *testing.T) {
t.Parallel() for _, c := range tests {
m := getEnv(t, nil) t.Run(c.name, func(t *testing.T) {
r := m.Execve(context.TODO(), ExecveParam{ runTest(t, c.param, c.expected, nil)
Args: []string{"not_exists"}, })
Env: []string{"PATH=/bin"},
})
if r.Status != runner.StatusRunnerError {
t.Fatal(r.Status, r.Error)
}
}
func TestContainerSyncFuncFail(t *testing.T) {
t.Parallel()
m := getEnv(t, nil)
err := errors.New("test error")
r := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
SyncFunc: func(pid int) error {
return err
},
})
if r.Status != runner.StatusRunnerError {
t.Fatal(r.Status, r.Error)
} }
} }

View File

@ -119,9 +119,7 @@ func (c *containerServer) handleExecve(cmd *execCmd, msg unixsocket.Msg) error {
if len(cmd.Argv) > 0 { if len(cmd.Argv) > 0 {
s = cmd.Argv[0] s = cmd.Argv[0]
} }
c.sendErrorReply("start: %s: %v", s, err) return c.sendErrorReply("start: %s: %v", s, err)
c.recvCmd()
return c.sendReply(reply{}, unixsocket.Msg{})
} }
if cmd.SyncAfter { if cmd.SyncAfter {
if err := syncPid(1); err != nil { if err := syncPid(1); err != nil {

View File

@ -88,7 +88,6 @@ func (c *container) Execve(ctx context.Context, param ExecveParam) runner.Result
} }
// if sync function did not involved // if sync function did not involved
if rep.Error != nil { if rep.Error != nil {
c.execveSyncKill()
return errResult("execve: %v", rep.Error) return errResult("execve: %v", rep.Error)
} }
// if pid not received // if pid not received
@ -103,8 +102,6 @@ func (c *container) Execve(ctx context.Context, param ExecveParam) runner.Result
if err := param.SyncFunc(int(msg.Cred.Pid)); err != nil { if err := param.SyncFunc(int(msg.Cred.Pid)); err != nil {
// tell sync function to exit and recv error // tell sync function to exit and recv error
c.execveSyncKill() c.execveSyncKill()
// tell kill function to exit and sync
c.execveSyncKill()
return errResult("execve: syncfunc failed %v", err) return errResult("execve: syncfunc failed %v", err)
} }
} }