chore: move test-sdk examples from marine repo (#95)

* move related examples

* update Cargo.lock

* update test macro invocations

* update naming + add CI step

* add marine download CI step

* remove aggressive cargo update

* fix ci
This commit is contained in:
Valery Antopol 2023-08-16 20:00:01 +03:00 committed by GitHub
parent 71b0645338
commit 0bf0489ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 982 additions and 438 deletions

View File

@ -51,12 +51,32 @@ jobs:
- name: Setup Rust toolchain - name: Setup Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1 uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Download marine artifact
id: marine
uses: actions/download-artifact@v3
continue-on-error: true
with:
name: marine
path: ~/.local/bin
- name: Make marine executable
if: steps.marine.outcome == 'success'
run: chmod +x ~/.local/bin/marine
- name: Setup marine
if: steps.marine.outcome == 'failure'
uses: fluencelabs/setup-marine@v1
- name: Set dependencies - name: Set dependencies
if: inputs.cargo-dependencies != '' if: inputs.cargo-dependencies != ''
uses: fluencelabs/github-actions/cargo-set-dependency@main uses: fluencelabs/github-actions/cargo-set-dependency@main
with: with:
dependencies: ${{ inputs.cargo-dependencies }} dependencies: ${{ inputs.cargo-dependencies }}
- name: Build examples
working-directory: examples
run: build.sh
- name: Run cargo check - name: Run cargo check
run: cargo check -v --all-features run: cargo check -v --all-features
@ -66,6 +86,9 @@ jobs:
- name: Run cargo clippy - name: Run cargo clippy
run: cargo clippy -Z unstable-options --all run: cargo clippy -Z unstable-options --all
- name: Test examples
run: cargo test -p single-service-example -p multi-service-example -p build-rs-example
- name: Run rustfmt - name: Run rustfmt
uses: actions-rust-lang/rustfmt@v1 uses: actions-rust-lang/rustfmt@v1

1058
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -34,4 +34,7 @@ members = [
"crates/marine-test-macro", "crates/marine-test-macro",
"crates/marine-test-macro-impl", "crates/marine-test-macro-impl",
"crates/marine-build-rs-generator", "crates/marine-build-rs-generator",
"examples/build_rs",
"examples/single-service",
"examples/multi-service",
] ]

13
examples/build.sh Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail
for dir in ./*; do
# skip non-directory entries
[ -d "$dir" ] || continue
# skip if there's no build.sh in the directory
[ -e "$dir/build.sh" ] || continue
(cd "$dir"; ./build.sh)
done

View File

@ -0,0 +1,21 @@
[package]
name = "build-rs-example"
version = "0.1.0"
authors = ["Fluence Labs"]
description = "The greeting module for the Fluence network"
repository = "https://github.com/fluencelabs/marine/tree/master/examples/build_rs"
edition = "2021"
publish = false
[[bin]]
name = "build_rs_test"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.8.1"
[dev-dependencies]
marine-rs-sdk-test = { path = "../.." }
[build-dependencies]
marine-rs-sdk-test = { path = "../.." }

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "build_rs_test"
max_heap_size = "10 KiB"
logger_enabled = false

View File

@ -0,0 +1,18 @@
use marine_rs_sdk_test::generate_marine_test_env;
use marine_rs_sdk_test::ServiceDescription;
fn main() {
let services = vec![(
"greeting".to_string(),
ServiceDescription {
config_path: "Config.toml".to_string(),
modules_dir: None,
},
)];
let target = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target != "wasm32" {
generate_marine_test_env(services, "marine_test_env.rs", file!());
}
println!("cargo:rerun-if-changed=src/main.rs");
}

9
examples/build_rs/build.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
marine build --release
rm artifacts/* || true
mkdir -p artifacts
cp ../../target/wasm32-wasi/release/build_rs_test.wasm artifacts/

View File

@ -0,0 +1,38 @@
/*
* Copyright 2021 Fluence Labs Limited
*
* 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.
*/
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub fn greeting(name: String) -> String {
format!("Hi, {}", name)
}
#[cfg(test)]
mod built_tests {
marine_rs_sdk_test::include_test_env!("/marine_test_env.rs");
#[test]
fn non_empty_string() {
let mut greeting = marine_test_env::greeting::ServiceInterface::new();
let actual = greeting.greeting("name".to_string());
assert_eq!(actual, "Hi, name");
}
}

