marine/fluence-faas/tests/records.rs

218 lines
6.6 KiB
Rust
Raw Normal View History

2020-08-23 22:47:04 +00:00
/*
* 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 fluence_faas::FluenceFaaS;
use fluence_faas::IValue;
2020-10-21 19:21:16 +00:00
use pretty_assertions::assert_eq;
2020-09-15 22:14:15 +00:00
use serde_json::json;
2020-08-23 22:47:04 +00:00
#[test]
pub fn records() {
let records_config_path = "../examples/records/Config.toml";
let records_config_raw = std::fs::read(records_config_path)
.expect("../examples/records/Config.toml should presence");
2020-10-01 09:19:38 +00:00
let mut records_config: fluence_faas::TomlFaaSConfig =
2020-09-14 11:22:56 +00:00
toml::from_slice(&records_config_raw).expect("records config should be well-formed");
2020-09-15 22:14:15 +00:00
records_config.modules_dir = Some(String::from("../examples/records/artifacts/"));
2020-08-23 22:47:04 +00:00
let mut faas = FluenceFaaS::with_raw_config(records_config)
2021-03-16 10:51:59 +00:00
.unwrap_or_else(|e| panic!("can't create Fluence FaaS instance: {}", e));
2020-08-23 22:47:04 +00:00
2020-09-15 22:14:15 +00:00
let result1 = faas
.call_with_ivalues("records_pure", "invoke", &[], <_>::default())
2020-08-23 22:47:04 +00:00
.unwrap_or_else(|e| panic!("can't invoke pure: {:?}", e));
2020-11-05 12:18:48 +00:00
let right_result = json!({
"field_0": 1,
"field_1": 1,
"field_2": 2,
"field_3": 3,
"field_4": 4,
"field_5": 5,
"field_6": 6,
"field_7": 7,
"field_8": 8,
"field_9": 9.0,
"field_10": 10.0,
"field_11": "field_11",
"field_12": [0x13, 0x37],
});
2020-08-23 22:47:04 +00:00
assert_eq!(
2020-09-15 22:14:15 +00:00
result1,
vec![IValue::Record(
2020-12-29 14:51:13 +00:00
wasmer_wit::NEVec::new(vec![
2020-09-15 22:14:15 +00:00
IValue::I32(1),
IValue::S8(1),
IValue::S16(2),
IValue::S32(3),
IValue::S64(4),
IValue::U8(5),
IValue::U16(6),
IValue::U32(7),
IValue::U64(8),
IValue::F32(9.0),
IValue::F64(10.0),
IValue::String(String::from("field_11")),
2020-09-22 22:14:54 +00:00
IValue::Array(vec![IValue::U8(0x13), IValue::U8(0x37)])
2020-09-15 22:14:15 +00:00
])
.unwrap()
)]
);
let result2 = faas
.call_with_json(
"records_effector",
"mutate_struct",
json!({
"test_record": {
"field_0": 0,
"field_1": 0,
"field_2": 0,
"field_3": 0,
"field_4": 0,
"field_5": 0,
"field_6": 0,
"field_7": 0,
"field_8": 0,
"field_9": 0,
"field_10": 0,
"field_11": "field",
"field_12": vec![1],
}
}),
<_>::default(),
)
.unwrap_or_else(|e| panic!("can't invoke pure: {:?}", e));
2020-11-05 12:18:48 +00:00
assert_eq!(result2, right_result);
2020-09-15 22:14:15 +00:00
let result3 = faas
.call_with_json(
"records_effector",
"mutate_struct",
json!({
"test_record": [0,0,0,0,0,0,0,0,0,0,0,"",[1]]
}),
<_>::default(),
)
.unwrap_or_else(|e| panic!("can't invoke pure: {:?}", e));
2020-11-05 12:18:48 +00:00
assert_eq!(result3, right_result);
2020-09-15 22:14:15 +00:00
let result4 = faas
.call_with_json(
"records_effector",
"mutate_struct",
json!([{
"field_0": 0,
"field_1": 0,
"field_2": 0,
"field_3": 0,
"field_4": 0,
"field_5": 0,
"field_6": 0,
"field_7": 0,
"field_8": 0,
"field_9": 0,
"field_10": 0,
"field_11": "",
"field_12": vec![1],
}
]),
<_>::default(),
)
.unwrap_or_else(|e| panic!("can't invoke pure: {:?}", e));
2020-11-05 12:18:48 +00:00
assert_eq!(result4, right_result);
2020-09-15 22:14:15 +00:00
let result5 = faas
.call_with_json(
"records_effector",
"mutate_struct",
json!([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", [1]]]),
<_>::default(),
)
.unwrap_or_else(|e| panic!("can't invoke pure: {:?}", e));
2020-11-05 12:18:48 +00:00
assert_eq!(result5, right_result);
2020-09-15 22:14:15 +00:00
}
#[test]
2021-04-13 09:36:14 +00:00
fn records_passing() {
let inner_records_config_raw = std::fs::read("./tests/wasm_tests/records_passing/Config.toml")
.expect("./tests/wasm_tests/records_passing/Config.toml should presence");
2020-09-15 22:14:15 +00:00
2021-04-13 09:36:14 +00:00
let mut records_passing_config: fluence_faas::TomlFaaSConfig =
2020-09-15 22:14:15 +00:00
toml::from_slice(&inner_records_config_raw)
.expect("argument passing test config should be well-formed");
2021-04-13 09:36:14 +00:00
records_passing_config.modules_dir =
Some(String::from("./tests/wasm_tests/records_passing/artifacts"));
2020-09-15 22:14:15 +00:00
2021-04-13 09:36:14 +00:00
let mut faas = FluenceFaaS::with_raw_config(records_passing_config)
2021-03-16 10:51:59 +00:00
.unwrap_or_else(|e| panic!("can't create Fluence FaaS instance: {}", e));
2020-09-15 22:14:15 +00:00
2021-04-13 09:36:14 +00:00
let mut test = |func_name: &str| {
let result = faas
.call_with_json(
"records_passing_pure",
func_name,
json!({
"test_record": {
2020-09-15 22:14:15 +00:00
"test_record_0": {
2021-04-13 09:36:14 +00:00
"field_0": 0
},
"test_record_1": {
"field_0": 1,
"field_1": "",
"field_2": vec![1],
"test_record_0": {
"field_0": 1
}
2020-09-15 22:14:15 +00:00
}
}
2021-04-13 09:36:14 +00:00
}),
<_>::default(),
)
.unwrap_or_else(|e| panic!("can't invoke inner_records_pure: {:?}", e));
2020-09-15 22:14:15 +00:00
2021-04-13 09:36:14 +00:00
let right_result = json!({
2020-11-05 12:18:48 +00:00
"test_record_0": {
"field_0": 1
},
"test_record_1": {
"field_0": 1,
2021-04-13 09:36:14 +00:00
"field_1": "fluence",
"field_2": vec![0x13, 0x37],
2020-11-05 12:18:48 +00:00
"test_record_0": {
2021-04-13 09:36:14 +00:00
"field_0": 5
2020-11-05 12:18:48 +00:00
}
}
2021-04-13 09:36:14 +00:00
});
assert_eq!(result, right_result);
};
2020-11-05 12:18:48 +00:00
2021-04-13 09:36:14 +00:00
test("test_record");
test("test_record_ref");
2020-08-23 22:47:04 +00:00
}