File: fakes_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20200923.4646bfe-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,324 kB
  • sloc: makefile: 37
file content (125 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package ssh_test

import (
	"bytes"
	"io"
	"io/ioutil"

	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/utils/v2/ssh"
)

type fakeClient struct {
	calls      []string
	hostArg    string
	commandArg []string
	optionsArg *ssh.Options
	copyArgs   []string

	err  error
	cmd  *ssh.Cmd
	impl fakeCommandImpl
}

func (cl *fakeClient) checkCalls(c *gc.C, host string, command []string, options *ssh.Options, copyArgs []string, calls ...string) {
	c.Check(cl.hostArg, gc.Equals, host)
	c.Check(cl.commandArg, jc.DeepEquals, command)
	c.Check(cl.optionsArg, gc.Equals, options)
	c.Check(cl.copyArgs, jc.DeepEquals, copyArgs)
	c.Check(cl.calls, jc.DeepEquals, calls)
}

func (cl *fakeClient) Command(host string, command []string, options *ssh.Options) *ssh.Cmd {
	cl.calls = append(cl.calls, "Command")
	cl.hostArg = host
	cl.commandArg = command
	cl.optionsArg = options
	cmd := cl.cmd
	if cmd == nil {
		cmd = ssh.TestNewCmd(&cl.impl)
	}
	return cmd
}

func (cl *fakeClient) Copy(args []string, options *ssh.Options) error {
	cl.calls = append(cl.calls, "Copy")
	cl.copyArgs = args
	cl.optionsArg = options
	return cl.err
}

type bufferWriter struct {
	bytes.Buffer
}

func (*bufferWriter) Close() error {
	return nil
}

type fakeCommandImpl struct {
	calls     []string
	stdinArg  io.Reader
	stdoutArg io.Writer
	stderrArg io.Writer
	stdinData bufferWriter

	err        error
	stdinRaw   io.Reader
	stdoutRaw  io.Writer
	stderrRaw  io.Writer
	stdoutData bytes.Buffer
	stderrData bytes.Buffer
}

func (ci *fakeCommandImpl) checkCalls(c *gc.C, stdin io.Reader, stdout, stderr io.Writer, calls ...string) {
	c.Check(ci.stdinArg, gc.Equals, stdin)
	c.Check(ci.stdoutArg, gc.Equals, stdout)
	c.Check(ci.stderrArg, gc.Equals, stderr)
	c.Check(ci.calls, jc.DeepEquals, calls)
}

func (ci *fakeCommandImpl) checkStdin(c *gc.C, data string) {
	c.Check(ci.stdinData.String(), gc.Equals, data)
}

func (ci *fakeCommandImpl) Start() error {
	ci.calls = append(ci.calls, "Start")
	return ci.err
}

func (ci *fakeCommandImpl) Wait() error {
	ci.calls = append(ci.calls, "Wait")
	return ci.err
}

func (ci *fakeCommandImpl) Kill() error {
	ci.calls = append(ci.calls, "Kill")
	return ci.err
}

func (ci *fakeCommandImpl) SetStdio(stdin io.Reader, stdout, stderr io.Writer) {
	ci.calls = append(ci.calls, "SetStdio")
	ci.stdinArg = stdin
	ci.stdoutArg = stdout
	ci.stderrArg = stderr
}

func (ci *fakeCommandImpl) StdinPipe() (io.WriteCloser, io.Reader, error) {
	ci.calls = append(ci.calls, "StdinPipe")
	return &ci.stdinData, ci.stdinRaw, ci.err
}

func (ci *fakeCommandImpl) StdoutPipe() (io.ReadCloser, io.Writer, error) {
	ci.calls = append(ci.calls, "StdoutPipe")
	return ioutil.NopCloser(&ci.stdoutData), ci.stdoutRaw, ci.err
}

func (ci *fakeCommandImpl) StderrPipe() (io.ReadCloser, io.Writer, error) {
	ci.calls = append(ci.calls, "StderrPipe")
	return ioutil.NopCloser(&ci.stderrData), ci.stderrRaw, ci.err
}