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
|
package backup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestFilesystemSink_GetReader(t *testing.T) {
t.Run("ok", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
dir := testhelper.TempDir(t)
const relativePath = "test.dat"
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, relativePath), []byte("test"), 0o644))
fsSink := NewFilesystemSink(dir)
reader, err := fsSink.GetReader(ctx, relativePath)
require.NoError(t, err)
defer func() { require.NoError(t, reader.Close()) }()
data, err := ioutil.ReadAll(reader)
require.NoError(t, err)
require.Equal(t, []byte("test"), data)
})
t.Run("no file", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
dir, err := os.Getwd()
require.NoError(t, err)
fsSink := NewFilesystemSink(dir)
reader, err := fsSink.GetReader(ctx, "not-existing")
require.Equal(t, fmt.Errorf(`filesystem sink: get reader for "not-existing": %w`, ErrDoesntExist), err)
require.Nil(t, reader)
})
}
func TestFilesystemSink_Write(t *testing.T) {
t.Run("ok", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
dir := testhelper.TempDir(t)
const relativePath = "nested/dir/test.dat"
fsSink := NewFilesystemSink(dir)
require.NoError(t, fsSink.Write(ctx, relativePath, strings.NewReader("test")))
require.FileExists(t, filepath.Join(dir, relativePath))
data, err := ioutil.ReadFile(filepath.Join(dir, relativePath))
require.NoError(t, err)
require.Equal(t, []byte("test"), data)
})
t.Run("no data doesn't create a file", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
dir := testhelper.TempDir(t)
const relativePath = "nested/dir/test.dat"
fsSink := NewFilesystemSink(dir)
require.NoError(t, fsSink.Write(ctx, relativePath, strings.NewReader("")))
require.NoFileExists(t, filepath.Join(dir, relativePath))
})
t.Run("overrides existing data", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
dir := testhelper.TempDir(t)
const relativePath = "nested/dir/test.dat"
fullPath := filepath.Join(dir, relativePath)
require.NoError(t, os.MkdirAll(filepath.Dir(fullPath), 0o755))
require.NoError(t, ioutil.WriteFile(fullPath, []byte("initial"), 0o655))
fsSink := NewFilesystemSink(dir)
require.NoError(t, fsSink.Write(ctx, relativePath, strings.NewReader("test")))
require.FileExists(t, fullPath)
data, err := ioutil.ReadFile(fullPath)
require.NoError(t, err)
require.Equal(t, []byte("test"), data)
})
t.Run("dir creation error", func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
dir := testhelper.TempDir(t)
const relativePath = "nested/test.dat"
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "nested"), []byte("lock"), os.ModePerm))
fsSink := NewFilesystemSink(dir)
err := fsSink.Write(ctx, relativePath, strings.NewReader("test"))
require.EqualError(t, err, fmt.Sprintf(`create directory structure %[1]q: mkdir %[1]s: not a directory`, filepath.Join(dir, "nested")))
})
}
|