-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: hamt dir and range test as go test
- Loading branch information
Showing
9 changed files
with
210 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Dataset Description / Sources | ||
|
||
TestGatewayHAMTDirectory.car generated with: | ||
|
||
```bash | ||
ipfs version | ||
# ipfs version 0.19.0 | ||
|
||
export HAMT_DIR=bafybeiggvykl7skb2ndlmacg2k5modvudocffxjesexlod2pfvg5yhwrqm | ||
export IPFS_PATH=$(mktemp -d) | ||
|
||
# Init and start daemon, ensure we have an empty repository. | ||
ipfs init --empty-repo | ||
ipfs daemon &> /dev/null & | ||
export IPFS_PID=$! | ||
|
||
# Retrieve the directory listing, forcing the daemon to download all required DAGs. Kill daemon. | ||
curl -o dir.html http://127.0.0.1:8080/ipfs/$HAMT_DIR/ | ||
kill $IPFS_PID | ||
|
||
# Get the list with all the downloaded refs and sanity check. | ||
ipfs refs local > required_refs | ||
cat required_refs | wc -l | ||
# 962 | ||
|
||
# Get the list of all the files CIDs inside the directory and sanity check. | ||
cat dir.html| pup '#content tbody .ipfs-hash attr{href}' | sed 's/\/ipfs\///g;s/\?filename=.*//g' > files_refs | ||
cat files_refs | wc -l | ||
# 10100 | ||
|
||
# Make and export our fixture. | ||
ipfs files mkdir --cid-version 1 /fixtures | ||
cat required_refs | xargs -I {} ipfs files cp /ipfs/{} /fixtures/{} | ||
cat files_refs | ipfs files write --create /fixtures/files_refs | ||
export FIXTURE_CID=$(ipfs files stat --hash /fixtures/) | ||
echo $FIXTURE_CID | ||
# bafybeig3yoibxe56aolixqa4zk55gp5sug3qgaztkakpndzk2b2ynobd4i | ||
ipfs dag export $FIXTURE_CID > TestGatewayHAMTDirectory.car | ||
``` | ||
|
||
TestGatewayMultiRange.car generated with: | ||
|
||
|
||
```sh | ||
ipfs version | ||
# ipfs version 0.19.0 | ||
|
||
export FILE_CID=bafybeibkzwf3ffl44yfej6ak44i7aly7rb4udhz5taskreec7qemmw5jiu | ||
export IPFS_PATH=$(mktemp -d) | ||
|
||
# Init and start daemon, ensure we have an empty repository. | ||
ipfs init --empty-repo | ||
ipfs daemon &> /dev/null & | ||
export IPFS_PID=$! | ||
|
||
# Get a specific byte range from the file. | ||
curl http://127.0.0.1:8080/ipfs/$FILE_CID -i -H "Range: bytes=2000-2002, 40000000000-40000000002" | ||
kill $IPFS_PID | ||
|
||
# Get the list with all the downloaded refs and sanity check. | ||
ipfs refs local > required_refs | ||
cat required_refs | wc -l | ||
# 48 | ||
|
||
# Make and export our fixture. | ||
ipfs files mkdir --cid-version 1 /fixtures | ||
cat required_refs | xargs -I {} ipfs files cp /ipfs/{} /fixtures/{} | ||
export FIXTURE_CID=$(ipfs files stat --hash /fixtures/) | ||
echo $FIXTURE_CID | ||
# bafybeihqs4hdx64a6wmrclp3a2pwxkd5prwdos45bdftpegls5ktzspi7a | ||
ipfs dag export $FIXTURE_CID > TestGatewayMultiRange.car | ||
``` |
Binary file renamed
BIN
+764 KB
...s/t0115-gateway-dir-listing/hamt-refs.car → ...cli/fixtures/TestGatewayHAMTDirectory.car
Binary file not shown.
Binary file renamed
BIN
+3.52 MB
test/sharness/t0110-gateway/hamt-refs.car → test/cli/fixtures/TestGatewayMultiRange.car
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ipfs/kubo/test/cli/harness" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGatewayHAMTDirectory(t *testing.T) { | ||
t.Parallel() | ||
|
||
const ( | ||
// The CID of the HAMT-sharded directory that has 10k items | ||
hamtCid = "bafybeiggvykl7skb2ndlmacg2k5modvudocffxjesexlod2pfvg5yhwrqm" | ||
|
||
// fixtureCid is the CID of root of the DAG that is a subset of hamtCid DAG | ||
// representing the minimal set of blocks necessary for directory listing. | ||
// It also includes a "files_refs" file with the list of the references | ||
// we do NOT needs to fetch (files inside the directory) | ||
fixtureCid = "bafybeig3yoibxe56aolixqa4zk55gp5sug3qgaztkakpndzk2b2ynobd4i" | ||
) | ||
|
||
// Start node | ||
h := harness.NewT(t) | ||
node := h.NewNode().Init("--empty-repo", "--profile=test").StartDaemon("--offline") | ||
client := node.GatewayClient() | ||
|
||
// Import fixtures | ||
r, err := os.Open("./fixtures/TestGatewayHAMTDirectory.car") | ||
assert.Nil(t, err) | ||
defer r.Close() | ||
cid := node.IPFSDagImport(r) | ||
assert.Equal(t, fixtureCid, cid) | ||
|
||
t.Run("Fetch HAMT directory succeeds with minimal refs", func(t *testing.T) { | ||
t.Parallel() | ||
resp := client.Get(fmt.Sprintf("/ipfs/%s/", hamtCid)) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
}) | ||
|
||
t.Run("Non-minimal refs are not present in the repository", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Fetch list with refs of files that should NOT be available locally. | ||
resp := client.Get(fmt.Sprintf("/ipfs/%s/files_refs", fixtureCid)) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
files := strings.Split(strings.TrimSpace(resp.Body), "\n") | ||
assert.Len(t, files, 10100) | ||
|
||
// Shuffle the files list and try fetching the first 200. | ||
rand.Seed(time.Now().UnixNano()) | ||
rand.Shuffle(len(files), func(i, j int) { files[i], files[j] = files[j], files[i] }) | ||
for _, cid := range files[:200] { | ||
resp = client.Get(fmt.Sprintf("/ipfs/%s", cid)) | ||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
} | ||
}) | ||
} | ||
|
||
func TestGatewayMultiRange(t *testing.T) { | ||
t.Parallel() | ||
|
||
const ( | ||
// fileCid is the CID of the large HAMT-sharded file. | ||
fileCid = "bafybeibkzwf3ffl44yfej6ak44i7aly7rb4udhz5taskreec7qemmw5jiu" | ||
|
||
// fixtureCid is the CID of root of the DAG that is a subset of fileCid DAG | ||
// representing the minimal set of blocks necessary for a simple byte range request. | ||
fixtureCid = "bafybeihqs4hdx64a6wmrclp3a2pwxkd5prwdos45bdftpegls5ktzspi7a" | ||
) | ||
|
||
// Start node | ||
h := harness.NewT(t) | ||
node := h.NewNode().Init("--empty-repo", "--profile=test").StartDaemon("--offline") | ||
client := node.GatewayClient() | ||
|
||
// Import fixtures | ||
r, err := os.Open("./fixtures/TestGatewayMultiRange.car") | ||
assert.Nil(t, err) | ||
defer r.Close() | ||
cid := node.IPFSDagImport(r) | ||
assert.Equal(t, fixtureCid, cid) | ||
|
||
t.Run("Succeeds to fetch range of blocks we have", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
resp := client.Get(fmt.Sprintf("/ipfs/%s", fileCid), func(r *http.Request) { | ||
r.Header.Set("Range", "bytes=2000-2002, 40000000000-40000000002") | ||
}) | ||
assert.Equal(t, http.StatusPartialContent, resp.StatusCode) | ||
assert.Contains(t, resp.Body, "Content-Type: application/octet-stream") | ||
assert.Contains(t, resp.Body, "Content-Range: bytes 2000-2002/87186935127") | ||
assert.Contains(t, resp.Body, "Content-Range: bytes 40000000000-40000000002/87186935127") | ||
}) | ||
|
||
t.Run("Fail to fetch range of blocks we do not have", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
req, err := http.NewRequest(http.MethodGet, client.BuildURL(fmt.Sprintf("/ipfs/%s", fileCid)), nil) | ||
assert.Nil(t, err) | ||
req.Header.Set("Range", "bytes=1000-1100, 87186935125-87186935127") | ||
httpResp, err := client.Client.Do(req) | ||
assert.Nil(t, err) | ||
assert.Equal(t, http.StatusPartialContent, httpResp.StatusCode) | ||
_, err = io.ReadAll(httpResp.Body) | ||
assert.Equal(t, err, io.ErrUnexpectedEOF) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters