Skip to content

Commit

Permalink
Merge pull request #151 from fastly/cee-dub/make-it-so
Browse files Browse the repository at this point in the history
Makefile: introduce tasks to aid testing; update CI
  • Loading branch information
cee-dub authored Jan 29, 2025
2 parents 26aa8cd + 8fedc9a commit 05b9383
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 96 deletions.
63 changes: 22 additions & 41 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ jobs:
matrix:
include:
# Newest supported configuration
- go-version: '1.22' # pairs with TinyGo 0.31.2
tinygo-version: '0.31.2'
wasmtime-version: 'latest'
tinygo-wasi-target: 'wasi'
- go-version: "1.23" # pairs with TinyGo 0.33.0+
tinygo-version: "0.35.0"
tinygo-wasi-target: "wasip1"
- go-version: "1.22" # pairs with TinyGo 0.31.2
tinygo-version: "0.31.2"
tinygo-wasi-target: "wasi"
# Oldest supported configuration
- go-version: '1.21' # pairs with TinyGo 0.29.0
tinygo-version: '0.29.0'
wasmtime-version: '21.0.1' # pairs with TinyGo 0.29.0
tinygo-wasi-target: 'wasi'
- go-version: '1.23' # pairs with TinyGo 0.33.0
tinygo-version: '0.33.0'
wasmtime-version: '21.0.1' # pairs with TinyGo 0.33.0
tinygo-wasi-target: 'wasip1'
- go-version: "1.21" # pairs with TinyGo 0.29.0
tinygo-version: "0.29.0"
tinygo-wasi-target: "wasi"

steps:
- uses: actions/checkout@v4
Expand All @@ -39,44 +36,28 @@ jobs:
- name: Setup Fastly CLI
uses: fastly/compute-actions/setup@v7

- name: Install Viceroy ${{ env.VICEROY_VERSION }}
shell: 'bash'
env:
VICEROY_VERSION: 0.12.2
run: |
echo "Install Viceroy ${{ env.VICEROY_VERSION }}..."
wget --no-verbose https://github.com/fastly/Viceroy/releases/download/v${{ env.VICEROY_VERSION }}/viceroy_v${{ env.VICEROY_VERSION }}_linux-amd64.tar.gz
mkdir -p $HOME/bin
tar -xzf viceroy_v${{ env.VICEROY_VERSION }}_linux-amd64.tar.gz --directory $HOME/bin
echo "$HOME/bin" >> $GITHUB_PATH
- name: Set up Wasmtime
uses: bytecodealliance/actions/wasmtime/setup@v1
with:
version: ${{ matrix.wasmtime-version }}
- name: Install Viceroy
run: make tools/viceroy

- name: Check our dependencies
- name: Print dependency versions
run: |
go version
tinygo version
fastly version
viceroy --version
wasmtime --version
- name: Tests - Go
run: go test -race -tags="fastlyinternaldebug nofastlyhostcalls" ./...

- name: Tests - TinyGo
run: tinygo test -target=${{ matrix.tinygo-wasi-target }} -tags="fastlyinternaldebug nofastlyhostcalls" ./...
run: make test-go

- name: Integration Tests - Go
env:
RUST_LOG: viceroy=info,viceroy-lib=info
GOARCH: wasm
GOOS: wasip1
run: go test -tags="fastlyinternaldebug" -exec "viceroy run -C fastly.toml" ./integration_tests/...
run: make test-integration-go

- name: Tests - TinyGo
# The slightly different TINYGO_TARGET and GO_BUILD_FLAGS for older
# versions of TinyGo can be removed once all versions are consistent.
run: make test-tinygo TINYGO_TARGET=./targets/fastly-compute-${{ matrix.tinygo-wasi-target }}.json GO_BUILD_FLAGS="-tags='fastlyinternaldebug nofastlyhostcalls'"

