~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to environs/manual/detection_test.go

  • Committer: John Arbash Meinel
  • Date: 2013-09-15 07:36:52 UTC
  • mfrom: (1817 juju-core)
  • mto: This revision was merged to the branch mainline in revision 1882.
  • Revision ID: john@arbash-meinel.com-20130915073652-80jnupeguvr1klea
Merge trunk, resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
        gc "launchpad.net/gocheck"
14
14
 
15
15
        "launchpad.net/juju-core/testing"
 
16
        jc "launchpad.net/juju-core/testing/checkers"
16
17
)
17
18
 
18
19
type detectionSuite struct {
34
35
        echo "ERROR: did not match expected input" >&2
35
36
        exit $exitcode
36
37
    fi
37
 
%s
 
38
    # stdout
 
39
    %s
 
40
    # stderr
 
41
    %s
38
42
    exit %d
39
43
else
40
44
    export PATH=${PATH#*:}
44
48
// sshresponse creates a fake "ssh" command in a new $PATH,
45
49
// updates $PATH, and returns a function to reset $PATH to
46
50
// its original value when called.
47
 
func sshresponse(c *gc.C, input, output string, rc int) func() {
 
51
//
 
52
// output may be:
 
53
//    - nil (no output)
 
54
//    - a string (stdout)
 
55
//    - a slice of strings, of length two (stdout, stderr)
 
56
func sshresponse(c *gc.C, input string, output interface{}, rc int) func() {
48
57
        fakebin := c.MkDir()
49
58
        ssh := filepath.Join(fakebin, "ssh")
50
59
        sshexpectedinput := ssh + ".expected-input"
51
 
        if output != "" {
52
 
                output = fmt.Sprintf("cat<<EOF\n%s\nEOF", output)
 
60
        var stdout, stderr string
 
61
        switch output := output.(type) {
 
62
        case nil:
 
63
        case string:
 
64
                stdout = fmt.Sprintf("cat<<EOF\n%s\nEOF", output)
 
65
        case []string:
 
66
                stdout = fmt.Sprintf("cat<<EOF\n%s\nEOF", output[0])
 
67
                stderr = fmt.Sprintf("cat>&2<<EOF\n%s\nEOF", output[1])
53
68
        }
54
 
        script := fmt.Sprintf(sshscript, output, rc)
 
69
        script := fmt.Sprintf(sshscript, stdout, stderr, rc)
55
70
        err := ioutil.WriteFile(ssh, []byte(script), 0777)
56
71
        c.Assert(err, gc.IsNil)
57
72
        err = ioutil.WriteFile(sshexpectedinput, []byte(input), 0644)
139
154
                c.Assert(hc.String(), gc.Equals, test.expectedHc)
140
155
        }
141
156
}
 
157
 
 
158
func (s *detectionSuite) TestCheckProvisioned(c *gc.C) {
 
159
        defer sshresponse(c, checkProvisionedScript, "", 0)()
 
160
        provisioned, err := checkProvisioned("example.com")
 
161
        c.Assert(err, gc.IsNil)
 
162
        c.Assert(provisioned, jc.IsFalse)
 
163
 
 
164
        defer sshresponse(c, checkProvisionedScript, "non-empty", 0)()
 
165
        provisioned, err = checkProvisioned("example.com")
 
166
        c.Assert(err, gc.IsNil)
 
167
        c.Assert(provisioned, jc.IsTrue)
 
168
 
 
169
        // stderr should not affect result.
 
170
        defer sshresponse(c, checkProvisionedScript, []string{"", "non-empty-stderr"}, 0)()
 
171
        provisioned, err = checkProvisioned("example.com")
 
172
        c.Assert(err, gc.IsNil)
 
173
        c.Assert(provisioned, jc.IsFalse)
 
174
 
 
175
        // if the script fails for whatever reason, then checkProvisioned
 
176
        // will return an error. stderr will be included in the error message.
 
177
        defer sshresponse(c, checkProvisionedScript, []string{"non-empty-stdout", "non-empty-stderr"}, 255)()
 
178
        _, err = checkProvisioned("example.com")
 
179
        c.Assert(err, gc.ErrorMatches, "exit status 255 \\(non-empty-stderr\\)")
 
180
}