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

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/shell/script_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 2014 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package shell_test
 
5
 
 
6
import (
 
7
        "bytes"
 
8
        "io/ioutil"
 
9
        "os"
 
10
        "os/exec"
 
11
        "path/filepath"
 
12
        "strings"
 
13
 
 
14
        "github.com/juju/testing"
 
15
        jc "github.com/juju/testing/checkers"
 
16
        gc "gopkg.in/check.v1"
 
17
 
 
18
        "github.com/juju/utils/shell"
 
19
)
 
20
 
 
21
type scriptSuite struct {
 
22
        testing.IsolationSuite
 
23
}
 
24
 
 
25
var _ = gc.Suite(&scriptSuite{})
 
26
 
 
27
func (*scriptSuite) TestDumpFileOnErrorScriptOutput(c *gc.C) {
 
28
        script := shell.DumpFileOnErrorScript("a b c")
 
29
        c.Assert(script, gc.Equals, `
 
30
dump_file() {
 
31
    code=$?
 
32
    if [ $code -ne 0 -a -e 'a b c' ]; then
 
33
        cat 'a b c' >&2
 
34
    fi
 
35
    exit $code
 
36
}
 
37
trap dump_file EXIT
 
38
`[1:])
 
39
}
 
40
 
 
41
func (*scriptSuite) TestDumpFileOnErrorScript(c *gc.C) {
 
42
        tempdir := c.MkDir()
 
43
        filename := filepath.Join(tempdir, "log.txt")
 
44
        err := ioutil.WriteFile(filename, []byte("abc"), 0644)
 
45
        c.Assert(err, gc.IsNil)
 
46
 
 
47
        dumpScript := shell.DumpFileOnErrorScript(filename)
 
48
        c.Logf("%s", dumpScript)
 
49
        run := func(command string) (stdout, stderr string) {
 
50
                var stdoutBuf, stderrBuf bytes.Buffer
 
51
                cmd := exec.Command("/bin/bash", "-s")
 
52
                cmd.Stdin = strings.NewReader(dumpScript + command)
 
53
                cmd.Stdout = &stdoutBuf
 
54
                cmd.Stderr = &stderrBuf
 
55
                cmd.Run()
 
56
                return stdoutBuf.String(), stderrBuf.String()
 
57
        }
 
58
 
 
59
        stdout, stderr := run("exit 0")
 
60
        c.Assert(stdout, gc.Equals, "")
 
61
        c.Assert(stderr, gc.Equals, "")
 
62
 
 
63
        stdout, stderr = run("exit 1")
 
64
        c.Assert(stdout, gc.Equals, "")
 
65
        c.Assert(stderr, gc.Equals, "abc")
 
66
 
 
67
        err = os.Remove(filename)
 
68
        c.Assert(err, gc.IsNil)
 
69
        stdout, stderr = run("exit 1")
 
70
        c.Assert(stdout, gc.Equals, "")
 
71
        c.Assert(stderr, gc.Equals, "")
 
72
}
 
73
 
 
74
func (*scriptSuite) TestWriteScriptUnix(c *gc.C) {
 
75
        renderer := &shell.BashRenderer{}
 
76
        script := `
 
77
exec a-command
 
78
exec another-command
 
79
`
 
80
        commands := shell.WriteScript(renderer, "spam", "/ham/eggs", strings.Split(script, "\n"))
 
81
 
 
82
        cmd := `
 
83
cat > '/ham/eggs/spam.sh' << 'EOF'
 
84
#!/usr/bin/env bash
 
85
 
 
86
 
 
87
exec a-command
 
88
exec another-command
 
89
 
 
90
EOF`[1:]
 
91
        c.Check(commands, jc.DeepEquals, []string{
 
92
                cmd,
 
93
                "chmod 0755 '/ham/eggs/spam.sh'",
 
94
        })
 
95
}
 
96
 
 
97
func (*scriptSuite) TestWriteScriptWindows(c *gc.C) {
 
98
        renderer := &shell.PowershellRenderer{}
 
99
        script := `
 
100
exec a-command
 
101
exec another-command
 
102
`
 
103
        commands := shell.WriteScript(renderer, "spam", `C:\ham\eggs`, strings.Split(script, "\n"))
 
104
 
 
105
        c.Check(commands, jc.DeepEquals, []string{
 
106
                `Set-Content 'C:\ham\eggs\spam.ps1' @"
 
107
 
 
108
exec a-command
 
109
exec another-command
 
110
 
 
111
"@`,
 
112
        })
 
113
}