- name: Integration Tests - TinyGo
env:
RUST_LOG: viceroy=info,viceroy-lib=info
run: tinygo test -tags="fastlyinternaldebug" -target=targets/fastly-compute-${{ matrix.tinygo-wasi-target }}.json ./integration_tests/...
# The slightly different TINYGO_TARGET for older versions of TinyGo can
# be removed once all versions are consistent.
run: make test-integration-tinygo TINYGO_TARGET=./targets/fastly-compute-${{ matrix.tinygo-wasi-target }}.json
45 changes: 16 additions & 29 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ on:
pull_request:

jobs:
lint:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: oldstable
go-version-file: go.mod

- name: go vet
run: go vet ./...
- uses: ./.github/actions/install-tinygo
with:
tinygo-version: "0.35.0"

- name: Print dependency versions
run: |
go version
tinygo version
- run: go vet ./...

- name: staticcheck
run: |
Expand All @@ -32,29 +40,8 @@ jobs:
go install github.com/gordonklaus/ineffassign@latest
ineffassign ./...
test:
needs: [lint]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- uses: ./.github/actions/install-tinygo
with:
tinygo-version: '0.33.0'

- name: Set up Wasmtime
uses: bytecodealliance/actions/wasmtime/setup@v1

- name: Check our dependencies
run: |
go version
tinygo version
wasmtime --version
- name: Tests - Go
run: go test -race -tags="fastlyinternaldebug nofastlyhostcalls" ./...
- name: Install viceroy
run: make tools/viceroy

- name: Tests - TinyGo
run: tinygo test -target=wasip1 -tags="fastlyinternaldebug nofastlyhostcalls" ./...
- name: Run Tests
run: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_examples/*/bin
_examples/*/pkg
tools/viceroy
60 changes: 60 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.DEFAULT: test

test: test-go test-tinygo test-integration
.PHONY: test

# Makes tools/viceroy available as an executable within Makefile recipes.
PATH := $(PWD)/tools:$(PATH)

# Override these with environment variables or directly on the make command line.
GO_BUILD_FLAGS := -tags=fastlyinternaldebug,nofastlyhostcalls
GO_TEST_FLAGS := -v
GO_PACKAGES := ./...

test-go:
go test $(GO_BUILD_FLAGS) $(GO_TEST_FLAGS) $(GO_PACKAGES)
.PHONY: test-go

# Using this target lets viceroy provide the wasm runtime, eliminating a dependency on wasmtime.
TINYGO_TARGET := ./targets/fastly-compute-wasip1.json

test-tinygo:
tinygo test -target=$(TINYGO_TARGET) $(GO_BUILD_FLAGS) $(GO_TEST_FLAGS) $(GO_PACKAGES)
.PHONY: test-tinygo

# Integration tests use viceroy and override the default values for these variables.
test-integration-%: GO_BUILD_FLAGS := -tags=fastlyinternaldebug
test-integration-%: GO_PACKAGES := ./integration_tests/...

test-integration: test-integration-go test-integration-tinygo
.PHONY: test-integration

test-integration-go: tools/viceroy
GOARCH=wasm GOOS=wasip1 go test -exec "viceroy run -C fastly.toml" $(GO_BUILD_FLAGS) $(GO_TEST_FLAGS) $(GO_PACKAGES)
.PHONY: test-integration-go

test-integration-tinygo: tools/viceroy
tinygo test -target=$(TINYGO_TARGET) $(GO_BUILD_FLAGS) $(GO_TEST_FLAGS) $(GO_PACKAGES)
.PHONY: test-integration-tinygo

