-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathstorage_test.go
97 lines (82 loc) · 2.22 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
93
94
95
96
97
package localstorage
import (
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/google/uuid"
)
func TestPut(t *testing.T) {
tdir := t.TempDir()
charmID := uuid.New().String()
buf := bytes.NewBufferString("")
lfs, err := NewLocalFileStore(tdir)
if err != nil {
t.Fatal(err)
}
paths := []string{"/", "///"}
for _, path := range paths {
err = lfs.Put(charmID, path, buf, fs.FileMode(0o644))
if err == nil {
t.Fatalf("expected error when file path is %s", path)
}
}
content := "hello world"
path := "/hello.txt"
t.Run(path, func(t *testing.T) {
buf = bytes.NewBufferString(content)
err = lfs.Put(charmID, path, buf, fs.FileMode(0o644))
if err != nil {
t.Fatalf("expected no error when file path is %s, %v", path, err)
}
file, err := os.Open(filepath.Join(tdir, charmID, path))
if err != nil {
t.Fatalf("expected no error when opening file %s", path)
}
defer file.Close() //nolint:errcheck
fileInfo, err := file.Stat()
if err != nil {
t.Fatalf("expected no error when getting file info for %s", path)
}
if fileInfo.IsDir() {
t.Fatalf("expected file %s to be a regular file", path)
}
read, err := io.ReadAll(file)
if err != nil {
t.Fatalf("expected no error when reading file %s", path)
}
if string(read) != content {
t.Fatalf("expected content to be %s, got %s", content, string(read))
}
})
content = "bar"
path = "/foo/hello.txt"
t.Run(path, func(t *testing.T) {
buf = bytes.NewBufferString(content)
err = lfs.Put(charmID, path, buf, fs.FileMode(0o644))
if err != nil {
t.Fatalf("expected no error when file path is %s, %v", path, err)
}
file, err := os.Open(filepath.Join(tdir, charmID, path))
if err != nil {
t.Fatalf("expected no error when opening file %s", path)
}
defer file.Close() //nolint:errcheck
fileInfo, err := file.Stat()
if err != nil {
t.Fatalf("expected no error when getting file info for %s", path)
}
if fileInfo.IsDir() {
t.Fatalf("expected file %s to be a regular file", path)
}
read, err := io.ReadAll(file)
if err != nil {
t.Fatalf("expected no error when reading file %s", path)
}
if string(read) != content {
t.Fatalf("expected content to be %s, got %s", content, string(read))
}
})
}