File: exec_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,748 kB
  • sloc: makefile: 20
file content (83 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2016 Canonical Ltd.
// Copyright 2016 Cloudbase Solutions
// Licensed under the LGPLv3, see LICENCE file for details.

package exec_test

import (
	"fmt"
	"os"
	"time"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/utils/clock"
	"github.com/juju/utils/exec"
)

type execSuite struct {
	testing.IsolationSuite
}

var _ = gc.Suite(&execSuite{})

func (*execSuite) TestWaitWithCancel(c *gc.C) {
	params := exec.RunParams{
		Commands: "sleep 100",
		Clock:    &mockClock{C: make(chan time.Time)},
	}

	err := params.Run()
	c.Assert(err, gc.IsNil)
	c.Assert(params.Process(), gc.Not(gc.IsNil))

	cancelChan := make(chan struct{}, 1)
	defer close(cancelChan)
	cancelChan <- struct{}{}
	result, err := params.WaitWithCancel(cancelChan)
	c.Assert(err, gc.Equals, exec.ErrCancelled)
	c.Assert(string(result.Stdout), gc.Equals, "")
	c.Assert(string(result.Stderr), gc.Equals, "")
	c.Assert(result.Code, gc.Equals, cancelErrCode)
}

func (s *execSuite) TestKillAbortedIfUnsuccessfull(c *gc.C) {
	killCalled := false

	mockChan := make(chan time.Time, 1)
	defer close(mockChan)
	params := exec.RunParams{
		Commands:    "sleep 100",
		WorkingDir:  "",
		Environment: []string{},
		Clock:       &mockClock{C: mockChan},
		KillProcess: func(*os.Process) error {
			killCalled = true
			return nil
		},
	}

	err := params.Run()
	c.Assert(err, gc.IsNil)
	c.Assert(params.Process(), gc.Not(gc.IsNil))

	cancelChan := make(chan struct{}, 1)
	defer close(cancelChan)
	cancelChan <- struct{}{}
	mockChan <- time.Now()
	res, err := params.WaitWithCancel(cancelChan)
	c.Assert(err, gc.ErrorMatches, fmt.Sprintf("tried to kill process %d, but timed out", params.Process().Pid))
	c.Assert(res, gc.IsNil)
	c.Assert(killCalled, jc.IsTrue)
}

type mockClock struct {
	clock.Clock
	C <-chan time.Time
}

func (m *mockClock) After(t time.Duration) <-chan time.Time {
	return m.C
}