github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/utils/ssh/fakes_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ssh_test 5 6 import ( 7 "bytes" 8 "io" 9 "io/ioutil" 10 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/utils/ssh" 15 ) 16 17 type fakeClient struct { 18 calls []string 19 hostArg string 20 commandArg []string 21 optionsArg *ssh.Options 22 copyArgs []string 23 24 err error 25 cmd *ssh.Cmd 26 impl fakeCommandImpl 27 } 28 29 func (cl *fakeClient) checkCalls(c *gc.C, host string, command []string, options *ssh.Options, copyArgs []string, calls ...string) { 30 c.Check(cl.hostArg, gc.Equals, host) 31 c.Check(cl.commandArg, jc.DeepEquals, command) 32 c.Check(cl.optionsArg, gc.Equals, options) 33 c.Check(cl.copyArgs, jc.DeepEquals, copyArgs) 34 c.Check(cl.calls, jc.DeepEquals, calls) 35 } 36 37 func (cl *fakeClient) Command(host string, command []string, options *ssh.Options) *ssh.Cmd { 38 cl.calls = append(cl.calls, "Command") 39 cl.hostArg = host 40 cl.commandArg = command 41 cl.optionsArg = options 42 cmd := cl.cmd 43 if cmd == nil { 44 cmd = ssh.TestNewCmd(&cl.impl) 45 } 46 return cmd 47 } 48 49 func (cl *fakeClient) Copy(args []string, options *ssh.Options) error { 50 cl.calls = append(cl.calls, "Copy") 51 cl.copyArgs = args 52 cl.optionsArg = options 53 return cl.err 54 } 55 56 type bufferWriter struct { 57 bytes.Buffer 58 } 59 60 func (*bufferWriter) Close() error { 61 return nil 62 } 63 64 type fakeCommandImpl struct { 65 calls []string 66 stdinArg io.Reader 67 stdoutArg io.Writer 68 stderrArg io.Writer 69 stdinData bufferWriter 70 71 err error 72 stdinRaw io.Reader 73 stdoutRaw io.Writer 74 stderrRaw io.Writer 75 stdoutData bytes.Buffer 76 stderrData bytes.Buffer 77 } 78 79 func (ci *fakeCommandImpl) checkCalls(c *gc.C, stdin io.Reader, stdout, stderr io.Writer, calls ...string) { 80 c.Check(ci.stdinArg, gc.Equals, stdin) 81 c.Check(ci.stdoutArg, gc.Equals, stdout) 82 c.Check(ci.stderrArg, gc.Equals, stderr) 83 c.Check(ci.calls, jc.DeepEquals, calls) 84 } 85 86 func (ci *fakeCommandImpl) checkStdin(c *gc.C, data string) { 87 c.Check(ci.stdinData.String(), gc.Equals, data) 88 } 89 90 func (ci *fakeCommandImpl) Start() error { 91 ci.calls = append(ci.calls, "Start") 92 return ci.err 93 } 94 95 func (ci *fakeCommandImpl) Wait() error { 96 ci.calls = append(ci.calls, "Wait") 97 return ci.err 98 } 99 100 func (ci *fakeCommandImpl) Kill() error { 101 ci.calls = append(ci.calls, "Kill") 102 return ci.err 103 } 104 105 func (ci *fakeCommandImpl) SetStdio(stdin io.Reader, stdout, stderr io.Writer) { 106 ci.calls = append(ci.calls, "SetStdio") 107 ci.stdinArg = stdin 108 ci.stdoutArg = stdout 109 ci.stderrArg = stderr 110 } 111 112 func (ci *fakeCommandImpl) StdinPipe() (io.WriteCloser, io.Reader, error) { 113 ci.calls = append(ci.calls, "StdinPipe") 114 return &ci.stdinData, ci.stdinRaw, ci.err 115 } 116 117 func (ci *fakeCommandImpl) StdoutPipe() (io.ReadCloser, io.Writer, error) { 118 ci.calls = append(ci.calls, "StdoutPipe") 119 return ioutil.NopCloser(&ci.stdoutData), ci.stdoutRaw, ci.err 120 } 121 122 func (ci *fakeCommandImpl) StderrPipe() (io.ReadCloser, io.Writer, error) { 123 ci.calls = append(ci.calls, "StderrPipe") 124 return ioutil.NopCloser(&ci.stderrData), ci.stderrRaw, ci.err 125 }