-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
92 lines (71 loc) · 1.87 KB
/
storage_test.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
package hibp
import (
"bytes"
"github.com/klauspost/compress/zstd"
"io"
"os"
"strings"
"testing"
)
func TestFSStorage(t *testing.T) {
testWriteRead := func(t *testing.T, useCompression bool) {
key := "key"
tmpDir := t.TempDir()
storage := newFSStorage(tmpDir, !useCompression)
err := storage.Save(key, "etag", []byte("data"))
if err != nil {
t.Fatalf("could not write: %v", err)
}
// First, let's check the raw file
var reader io.Reader
// We have to explicitly refer to the upper-cased key as Save/Load perform this operation internally.
// Depending on the file system that does make a difference.
file, err := os.Open(storage.filePath(strings.ToUpper(key)))
if err != nil {
t.Fatalf("could not open file: %v", err)
}
defer file.Close()
reader = file
if useCompression {
dec, err := zstd.NewReader(file)
if err != nil {
t.Fatalf("could not create decompressor: %v", err)
}
defer dec.Close()
reader = dec
}
raw, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("could not read file: %v", err)
}
if !bytes.Equal([]byte("etag\ndata"), raw) {
t.Fatalf("unexpected data: %q", raw)
}
// Then, let's check the API
etag, err := storage.LoadETag(key)
if err != nil {
t.Fatalf("could not read etag: %v", err)
}
if etag != "etag" {
t.Fatalf("unexpected etag: %q", etag)
}
readCloser, err := storage.LoadData(key)
if err != nil {
t.Fatalf("could not open reader: %v", err)
}
defer readCloser.Close()
all, err := io.ReadAll(readCloser)
if err != nil {
t.Fatalf("could not read file: %v", err)
}
if !bytes.Equal([]byte("data"), all) {
t.Fatalf("unexpected data: %q", all)
}
}
t.Run("write and read data without compression", func(t *testing.T) {
testWriteRead(t, false)
})
t.Run("write and read data with compression", func(t *testing.T) {
testWriteRead(t, true)
})
}