tools/viceroy: | tools # Download latest version of Viceroy ./tools/viceroy; delete it if you'd like to upgrade
@arch=$$(uname -m | sed 's/x86_64/amd64/'); \
os=$$(uname -s | tr '[:upper:]' '[:lower:]'); \
url=$$(curl -s https://api.github.com/repos/fastly/viceroy/releases/latest | jq --arg arch $$arch --arg os $$os -r '.assets[] | select((.name | contains($$arch)) and (.name | contains($$os))) | .browser_download_url'); \
filename=$$(basename $$url); \
curl -sSLO $$url && \
tar -xzf $$filename --directory ./tools/ && \
rm $$filename && \
./tools/viceroy --version && \
touch ./tools/viceroy
ifneq ($(strip $(GITHUB_PATH)),)
@echo "$(PWD)/tools" >> "$(GITHUB_PATH)"
endif

tools:
@mkdir -p tools

viceroy-update:
@rm -f tools/viceroy
@$(MAKE) tools/viceroy
.PHONY: viceroy-update
24 changes: 5 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,19 @@ The API reference documentation can be found on [pkg.go.dev/github.com/fastly/co

## Testing

Tests that rely on a Compute runtime can utilize [Viceroy](https://github.com/fastly/Viceroy), our local development tool.
Tests that rely on a Compute runtime use [Viceroy](https://github.com/fastly/Viceroy), our local development tool.

Install Viceroy and ensure the `viceroy` command is available in your path.
The `Makefile` installs viceroy in ./tools/ and uses this version to run tests.

Write your tests as ordinary Go tests. Viceroy provides the Compute APIs locally, although be aware that not all platform functionality is available. You can look at the `integration_tests` directory for examples.

### TinyGo

The `fastly-compute.json` file provides a TinyGo target to run Viceroy. (In the future, we will include this in the Go starter kits.)

To run your tests:

tinygo test -target=targets/fastly-compute-wasip1.json ./...

You can try it out and make sure your local Viceroy environment is set up correctly by running the integration tests in this repository:

tinygo test -target=targets/fastly-compute-wasip1.json ./integration_tests/...

### Go

To run tests with Viceroy and Go

GOARCH=wasm GOOS=wasip1 go test -exec "viceroy run -C fastly.toml" -v ./...
make test

You can try it out and make sure your local Viceroy environment is set up correctly by running the integration tests in this repository:
This target runs tests in both Go and TinyGo, and `integration_tests` in both Go and TinyGo in Viceroy. See additional targets in `Makefile` for running subsets of these tests.

GOARCH=wasm GOOS=wasip1 go test -exec "viceroy run -C fastly.toml" -v ./integration_tests/...
The `targets/fastly-compute-wasi{,p1}.json` files provide TinyGo targets to run Viceroy.

## Logging

Expand Down
Empty file added cache/core/fastly.toml
Empty file.
Empty file added configstore/fastly.toml
Empty file.
Empty file added edgedict/fastly.toml
Empty file.
Empty file added erl/fastly.toml
Empty file.
Empty file added fsthttp/fastly.toml
Empty file.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/fastly/compute-sdk-go

go 1.20
go 1.21
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/tinygo v0.0.0-20211217141205-d684fd97f7cf h1:GNaFMjcDLzxr4j3RNBsbiiwUfCU3AGUJRTz7qG96MNM=
github.com/json-iterator/tinygo v0.0.0-20211217141205-d684fd97f7cf/go.mod h1:sR5SXbtbtp8PxPu3yGjZug4AS5aAur8jQZl9DXYTpP0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
12 changes: 12 additions & 0 deletions integration_tests/_version/fastly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file describes a Fastly Compute package. To learn more visit:
# https://www.fastly.com/documentation/reference/compute/fastly-toml

authors = ["[email protected]"]
description = ""
language = "go"
manifest_version = 2
name = "print-request"

[scripts]
# build = "GOARCH=wasm GOOS=wasip1 go build -o bin/main.wasm ."
# build = "tinygo build -target=wasip1 -gc=conservative -o ./bin/main.wasm ./"
13 changes: 13 additions & 0 deletions integration_tests/_version/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2025 Fastly, Inc.

package main

import (
"runtime/debug"
"testing"
)

func TestGoVersion(t *testing.T) {
bi, _ := debug.ReadBuildInfo()
t.Log(bi.GoVersion)
}

0 comments on commit 05b9383

Please sign in to comment.