Skip to content

Commit

Permalink
feat: add multi-test and complete t0123
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed May 30, 2023
1 parent d9cce79 commit cc68f06
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 162 deletions.
66 changes: 66 additions & 0 deletions fixtures/t0123/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Dataset description/sources

- dag-cbor-traversal.car

- dag-json-traversal.car

- dag-pb.car

- dag-pb.json

- fixtures.car
- raw CARv1

generated with:

```sh
# using ipfs version 0.21.0-dev (03a98280e3e642774776cd3d0435ab53e5dfa867)

mkdir -p rootDir/ipfs &&
mkdir -p rootDir/ipns &&
mkdir -p rootDir/api &&
mkdir -p rootDir/ą/ę &&
echo "{ \"test\": \"i am a plain json file\" }" > rootDir/ą/ę/t.json &&
echo "I am a txt file on path with utf8" > rootDir/ą/ę/file-źł.txt &&
echo "I am a txt file in confusing /api dir" > rootDir/api/file.txt &&
echo "I am a txt file in confusing /ipfs dir" > rootDir/ipfs/file.txt &&
echo "I am a txt file in confusing /ipns dir" > rootDir/ipns/file.txt &&
DIR_CID=$(ipfs add -Qr --cid-version 1 rootDir) &&
FILE_JSON_CID=$(ipfs files stat --enc=json /ipfs/$DIR_CID/ą/ę/t.json | jq -r .Hash) &&
FILE_CID=$(ipfs files stat --enc=json /ipfs/$DIR_CID/ą/ę/file-źł.txt | jq -r .Hash) &&
FILE_SIZE=$(ipfs files stat --enc=json /ipfs/$DIR_CID/ą/ę/file-źł.txt | jq -r .Size)
echo "$FILE_CID / $FILE_SIZE"

echo DIR_CID=${DIR_CID} # ./rootDir
echo FILE_JSON_CID=${FILE_JSON_CID} # ./rootDir/ą/ę/t.json
echo FILE_CID=${FILE_CID} # ./rootDir/ą/ę/file-źł.txt
echo FILE_SIZE=${FILE_SIZE}

ipfs dag export ${DIR_CID} > fixtures.car

DAG_CBOR_TRAVERSAL_CID="bafyreibs4utpgbn7uqegmd2goqz4bkyflre2ek2iwv743fhvylwi4zeeim"
DAG_JSON_TRAVERSAL_CID="baguqeeram5ujjqrwheyaty3w5gdsmoz6vittchvhk723jjqxk7hakxkd47xq"
DAG_PB_CID="bafybeiegxwlgmoh2cny7qlolykdf7aq7g6dlommarldrbm7c4hbckhfcke"

test_native_dag() {
NAME=$1
CID=$2

IPNS_ID=$(ipfs key gen --ipns-base=base36 --type=ed25519 ${NAME}_test_key | head -n1 | tr -d "\n")
ipfs name publish --key ${NAME}_test_key --allow-offline --ttl=876600h --lifetime=876600h -Q "/ipfs/${CID}" > name_publish_out

ipfs routing get /ipns/${IPNS_ID} > ${IPNS_ID}.ipns-record

echo "IPNS_ID_${NAME}=${IPNS_ID}"
}

test_native_dag "DAG_JSON" "$DAG_JSON_TRAVERSAL_CID"
test_native_dag "DAG_CBOR" "$DAG_CBOR_TRAVERSAL_CID"

# DIR_CID=bafybeiafyvqlazbbbtjnn6how5d6h6l6rxbqc4qgpbmteaiskjrffmyy4a # ./rootDir
# FILE_JSON_CID=bafkreibrppizs3g7axs2jdlnjua6vgpmltv7k72l7v7sa6mmht6mne3qqe # ./rootDir/ą/ę/t.json
# FILE_CID=bafkreialihlqnf5uwo4byh4n3cmwlntwqzxxs2fg5vanqdi3d7tb2l5xkm # ./rootDir/ą/ę/file-źł.txt
# FILE_SIZE=34
# IPNS_ID_DAG_JSON=k51qzi5uqu5dhjghbwdvbo6mi40htrq6e2z4pwgp15pgv3ho1azvidttzh8yy2
# IPNS_ID_DAG_CBOR=k51qzi5uqu5dghjous0agrwavl8vzl64xckoqzwqeqwudfr74kfd11zcyk3b7l
```
29 changes: 25 additions & 4 deletions tests/t0123_gateway_json_cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ func TestGatewayJSONCborAndIPNS(t *testing.T) {
tests := SugarTests{}

for _, row := range table {
plain := car.MustOpenUnixfsCar(Fmt("t0123/plain.{{format}}.car", row.Format)).MustGetRoot()
plain := car.MustOpenUnixfsCar(Fmt("t0123/dag-{{format}}-traversal.car", row.Format)).MustGetRoot()
plainCID := plain.Cid()

// # IPNS behavior (should be same as immutable /ipfs, but with different caching headers)
Expand All @@ -849,14 +849,35 @@ func TestGatewayJSONCborAndIPNS(t *testing.T) {
// curl -sX GET "http://127.0.0.1:$GWAY_PORT/ipns/$IPNS_ID" -o ipns_output &&
// test_cmp ipfs_output ipns_output
// '
// TODO: compare outputs.

{
Name: Fmt("GET {{name}} from /ipns without explicit format returns the same payload as /ipfs", row.Name),
Requests: Requests(
Request().
Path("ipfs/{{cid}}", plainCID),
Request().
Path("ipns/{{id}}", row.fixture.Key()),
),
Responses: Responses().
HaveTheSamePayload(),
},
// test_expect_success "GET $name from /ipns without explicit format returns the same payload as /ipfs" '
// curl -sX GET "http://127.0.0.1:$GWAY_PORT/ipfs/$CID?format=dag-$format" -o ipfs_output &&
// curl -sX GET "http://127.0.0.1:$GWAY_PORT/ipns/$IPNS_ID?format=dag-$format" -o ipns_output &&
// test_cmp ipfs_output ipns_output
// '
// TODO: compare outputs.
{
Name: Fmt("GET {{name}} from /ipns with explicit format returns the same payload as /ipfs", row.Name),
Requests: Requests(
Request().
Path("ipfs/{{cid}}", plainCID).
Query("format", "dag-{{format}}", row.Format),
Request().
Path("ipns/{{id}}", row.fixture.Key()).
Query("format", "dag-{{format}}", row.Format),
),
Responses: Responses().
HaveTheSamePayload(),
},

// test_expect_success "GET $name from /ipns with explicit application/vnd.ipld.dag-$format has expected headers" '
// curl -svX GET -H "Accept: application/vnd.ipld.dag-$format" "http://127.0.0.1:$GWAY_PORT/ipns/$IPNS_ID" >/dev/null 2>curl_output &&
Expand Down
5 changes: 4 additions & 1 deletion tooling/test/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ func report(t *testing.T, test SugarTest, req *http.Request, res *http.Response,
var err error
switch v := v.(type) {
case *http.Request:
if v == nil {
return "nil" // golang does not catch the nil case above
}
b, err = httputil.DumpRequestOut(v, true)
case *http.Response:
if v == nil {
return "nil"
return "nil" // golang does not catch the nil case above
}
// TODO: we have to disable the body dump because
// it triggers an error:
Expand Down
106 changes: 106 additions & 0 deletions tooling/test/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package test

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
)

type Reporter func(t *testing.T, msg interface{}, rest ...interface{})

func runRequest(ctx context.Context, t *testing.T, test SugarTest, builder RequestBuilder) (*http.Request, *http.Response, Reporter) {
method := builder.Method_
if method == "" {
method = "GET"
}

// Prepare a client,
// use proxy, deal with redirects, etc.
client := &http.Client{}
if builder.UseProxyTunnel_ {
if builder.Proxy_ == "" {
t.Fatal("ProxyTunnel requires a proxy")
}

client = NewProxyTunnelClient(builder.Proxy_)
} else if builder.Proxy_ != "" {
client = NewProxyClient(builder.Proxy_)
}

if builder.DoNotFollowRedirects_ {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}

var res *http.Response = nil
var req *http.Request = nil

var localReport Reporter = func(t *testing.T, msg interface{}, rest ...interface{}) {
var err error
switch msg := msg.(type) {
case string:
err = fmt.Errorf(msg, rest...)
case error:
err = msg
default:
panic("msg must be string or error")
}

report(t, test, req, res, err)
}

var url string
if builder.URL_ != "" && builder.Path_ != "" {
localReport(t, "Both 'URL' and 'Path' are set")
}
if builder.URL_ == "" && builder.Path_ == "" {
localReport(t, "Neither 'URL' nor 'Path' are set")
}
if builder.URL_ != "" {
url = builder.URL_
}
if builder.Path_ != "" {
url = fmt.Sprintf("%s/%s", GatewayURL, builder.Path_)
}

query := builder.Query_.Encode()
if query != "" {
url = fmt.Sprintf("%s?%s", url, query)
}

var body io.Reader
if builder.Body_ != nil {
body = bytes.NewBuffer(builder.Body_)
}

// create a request
req, err := http.NewRequest(method, url, body)
if err != nil {
t.Fatal(err)
}

// add headers
for key, value := range builder.Headers_ {
req.Header.Add(key, value)

// https://github.com/golang/go/issues/7682
if key == "Host" {
req.Host = value
}
}

// send request
log.Debugf("Querying %s", url)
req = req.WithContext(ctx)

res, err = client.Do(req)
if err != nil {
localReport(t, "Querying %s failed: %s", url, err)
}

return req, res, localReport
}
21 changes: 21 additions & 0 deletions tooling/test/sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func Request() RequestBuilder {
Query_: make(url.Values)}
}

func Requests(rs ...RequestBuilder) []RequestBuilder {
return rs
}

func (r RequestBuilder) Path(path string, args ...any) RequestBuilder {
r.Path_ = tmpl.Fmt(path, args...)
return r
Expand Down Expand Up @@ -98,6 +102,10 @@ func Expect() ExpectBuilder {
return ExpectBuilder{Body_: nil}
}

func ResponsesAreEqual() ExpectBuilder {
return ExpectBuilder{Body_: check.IsEqual}
}

func (e ExpectBuilder) Status(statusCode int) ExpectBuilder {
e.StatusCode_ = statusCode
return e
Expand Down Expand Up @@ -226,3 +234,16 @@ func (h HeaderBuilder) Not() HeaderBuilder {
func (h HeaderBuilder) Exists() HeaderBuilder {
return h.Not().IsEmpty()
}

type ExpectsBuilder struct {
payloadsAreEquals bool
}

func Responses() ExpectsBuilder {
return ExpectsBuilder{}
}

func (e ExpectsBuilder) HaveTheSamePayload() ExpectsBuilder {
e.payloadsAreEquals = true
return e
}
Loading

0 comments on commit cc68f06

Please sign in to comment.