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() {
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"
52
output = fmt.Sprintf("cat<<EOF\n%s\nEOF", output)
60
var stdout, stderr string
61
switch output := output.(type) {
64
stdout = fmt.Sprintf("cat<<EOF\n%s\nEOF", output)
66
stdout = fmt.Sprintf("cat<<EOF\n%s\nEOF", output[0])
67
stderr = fmt.Sprintf("cat>&2<<EOF\n%s\nEOF", output[1])
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)
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)
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)
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)
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\\)")