Debian Codegraph @

cmd/desync/untar_test.go

raw
1 //go:build !windows
2
3 package main
4
5 import (
6 "context"
7 "fmt"
8 "os"
9 "path"
10 "testing"
11
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestUntarCommandArchive(t *testing.T) {
16 // Create an output dir to extract into
17 out := t.TempDir()
18
19 // Run "untar" command to unpack an archive
20 cmd := newUntarCommand(context.Background())
21 cmd.SetArgs([]string{"--no-same-owner", "--no-same-permissions", "testdata/tree.catar", out})
22 _, err := cmd.ExecuteC()
23 require.NoError(t, err)
24 }
25
26 func TestUntarCommandIndex(t *testing.T) {
27 // Create an output dir to extract into
28 out := t.TempDir()
29
30 // Run "untar" to extract from a caidx index
31 cmd := newUntarCommand(context.Background())
32 cmd.SetArgs([]string{"-s", "testdata/tree.store", "-i", "--no-same-owner", "--no-same-permissions", "testdata/tree.caidx", out})
33 _, err := cmd.ExecuteC()
34 require.NoError(t, err)
35 }
36
37 // Check that we repair broken chunks in cache
38 func TestUntarCommandRepair(t *testing.T) {
39 // Create an output dir to extract into
40 out := t.TempDir()
41
42 // Create cache with invalid chunk by reading a chunk from another store, and writing it to the cache with the wrong id
43 cache := t.TempDir()
44
45 chunkId := "0589328ff916d08f5fe59a9aa0731571448e91341f37ca5484a85b9f0af14de3"
46 badChunkHash := "0b2a199263ffb2600b6f8be2e03b7439ffb0ad05a00b867f427a716e3e386c2d"
47 err := os.Mkdir(path.Join(cache, chunkId[:4]), os.ModePerm)
48 require.NoError(t, err)
49 b, err := os.ReadFile(path.Join("testdata/blob1.store", badChunkHash[:4], badChunkHash+".cacnk"))
50 require.NoError(t, err)
51 err = os.WriteFile(path.Join(cache, chunkId[:4], chunkId+".cacnk"), b, os.ModePerm)
52 require.NoError(t, err)
53
54 // Run "untar" with "--repair=false" -> get error
55 cmd := newUntarCommand(context.Background())
56 cmd.SetArgs([]string{"-s", "testdata/tree.store", "-c", cache, "--cache-repair=false", "-i", "--no-same-owner", "--no-same-permissions", "testdata/tree.caidx", out})
57 _, err = cmd.ExecuteC()
58 require.EqualError(t, err, fmt.Sprintf("chunk id %s does not match its hash %s", chunkId, badChunkHash))
59
60 // Now run "untar" with "--repair=true" -> no error
61 cmd = newUntarCommand(context.Background())
62 cmd.SetArgs([]string{"-s", "testdata/tree.store", "-c", cache, "--cache-repair=true", "-i", "--no-same-owner", "--no-same-permissions", "testdata/tree.caidx", out})
63 _, err = cmd.ExecuteC()
64 require.NoError(t, err)
65 }