-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader_test.go
86 lines (69 loc) · 1.77 KB
/
uploader_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
package main
import (
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/ipfs/go-cid"
_ "github.com/ipfs/go-unixfsnode/file"
"github.com/ipld/go-car/v2/blockstore"
"github.com/ipld/go-car/v2/storage"
"github.com/stretchr/testify/require"
)
// TestUploader uses a mock to test a lot of internal things that should be happening under the hood.
func TestUploader(t *testing.T) {
client := &mockClient{t: t}
uploader := &Uploader{
w3s: client,
tmpDir: t.TempDir(),
}
_, err := uploader.Upload(context.Background(), strings.NewReader("Hello"))
require.NoError(t, err)
// check that the tmp files were removed
_, err = os.Stat(client.dest)
require.True(t, os.IsNotExist(err))
_, err = os.Stat(fmt.Sprintf("%s.car", client.dest))
require.True(t, os.IsNotExist(err))
}
type mockClient struct {
t *testing.T
dest string
}
func (c *mockClient) upload(_ cid.Cid, dest string) (cid.Cid, error) {
c.dest = dest
// check tmp file exists
_, err := os.Stat(dest)
require.NoError(c.t, err)
// check tmp car file exists
_, err = os.Stat(fmt.Sprintf("%s.car", dest))
require.NoError(c.t, err)
// check content being uploaded
content, err := extract(fmt.Sprintf("%s.car", dest))
require.NoError(c.t, err)
require.Equal(c.t, "Hello", content)
return cid.Cid{}, nil
}
func extract(filename string) (string, error) {
bs, err := blockstore.OpenReadOnly(filename)
if err != nil {
return "", err
}
carFile, err := os.Open(filename)
if err != nil {
return "", err
}
store, err := storage.OpenReadable(carFile)
if err != nil {
return "", err
}
blkCid, err := cid.Parse(store.Roots()[0].String())
if err != nil {
return "", err
}
blk, err := bs.Get(context.Background(), blkCid)
if err != nil {
return "", err
}
return string(blk.RawData()), nil
}