aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/geth/run_test.go
blob: a82eb9d68aabcd7d879c2c4b8849b60477ec6d35 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "html/template"
    "io"
    "io/ioutil"
    "os"
    "os/exec"
    "regexp"
    "sync"
    "testing"
    "time"
)

func tmpdir(t *testing.T) string {
    dir, err := ioutil.TempDir("", "geth-test")
    if err != nil {
        t.Fatal(err)
    }
    return dir
}

type testgeth struct {
    // For total convenience, all testing methods are available.
    *testing.T
    // template variables for expect
    Datadir    string
    Executable string
    Func       template.FuncMap

    removeDatadir bool
    cmd           *exec.Cmd
    stdout        *bufio.Reader
    stdin         io.WriteCloser
    stderr        *testlogger
}

func init() {
    // Run the app if we're the child process for runGeth.
    if os.Getenv("GETH_TEST_CHILD") != "" {
        app.RunAndExitOnError()
        os.Exit(0)
    }
}

// spawns geth with the given command line args. If the args don't set --datadir, the
// child g gets a temporary data directory.
func runGeth(t *testing.T, args ...string) *testgeth {
    tt := &testgeth{T: t, Executable: os.Args[0]}
    for i, arg := range args {
        if arg == "-datadir" || arg == "--datadir" {
            if i < len(args)-1 {
                tt.Datadir = args[i+1]
            }
            break
        }
    }
    if tt.Datadir == "" {
        tt.Datadir = tmpdir(t)
        tt.removeDatadir = true
        args = append([]string{"-datadir", tt.Datadir}, args...)
        // Remove the temporary datadir if something fails below.
        defer func() {
            if t.Failed() {
                os.RemoveAll(tt.Datadir)
            }
        }()
    }

    // Boot "geth". This actually runs the test binary but the init function
    // will prevent any tests from running.
    tt.stderr = &testlogger{t: t}
    tt.cmd = exec.Command(os.Args[0], args...)
    tt.cmd.Env = append(os.Environ(), "GETH_TEST_CHILD=1")
    tt.cmd.Stderr = tt.stderr
    stdout, err := tt.cmd.StdoutPipe()
    if err != nil {
        t.Fatal(err)
    }
    tt.stdout = bufio.NewReader(stdout)
    if tt.stdin, err = tt.cmd.StdinPipe(); err != nil {
        t.Fatal(err)
    }
    if err := tt.cmd.Start(); err != nil {
        t.Fatal(err)
    }
    return tt
}

// InputLine writes the given text to the childs stdin.
// This method can also be called from an expect template, e.g.:
//
//     geth.expect(`Passphrase: {{.InputLine "password"}}`)
func (tt *testgeth) InputLine(s string) string {
    io.WriteString(tt.stdin, s+"\n")
    return ""
}

func (tt *testgeth) setTemplateFunc(name string, fn interface{}) {
    if tt.Func == nil {
        tt.Func = make(map[string]interface{})
    }
    tt.Func[name] = fn
}

// expect runs its argument as a template, then expects the
// child process to output the result of the template within 5s.
//
// If the template starts with a newline, the newline is removed
// before matching.
func (tt *testgeth) expect(tplsource string) {
    // Generate the expected output by running the template.
    tpl := template.Must(template.New("").Funcs(tt.Func).Parse(tplsource))
    wantbuf := new(bytes.Buffer)
    if err := tpl.Execute(wantbuf, tt); err != nil {
        panic(err)
    }
    // Trim exactly one newline at the beginning. This makes tests look
    // much nicer because all expect strings are at column 0.
    want := bytes.TrimPrefix(wantbuf.Bytes(), []byte("\n"))
    if err := tt.matchExactOutput(want); err != nil {
        tt.Fatal(err)
    }
    tt.Logf("Matched stdout text:\n%s", want)
}