View File

@ -0,0 +1,11 @@
[package]
name = "multi-service-example"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2021"
publish = false
[dependencies]
[dev-dependencies]
marine-rs-sdk-test = { path = "../.." }

View File

@ -0,0 +1,13 @@
[package]
name = "consumer"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2021"
publish = false
[[bin]]
name = "consumer"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.8.1"

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "consumer"
max_heap_size = "10 KiB"
logger_enabled = false

View File

@ -0,0 +1,9 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
marine build --release
rm artifacts/* || true
mkdir -p artifacts
cp target/wasm32-wasi/release/consumer.wasm artifacts/

View File

@ -0,0 +1,16 @@
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub struct Data {
pub name: String,
}
#[marine]
pub fn consume(data: Data) -> String {
data.name
}

View File

@ -0,0 +1,13 @@
[package]
name = "producer"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2021"
publish = false
[[bin]]
name = "producer"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.8.1"

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "producer"
max_heap_size = "10 KiB"
logger_enabled = false

View File

@ -0,0 +1,9 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
marine build --release
rm artifacts/* || true
mkdir -p artifacts
cp target/wasm32-wasi/release/producer.wasm artifacts/

View File

@ -0,0 +1,24 @@
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub struct Data {
pub name: String,
}
#[marine]
pub struct Input {
pub first_name: String,
pub last_name: String,
}
#[marine]
pub fn produce(data: Input) -> Data {
Data {
name: format!("{} {}", data.first_name, data.last_name),
}
}

View File

@ -0,0 +1,47 @@
fn main() {}
#[cfg(test)]
mod tests {
use marine_rs_sdk_test::marine_test;
#[marine_test(
producer(config_path = "../producer/Config.toml"),
consumer(config_path = "../consumer/Config.toml")
)]
fn test() {
let mut producer = marine_test_env::producer::ServiceInterface::new();
let mut consumer = marine_test_env::consumer::ServiceInterface::new();
let input = marine_test_env::producer::Input {
first_name: String::from("John"),
last_name: String::from("Doe"),
};
let data = producer.produce(input);
let result = consumer.consume(data);
assert_eq!(result, "John Doe")
}
}
#[cfg(test)]
#[marine_rs_sdk_test::marine_test(
producer(
config_path = "../producer/Config.toml",
modules_dir = "../producer/artifacts"
),
consumer(
config_path = "../consumer/Config.toml",
modules_dir = "../consumer/artifacts"
)
)]
mod tests_on_mod {
#[test]
fn test() {
let mut producer = marine_test_env::producer::ServiceInterface::new();
let mut consumer = marine_test_env::consumer::ServiceInterface::new();
let input = marine_test_env::producer::Input {
first_name: String::from("John"),
last_name: String::from("Doe"),
};
let data = producer.produce(input);
let result = consumer.consume(data);
assert_eq!(result, "John Doe")
}
}

View File

@ -0,0 +1,18 @@
[package]
name = "single-service-example"
version = "0.1.0"
authors = ["Fluence Labs"]
description = "The greeting module for the Fluence network"
repository = "https://github.com/fluencelabs/marine/tree/master/examples/greeting"
edition = "2021"
publish = false
[[bin]]
name = "greeting"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.8.1"
[dev-dependencies]
marine-rs-sdk-test = { path = "../.." }

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "greeting"
max_heap_size = "10 KiB"
logger_enabled = false

View File

@ -0,0 +1,9 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
marine build --release
rm artifacts/* || true
mkdir -p artifacts
cp ../../target/wasm32-wasi/release/greeting.wasm artifacts/

View File

@ -0,0 +1,44 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* 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.
*/
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub fn greeting(name: String) -> String {
format!("Hi, {}", name)
}
#[cfg(test)]
mod tests {
use marine_rs_sdk_test::marine_test;
#[marine_test(config_path = "../Config.toml")]
fn empty_string(greeting: marine_test_env::greeting::ModuleInterface) {
let actual = greeting.greeting(String::new());
assert_eq!(actual, "Hi, ");
}
#[marine_test(config_path = "../Config.toml")]
fn non_empty_string(greeting: marine_test_env::greeting::ModuleInterface) {
let actual = greeting.greeting("name".to_string());
assert_eq!(actual, "Hi, name");
}
}