Skip to content

Commit

Permalink
refactor(tests): relocate testdata to new structure
Browse files Browse the repository at this point in the history
Just moved some stuff around to keep the testdata more organized.
When a single directory just can't handle the fame, split it!
  • Loading branch information
madflojo committed Oct 13, 2024
1 parent f1053c0 commit 4c59b77
Show file tree
Hide file tree
Showing 57 changed files with 346 additions and 31 deletions.
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
build: build-testdata

build-testdata:
$(MAKE) -C testdata/default build
$(MAKE) -C testdata/fail build
$(MAKE) -C testdata/kv build
$(MAKE) -C testdata/sql build
$(MAKE) -C testdata/logger build
$(MAKE) -C testdata/function build
$(MAKE) -C testdata/successafter5 build
$(MAKE) -C testdata/sdkv1/kv build
$(MAKE) -C testdata/sdkv1/sql build
$(MAKE) -C testdata/sdkv1/logger build
$(MAKE) -C testdata/base/default build
$(MAKE) -C testdata/base/fail build
$(MAKE) -C testdata/base/kv build
$(MAKE) -C testdata/base/sql build
$(MAKE) -C testdata/base/logger build
$(MAKE) -C testdata/base/function build
$(MAKE) -C testdata/base/successafter5 build

tests: build tests-nobuild
tests-nobuild: tests-base tests-redis tests-cassandra tests-mysql tests-postgres tests-boltdb tests-inmemory
Expand Down
12 changes: 6 additions & 6 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestRunningServer(t *testing.T) {
cfg.Set("use_consul", false)
cfg.Set("debug", true)
cfg.Set("trace", true)
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
srv := New(cfg)
go func() {
err := srv.Run()
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestPProfServerEnabled(t *testing.T) {
cfg.Set("debug", true)
cfg.Set("trace", true)
cfg.Set("enable_pprof", true)
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
srv := New(cfg)
go func() {
err := srv.Run()
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestPProfServerDisabled(t *testing.T) {
cfg.Set("use_consul", false)
cfg.Set("debug", true)
cfg.Set("trace", true)
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
srv := New(cfg)
go func() {
err := srv.Run()
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestRunningTLSServer(t *testing.T) {
cfg.Set("enable_sql", true)
cfg.Set("sql_type", "mysql")
cfg.Set("sql_dsn", "root:example@tcp(mysql:3306)/example")
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
err = cfg.AddRemoteProvider("consul", "consul:8500", "tarmac/config")
if err != nil {
t.Fatalf("Failed to create Consul config provider - %s", err)
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestRunningMTLSServer(t *testing.T) {
cfg.Set("enable_sql", true)
cfg.Set("sql_type", "mysql")
cfg.Set("sql_dsn", "root:example@tcp(mysql:3306)/example")
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
err = cfg.AddRemoteProvider("consul", "consul:8500", "tarmac/config")
if err != nil {
t.Fatalf("Failed to create Consul config provider - %s", err)
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestRunningFailMTLSServer(t *testing.T) {
cfg.Set("enable_sql", true)
cfg.Set("sql_type", "mysql")
cfg.Set("sql_dsn", "root:example@tcp(mysql:3306)/example")
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
err = cfg.AddRemoteProvider("consul", "consul:8500", "tarmac/config")
if err != nil {
t.Fatalf("Failed to create Consul config provider - %s", err)
Expand Down
33 changes: 26 additions & 7 deletions pkg/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBasicFunction(t *testing.T) {
cfg.Set("disable_logging", false)
cfg.Set("debug", true)
cfg.Set("listen_addr", "localhost:9001")
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")

srv := New(cfg)
go func() {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestMaintenanceMode(t *testing.T) {
cfg.Set("disable_logging", false)
cfg.Set("debug", true)
cfg.Set("listen_addr", "localhost:9001")
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
cfg.Set("enable_maintenance_mode", true)

srv := New(cfg)
Expand Down Expand Up @@ -200,6 +200,25 @@ func TestFullService(t *testing.T) {
tc.cfg.Set("wasm_function_config", "/testdata/tarmac.json")
tt = append(tt, tc)

tc = FullServiceTestCase{name: "In-Memory SDKv1", cfg: viper.New()}
tc.cfg.Set("disable_logging", false)
tc.cfg.Set("debug", true)
tc.cfg.Set("listen_addr", "localhost:9001")
tc.cfg.Set("kvstore_type", "in-memory")
tc.cfg.Set("enable_kvstore", true)
tc.cfg.Set("wasm_function_config", "/testdata/sdkv1/tarmac.json")
tt = append(tt, tc)

tc = FullServiceTestCase{name: "MySQL SDKv1", cfg: viper.New()}
tc.cfg.Set("disable_logging", false)
tc.cfg.Set("debug", true)
tc.cfg.Set("listen_addr", "localhost:9001")
tc.cfg.Set("enable_sql", true)
tc.cfg.Set("sql_type", "mysql")
tc.cfg.Set("sql_dsn", "root:example@tcp(mysql:3306)/example")
tc.cfg.Set("wasm_function_config", "/testdata/sdkv1/tarmac.json")
tt = append(tt, tc)

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
srv := New(tc.cfg)
Expand Down Expand Up @@ -292,7 +311,7 @@ func TestInitFuncs(t *testing.T) {
tc.cfg.Set("kvstore_type", "in-memory")
tc.cfg.Set("enable_kvstore", true)
tc.cfg.Set("run_mode", "job")
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"default":{"filepath":"/testdata/default/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","function":"default"}]}}}`)
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"default":{"filepath":"/testdata/base/default/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","function":"default"}]}}}`)
tt = append(tt, tc)

tc = InitFuncTestCase{name: "Fails", cfg: viper.New()}
Expand All @@ -302,7 +321,7 @@ func TestInitFuncs(t *testing.T) {
tc.cfg.Set("kvstore_type", "in-memory")
tc.cfg.Set("enable_kvstore", true)
tc.cfg.Set("run_mode", "job")
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"fail":{"filepath":"/testdata/fail/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","function":"fail"}]}}}`)
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"fail":{"filepath":"/testdata/base/fail/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","function":"fail"}]}}}`)
tc.err = true
tt = append(tt, tc)

Expand All @@ -313,7 +332,7 @@ func TestInitFuncs(t *testing.T) {
tc.cfg.Set("kvstore_type", "in-memory")
tc.cfg.Set("enable_kvstore", true)
tc.cfg.Set("run_mode", "job")
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"successafter5":{"filepath":"/testdata/successafter5/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","retries":10,"function":"successafter5"}]}}}`)
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"successafter5":{"filepath":"/testdata/base/successafter5/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","retries":10,"function":"successafter5"}]}}}`)
tt = append(tt, tc)

tc = InitFuncTestCase{name: "Fail After 10 Retries", cfg: viper.New()}
Expand All @@ -323,7 +342,7 @@ func TestInitFuncs(t *testing.T) {
tc.cfg.Set("kvstore_type", "in-memory")
tc.cfg.Set("enable_kvstore", true)
tc.cfg.Set("run_mode", "job")
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"fail":{"filepath":"/testdata/fail/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","retries":10,"function":"fail"}]}}}`)
tc.config = []byte(`{"services":{"test-service":{"name":"test-service","functions":{"fail":{"filepath":"/testdata/base/fail/tarmac.wasm","pool_size":1}},"routes":[{"type":"init","retries":10,"function":"fail"}]}}}`)
tc.err = true
tt = append(tt, tc)

Expand Down Expand Up @@ -381,7 +400,7 @@ func TestWASMRunner(t *testing.T) {
cfg.Set("disable_logging", false)
cfg.Set("debug", true)
cfg.Set("listen_addr", "localhost:9001")
cfg.Set("wasm_function", "/testdata/default/tarmac.wasm")
cfg.Set("wasm_function", "/testdata/base/default/tarmac.wasm")
srv := New(cfg)
go func() {
err := srv.Run()
Expand Down
10 changes: 5 additions & 5 deletions pkg/wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestWASMModuleCreation(t *testing.T) {
ModuleConf: ModuleConfig{
Name: "A Module",
PoolSize: 99,
Filepath: "/testdata/logger/tarmac.wasm",
Filepath: "/testdata/base/logger/tarmac.wasm",
},
})

Expand All @@ -47,7 +47,7 @@ func TestWASMModuleCreation(t *testing.T) {
Pass: false,
ModuleConf: ModuleConfig{
PoolSize: 99,
Filepath: "/testdata/logger/tarmac.wasm",
Filepath: "/testdata/base/logger/tarmac.wasm",
},
})

Expand All @@ -57,7 +57,7 @@ func TestWASMModuleCreation(t *testing.T) {
Pass: true,
ModuleConf: ModuleConfig{
Name: "A Module",
Filepath: "/testdata/logger/tarmac.wasm",
Filepath: "/testdata/base/logger/tarmac.wasm",
},
})

Expand All @@ -78,7 +78,7 @@ func TestWASMModuleCreation(t *testing.T) {
ModuleConf: ModuleConfig{
Name: "A Module",
PoolSize: 99,
Filepath: "/doesntexist/testdata/logger/tarmac.wasm",
Filepath: "/doesntexist/testdata/base/logger/tarmac.wasm",
},
})

Expand Down Expand Up @@ -119,7 +119,7 @@ func TestWASMExecution(t *testing.T) {

err = s.LoadModule(ModuleConfig{
Name: "AModule",
Filepath: "/testdata/logger/tarmac.wasm",
Filepath: "/testdata/base/logger/tarmac.wasm",
})
if err != nil {
t.Fatalf("Failed to load module - %s", err)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions testdata/sdkv1/kv/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Makefile for Go example for Tarmac WASM functions

build:
## Run TinyGo build via Docker because its easier
docker run --rm -v `pwd`/:/build -w /build tinygo/tinygo:0.25.0 tinygo build -o /build/tarmac.wasm -target wasi /build/main.go

docker-compose:
docker compose up

run: build docker-compose
12 changes: 12 additions & 0 deletions testdata/sdkv1/kv/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'
services:
tarmac-example:
image: madflojo/tarmac
ports:
- 80:8080
environment:
- "APP_ENABLE_TLS=false"
- "APP_LISTEN_ADDR=0.0.0.0:8080"
- "APP_DEBUG=true"
volumes:
- "./:/functions"
10 changes: 10 additions & 0 deletions testdata/sdkv1/kv/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/tarmac-project/tarmac/testdata/kv

go 1.21

require github.com/tarmac-project/tarmac/pkg/sdk v0.5.0

require (
github.com/valyala/fastjson v1.6.4 // indirect
github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect
)
8 changes: 8 additions & 0 deletions testdata/sdkv1/kv/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
43 changes: 43 additions & 0 deletions testdata/sdkv1/kv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// This program is a test program used to facilitate unit testing with Tarmac.
package main

import (
"fmt"

"github.com/tarmac-project/tarmac/pkg/sdk"
)

var tarmac *sdk.Tarmac

func main() {
var err error

// Initialize the Tarmac SDK
tarmac, err = sdk.New(sdk.Config{Namespace: "tarmac", Handler: Handler})
if err != nil {
return
}
}

func Handler(payload []byte) ([]byte, error) {
// Store data within KV datastore
err := tarmac.KV.Set("test-data", []byte("i am a little teapot"))
if err != nil {
return []byte(""), fmt.Errorf(`Failed to store data via KVStore - %s`, err)
}

// Fetch data from KV datastore
data, err := tarmac.KV.Get("test-data")
if err != nil {
return []byte(""), fmt.Errorf(`Failed to fetch data via KVStore - %s`, err)
}

tarmac.Logger.Info(fmt.Sprintf("Fetched %s from datastore", data))

if len(data) != len([]byte("i am a little teapot")) {
return []byte(""), fmt.Errorf("not able to fetch data from KVStore")
}

// Return a happy message
return []byte("Howdie"), nil
}
10 changes: 10 additions & 0 deletions testdata/sdkv1/logger/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Makefile for Go example for Tarmac WASM functions

build:
## Run TinyGo build via Docker because its easier
docker run --rm -v `pwd`/:/build -w /build tinygo/tinygo:0.25.0 tinygo build -o /build/tarmac.wasm -target wasi /build/main.go

docker-compose:
docker compose up

run: build docker-compose
12 changes: 12 additions & 0 deletions testdata/sdkv1/logger/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'
services:
tarmac-example:
image: madflojo/tarmac
ports:
- 80:8080
environment:
- "APP_ENABLE_TLS=false"
- "APP_LISTEN_ADDR=0.0.0.0:8080"
- "APP_DEBUG=true"
volumes:
- "./:/functions"
10 changes: 10 additions & 0 deletions testdata/sdkv1/logger/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/tarmac-project/tarmac/testdata/logger

go 1.20

require github.com/tarmac-project/tarmac/pkg/sdk v0.5.0

require (
github.com/valyala/fastjson v1.6.4 // indirect
github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect
)
7 changes: 7 additions & 0 deletions testdata/sdkv1/logger/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
28 changes: 28 additions & 0 deletions testdata/sdkv1/logger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This program is a test program used to facilitate unit testing with Tarmac.
package main

import (
"fmt"

"github.com/tarmac-project/tarmac/pkg/sdk"
)

var tarmac *sdk.Tarmac

func main() {
var err error

// Initialize the Tarmac SDK
tarmac, err = sdk.New(sdk.Config{Namespace: "tarmac", Handler: Handler})
if err != nil {
return
}
}

func Handler(payload []byte) ([]byte, error) {
// Log the payload
tarmac.Logger.Info(fmt.Sprintf("Testdata function - %s", payload))

// Return a happy message
return payload, nil
}
10 changes: 10 additions & 0 deletions testdata/sdkv1/sql/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Makefile for Go example for Tarmac WASM functions

build:
## Run TinyGo build via Docker because its easier
docker run --rm -v `pwd`/:/build -w /build tinygo/tinygo:0.25.0 tinygo build -o /build/tarmac.wasm -target wasi /build/main.go

docker-compose:
docker compose up

run: build docker-compose
12 changes: 12 additions & 0 deletions testdata/sdkv1/sql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'
services:
tarmac-example:
image: madflojo/tarmac
ports:
- 80:8080
environment:
- "APP_ENABLE_TLS=false"
- "APP_LISTEN_ADDR=0.0.0.0:8080"
- "APP_DEBUG=true"
volumes:
- "./:/functions"
Loading

0 comments on commit 4c59b77

Please sign in to comment.