Skip to content

Commit c03ccb8

Browse files
committed
integration: add runCmd and randomString utils
Signed-off-by: CrazyMax <[email protected]>
1 parent 3d93d64 commit c03ccb8

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

util/testutil/integration/util.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package integration
33
import (
44
"bytes"
55
"context"
6+
"crypto/rand"
67
"fmt"
78
"io"
89
"net"
@@ -20,17 +21,20 @@ import (
2021
"golang.org/x/sync/errgroup"
2122
)
2223

24+
func runCmd(cmd *exec.Cmd, logs map[string]*bytes.Buffer) error {
25+
if logs != nil {
26+
setCmdLogs(cmd, logs)
27+
}
28+
fmt.Fprintf(cmd.Stderr, "> runCmd %v %+v\n", time.Now(), cmd.String())
29+
return cmd.Run()
30+
}
31+
2332
func startCmd(cmd *exec.Cmd, logs map[string]*bytes.Buffer) (func() error, error) {
2433
if logs != nil {
25-
b := new(bytes.Buffer)
26-
logs["stdout: "+cmd.Path] = b
27-
cmd.Stdout = &lockingWriter{Writer: b}
28-
b = new(bytes.Buffer)
29-
logs["stderr: "+cmd.Path] = b
30-
cmd.Stderr = &lockingWriter{Writer: b}
34+
setCmdLogs(cmd, logs)
3135
}
3236

33-
fmt.Fprintf(cmd.Stderr, "> startCmd %v %+v\n", time.Now(), cmd.Args)
37+
fmt.Fprintf(cmd.Stderr, "> startCmd %v %+v\n", time.Now(), cmd.String())
3438

3539
if err := cmd.Start(); err != nil {
3640
return nil, err
@@ -75,6 +79,15 @@ func startCmd(cmd *exec.Cmd, logs map[string]*bytes.Buffer) (func() error, error
7579
}, nil
7680
}
7781

82+
func setCmdLogs(cmd *exec.Cmd, logs map[string]*bytes.Buffer) {
83+
b := new(bytes.Buffer)
84+
logs["stdout: "+cmd.String()] = b
85+
cmd.Stdout = &lockingWriter{Writer: b}
86+
b = new(bytes.Buffer)
87+
logs["stderr: "+cmd.String()] = b
88+
cmd.Stderr = &lockingWriter{Writer: b}
89+
}
90+
7891
func waitUnix(address string, d time.Duration) error {
7992
address = strings.TrimPrefix(address, "unix://")
8093
addr, err := net.ResolveUnixAddr("unix", address)
@@ -167,3 +180,13 @@ func Tmpdir(t *testing.T, appliers ...fstest.Applier) (string, error) {
167180
}
168181
return tmpdir, nil
169182
}
183+
184+
func randomString(n int) string {
185+
chars := "abcdefghijklmnopqrstuvwxyz"
186+
var b = make([]byte, n)
187+
_, _ = rand.Read(b)
188+
for k, v := range b {
189+
b[k] = chars[v%byte(len(chars))]
190+
}
191+
return string(b)
192+
}

0 commit comments

Comments
 (0)