-
Notifications
You must be signed in to change notification settings - Fork 145
/
testutils.go
56 lines (48 loc) · 1.22 KB
/
testutils.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
package testutils
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"io"
"log"
"os"
"testing"
pb "github.com/buchgr/bazel-remote/v2/genproto/build/bazel/remote/execution/v2"
)
// TempDir creates a temporary directory and returns its name. If an error
// occurs, then it panics.
func TempDir(t *testing.T) string {
dir, err := os.MkdirTemp("", "bazel-remote")
if err != nil {
t.Fatal(err)
}
return dir
}
// RandomDataAndHash creates a random blob of the specified size, and
// returns that blob along with its sha256 hash.
func RandomDataAndHash(size int64) ([]byte, string) {
data := make([]byte, size)
for i := 0; i < 3; i++ {
// This is not expected to fail, but hopefully it convinces
// linters that we checked for errors.
_, err := rand.Read(data)
if err == nil {
break
}
}
hash := sha256.Sum256(data)
hashStr := hex.EncodeToString(hash[:])
return data, hashStr
}
func RandomDataAndDigest(size int64) ([]byte, pb.Digest) {
data, hash := RandomDataAndHash(size)
return data, pb.Digest{
Hash: hash,
SizeBytes: size,
}
}
// NewSilentLogger returns a cheap logger that doesn't print anything, useful
// for tests.
func NewSilentLogger() *log.Logger {
return log.New(io.Discard, "", 0)
}