~juju-qa/ubuntu/trusty/juju/juju-1.25.8

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/packaging/manager/utils_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-02 18:01:10 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161202180110-dl1helep8qfebmhx
ImportĀ upstreamĀ 1.25.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Copyright 2015 Cloudbase Solutions SRL
 
3
// Licensed under the LGPLv3, see LICENCE file for details.
 
4
 
 
5
package manager_test
 
6
 
 
7
import (
 
8
        "os"
 
9
        "os/exec"
 
10
 
 
11
        "github.com/juju/testing"
 
12
        "github.com/juju/utils"
 
13
        "github.com/juju/utils/packaging/manager"
 
14
        gc "gopkg.in/check.v1"
 
15
)
 
16
 
 
17
var _ = gc.Suite(&UtilsSuite{})
 
18
 
 
19
type UtilsSuite struct {
 
20
        testing.IsolationSuite
 
21
}
 
22
 
 
23
func (s *UtilsSuite) SetUpSuite(c *gc.C) {
 
24
        s.IsolationSuite.SetUpSuite(c)
 
25
}
 
26
 
 
27
func (s *UtilsSuite) SetUpTest(c *gc.C) {
 
28
        s.IsolationSuite.SetUpTest(c)
 
29
}
 
30
 
 
31
func (s *UtilsSuite) TearDownTest(c *gc.C) {
 
32
        s.IsolationSuite.TearDownTest(c)
 
33
}
 
34
 
 
35
func (s *UtilsSuite) TearDownSuite(c *gc.C) {
 
36
        s.IsolationSuite.TearDownSuite(c)
 
37
}
 
38
 
 
39
type mockExitStatuser int
 
40
 
 
41
func (es mockExitStatuser) ExitStatus() int {
 
42
        return int(es)
 
43
}
 
44
 
 
45
func (s *UtilsSuite) TestRunCommandWithRetryDoesNotCallCombinedOutputTwice(c *gc.C) {
 
46
        const minRetries = 3
 
47
        var calls int
 
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.
 
53
        })
 
54
        s.PatchValue(&manager.CommandOutput, func(cmd *exec.Cmd) ([]byte, error) {
 
55
                calls++
 
56
                // Replace the command path and args so it's a no-op.
 
57
                cmd.Path = ""
 
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")
 
65
                }
 
66
                return output, cmdError
 
67
        })
 
68
 
 
69
        apt := manager.NewAptPackageManager()
 
70
 
 
71
        err := apt.Install(testedPackageName)
 
72
        c.Check(err, gc.ErrorMatches, "packaging command failed: exit status.*")
 
73
        c.Check(calls, gc.Equals, minRetries)
 
74
 
 
75
        // reset calls and re-test for Yum calls:
 
76
        calls = 0
 
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)
 
81
}