func (tt *testgeth) matchExactOutput(want []byte) error {
    buf := make([]byte, len(want))
    n := 0
    tt.withKillTimeout(func() { n, _ = io.ReadFull(tt.stdout, buf) })
    buf = buf[:n]
    if n < len(want) || !bytes.Equal(buf, want) {
        // Grab any additional buffered output in case of mismatch
        // because it might help with debugging.
        buf = append(buf, make([]byte, tt.stdout.Buffered())...)
        tt.stdout.Read(buf[n:])
        // Find the mismatch position.
        for i := 0; i < n; i++ {
            if want[i] != buf[i] {
                return fmt.Errorf("Output mismatch at ◊:\n---------------- (stdout text)\n%s%s\n---------------- (expected text)\n%s",
                    buf[:i], buf[i:n], want)
            }
        }
        if n < len(want) {
            return fmt.Errorf("Not enough output, got until ◊:\n---------------- (stdout text)\n%s\n---------------- (expected text)\n%s%s",
                buf, want[:n], want[n:])
        }
    }
    return nil
}

// expectRegexp expects the child process to output text matching the
// given regular expression within 5s.
//
// Note that an arbitrary amount of output may be consumed by the
// regular expression. This usually means that expect cannot be used
// after expectRegexp.
func (tt *testgeth) expectRegexp(resource string) (*regexp.Regexp, []string) {
    var (
        re      = regexp.MustCompile(resource)
        rtee    = &runeTee{in: tt.stdout}
        matches []int
    )
    tt.withKillTimeout(func() { matches = re.FindReaderSubmatchIndex(rtee) })
    output := rtee.buf.Bytes()
    if matches == nil {
        tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s",
            output, resource)
        return re, nil
    }
    tt.Logf("Matched stdout text:\n%s", output)
    var submatch []string
    for i := 0; i < len(matches); i += 2 {
        submatch = append(submatch, string(output[i:i+1]))
    }
    return re, submatch
}

// expectExit expects the child process to exit within 5s without
// printing any additional text on stdout.
func (tt *testgeth) expectExit() {
    var output []byte
    tt.withKillTimeout(func() {
        output, _ = ioutil.ReadAll(tt.stdout)
    })
    tt.cmd.Wait()
    if tt.removeDatadir {
        os.RemoveAll(tt.Datadir)
    }
    if len(output) > 0 {
        tt.Errorf("Unmatched stdout text:\n%s", output)
    }
}

func (tt *testgeth) interrupt() {
    tt.cmd.Process.Signal(os.Interrupt)
}

// stderrText returns any stderr output written so far.
// The returned text holds all log lines after expectExit has
// returned.
func (tt *testgeth) stderrText() string {
    tt.stderr.mu.Lock()
    defer tt.stderr.mu.Unlock()
    return tt.stderr.buf.String()
}

func (tt *testgeth) withKillTimeout(fn func()) {
    timeout := time.AfterFunc(5*time.Second, func() {
        tt.Log("killing the child process (timeout)")
        tt.cmd.Process.Kill()
        if tt.removeDatadir {
            os.RemoveAll(tt.Datadir)
        }
    })
    defer timeout.Stop()
    fn()
}

// testlogger logs all written lines via t.Log and also
// collects them for later inspection.
type testlogger struct {
    t   *testing.T
    mu  sync.Mutex
    buf bytes.Buffer
}

func (tl *testlogger) Write(b []byte) (n int, err error) {
    lines := bytes.Split(b, []byte("\n"))
    for _, line := range lines {
        if len(line) > 0 {
            tl.t.Logf("(stderr) %s", line)
        }
    }
    tl.mu.Lock()
    tl.buf.Write(b)
    tl.mu.Unlock()
    return len(b), err
}

// runeTee collects text read through it into buf.
type runeTee struct {
    in interface {
        io.Reader
        io.ByteReader
        io.RuneReader
    }
    buf bytes.Buffer
}

func (rtee *runeTee) Read(b []byte) (n int, err error) {
    n, err = rtee.in.Read(b)
    rtee.buf.Write(b[:n])
    return n, err
}

func (rtee *runeTee) ReadRune() (r rune, size int, err error) {
    r, size, err = rtee.in.ReadRune()
    if err == nil {
        rtee.buf.WriteRune(r)
    }
    return r, size, err
}

func (rtee *runeTee) ReadByte() (b byte, err error) {
    b, err = rtee.in.ReadByte()
    if err == nil {
        rtee.buf.WriteByte(b)
    }
    return b, err
}