*: add DrainExecSyncIOTimeout config and disable as by default
Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
		| @@ -18,8 +18,14 @@ package server | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"context" | ||||
| 	"os" | ||||
| 	"syscall" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/containerd/containerd" | ||||
| 	"github.com/containerd/containerd/cio" | ||||
| 	cioutil "github.com/containerd/containerd/pkg/ioutil" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
| @@ -50,3 +56,95 @@ func TestCWClose(t *testing.T) { | ||||
| 	err := cw.Close() | ||||
| 	assert.NoError(t, err) | ||||
| } | ||||
|  | ||||
| func TestDrainExecSyncIO(t *testing.T) { | ||||
| 	ctx := context.TODO() | ||||
|  | ||||
| 	t.Run("NoTimeout", func(t *testing.T) { | ||||
| 		ep := &fakeExecProcess{ | ||||
| 			id:  t.Name(), | ||||
| 			pid: uint32(os.Getpid()), | ||||
| 		} | ||||
|  | ||||
| 		attachDoneCh := make(chan struct{}) | ||||
| 		time.AfterFunc(2*time.Second, func() { close(attachDoneCh) }) | ||||
| 		assert.NoError(t, drainExecSyncIO(ctx, ep, 0, attachDoneCh)) | ||||
| 		assert.Equal(t, 0, len(ep.actionEvents)) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("With3Seconds", func(t *testing.T) { | ||||
| 		ep := &fakeExecProcess{ | ||||
| 			id:  t.Name(), | ||||
| 			pid: uint32(os.Getpid()), | ||||
| 		} | ||||
|  | ||||
| 		attachDoneCh := make(chan struct{}) | ||||
| 		time.AfterFunc(100*time.Second, func() { close(attachDoneCh) }) | ||||
| 		assert.Error(t, drainExecSyncIO(ctx, ep, 3*time.Second, attachDoneCh)) | ||||
| 		assert.Equal(t, []string{"Delete"}, ep.actionEvents) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type fakeExecProcess struct { | ||||
| 	id           string | ||||
| 	pid          uint32 | ||||
| 	actionEvents []string | ||||
| } | ||||
|  | ||||
| // ID of the process | ||||
| func (p *fakeExecProcess) ID() string { | ||||
| 	return p.id | ||||
| } | ||||
|  | ||||
| // Pid is the system specific process id | ||||
| func (p *fakeExecProcess) Pid() uint32 { | ||||
| 	return p.pid | ||||
| } | ||||
|  | ||||
| // Start starts the process executing the user's defined binary | ||||
| func (p *fakeExecProcess) Start(context.Context) error { | ||||
| 	p.actionEvents = append(p.actionEvents, "Start") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Delete removes the process and any resources allocated returning the exit status | ||||
| func (p *fakeExecProcess) Delete(context.Context, ...containerd.ProcessDeleteOpts) (*containerd.ExitStatus, error) { | ||||
| 	p.actionEvents = append(p.actionEvents, "Delete") | ||||
| 	return nil, nil | ||||
| } | ||||
|  | ||||
| // Kill sends the provided signal to the process | ||||
| func (p *fakeExecProcess) Kill(context.Context, syscall.Signal, ...containerd.KillOpts) error { | ||||
| 	p.actionEvents = append(p.actionEvents, "Kill") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Wait asynchronously waits for the process to exit, and sends the exit code to the returned channel | ||||
| func (p *fakeExecProcess) Wait(context.Context) (<-chan containerd.ExitStatus, error) { | ||||
| 	p.actionEvents = append(p.actionEvents, "Wait") | ||||
| 	return nil, nil | ||||
| } | ||||
|  | ||||
| // CloseIO allows various pipes to be closed on the process | ||||
| func (p *fakeExecProcess) CloseIO(context.Context, ...containerd.IOCloserOpts) error { | ||||
| 	p.actionEvents = append(p.actionEvents, "CloseIO") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Resize changes the width and height of the process's terminal | ||||
| func (p *fakeExecProcess) Resize(ctx context.Context, w, h uint32) error { | ||||
| 	p.actionEvents = append(p.actionEvents, "Resize") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // IO returns the io set for the process | ||||
| func (p *fakeExecProcess) IO() cio.IO { | ||||
| 	p.actionEvents = append(p.actionEvents, "IO") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Status returns the executing status of the process | ||||
| func (p *fakeExecProcess) Status(context.Context) (containerd.Status, error) { | ||||
| 	p.actionEvents = append(p.actionEvents, "Status") | ||||
| 	return containerd.Status{}, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Wei Fu
					Wei Fu