// installFakeSSH creates a fake "ssh" command in a new $PATH, // updates $PATH, and returns a function to reset $PATH to its // original value when called. // // input may be: // - nil (ignore input) // - a string (match input exactly) // output may be: // - nil (no output) // - a string (stdout) // - a slice of strings, of length two (stdout, stderr) func installFakeSSH(c *gc.C, input, output interface{}, rc int) testing.Restorer { fakebin := c.MkDir() ssh := filepath.Join(fakebin, "ssh") switch input := input.(type) { case nil: case string: sshexpectedinput := ssh + ".expected-input" err := ioutil.WriteFile(sshexpectedinput, []byte(input), 0644) c.Assert(err, gc.IsNil) default: c.Errorf("input has invalid type: %T", input) } var stdout, stderr string switch output := output.(type) { case nil: case string: stdout = fmt.Sprintf("cat<<EOF\n%s\nEOF", output) case []string: c.Assert(output, gc.HasLen, 2) stdout = fmt.Sprintf("cat<<EOF\n%s\nEOF", output[0]) stderr = fmt.Sprintf("cat>&2<<EOF\n%s\nEOF", output[1]) } script := fmt.Sprintf(sshscript, stdout, stderr, rc) err := ioutil.WriteFile(ssh, []byte(script), 0777) c.Assert(err, gc.IsNil) return testing.PatchEnvPathPrepend(fakebin) }
func (s *BaseSuiteUnpatched) IsRunningLocally(c *gc.C) bool { restore := gitjujutesting.PatchEnvPathPrepend(s.osPathOrig) defer restore() running, err := lxdclient.IsRunningLocally() c.Assert(err, jc.ErrorIsNil) return running }
func (*PatchEnvironmentSuite) TestPatchEnvPathPrepend(c *gc.C) { oldPath := os.Getenv("PATH") dir := "/bin/bar" // just in case something goes wrong defer os.Setenv("PATH", oldPath) restore := testing.PatchEnvPathPrepend(dir) expect := dir + string(os.PathListSeparator) + oldPath c.Check(os.Getenv("PATH"), gc.Equals, expect) restore() c.Check(os.Getenv("PATH"), gc.Equals, oldPath) }