Skip to content

Commit

Permalink
refactor: entrypoint
Browse files Browse the repository at this point in the history
- add support for go test args to the entrypoint
- update Go version to 1.20
- rename is-subdomain to subdomain-gateway-spec
- add merged as argument to the extract-fixtures command
- rename entrypoint as gateway-conformance
- rename tests package from main to tests
- rename build tag to no_subdomain_gateway_spec
- add a utility function for accessing GATEWAY_CONFORMANCE_HOME
  • Loading branch information
galargh committed Mar 17, 2023
1 parent 6753a21 commit c67b7df
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 151 deletions.
7 changes: 6 additions & 1 deletion .github/actions/extract-fixtures/action.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ inputs:
description: 'The path where the fixtures will be saved' description: 'The path where the fixtures will be saved'
required: true required: true
default: 'fixtures' default: 'fixtures'
merged:
description: 'Whether the fixtures should be merged into a single file'
required: false
default: 'false'
runs: runs:
using: 'composite' using: 'composite'
steps: steps:
Expand All @@ -15,8 +19,9 @@ runs:
uses: pl-strflt/docker-container-action@v1 uses: pl-strflt/docker-container-action@v1
env: env:
OUTPUT: ${{ inputs.output }} OUTPUT: ${{ inputs.output }}
MERGED: ${{ inputs.merged }}
with: with:
repository: ${{ steps.github.outputs.action_repository }} repository: ${{ steps.github.outputs.action_repository }}
ref: ${{ steps.github.outputs.action_ref }} ref: ${{ steps.github.outputs.action_ref }}
dockerfile: Dockerfile dockerfile: Dockerfile
args: extract-fixtures "$OUTPUT" args: extract-fixtures --directory="$OUTPUT" --merged="$MERGED"
26 changes: 9 additions & 17 deletions .github/actions/test/action.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,38 +18,30 @@ inputs:
markdown: markdown:
description: "The path where the Markdown report will be saved" description: "The path where the Markdown report will be saved"
required: false required: false
is-subdomain: subdomain-gateway-spec:
description: "Enable the test suite for subdomain gateways" description: "Whether the gateway implements the subdomain gateway spec"
required: false
default: "true"
args:
description: "The arguments to pass to the test command"
required: false required: false
runs: runs:
using: "composite" using: "composite"
steps: steps:
- id: github - id: github
uses: pl-strflt/docker-container-action/.github/actions/github@v1 uses: pl-strflt/docker-container-action/.github/actions/github@v1
- id: generate-args
name: Generate additional arguments
shell: bash
env:
IS_SUBDOMAIN: ${{ inputs.is-subdomain }}
run: |
ARGS=""
if [ "$IS_SUBDOMAIN" = "true" ]; then
ARGS="$ARGS --is-subdomain"
fi
echo "args=${ARGS}" >> "$GITHUB_OUTPUT"
- name: Run the test - name: Run the test
uses: pl-strflt/docker-container-action@v1 uses: pl-strflt/docker-container-action@v1
env: env:
GATEWAY_URL: ${{ inputs.gateway-url }} URL: ${{ inputs.gateway-url }}
JSON: ${{ inputs.json }} JSON: ${{ inputs.json }}
ADDITIONAL_ARGS: ${{ steps.generate-args.outputs.args }} SUBDOMAIN: ${{ inputs.subdomain-gateway-spec }}
with: with:
repository: ${{ steps.github.outputs.action_repository }} repository: ${{ steps.github.outputs.action_repository }}
ref: ${{ steps.github.outputs.action_ref }} ref: ${{ steps.github.outputs.action_ref }}
dockerfile: Dockerfile dockerfile: Dockerfile
opts: --network=host opts: --network=host
args: test --gateway-url "$GATEWAY_URL" --json-output "$JSON" ${ADDITIONAL_ARGS} args: test --url="$URL" --json="$JSON" --subdomain="$SUBDOMAIN" -- ${{ inputs.args }}
- name: Create the XML - name: Create the XML
if: (inputs.xml || inputs.html || inputs.markdown) && (failure() || success()) if: (inputs.xml || inputs.html || inputs.markdown) && (failure() || success())
uses: pl-strflt/gotest-json-to-junit-xml@v1 uses: pl-strflt/gotest-json-to-junit-xml@v1
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jobs:
xml: output.xml xml: output.xml
html: output.html html: output.html
markdown: output.md markdown: output.md
is-subdomain: false subdomain-gateway-spec: false
args: -skip TestGatewayCar
- name: Set summary - name: Set summary
if: (failure() || success()) if: (failure() || success())
run: cat ./output.md >> $GITHUB_STEP_SUMMARY run: cat ./output.md >> $GITHUB_STEP_SUMMARY
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/fixtures.car /fixtures.car
/merge-fixtures /merge-fixtures
/entrypoint /entrypoint
/gateway-conformance


