Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bazel/WASM.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,17 @@
WebAssembly tests are built using [Proxy-Wasm C++ SDK] and [Proxy-Wasm Rust SDK],
as such, they bring their own set of dependencies.

## Cargo dependencies.

In order to update Cargo dependencies, please make sure that Rust and Cargo
are installed, and run this tool:

```
bash tools/update_crates.sh
```

which will regenerate Bazel rules in `bazel/external/cargo/`.


[Proxy-Wasm C++ SDK]: https://github.com/proxy-wasm/proxy-wasm-cpp-sdk
[Proxy-Wasm Rust SDK]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk
30 changes: 30 additions & 0 deletions bazel/external/cargo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
@generated
cargo-raze generated Bazel file.

DO NOT EDIT! Replaced on runs of cargo-raze
"""

package(default_visibility = ["//visibility:public"])

licenses([
"notice", # See individual crates for specific licenses
])

# Aliased targets
alias(
name = "protobuf",
actual = "@raze__protobuf__2_24_1//:protobuf",
tags = [
"cargo-raze",
"manual",
],
)

# Export file for Stardoc support
exports_files(
[
"crates.bzl",
],
visibility = ["//visibility:public"],
)
14 changes: 14 additions & 0 deletions bazel/external/cargo/Cargo.raze.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "protobuf"
version = "2.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db50e77ae196458ccd3dc58a31ea1a90b0698ab1b7928d89f644c25d72070267"

[[package]]
name = "proxy-wasm-envoy-tests"
version = "0.0.1"
dependencies = [
"protobuf",
]
29 changes: 29 additions & 0 deletions bazel/external/cargo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
description = "Proxy-Wasm tests for Envoy"
name = "proxy-wasm-envoy-tests"
version = "0.0.1"
authors = ["Piotr Sikora <piotrsikora@google.com>"]
edition = "2018"

[dependencies]
protobuf = "2"

[profile.release]
lto = true
opt-level = 3
panic = "abort"

[package.metadata.raze]
package_aliases_dir = "."
workspace_path = "//bazel/external/cargo"
genmode = "Remote"

[[example]]
name = "grpc_call_rust"
path = "../../../test/extensions/filters/http/wasm/test_data/grpc_call_rust.rs"
crate-type = ["cdylib"]

[[example]]
name = "grpc_stream_rust"
path = "../../../test/extensions/filters/http/wasm/test_data/grpc_stream_rust.rs"
crate-type = ["cdylib"]
22 changes: 22 additions & 0 deletions bazel/external/cargo/crates.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
@generated
cargo-raze generated Bazel file.

DO NOT EDIT! Replaced on runs of cargo-raze
"""

load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") # buildifier: disable=load
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # buildifier: disable=load
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") # buildifier: disable=load

def raze_fetch_remote_crates():
"""This function defines a collection of repos and should be called in a WORKSPACE file"""
maybe(
http_archive,
name = "raze__protobuf__2_24_1",
url = "https://crates.io/api/v1/crates/protobuf/2.24.1/download",
type = "tar.gz",
sha256 = "db50e77ae196458ccd3dc58a31ea1a90b0698ab1b7928d89f644c25d72070267",
strip_prefix = "protobuf-2.24.1",
build_file = Label("//bazel/external/cargo/remote:BUILD.protobuf-2.24.1.bazel"),
)
Empty file.
87 changes: 87 additions & 0 deletions bazel/external/cargo/remote/BUILD.protobuf-2.24.1.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
@generated
cargo-raze crate build file.

