aquavm/air/tests/test_module/integration/create_service.rs

90 lines
3.1 KiB
Rust
Raw Normal View History

2020-10-30 20:29:05 +03:00
/*
2022-03-10 16:06:43 +03:00
* Copyright 2022 Fluence Labs Limited
2020-10-30 20:29:05 +03:00
*
* 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 air_test_utils::prelude::*;
2020-10-30 20:29:05 +03:00
#[test]
fn create_service() {
let module = "greeting";
let module_config = json!(
{
"name": module,
"mem_pages_count": 100,
"logger_enabled": true,
"wasi": {
"envs": json!({}),
"preopened_files": vec!["/tmp"],
"mapped_dirs": json!({}),
}
}
);
let module_bytes = json!([1, 2]);
let blueprint = json!({ "name": "blueprint", "dependencies": [module]});
let variables_mapping = maplit::hashmap!(
"module_bytes".to_string() => module_bytes.clone(),
"module_config".to_string() => module_config.clone(),
"blueprint".to_string() => blueprint.clone(),
2020-10-30 20:29:05 +03:00
);
2021-11-29 18:35:11 +03:00
let mut set_variables_vm = create_avm(
set_variables_call_service(variables_mapping, VariableOptionSource::Argument(0)),
"set_variables",
);
2020-10-30 20:29:05 +03:00
let add_module_response = "add_module response";
let add_blueprint_response = "add_blueprint response";
let create_response = "create response";
2020-10-30 20:29:05 +03:00
let call_service: CallServiceClosure = Box::new(move |params| -> CallServiceResult {
let response = match params.service_id.as_str() {
"add_module" => add_module_response,
"add_blueprint" => add_blueprint_response,
"create" => create_response,
_ => "unknown response",
2020-10-30 20:29:05 +03:00
};
CallServiceResult::ok(json!(response))
2020-10-30 20:29:05 +03:00
});
let mut vm = create_avm(call_service, "A");
2020-10-30 20:29:05 +03:00
2020-12-22 21:05:04 +03:00
let script = include_str!("./scripts/create_service.clj");
2020-10-30 20:29:05 +03:00
2022-04-20 23:05:37 +03:00
let test_params = TestRunParameters::from_init_peer_id("init_peer_id");
let result = checked_call_vm!(set_variables_vm, test_params.clone(), script, "", "");
let result = checked_call_vm!(vm, test_params.clone(), script, "", result.data);
2020-10-30 20:29:05 +03:00
2021-05-16 22:52:22 +03:00
let add_module_response = "add_module response";
let add_blueprint_response = "add_blueprint response";
let create_response = "create response";
2021-08-24 16:14:15 +03:00
let actual_trace = trace_from_result(&result);
2021-01-15 00:38:58 +03:00
let expected_trace = vec![
2021-08-24 16:14:15 +03:00
executed_state::scalar(module_bytes),
executed_state::scalar(module_config),
executed_state::scalar(blueprint),
2021-05-16 22:52:22 +03:00
executed_state::scalar_string(add_module_response),
executed_state::scalar_string(add_blueprint_response),
executed_state::scalar_string(create_response),
executed_state::request_sent_by("A"),
2020-10-30 20:29:05 +03:00
];
2021-01-15 00:38:58 +03:00
assert_eq!(actual_trace, expected_trace);
2021-08-24 16:14:15 +03:00
assert_eq!(result.next_peer_pks, vec![String::from("remote_peer_id")]);
2020-10-30 20:29:05 +03:00
}