~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/exec/exec_windows_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 2014 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package exec_test
 
5
 
 
6
import (
 
7
        "path/filepath"
 
8
        "strings"
 
9
        "syscall"
 
10
 
 
11
        jc "github.com/juju/testing/checkers"
 
12
        gc "gopkg.in/check.v1"
 
13
 
 
14
        "github.com/juju/utils/exec"
 
15
)
 
16
 
 
17
// 1 is thrown by powershell after the a command is cancelled
 
18
const cancelErrCode = 1
 
19
 
 
20
// longPath is copied over from the symlink package. This should be removed
 
21
// if we add it to gc or in some other convenience package
 
22
func longPath(path string) ([]uint16, error) {
 
23
        pathp, err := syscall.UTF16FromString(path)
 
24
        if err != nil {
 
25
                return nil, err
 
26
        }
 
27
 
 
28
        longp := pathp
 
29
        n, err := syscall.GetLongPathName(&pathp[0], &longp[0], uint32(len(longp)))
 
30
        if err != nil {
 
31
                return nil, err
 
32
        }
 
33
        if n > uint32(len(longp)) {
 
34
                longp = make([]uint16, n)
 
35
                n, err = syscall.GetLongPathName(&pathp[0], &longp[0], uint32(len(longp)))
 
36
                if err != nil {
 
37
                        return nil, err
 
38
                }
 
39
        }
 
40
        longp = longp[:n]
 
41
 
 
42
        return longp, nil
 
43
}
 
44
 
 
45
func longPathAsString(path string) (string, error) {
 
46
        longp, err := longPath(path)
 
47
        if err != nil {
 
48
                return "", err
 
49
        }
 
50
        return syscall.UTF16ToString(longp), nil
 
51
}
 
52
 
 
53
func (*execSuite) TestRunCommands(c *gc.C) {
 
54
        newDir, err := longPathAsString(c.MkDir())
 
55
        c.Assert(err, gc.IsNil)
 
56
        for i, test := range []struct {
 
57
                message     string
 
58
                commands    string
 
59
                workingDir  string
 
60
                environment []string
 
61
                stdout      string
 
62
                stderr      string
 
63
                code        int
 
64
        }{
 
65
                {
 
66
                        message:  "test stdout capture",
 
67
                        commands: "echo 'testing stdout'",
 
68
                        stdout:   "testing stdout\r\n",
 
69
                }, {
 
70
                        message:  "test stderr capture",
 
71
                        commands: "Write-Error 'testing stderr'",
 
72
                        stderr:   "testing stderr\r\n",
 
73
                }, {
 
74
                        message:  "test return code",
 
75
                        commands: "exit 42",
 
76
                        code:     42,
 
77
                }, {
 
78
                        message:    "test working dir",
 
79
                        commands:   "(pwd).Path",
 
80
                        workingDir: newDir,
 
81
                        stdout:     filepath.FromSlash(newDir) + "\r\n",
 
82
                }, {
 
83
                        message:     "test environment",
 
84
                        commands:    "echo $env:OMG_IT_WORKS",
 
85
                        environment: []string{"OMG_IT_WORKS=like magic"},
 
86
                        stdout:      "like magic\r\n",
 
87
                },
 
88
        } {
 
89
                c.Logf("%v: %s", i, test.message)
 
90
 
 
91
                params := exec.RunParams{
 
92
                        Commands:    test.commands,
 
93
                        WorkingDir:  test.workingDir,
 
94
                        Environment: test.environment,
 
95
                }
 
96
 
 
97
                result, err := exec.RunCommands(params)
 
98
                c.Assert(err, gc.IsNil)
 
99
                c.Assert(string(result.Stdout), gc.Equals, test.stdout)
 
100
                c.Assert(string(result.Stderr), jc.Contains, test.stderr)
 
101
                c.Assert(result.Code, gc.Equals, test.code)
 
102
 
 
103
                err = params.Run()
 
104
                c.Assert(err, gc.IsNil)
 
105
                c.Assert(params.Process(), gc.Not(gc.IsNil))
 
106
                result, err = params.Wait()
 
107
                c.Assert(err, gc.IsNil)
 
108
                c.Assert(string(result.Stdout), gc.Equals, test.stdout)
 
109
                c.Assert(string(result.Stderr), jc.Contains, test.stderr)
 
110
                c.Assert(result.Code, gc.Equals, test.code)
 
111
 
 
112
                err = params.Run()
 
113
                c.Assert(err, gc.IsNil)
 
114
                c.Assert(params.Process(), gc.Not(gc.IsNil))
 
115
                result, err = params.WaitWithCancel(nil)
 
116
                c.Assert(err, gc.IsNil)
 
117
                c.Assert(string(result.Stdout), gc.Equals, test.stdout)
 
118
                c.Assert(string(result.Stderr), jc.Contains, test.stderr)
 
119
                c.Assert(result.Code, gc.Equals, test.code)
 
120
        }
 
121
}
 
122
 
 
123
func (*execSuite) TestExecUnknownCommand(c *gc.C) {
 
124
        result, err := exec.RunCommands(
 
125
                exec.RunParams{
 
126
                        Commands: "unknown-command",
 
127
                },
 
128
        )
 
129
        c.Assert(err, gc.IsNil)
 
130
        c.Assert(result.Stdout, gc.HasLen, 0)
 
131
        stderr := strings.Replace(string(result.Stderr), "\r\n", "", -1)
 
132
        c.Assert(stderr, jc.Contains, "is not recognized as the name of a cmdlet")
 
133
        // 1 is returned by RunCommands when powershell commands throw exceptions
 
134
        c.Assert(result.Code, gc.Equals, 1)
 
135
}