1
// Copyright 2015 Canonical Ltd.
2
// Copyright 2015 Cloudbase Solutions SRL
3
// Licensed under the LGPLv3, see LICENCE file for details.
11
"github.com/juju/testing"
12
"github.com/juju/utils"
13
"github.com/juju/utils/packaging/manager"
14
gc "gopkg.in/check.v1"
17
var _ = gc.Suite(&UtilsSuite{})
19
type UtilsSuite struct {
20
testing.IsolationSuite
23
func (s *UtilsSuite) SetUpSuite(c *gc.C) {
24
s.IsolationSuite.SetUpSuite(c)
27
func (s *UtilsSuite) SetUpTest(c *gc.C) {
28
s.IsolationSuite.SetUpTest(c)
31
func (s *UtilsSuite) TearDownTest(c *gc.C) {
32
s.IsolationSuite.TearDownTest(c)
35
func (s *UtilsSuite) TearDownSuite(c *gc.C) {
36
s.IsolationSuite.TearDownSuite(c)
39
type mockExitStatuser int
41
func (es mockExitStatuser) ExitStatus() int {
45
func (s *UtilsSuite) TestRunCommandWithRetryDoesNotCallCombinedOutputTwice(c *gc.C) {
48
state := os.ProcessState{}
49
cmdError := &exec.ExitError{ProcessState: &state}
50
s.PatchValue(&manager.AttemptStrategy, utils.AttemptStrategy{Min: minRetries})
51
s.PatchValue(&manager.ProcessStateSys, func(*os.ProcessState) interface{} {
52
return mockExitStatuser(100) // retry each time.
54
s.PatchValue(&manager.CommandOutput, func(cmd *exec.Cmd) ([]byte, error) {
56
// Replace the command path and args so it's a no-op.
58
cmd.Args = []string{"version"}
59
// Call the real cmd.CombinedOutput to simulate better what
60
// happens in production. See also http://pad.lv/1394524.
61
output, err := cmd.CombinedOutput()
62
if _, ok := err.(*exec.Error); err != nil && !ok {
63
c.Check(err, gc.ErrorMatches, "exec: Stdout already set")
64
c.Fatalf("CommandOutput called twice unexpectedly")
66
return output, cmdError
69
apt := manager.NewAptPackageManager()
71
err := apt.Install(testedPackageName)
72
c.Check(err, gc.ErrorMatches, "packaging command failed: exit status.*")
73
c.Check(calls, gc.Equals, minRetries)
75
// reset calls and re-test for Yum calls:
77
yum := manager.NewYumPackageManager()
78
err = yum.Install(testedPackageName)
79
c.Check(err, gc.ErrorMatches, "packaging command failed: exit status.*")
80
c.Check(calls, gc.Equals, minRetries)