Skip to content

Commit

Permalink
chore: test fixtures for various unixfs queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jun 27, 2023
1 parent 00b54cc commit 461d7a0
Show file tree
Hide file tree
Showing 9 changed files with 671 additions and 25 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/ipfs/go-ipfs-delay v0.0.1
github.com/ipfs/go-ipfs-exchange-interface v0.2.0
github.com/ipfs/go-ipld-format v0.5.0
github.com/ipfs/go-libipfs v0.6.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipfs/go-unixfsnode v1.7.1
github.com/ipld/go-car/v2 v2.10.1
Expand All @@ -37,6 +36,7 @@ require (
github.com/multiformats/go-multihash v0.2.3
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.24.4
github.com/warpfork/go-testmark v0.12.1
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/trace v1.14.0
go.uber.org/multierr v1.11.0
Expand Down Expand Up @@ -84,6 +84,7 @@ require (
github.com/ipfs/go-bitfield v1.1.0 // indirect
github.com/ipfs/go-ipfs-chunker v0.0.5 // indirect
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
github.com/ipfs/go-ipfs-files v0.3.0 // indirect
github.com/ipfs/go-ipfs-pq v0.0.3 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipld-cbor v0.0.6 // indirect
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ github.com/ipfs/go-ipfs-exchange-interface v0.2.0 h1:8lMSJmKogZYNo2jjhUs0izT+dck
github.com/ipfs/go-ipfs-exchange-interface v0.2.0/go.mod h1:z6+RhJuDQbqKguVyslSOuVDhqF9JtTrO3eptSAiW2/Y=
github.com/ipfs/go-ipfs-exchange-offline v0.3.0 h1:c/Dg8GDPzixGd0MC8Jh6mjOwU57uYokgWRFidfvEkuA=
github.com/ipfs/go-ipfs-files v0.3.0 h1:fallckyc5PYjuMEitPNrjRfpwl7YFt69heCOUhsbGxQ=
github.com/ipfs/go-ipfs-files v0.3.0/go.mod h1:xAUtYMwB+iu/dtf6+muHNSFQCJG2dSiStR2P6sn9tIM=
github.com/ipfs/go-ipfs-posinfo v0.0.1 h1:Esoxj+1JgSjX0+ylc0hUmJCOv6V2vFoZiETLR6OtpRs=
github.com/ipfs/go-ipfs-pq v0.0.3 h1:YpoHVJB+jzK15mr/xsWC574tyDLkezVrDNeaalQBsTE=
github.com/ipfs/go-ipfs-pq v0.0.3/go.mod h1:btNw5hsHBpRcSSgZtiNm/SLj5gYIZ18AKtv3kERkRb4=
github.com/ipfs/go-ipfs-routing v0.3.0 h1:9W/W3N+g+y4ZDeffSgqhgo7BsBSJwPMcyssET9OWevc=
github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc=
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
Expand All @@ -312,7 +312,6 @@ github.com/ipfs/go-ipld-format v0.5.0 h1:WyEle9K96MSrvr47zZHKKcDxJ/vlpET6PSiQsAF
github.com/ipfs/go-ipld-format v0.5.0/go.mod h1:ImdZqJQaEouMjCvqCe0ORUS+uoBmf7Hf+EO/jh+nk3M=
github.com/ipfs/go-ipld-legacy v0.2.1 h1:mDFtrBpmU7b//LzLSypVrXsD8QxkEWxu5qVxN99/+tk=
github.com/ipfs/go-libipfs v0.6.0 h1:3FuckAJEm+zdHbHbf6lAyk0QUzc45LsFcGw102oBCZM=
github.com/ipfs/go-libipfs v0.6.0/go.mod h1:UjjDIuehp2GzlNP0HEr5I9GfFT7zWgst+YfpUEIThtw=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/ipfs/go-log v1.0.0/go.mod h1:JO7RzlMK6rA+CIxFMLOuB6Wf5b81GDiKElL7UPSIKjA=
github.com/ipfs/go-log v1.0.1/go.mod h1:HuWlQttfN6FWNHRhlY5yMk/lW7evQC0HHGOxEwMRR8I=
Expand Down Expand Up @@ -614,7 +613,9 @@ github.com/urfave/cli/v2 v2.24.4 h1:0gyJJEBYtCV87zI/x2nZCPyDxD51K6xM8SkwjHFCNEU=
github.com/urfave/cli/v2 v2.24.4/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/warpfork/go-fsx v0.3.0/go.mod h1:oTACCMj+Zle+vgVa5SAhGAh7WksYpLgGUCKEAVc+xPg=
github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s=
github.com/warpfork/go-testmark v0.12.1/go.mod h1:kHwy7wfvGSPh1rQJYKayD4AbtNaeyZdcGi9tNJTaa5Y=
github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
github.com/warpfork/go-wish v0.0.0-20190328234359-8b3e70f8e830/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ=
Expand Down
94 changes: 94 additions & 0 deletions pkg/internal/fixtures/testcases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package fixtures

import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
)

type TestCase struct {
Name string
Root cid.Cid
Path string
Scope types.DagScope
Duplicates bool
ByteRange *types.ByteRange
ExpectedCids []cid.Cid
}

func (tc TestCase) AsQuery() string {
pp := ipld.ParsePath(tc.Path).String()
if pp != "" {
pp = "/" + pp
}
br := ""
if tc.ByteRange != nil && !tc.ByteRange.IsDefault() {
br = fmt.Sprintf("&entity-bytes=%s", tc.ByteRange.String())
}
dup := ""
if tc.Duplicates {
dup = "&dups=y"
}
return fmt.Sprintf("/ipfs/%s%s?dag-scope=%s%s%s", tc.Root, pp, tc.Scope, br, dup)
}

func ParseCase(name, spec, exec string) (TestCase, error) {
lines := strings.Split(exec, "\n")
for len(lines) > 0 && strings.TrimSpace(lines[0]) == "" {
lines = lines[1:]
}
for len(lines) > 0 && strings.TrimSpace(lines[len(lines)-1]) == "" {
lines = lines[:len(lines)-1]
}
specParts := strings.Split(strings.TrimSpace(spec), "?")
if len(specParts) != 2 {
return TestCase{}, errors.New("invalid spec")
}
spec = specParts[0]
query, err := url.ParseQuery(specParts[1])
if err != nil {
return TestCase{}, err
}
specParts = strings.Split(spec, "/")
if specParts[0] != "" && specParts[1] != "ipfs" {
return TestCase{}, errors.New("invalid spec")
}
root, err := cid.Parse(specParts[2])
if err != nil {
return TestCase{}, err
}
path := "/" + ipld.ParsePath(strings.Join(specParts[3:], "/")).String()
scope, err := types.ParseDagScope(query.Get("dag-scope")) // required
if err != nil {
return TestCase{}, err
}
duplicates := query.Get("dups") == "y"
var byteRange *types.ByteRange
if query.Get("byte-range") != "" {
if br, err := types.ParseByteRange(query.Get("byte-range")); err != nil {
return TestCase{}, err
} else {
byteRange = &br
}
}
expectedCids := make([]cid.Cid, 0, len(lines))
for _, line := range lines {
la := strings.Split(line, "|")
c := cid.MustParse(strings.TrimSpace(la[0]))
expectedCids = append(expectedCids, c)
}
return TestCase{
Name: name,
Root: root,
Path: path,
Scope: scope,
Duplicates: duplicates,
ByteRange: byteRange,
ExpectedCids: expectedCids,
}, nil
}
75 changes: 75 additions & 0 deletions pkg/internal/fixtures/unixfs_20m_variety.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package fixtures

import (
"fmt"
"io"
"os"
"strings"

"github.com/ipfs/go-cid"
carstorage "github.com/ipld/go-car/v2/storage"
"github.com/ipld/go-ipld-prime/storage"
"github.com/warpfork/go-testmark"
)

var Unixfs20mVarietyRoot = cid.MustParse("bafybeifrrglx2issn2had5rtstn3xltla6vxmpjfwfz7o3hapvkynh4zoq")

const file = "internal/testdata/unixfs_20m_variety."

func filepath(typ string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
rootInd := strings.LastIndex(wd, "/lassie/pkg/")
if rootInd == -1 {
return "", fmt.Errorf("could not find root of lassie package")
}
filename := wd[:rootInd] + "/lassie/pkg/" + file + typ
fmt.Println("Using", filename)
return filename, nil
}

func Unixfs20mVarietyReadableStorage() (storage.ReadableStorage, io.Closer, error) {
file, err := filepath("car")
if err != nil {
return nil, nil, err
}
carFile, err := os.Open(file)
if err != nil {
return nil, nil, err
}
reader, err := carstorage.OpenReadable(carFile)
if err != nil {
carFile.Close()
return nil, nil, err
}
return reader, carFile, nil
}

func Unixfs20mVarietyCases() ([]TestCase, error) {
file, err := filepath("md")
if err != nil {
return nil, err
}
doc, err := testmark.ReadFile(file)
if err != nil {
return nil, err
}
doc.BuildDirIndex()
testCases := make([]TestCase, 0)
for _, test := range doc.DirEnt.Children["test"].ChildrenList {
for _, scope := range test.ChildrenList {
tc, err := ParseCase(test.Name+"/"+scope.Name, dstr(scope, "query"), dstr(scope, "execution"))
if err != nil {
return nil, err
}
testCases = append(testCases, tc)
}
}
return testCases, nil
}

func dstr(dir *testmark.DirEnt, ch string) string {
return string(dir.Children[ch].Hunk.Body)
}
Loading

0 comments on commit 461d7a0

Please sign in to comment.