DO NOT EDIT! Replaced on runs of cargo-raze
"""

# buildifier: disable=load
load("@bazel_skylib//lib:selects.bzl", "selects")

# buildifier: disable=load
load(
"@rules_rust//rust:rust.bzl",
"rust_binary",
"rust_library",
"rust_test",
)

package(default_visibility = [
# Public for visibility by "@raze__crate__version//" targets.
#
# Prefer access through "//bazel/external/cargo", which limits external
# visibility to explicit Cargo.toml dependencies.
"//visibility:public",
])

licenses([
"notice", # MIT from expression "MIT"
])

# Generated Targets
# buildifier: disable=out-of-order-load
# buildifier: disable=load-on-top
load(
"@rules_rust//cargo:cargo_build_script.bzl",
"cargo_build_script",
)

cargo_build_script(
name = "protobuf_build_script",
srcs = glob(["**/*.rs"]),
build_script_env = {
},
crate_features = [
],
crate_root = "build.rs",
data = glob(["**"]),
edition = "2018",
rustc_flags = [
"--cap-lints=allow",
],
tags = [
"cargo-raze",
"manual",
],
version = "2.24.1",
visibility = ["//visibility:private"],
deps = [
],
)

# Unsupported target "coded_input_stream" with type "bench" omitted

# Unsupported target "coded_output_stream" with type "bench" omitted

rust_library(
name = "protobuf",
srcs = glob(["**/*.rs"]),
crate_features = [
],
crate_root = "src/lib.rs",
crate_type = "lib",
data = [],
edition = "2018",
rustc_flags = [
"--cap-lints=allow",
],
tags = [
"cargo-raze",
"manual",
],
version = "2.24.1",
# buildifier: leave-alone
deps = [
":protobuf_build_script",
],
)
2 changes: 2 additions & 0 deletions bazel/repositories_extra.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@rules_python//python:repositories.bzl", "py_repositories")
load("@rules_python//python:pip.bzl", "pip_install")
load("@proxy_wasm_cpp_host//bazel/cargo:crates.bzl", "proxy_wasm_cpp_host_fetch_remote_crates")
load("//bazel/external/cargo:crates.bzl", "raze_fetch_remote_crates")

# Python dependencies.
def _python_deps():
Expand Down Expand Up @@ -131,3 +132,4 @@ def _python_deps():
def envoy_dependencies_extra():
_python_deps()
proxy_wasm_cpp_host_fetch_remote_crates()
raze_fetch_remote_crates()
12 changes: 6 additions & 6 deletions bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -999,12 +999,12 @@ REPOSITORY_LOCATIONS_SPEC = dict(
project_name = "WebAssembly for Proxies (Rust SDK)",
project_desc = "WebAssembly for Proxies (Rust SDK)",
project_url = "https://github.com/proxy-wasm/proxy-wasm-rust-sdk",
version = "28a94df25659b2107b67a11df0112f8f6833558b",
sha256 = "d3da0042fc119282223b7955962e8b3eed261242c8493f9dc8d07a08ca7e2e3e",
version = "4f79a79400713cd68d5df4943476f6fe4507bbd2",
sha256 = "7a6793ef05f108ea9cf55ab00e4e3cdc37cbc961aecb7a956a301820874c64e2",
strip_prefix = "proxy-wasm-rust-sdk-{version}",
urls = ["https://github.com/proxy-wasm/proxy-wasm-rust-sdk/archive/{version}.tar.gz"],
use_category = ["test_only"],
release_date = "2021-02-09",
release_date = "2021-07-01",
cpe = "N/A",
),
emscripten_toolchain = dict(
Expand All @@ -1022,13 +1022,13 @@ REPOSITORY_LOCATIONS_SPEC = dict(
project_name = "Bazel rust rules",
project_desc = "Bazel rust rules (used by Wasm)",
project_url = "https://github.com/bazelbuild/rules_rust",
version = "1b648302edb64d3ddcc159655bf065bff40e6571",
sha256 = "242deacf4c9e4274d90964689dfae6c245bfb1bfa5e3336b2ad3b44f2541b70c",
version = "7e7246f6c48a5d4e69744cd79b9ccb8886966ee2",
sha256 = "d54b379559f3fe6ff0cd251be216a5e35acf241451eec8144455482e8f4748f8",
strip_prefix = "rules_rust-{version}",
urls = ["https://github.com/bazelbuild/rules_rust/archive/{version}.tar.gz"],
use_category = ["dataplane_ext"],
extensions = ["envoy.wasm.runtime.wasmtime"],
release_date = "2021-04-02",
release_date = "2021-06-29",
cpe = "N/A",
),
rules_antlr = dict(
Expand Down
2 changes: 2 additions & 0 deletions test/extensions/filters/http/wasm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ envoy_extension_cc_test(
]) + envoy_select_wasm_rust_tests([
"//test/extensions/filters/http/wasm/test_data:async_call_rust.wasm",
"//test/extensions/filters/http/wasm/test_data:body_rust.wasm",
"//test/extensions/filters/http/wasm/test_data:grpc_call_rust.wasm",
"//test/extensions/filters/http/wasm/test_data:grpc_stream_rust.wasm",
"//test/extensions/filters/http/wasm/test_data:headers_rust.wasm",
"//test/extensions/filters/http/wasm/test_data:metadata_rust.wasm",
"//test/extensions/filters/http/wasm/test_data:panic_rust.wasm",
Expand Down
20 changes: 20 additions & 0 deletions test/extensions/filters/http/wasm/test_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ wasm_rust_binary(
],
)

wasm_rust_binary(
name = "grpc_call_rust.wasm",
srcs = ["grpc_call_rust.rs"],
deps = [
"@proxy_wasm_rust_sdk//:proxy_wasm",
"@proxy_wasm_rust_sdk//bazel/cargo:log",
"//bazel/external/cargo:protobuf",
],
)

wasm_rust_binary(
name = "grpc_stream_rust.wasm",
srcs = ["grpc_stream_rust.rs"],
deps = [
"@proxy_wasm_rust_sdk//:proxy_wasm",
"@proxy_wasm_rust_sdk//bazel/cargo:log",
"//bazel/external/cargo:protobuf",
],
)

wasm_rust_binary(
name = "headers_rust.wasm",
srcs = ["headers_rust.rs"],
Expand Down
84 changes: 84 additions & 0 deletions test/extensions/filters/http/wasm/test_data/grpc_call_rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use log::{debug, error};
use protobuf::well_known_types::Value;
use protobuf::Message;
use proxy_wasm::traits::{Context, HttpContext, RootContext};
use proxy_wasm::types::*;
use std::cell::Cell;
use std::time::Duration;

thread_local! {
// TODO(PiotrSikora): use child-to-parent reference once improved in the SDK.
static CALLOUT_ID: Cell<Option<u32>> = Cell::new(None);
}

#[no_mangle]
pub fn _start() {
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(TestGrpcCallRoot) });
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(TestGrpcCall) });
}

struct TestGrpcCallRoot;

impl RootContext for TestGrpcCallRoot {
fn on_queue_ready(&mut self, _: u32) {
CALLOUT_ID.with(|saved_id| {
if let Some(callout_id) = saved_id.get() {
self.cancel_grpc_call(callout_id);
}
});
}
}

impl Context for TestGrpcCallRoot {}

struct TestGrpcCall;

impl HttpContext for TestGrpcCall {
fn on_http_request_headers(&mut self, _: usize) -> Action {
let mut value = Value::new();
value.set_string_value(String::from("request"));
let message = value.write_to_bytes().unwrap();

match self.dispatch_grpc_call(
"bogus grpc_service",
"service",
"method",
vec![("source", b"grpc_call")],
Some(&message),
Duration::from_secs(1),
) {
Ok(_) => error!("bogus grpc_service succeeded"),
Err(_) => error!("bogus grpc_service rejected"),
};

match self.dispatch_grpc_call(
"cluster",
"service",
"method",
vec![("source", b"grpc_call")],
Some(&message),
Duration::from_secs(1),
) {
Ok(callout_id) => {
CALLOUT_ID.with(|saved_id| saved_id.set(Some(callout_id)));
error!("cluster call succeeded")
}
Err(_) => error!("cluster call rejected"),
};

Action::Pause
}
}

impl Context for TestGrpcCall {
fn on_grpc_call_response(&mut self, _: u32, status_code: u32, response_size: usize) {
if status_code != 0 {
let (_, message) = self.get_grpc_status();
debug!("failure {}", &message.as_deref().unwrap_or(""));
} else if let Some(response_bytes) = self.get_grpc_call_response_body(0, response_size) {
let value = Value::parse_from_bytes(&response_bytes).unwrap();
debug!("{}", value.get_string_value());
}
}
}
Loading