| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/sha256" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"sync" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMountIndex(t *testing.T) { |
| 16 |
// Create the mount point |
| 17 |
mnt := t.TempDir() |
| 18 |
|
| 19 |
// Define the store |
| 20 |
s, err := NewLocalStore("testdata/blob1.store", StoreOptions{}) |
| 21 |
require.NoError(t, err) |
| 22 |
defer s.Close() |
| 23 |
|
| 24 |
// Read the index |
| 25 |
f, err := os.Open("testdata/blob1.caibx") |
| 26 |
require.NoError(t, err) |
| 27 |
defer f.Close() |
| 28 |
index, err := IndexFromReader(f) |
| 29 |
require.NoError(t, err) |
| 30 |
|
| 31 |
// Calculate the expected hash |
| 32 |
b, err := os.ReadFile("testdata/blob1") |
| 33 |
require.NoError(t, err) |
| 34 |
wantHash := sha256.Sum256(b) |
| 35 |
|
| 36 |
// Make sure that the unmount happens on exit |
| 37 |
var wg sync.WaitGroup |
| 38 |
wg.Add(1) |
| 39 |
ctx, cancel := context.WithCancel(context.Background()) |
| 40 |
defer func() { |
| 41 |
cancel() |
| 42 |
wg.Wait() |
| 43 |
}() |
| 44 |
|
| 45 |
// Start the Fuse mount |
| 46 |
c := make(chan error, 1) |
| 47 |
go func() { |
| 48 |
ifs := NewIndexMountFS(index, "blob1", s) |
| 49 |
c <- MountIndex(ctx, index, ifs, mnt, s, 10) |
| 50 |
wg.Done() |
| 51 |
}() |
| 52 |
|
| 53 |
select { |
| 54 |
case err = <-c: |
| 55 |
require.FailNow(t, "mount exited early", "%v", err) |
| 56 |
case <-time.After(time.Second): |
| 57 |
} |
| 58 |
|
| 59 |
// Calculate the hash of the file in the mount point |
| 60 |
b, err = os.ReadFile(filepath.Join(mnt, "blob1")) |
| 61 |
require.NoError(t, err) |
| 62 |
gotHash := sha256.Sum256(b) |
| 63 |
|
| 64 |
// Compare the checksums |
| 65 |
require.Equal(t, wantHash, gotHash) |
| 66 |
} |