Skip to content

Commit

Permalink
Merge branch 'master' into lock-conftests
Browse files Browse the repository at this point in the history
  • Loading branch information
ItalyPaleAle authored Aug 7, 2023
2 parents 9a18e33 + c957420 commit 41b40ef
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 9 deletions.
21 changes: 17 additions & 4 deletions middleware/http/wasm/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wasm

import (
Expand Down Expand Up @@ -28,8 +41,8 @@ func BenchmarkNative(b *testing.B) {
}

func BenchmarkTinygo(b *testing.B) {
path := "file://internal/e2e-guests/rewrite/main.wasm"
benchmarkMiddleware(b, path)
url := "file://internal/e2e-guests/rewrite/main.wasm"
benchmarkMiddleware(b, url)
}

// BenchmarkWat gives baseline performance for the same handler by
Expand All @@ -39,8 +52,8 @@ func BenchmarkWat(b *testing.B) {
benchmarkMiddleware(b, url)
}

func benchmarkMiddleware(b *testing.B, path string) {
md := metadata.Base{Properties: map[string]string{"url": path}}
func benchmarkMiddleware(b *testing.B, url string) {
md := metadata.Base{Properties: map[string]string{"url": url}}

l := logger.NewLogger(b.Name())
l.SetOutput(io.Discard)
Expand Down
13 changes: 13 additions & 0 deletions middleware/http/wasm/example/router.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
30 changes: 29 additions & 1 deletion middleware/http/wasm/httpwasm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wasm

import (
Expand All @@ -22,6 +35,12 @@ type middleware struct {
logger logger.Logger
}

type Metadata struct {
// GuestConfig is an optional configuration passed to WASM guests.
// Users can pass an arbitrary string to be parsed by the guest code.
GuestConfig string `mapstructure:"guestConfig"`
}

func NewMiddleware(logger logger.Logger) dapr.Middleware {
return &middleware{logger: logger}
}
Expand All @@ -36,18 +55,27 @@ func (m *middleware) GetHandler(ctx context.Context, metadata dapr.Metadata) (fu

// getHandler is extracted for unit testing.
func (m *middleware) getHandler(ctx context.Context, metadata dapr.Metadata) (*requestHandler, error) {
// parse common wasm metadata configuration
meta, err := wasm.GetInitMetadata(ctx, metadata.Base)
if err != nil {
return nil, fmt.Errorf("wasm: failed to parse metadata: %w", err)
}

// parse wasm middleware specific metadata
var middlewareMeta Metadata
err = mdutils.DecodeMetadata(metadata.Base, &middlewareMeta)
if err != nil {
return nil, fmt.Errorf("wasm: failed to parse wasm middleware metadata: %w", err)
}

var stdout, stderr bytes.Buffer
mw, err := wasmnethttp.NewMiddleware(ctx, meta.Guest,
handler.Logger(m),
handler.ModuleConfig(wasm.NewModuleConfig(meta).
WithName(meta.GuestName).
WithStdout(&stdout). // reset per request
WithStderr(&stderr))) // reset per request
WithStderr(&stderr)), // reset per request
handler.GuestConfig([]byte(middlewareMeta.GuestConfig)))
if err != nil {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions middleware/http/wasm/httpwasm_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wasm

import (
Expand Down
25 changes: 25 additions & 0 deletions middleware/http/wasm/internal/e2e-guests/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main ensures tests can prove logging or stdio isn't missed, both
// during initialization (main) and request (rewrite).
package main

import (
"github.com/http-wasm/http-wasm-guest-tinygo/handler"
"github.com/http-wasm/http-wasm-guest-tinygo/handler/api"
)

func main() {
handler.Host.Log(api.LogLevelInfo, string(handler.Host.GetConfig()))
}
Binary file not shown.
13 changes: 13 additions & 0 deletions middleware/http/wasm/internal/e2e-guests/output/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main ensures tests can prove logging or stdio isn't missed, both
// during initialization (main) and request (rewrite).
package main
Expand Down
13 changes: 13 additions & 0 deletions middleware/http/wasm/internal/e2e-guests/rewrite/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
41 changes: 37 additions & 4 deletions middleware/http/wasm/internal/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internal_test

import (
Expand Down Expand Up @@ -25,11 +38,12 @@ var guestWasm map[string][]byte
const (
guestWasmOutput = "output"
guestWasmRewrite = "rewrite"
guestWasmConfig = "config"
)

// TestMain ensures we can read the test wasm prior to running e2e tests.
func TestMain(m *testing.M) {
wasms := []string{guestWasmOutput, guestWasmRewrite}
wasms := []string{guestWasmOutput, guestWasmRewrite, guestWasmConfig}
guestWasm = make(map[string][]byte, len(wasms))
for _, name := range wasms {
if wasm, err := os.ReadFile(path.Join("e2e-guests", name, "main.wasm")); err != nil {
Expand All @@ -48,9 +62,10 @@ func Test_EndToEnd(t *testing.T) {
l.SetOutput(&buf)

type testCase struct {
name string
guest []byte
test func(t *testing.T, handler http.Handler, log *bytes.Buffer)
name string
guest []byte
property map[string]string
test func(t *testing.T, handler http.Handler, log *bytes.Buffer)
}

tests := []testCase{
Expand Down Expand Up @@ -127,6 +142,14 @@ func Test_EndToEnd(t *testing.T) {
require.Equal(t, "/v1.0/hello?name=teddy", r.URL.RequestURI())
},
},
{
name: "log config to console",
guest: guestWasm[guestWasmConfig],
property: map[string]string{"guestConfig": "config bytes in any format"},
test: func(t *testing.T, handler http.Handler, log *bytes.Buffer) {
require.Contains(t, log.String(), "config bytes in any format")
},
},
}

t.Run("local", func(t *testing.T) {
Expand All @@ -139,6 +162,11 @@ func Test_EndToEnd(t *testing.T) {
require.NoError(t, os.WriteFile(wasmPath, tc.guest, 0o600))

meta := metadata.Base{Properties: map[string]string{"url": "file://" + wasmPath}}
if len(tc.property) > 0 {
for k, v := range tc.property {
meta.Properties[k] = v
}
}
handlerFn, err := wasm.NewMiddleware(l).GetHandler(context.Background(), middleware.Metadata{Base: meta})
require.NoError(t, err)
handler := handlerFn(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
Expand Down Expand Up @@ -182,6 +210,11 @@ func Test_EndToEnd(t *testing.T) {
defer ts.Close()

meta := metadata.Base{Properties: map[string]string{"url": ts.URL + "/guest.wasm"}}
if len(tc.property) > 0 {
for k, v := range tc.property {
meta.Properties[k] = v
}
}
handlerFn, err := wasm.NewMiddleware(l).GetHandler(context.Background(), middleware.Metadata{Base: meta})
require.NoError(t, err)
handler := handlerFn(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
Expand Down

0 comments on commit 41b40ef

Please sign in to comment.