forked from thingio/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandtest.go
139 lines (126 loc) · 2.96 KB
/
commandtest.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package commandtest
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"reflect"
"sync"
"github.com/influxdata/kapacitor/command"
)
type Commander struct {
sync.Mutex
cmds []*Command
}
func (c *Commander) NewCommand(s command.Spec) command.Command {
c.Lock()
defer c.Unlock()
cmd := &Command{
Spec: s,
}
c.cmds = append(c.cmds, cmd)
return cmd
}
func (c *Commander) Commands() []*Command {
c.Lock()
defer c.Unlock()
return c.cmds
}
type Command struct {
sync.Mutex
Spec command.Spec
Started bool
Waited bool
Killed bool
StdinData []byte
StdinPipeCalled bool
StdoutPipeCalled bool
StderrPipeCalled bool
stdin io.Reader
stdinR *io.PipeReader
stdinW *io.PipeWriter
}
func (c *Command) Compare(o *Command) error {
c.Lock()
o.Lock()
defer c.Unlock()
defer o.Unlock()
if got, exp := o.Spec, c.Spec; !reflect.DeepEqual(got, exp) {
return fmt.Errorf("unexpected command infos value: got %v exp %v", got, exp)
}
if got, exp := o.Started, c.Started; got != exp {
return fmt.Errorf("unexpected started value: got %v exp %v", got, exp)
}
if got, exp := o.Waited, c.Waited; got != exp {
return fmt.Errorf("unexpected waited value: got %v exp %v", got, exp)
}
if got, exp := o.Killed, c.Killed; got != exp {
return fmt.Errorf("unexpected killed value: got %v exp %v", got, exp)
}
if got, exp := o.StdinData, c.StdinData; !bytes.Equal(got, exp) {
return fmt.Errorf("unexpected stdin data value:\ngot\n%q\nexp\n%q\n", string(got), string(exp))
}
if got, exp := o.StdinPipeCalled, c.StdinPipeCalled; got != exp {
return fmt.Errorf("unexpected StdinPipeCalled value: got %v exp %v", got, exp)
}
if got, exp := o.StdoutPipeCalled, c.StdoutPipeCalled; got != exp {
return fmt.Errorf("unexpected StdoutPipeCalled value: got %v exp %v", got, exp)
}
if got, exp := o.StderrPipeCalled, c.StderrPipeCalled; got != exp {
return fmt.Errorf("unexpected StderrPipeCalled value: got %v exp %v", got, exp)
}
return nil
}
func (c *Command) Start() error {
c.Lock()
defer c.Unlock()
c.Started = true
data, err := ioutil.ReadAll(c.stdin)
if err != nil {
return err
}
c.StdinData = data
return nil
}
func (c *Command) Wait() error {
c.Lock()
c.Waited = true
c.Unlock()
return nil
}
func (c *Command) Stdin(in io.Reader) {
c.Lock()
c.stdin = in
c.Unlock()
}
func (c *Command) Stdout(out io.Writer) {
// Not useful to keep value so just ignore it
}
func (c *Command) Stderr(err io.Writer) {
// Not useful to keep value so just ignore it
}
func (c *Command) Kill() {
c.Lock()
c.Killed = true
c.Unlock()
}
func (c *Command) StdinPipe() (io.WriteCloser, error) {
c.Lock()
defer c.Unlock()
c.StdinPipeCalled = true
c.stdinR, c.stdinW = io.Pipe()
c.stdin = c.stdinR
return c.stdinW, nil
}
func (c *Command) StdoutPipe() (io.Reader, error) {
c.Lock()
defer c.Unlock()
c.StdoutPipeCalled = true
return new(bytes.Buffer), nil
}
func (c *Command) StderrPipe() (io.Reader, error) {
c.Lock()
defer c.Unlock()
c.StderrPipeCalled = true
return new(bytes.Buffer), nil
}