1
// Copyright 2016 Canonical Ltd.
2
// Copyright 2016 Cloudbase Solutions
3
// Licensed under the LGPLv3, see LICENCE file for details.
12
"github.com/juju/testing"
13
jc "github.com/juju/testing/checkers"
14
gc "gopkg.in/check.v1"
16
"github.com/juju/utils/clock"
17
"github.com/juju/utils/exec"
20
type execSuite struct {
21
testing.IsolationSuite
24
var _ = gc.Suite(&execSuite{})
26
func (*execSuite) TestWaitWithCancel(c *gc.C) {
27
params := exec.RunParams{
28
Commands: "sleep 100",
29
Clock: &mockClock{C: make(chan time.Time)},
33
c.Assert(err, gc.IsNil)
34
c.Assert(params.Process(), gc.Not(gc.IsNil))
36
cancelChan := make(chan struct{}, 1)
37
defer close(cancelChan)
38
cancelChan <- struct{}{}
39
result, err := params.WaitWithCancel(cancelChan)
40
c.Assert(err, gc.Equals, exec.ErrCancelled)
41
c.Assert(string(result.Stdout), gc.Equals, "")
42
c.Assert(string(result.Stderr), gc.Equals, "")
43
c.Assert(result.Code, gc.Equals, cancelErrCode)
46
func (s *execSuite) TestKillAbortedIfUnsuccessfull(c *gc.C) {
49
mockChan := make(chan time.Time, 1)
51
params := exec.RunParams{
52
Commands: "sleep 100",
54
Environment: []string{},
55
Clock: &mockClock{C: mockChan},
56
KillProcess: func(*os.Process) error {
63
c.Assert(err, gc.IsNil)
64
c.Assert(params.Process(), gc.Not(gc.IsNil))
66
cancelChan := make(chan struct{}, 1)
67
defer close(cancelChan)
68
cancelChan <- struct{}{}
69
mockChan <- time.Now()
70
res, err := params.WaitWithCancel(cancelChan)
71
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("tried to kill process %d, but timed out", params.Process().Pid))
72
c.Assert(res, gc.IsNil)
73
c.Assert(killCalled, jc.IsTrue)
76
type mockClock struct {
81
func (m *mockClock) After(t time.Duration) <-chan time.Time {