~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/exec/exec_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Copyright 2016 Cloudbase Solutions
 
3
// Licensed under the LGPLv3, see LICENCE file for details.
 
4
 
 
5
package exec_test
 
6
 
 
7
import (
 
8
        "fmt"
 
9
        "os"
 
10
        "time"
 
11
 
 
12
        "github.com/juju/testing"
 
13
        jc "github.com/juju/testing/checkers"
 
14
        gc "gopkg.in/check.v1"
 
15
 
 
16
        "github.com/juju/utils/clock"
 
17
        "github.com/juju/utils/exec"
 
18
)
 
19
 
 
20
type execSuite struct {
 
21
        testing.IsolationSuite
 
22
}
 
23
 
 
24
var _ = gc.Suite(&execSuite{})
 
25
 
 
26
func (*execSuite) TestWaitWithCancel(c *gc.C) {
 
27
        params := exec.RunParams{
 
28
                Commands: "sleep 100",
 
29
                Clock:    &mockClock{C: make(chan time.Time)},
 
30
        }
 
31
 
 
32
        err := params.Run()
 
33
        c.Assert(err, gc.IsNil)
 
34
        c.Assert(params.Process(), gc.Not(gc.IsNil))
 
35
 
 
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)
 
44
}
 
45
 
 
46
func (s *execSuite) TestKillAbortedIfUnsuccessfull(c *gc.C) {
 
47
        killCalled := false
 
48
 
 
49
        mockChan := make(chan time.Time, 1)
 
50
        defer close(mockChan)
 
51
        params := exec.RunParams{
 
52
                Commands:    "sleep 100",
 
53
                WorkingDir:  "",
 
54
                Environment: []string{},
 
55
                Clock:       &mockClock{C: mockChan},
 
56
                KillProcess: func(*os.Process) error {
 
57
                        killCalled = true
 
58
                        return nil
 
59
                },
 
60
        }
 
61
 
 
62
        err := params.Run()
 
63
        c.Assert(err, gc.IsNil)
 
64
        c.Assert(params.Process(), gc.Not(gc.IsNil))
 
65
 
 
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)
 
74
}
 
75
 
 
76
type mockClock struct {
 
77
        clock.Clock
 
78
        C <-chan time.Time
 
79
}
 
80
 
 
81
func (m *mockClock) After(t time.Duration) <-chan time.Time {
 
82
        return m.C
 
83
}