# Logs # Logs
logs logs
Expand Down
10 changes: 4 additions & 6 deletions Dockerfile
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,11 @@
FROM golang:1.19.1-buster FROM golang:1.20-alpine
WORKDIR /app WORKDIR /app
ENV TEST_PATH=/app ENV GATEWAY_CONFORMANCE_HOME=/app

RUN go install gotest.tools/gotestsum@v1.9.0


COPY ./go.mod ./go.sum ./ COPY ./go.mod ./go.sum ./
RUN go mod download RUN go mod download


COPY . . COPY . .
RUN go build -o /entrypoint ./entrypoint.go RUN go build -o ./gateway-conformance ./entrypoint.go


ENTRYPOINT ["/entrypoint"] ENTRYPOINT ["/app/gateway-conformance"]
20 changes: 10 additions & 10 deletions Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ merge-fixtures:
go build -o merge-fixtures ./tooling/cmd/merge_fixtures.go go build -o merge-fixtures ./tooling/cmd/merge_fixtures.go


# tools # tools
fixtures.car: entrypoint fixtures.car: gateway-conformance
./entrypoint merge-fixtures ./fixtures.car ./gateway-conformance extract-fixtures --merged=true --dir=.


entrypoint: gateway-conformance:
go build -o ./entrypoint ./entrypoint.go go build -o ./gateway-conformance ./entrypoint.go


_test: fixtures.car entrypoint _test: fixtures.car gateway-conformance
./entrypoint test --json-output output.json --gateway-url ${GATEWAY_URL} --is-subdomain ./gateway-conformance test --json output.json --gateway-url ${GATEWAY_URL}


test-docker: fixtures.car entrypoint test-docker: fixtures.car gateway-conformance
docker build -t gway-test . docker build -t gateway-conformance .
docker run --rm -v "${PWD}:/workspace" -w "/workspace" --network=host gway-test test docker run --rm -v "${PWD}:/workspace" -w "/workspace" --network=host gateway-conformance test


output.xml: test-kubo output.xml: test-kubo
docker run --rm -v "${PWD}:/workspace" -w "/workspace" --entrypoint "/bin/bash" ghcr.io/pl-strflt/saxon:v1 -c """ docker run --rm -v "${PWD}:/workspace" -w "/workspace" --entrypoint "/bin/bash" ghcr.io/pl-strflt/saxon:v1 -c """
Expand All @@ -37,4 +37,4 @@ output.html: output.xml
docker run --rm -v "${PWD}:/workspace" -w "/workspace" ghcr.io/pl-strflt/saxon:v1 -s:output.xml -xsl:/etc/junit-noframes-saxon.xsl -o:output.html docker run --rm -v "${PWD}:/workspace" -w "/workspace" ghcr.io/pl-strflt/saxon:v1 -s:output.xml -xsl:/etc/junit-noframes-saxon.xsl -o:output.html
open ./output.html open ./output.html


.PHONY: entrypoint .PHONY: gateway-conformance
183 changes: 127 additions & 56 deletions entrypoint.go
Original file line number Original file line Diff line number Diff line change
@@ -1,119 +1,190 @@
package main package main


