Skip to content

Commit

Permalink
ref(sdk/go): use spin build for building test fixtures
Browse files Browse the repository at this point in the history
This commit changes test fixtures to be built using `spin build` rather
than using TinyGo directly.

Signed-off-by: Adam Reese <[email protected]>
  • Loading branch information
adamreese committed May 11, 2023
1 parent d15b469 commit 7082744
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 18 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ignored-assets
main.wasm
.parcel-cache
.vscode/*.log
go.sum
tests/**/Cargo.lock
crates/**/Cargo.lock
examples/**/Cargo.lock
Expand Down
2 changes: 2 additions & 0 deletions examples/config-tinygo/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2 changes: 2 additions & 0 deletions examples/http-tinygo-outbound-http/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2 changes: 2 additions & 0 deletions examples/http-tinygo/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2 changes: 2 additions & 0 deletions examples/tinygo-key-value/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
1 change: 1 addition & 0 deletions examples/tinygo-redis/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2 changes: 2 additions & 0 deletions sdk/go/http/testdata/http-tinygo/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ id = "http-tinygo-test"
source = "main.wasm"
[component.trigger]
route = "/hello/..."
[component.build]
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
2 changes: 2 additions & 0 deletions sdk/go/http/testdata/spin-roundtrip/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ source = "main.wasm"
allowed_http_hosts = ["example.com"]
[component.trigger]
route = "/hello/..."
[component.build]
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
26 changes: 9 additions & 17 deletions sdk/go/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
"time"
)

const spin_binary = "../../target/debug/spin"
const spinBinary = "../../target/debug/spin"

func retryGet(t *testing.T, url string) *http.Response {
t.Helper()

const tries = 10
for i := 1; i < tries; i++ {
const maxTries = 24
for i := 1; i < maxTries; i++ {
if res, err := http.Get(url); err != nil {
t.Log(err)
} else {
return res
}
time.Sleep(3 * time.Second)
time.Sleep(4 * time.Second)
}
t.Fatal("Get request timeout: ", url)
return nil
Expand All @@ -37,11 +37,11 @@ type testSpin struct {
}

func startSpin(t *testing.T, spinfile string) *testSpin {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)

url := getFreePort(t)

cmd := exec.CommandContext(ctx, spin_binary, "up", "--file", spinfile, "--listen", url)
cmd := exec.CommandContext(ctx, spinBinary, "build", "--up", "--file", spinfile, "--listen", url)
stderr := new(bytes.Buffer)
cmd.Stderr = stderr
if err := cmd.Start(); err != nil {
Expand All @@ -56,18 +56,12 @@ func startSpin(t *testing.T, spinfile string) *testSpin {
}
}

func buildTinyGo(t *testing.T, dir string) {
func build(t *testing.T, dir string) {
t.Helper()

t.Log("building example: ", dir)

getCmd := exec.Command("go", "mod", "tidy")
getCmd.Dir = dir
if err := getCmd.Run(); err != nil {
t.Errorf("Failed to go mod tidy %q, %v", dir, err)
}

cmd := exec.Command("tinygo", "build", "-target=wasi", "-gc=leaking", "-o", "main.wasm", "main.go")
cmd := exec.Command(spinBinary, "build")
cmd.Dir = dir

stderr := new(bytes.Buffer)
Expand All @@ -79,7 +73,6 @@ func buildTinyGo(t *testing.T, dir string) {
}

func TestSpinRoundTrip(t *testing.T) {
buildTinyGo(t, "http/testdata/spin-roundtrip")
spin := startSpin(t, "http/testdata/spin-roundtrip/spin.toml")
defer spin.cancel()

Expand All @@ -104,7 +97,6 @@ func TestSpinRoundTrip(t *testing.T) {
}

func TestHTTPTriger(t *testing.T) {
buildTinyGo(t, "http/testdata/http-tinygo")
spin := startSpin(t, "http/testdata/http-tinygo/spin.toml")
defer spin.cancel()

Expand Down Expand Up @@ -143,7 +135,7 @@ func TestBuildExamples(t *testing.T) {
"../../examples/tinygo-redis",
"../../examples/tinygo-key-value",
} {
buildTinyGo(t, example)
build(t, example)
}
}

Expand Down

0 comments on commit 7082744

Please sign in to comment.