-
Notifications
You must be signed in to change notification settings - Fork 7
/
replay.go
329 lines (301 loc) · 9.02 KB
/
replay.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2020 YourBase Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Package replay provides a biome that records all events to an underlying
// biome and then allows replaying those command invocations later.
package replay
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"sync"
"github.com/yourbase/commons/jsonstring"
"github.com/yourbase/yb/internal/biome"
"zombiezen.com/go/log"
)
// replayData is the JSON document saved by a Recorder.
type replayData struct {
Descriptor *biome.Descriptor
Dirs *biome.Dirs
JoinedPaths []*joinPathLookup
AbsPaths map[string]bool
Invocations []*invocation
}
type joinPathLookup struct {
Elems []string
Result string
}
// invocation is the result of a single command run.
type invocation struct {
Argv []string
Dir string `json:",omitempty"`
EnvVars map[string]string `json:",omitempty"`
PrependPath []string `json:",omitempty"`
AppendPath []string `json:",omitempty"`
StdinSHA256 hexString `json:",omitempty"`
Output *invocationOutput `json:",omitempty"`
Error string `json:",omitempty"`
}
func (i *invocation) env() biome.Environment {
return biome.Environment{
Vars: i.EnvVars,
AppendPath: i.AppendPath,
PrependPath: i.PrependPath,
}
}
type invocationOutput struct {
combined []byte
stdout []byte
stderr []byte
}
func (out *invocationOutput) MarshalJSON() ([]byte, error) {
if out == nil {
return []byte("null"), nil
}
var buf []byte
buf = append(buf, '{')
if out.combined != nil {
buf = append(buf, `"Combined":`...)
buf = jsonstring.Append(buf, base64.StdEncoding.EncodeToString(out.combined))
buf = append(buf, '}')
return buf, nil
}
if out.stdout != nil {
buf = append(buf, `"Stdout":`...)
buf = jsonstring.Append(buf, base64.StdEncoding.EncodeToString(out.stdout))
if out.stderr != nil {
buf = append(buf, ',')
}
}
if out.stderr != nil {
buf = append(buf, `"Stderr":`...)
buf = jsonstring.Append(buf, base64.StdEncoding.EncodeToString(out.stderr))
}
buf = append(buf, '}')
return buf, nil
}
func (out *invocationOutput) UnmarshalJSON(b []byte) error {
var temp struct {
Combined *string
Stdout *string
Stderr *string
}
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
if temp.Combined != nil {
var err error
out.combined, err = base64.StdEncoding.DecodeString(*temp.Combined)
if err != nil {
return fmt.Errorf("decode combined output: %w", err)
}
return nil
}
if temp.Stdout != nil {
var err error
out.stdout, err = base64.StdEncoding.DecodeString(*temp.Stdout)
if err != nil {
return fmt.Errorf("decode stdout: %w", err)
}
}
if temp.Stderr != nil {
var err error
out.stderr, err = base64.StdEncoding.DecodeString(*temp.Stderr)
if err != nil {
return fmt.Errorf("decode stderr: %w", err)
}
}
return nil
}
// Replay is a biome that replays Run commands from a recorded file.
type Replay struct {
data replayData
mu sync.Mutex
invokeIndex int
failed bool
}
// Load reads the biome interactions in the given directory for the given
// descriptor and returns a biome that repeats those commands.
func Load(dir string, desc *biome.Descriptor) (*Replay, error) {
jsonData, err := ioutil.ReadFile(filepath.Join(dir, recordFilename(desc)))
if err != nil {
return nil, fmt.Errorf("load replay for %s (%s/%s): %w", dir, desc.OS, desc.Arch, err)
}
r := new(Replay)
if err := json.Unmarshal(jsonData, &r.data); err != nil {
return nil, fmt.Errorf("load replay for %s (%s/%s): %w", dir, desc.OS, desc.Arch, err)
}
return r, nil
}
// Describe returns the recorded descriptor.
func (r *Replay) Describe() *biome.Descriptor {
return r.data.Descriptor
}
// Dirs returns the directories used in the recording.
func (r *Replay) Dirs() *biome.Dirs {
return r.data.Dirs
}
// Run attempts to use the next invocation in the replay sequence, returning an
// error if the invocation parameters don't match. Once Run encounters a
// mismatched invocation, subsequent calls to Run will fail.
func (r *Replay) Run(ctx context.Context, invoke *biome.Invocation) error {
// Find invocation to replay.
invokeLine := strings.Join(invoke.Argv, " ")
r.mu.Lock()
if r.failed {
r.mu.Unlock()
return fmt.Errorf("run replay: `%s`: replay aborted due to previous failure", invokeLine)
}
if r.invokeIndex >= len(r.data.Invocations) {
r.failed = true
r.mu.Unlock()
return fmt.Errorf("run replay: `%s`: ran out of recorded commands", invokeLine)
}
recorded := r.data.Invocations[r.invokeIndex]
if !stringSliceEqual(invoke.Argv, recorded.Argv) {
r.failed = true
r.mu.Unlock()
return fmt.Errorf("run replay: `%s`: expected `%s` at index %d", invokeLine, strings.Join(recorded.Argv, " "), r.invokeIndex)
}
r.invokeIndex++
r.mu.Unlock()
// Replay recorded invocation.
log.Debugf(ctx, "Replay run: %s", invokeLine)
if invoke.Stdin == nil {
if len(recorded.StdinSHA256) > 0 {
return fmt.Errorf("run replay: `%s`: stdin not provided", invokeLine)
}
} else {
if len(recorded.StdinSHA256) == 0 {
return fmt.Errorf("run replay: `%s`: stdin provided but not expected", invokeLine)
}
h := sha256.New()
if _, err := io.Copy(h, invoke.Stdin); err != nil {
return fmt.Errorf("run replay: `%s`: read stdin: %w", invokeLine, err)
}
if got := h.Sum(nil); !bytes.Equal(got, recorded.StdinSHA256) {
return fmt.Errorf("run replay: `%s`: read stdin: does not match recorded input", invokeLine)
}
}
if want := recorded.env(); !envEqual(invoke.Env, want) {
return fmt.Errorf("run replay: `%s`: environment:\n%v\n; expected:\n%s",
invokeLine, invoke.Env, want)
}
if recorded.Output == nil {
if invoke.Stdout != nil || invoke.Stderr != nil {
return fmt.Errorf("run replay: `%s`: output requested but recording does not include output", invokeLine)
}
} else {
if isCombinedOutput(invoke.Stdout, invoke.Stderr) {
if recorded.Output.combined == nil {
return fmt.Errorf("run replay: `%s`: combined output requested but recording has separated output", invokeLine)
}
if _, err := invoke.Stdout.Write(recorded.Output.combined); err != nil {
return fmt.Errorf("run replay: `%s`: write combined output: %w", invokeLine, err)
}
} else if recorded.Output.combined != nil {
return fmt.Errorf("run replay: `%s`: separated output requested but recording has combined output", invokeLine)
} else {
if invoke.Stdout != nil {
if recorded.Output.stdout == nil {
return fmt.Errorf("run replay: `%s`: stdout requested but recording does not have stdout", invokeLine)
}
if _, err := invoke.Stdout.Write(recorded.Output.stdout); err != nil {
return fmt.Errorf("run replay: `%s`: write stdout: %w", invokeLine, err)
}
}
if invoke.Stderr != nil {
if recorded.Output.stderr == nil {
return fmt.Errorf("run replay: `%s`: stderr requested but recording does not have stderr", invokeLine)
}
if _, err := invoke.Stderr.Write(recorded.Output.stderr); err != nil {
return fmt.Errorf("run replay: `%s`: write stderr: %w", invokeLine, err)
}
}
}
}
if recorded.Error != "" {
return errors.New(recorded.Error)
}
return nil
}
func (r *Replay) JoinPath(elem ...string) string {
if len(elem) == 0 {
return ""
}
for _, possible := range r.data.JoinedPaths {
if stringSliceEqual(possible.Elems, elem) {
return possible.Result
}
}
return "__UNKNOWN_JOIN_PATH__"
}
func (r *Replay) IsAbsPath(path string) bool {
absPath, ok := r.data.AbsPaths[path]
if !ok {
return false
}
return absPath
}
type hexString []byte
func (h hexString) MarshalText() ([]byte, error) {
buf := make([]byte, hex.EncodedLen(len(h)))
hex.Encode(buf, h)
return buf, nil
}
func (h *hexString) UnmarshalText(text []byte) error {
*h = make([]byte, hex.DecodedLen(len(text)))
_, err := hex.Decode(*h, text)
return err
}
func (h hexString) String() string {
return hex.EncodeToString(h)
}
func envEqual(e1, e2 biome.Environment) bool {
return mapEqual(e1.Vars, e2.Vars) &&
stringSliceEqual(e1.PrependPath, e2.PrependPath) &&
stringSliceEqual(e1.AppendPath, e2.AppendPath)
}
func stringSliceEqual(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}
func mapEqual(m1, m2 map[string]string) bool {
if len(m1) != len(m2) {
return false
}
for k, v1 := range m1 {
if v2, ok := m2[k]; !ok || v1 != v2 {
return false
}
}
return true
}