import ( import (
"bytes"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"


"github.com/ipfs/gateway-conformance/tooling"
"github.com/ipfs/gateway-conformance/tooling/cmd" "github.com/ipfs/gateway-conformance/tooling/cmd"

"github.com/ipfs/gateway-conformance/tooling/fixtures"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )


type event struct {
Action string
Test string `json:",omitempty"`
}

type out struct {
Writer io.Writer
}

func (o out) Write(p []byte) (n int, err error) {
os.Stdout.Write(p)
return o.Writer.Write(p)
}

func copyFiles(inputPaths []string, outputDirectoryPath string) error {
err := os.MkdirAll(outputDirectoryPath, 0755)
if err != nil {
return err
}
for _, inputPath := range inputPaths {
outputPath := filepath.Join(outputDirectoryPath, filepath.Base(inputPath))
src, err := os.Open(inputPath)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(outputPath)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
}
return nil
}

func main() { func main() {
var subdomain bool
var gatewayURL string var gatewayURL string
var jsonOutput string var jsonOutput string
var subdomainGatewaySpec bool
var directory string
var merged bool


app := &cli.App{ app := &cli.App{
Name: "entrypoint", Name: "gateway-conformance",
Usage: "Tooling for the gateway test suite", Usage: "Tooling for the gateway test suite",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "test", Name: "test",
Aliases: []string{"t"}, Aliases: []string{"t"},
Usage: "Run the conformance test suite against your gateway", Usage: "Run the conformance test suite against your gateway",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{
Name: "is-subdomain",
Aliases: []string{"s"},
Usage: "Run the testsuite for subdomain gateways",
Value: false,
Destination: &subdomain,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "gateway-url", Name: "gateway-url",
Aliases: []string{"g"}, Aliases: []string{"url", "g"},
Usage: "The URL of the gateway to test", Usage: "The URL of the gateway to test",
Value: "http://localhost:8080", Value: "http://localhost:8080",
Destination: &gatewayURL, Destination: &gatewayURL,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "json-output", Name: "json-output",
Aliases: []string{"j"}, Aliases: []string{"json", "j"},
Usage: "The path to the JSON output file", Usage: "The path to the JSON output file",
Value: "results.json", Value: "",
Destination: &jsonOutput, Destination: &jsonOutput,
}, },
&cli.BoolFlag{
Name: "subdomain-gateway-spec",
Aliases: []string{"subdomain-gateway", "subdomain"},
Usage: "Whether the gateway implements the subdomain gateway spec",
Value: true,
Destination: &subdomainGatewaySpec,
},
}, },
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
// Capture the output path, we run the tests in a different folder. args := []string{"test", "./tests", "-test.v=test2json"}
jsonOutputAbs, err := filepath.Abs(jsonOutput)
if err != nil {
panic(err)
}


testTagsList := []string{} tags := []string{}
if subdomain { if !subdomainGatewaySpec {
testTagsList = append(testTagsList, "test_subdomains") tags = append(tags, "no_subdomain_gateway_spec")
}
if len(tags) > 0 {
args = append(args, "-tags", strings.Join(tags, ","))
} }
testTags := strings.Join(testTagsList, ",")


// run gotestsum --jsonfile ${...} ./tests -tags="${testTags}" args = append(args, cCtx.Args().Slice()...)
cmd := exec.Command("gotestsum", "--jsonfile", jsonOutputAbs, "./tests", "-tags="+testTags)
cmd.Env = append(os.Environ(), "GATEWAY_URL="+gatewayURL)


// if environ containts "TEST_PATH" then use its value in cmd.Dir fmt.Println("go " + strings.Join(args, " "))
if testPath, ok := os.LookupEnv("TEST_PATH"); ok {
cmd.Dir = testPath output := &bytes.Buffer{}
} cmd := exec.Command("go", args...)
cmd.Stdout = os.Stdout cmd.Dir = tooling.Home()
cmd.Env = append(os.Environ(), fmt.Sprintf("GATEWAY_URL=%s", gatewayURL))
cmd.Stdout = out{output}
cmd.Stderr = os.Stderr
testErr := cmd.Run()


fmt.Printf("running: %s\n", cmd.String()) if jsonOutput != "" {
err = cmd.Run() json := &bytes.Buffer{}
cmd = exec.Command("go", "tool", "test2json", "-p", "Gateway Tests", "-t")
cmd.Stdin = output
cmd.Stdout = json
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
// write jsonOutput to json file
f, err := os.Create(jsonOutput)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(json.Bytes())
if err != nil {
return err return err
}
}

return testErr
}, },
}, },
{ {
Name: "extract-fixtures", Name: "extract-fixtures",
Aliases: []string{"e"}, Aliases: []string{"e"},
Usage: "Extract gateway testing fixture that is used by the conformance test suite", Usage: "Extract gateway testing fixtures that are used by the conformance test suite",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "directory",
Aliases: []string{"dir"},
Usage: "The directory to extract the fixtures to",
Required: true,
Destination: &directory,
},
&cli.BoolFlag{
Name: "merged",
Usage: "Merge the fixtures into a single CAR file",
Value: false,
Destination: &merged,
},
},
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
output := cCtx.Args().First() err := os.MkdirAll(directory, 0755)
if output == "" { if err != nil {
return fmt.Errorf("output path is required") return err
} }


// mkdir -p output: files, err := fixtures.List()
err := os.MkdirAll(output, 0755)
if err != nil { if err != nil {
return err return err
} }


// run shell command: `find /app/fixtures -name '*.car' -exec cp {} "${2}/" \;` merged := cCtx.Bool("merged")
cmd := exec.Command("find", "/app/fixtures", "-name", "*.car", "-exec", "cp", "{}", output+"/", ";") if merged {
err = cmd.Run() err = cmd.Merge(filepath.Join(directory, "fixtures.car"))

if err != nil {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

return err return err
}, }
}, } else {
{ err = copyFiles(files, directory)
Name: "merge-fixtures", if err != nil {
Aliases: []string{"m"}, return err
Usage: "Merge all the fixtures into a single CAR file", }
Action: func(cCtx *cli.Context) error {
output := cCtx.Args().First()
if output == "" {
return fmt.Errorf("output path is required")
} }


return cmd.MergeFixtures(output) return nil
}, },
}, },
}, },
Expand Down
1 change: 0 additions & 1 deletion fixtures/dir/ascii.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/t0113_gateway_symlink_test.go
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
package main package tests


import ( import (
"fmt" "fmt"
Expand Down
Loading

0 comments on commit c67b7df

Please sign in to comment.