Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: move tests to be integration tests #1154

Merged
merged 1 commit into from
Jun 15, 2020
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
2 changes: 1 addition & 1 deletion docs/programming-oak.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ crate allows Node gRPC service methods to be tested with the [Oak SDK](sdk.md)
framework via the Oak Runtime:

<!-- prettier-ignore-start -->
[embedmd]:# (../examples/hello_world/module/rust/src/tests.rs Rust /^.*Test invoking the SayHello Node service method via the Oak runtime.*/ /^}$/)
[embedmd]:# (../examples/hello_world/module/rust/tests/integration_test.rs Rust /^.*Test invoking the SayHello Node service method via the Oak runtime.*/ /^}$/)
```Rust
// Test invoking the SayHello Node service method via the Oak runtime.
#[test]
Expand Down
2 changes: 1 addition & 1 deletion examples/aggregator/module/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is rlib added to make the modules available to the integration tests? How is oak/server/rust/oak_runtime/tests/integration_test.rs working without it? What is its magic?

Not so important, but perhaps oak/server/rust/oak_runtime/tests/integration_test.rs could be renamed to integration_tests.rs as well, to be consistent :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Just saw your description!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the magic is that rlib is the default (and so not mentioned in oak_runtime/Cargo.toml; however, the examples need to be built for the Wasm target and that (I think) needs cdylib crate type.

Also, it looks like integration_test.rs is more canonical so I'll move to that in the examples.


[dependencies]
aggregator_common = { path = "../../common" }
Expand Down
9 changes: 3 additions & 6 deletions examples/aggregator/module/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
//! Clients invoke the module by providing data samples that contain a bucket ID
//! and a Sparse Vector - a dictionary with integer keys.

mod data;
mod proto {
pub mod data;
pub mod proto {
include!(concat!(env!("OUT_DIR"), "/oak.examples.aggregator.rs"));
}

#[cfg(test)]
mod tests;

use aggregator_common::ThresholdAggregator;
use data::SparseVector;
use log::{debug, error};
Expand All @@ -39,7 +36,7 @@ use proto::{Aggregator, AggregatorClient, AggregatorDispatcher, Sample};
use std::{collections::HashMap, convert::TryFrom};

/// Currently threshold value is hardcoded.
const SAMPLE_THRESHOLD: u64 = 5;
pub const SAMPLE_THRESHOLD: u64 = 5;

/// Oak Node that collects and aggregates data.
/// Data is collected in the `aggregators` map where keys are buckets and values are instances of a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
//

use crate::{
use aggregator::{
data::SparseVector,
proto::{Sample, SerializedSparseVector},
SAMPLE_THRESHOLD,
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world/module/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
log = "*"
Expand Down
4 changes: 1 addition & 3 deletions examples/hello_world/module/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
// limitations under the License.
//

mod proto {
pub mod proto {
include!(concat!(env!("OUT_DIR"), "/oak.examples.hello_world.rs"));
}
#[cfg(test)]
mod tests;

use log::info;
use oak::grpc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// limitations under the License.
//

use crate::proto::{HelloRequest, HelloResponse};
use assert_matches::assert_matches;
use hello_world::proto::{HelloRequest, HelloResponse};
use oak::grpc;

const MODULE_CONFIG_NAME: &str = "hello_world";
Expand Down
2 changes: 1 addition & 1 deletion examples/private_set_intersection/module/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
log = "*"
Expand Down
4 changes: 1 addition & 3 deletions examples/private_set_intersection/module/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
//! TODO(#747): Consider stopping accepting contributions after the first client retrieves the
//! intersection.

mod proto {
pub mod proto {
include!(concat!(
env!("OUT_DIR"),
"/oak.examples.private_set_intersection.rs"
));
}
#[cfg(test)]
mod tests;

use oak::grpc;
use proto::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// limitations under the License.
//

use crate::proto::{GetIntersectionResponse, SubmitSetRequest};
use assert_matches::assert_matches;
use oak::grpc;
use private_set_intersection::proto::{GetIntersectionResponse, SubmitSetRequest};
use std::{collections::HashSet, iter::FromIterator};

const MODULE_CONFIG_NAME: &str = "private_set_intersection";
Expand Down
2 changes: 1 addition & 1 deletion examples/running_average/module/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
log = "*"
Expand Down
4 changes: 1 addition & 3 deletions examples/running_average/module/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
//! expressed in base 10, and get back a string representation of the accumulated average value up
//! to and including the value provided in the request.

mod proto {
pub mod proto {
include!(concat!(env!("OUT_DIR"), "/oak.examples.running_average.rs"));
}
#[cfg(test)]
mod tests;

use oak::grpc;
use proto::{GetAverageResponse, RunningAverage, RunningAverageDispatcher, SubmitSampleRequest};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// limitations under the License.
//

use crate::proto::{GetAverageResponse, SubmitSampleRequest};
use assert_matches::assert_matches;
use oak::grpc;
use running_average::proto::{GetAverageResponse, SubmitSampleRequest};

const MODULE_CONFIG_NAME: &str = "running_average";

Expand Down
3 changes: 0 additions & 3 deletions examples/translator/module/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
// limitations under the License.
//

#[cfg(test)]
mod tests;

use log::info;
use oak::grpc;
use translator_common::proto::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
itertools = "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! Clients send their location coordinates (latitude and longitude) and the Oak Node returns the
//! location of the closest Point Of Interest (in our case a bike station).

mod proto {
pub mod proto {
include!(concat!(
env!("OUT_DIR"),
"/oak.examples.trusted_information_retrieval.rs"
Expand Down
89 changes: 2 additions & 87 deletions examples/trusted_information_retrieval/module/rust/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,8 @@
// limitations under the License.
//

use crate::{
database::parse_database,
proto::{ListPointsOfInterestRequest, ListPointsOfInterestResponse, Location, PointOfInterest},
TrustedInformationRetrievalNode,
};
use crate::{database::parse_database, proto::Location, TrustedInformationRetrievalNode};
use assert_matches::assert_matches;
use maplit::hashmap;
use oak::grpc;
use oak_abi::{proto::oak::application::ConfigMap, OakStatus};
use oak_runtime::io::Encodable;

const MODULE_CONFIG_NAME: &str = "trusted_information_retrieval";

const XML_DATABASE: &str = r#"<?xml version="1.0" encoding="utf-8"?><stations lastUpdate="1590775020879" version="2.0">
<station>
Expand Down Expand Up @@ -90,37 +80,6 @@ const XML_DATABASE: &str = r#"<?xml version="1.0" encoding="utf-8"?><stations la
</station>
</stations>"#;

fn send_database(
runtime: &oak_runtime::RuntimeProxy,
entry_handle: oak_abi::Handle,
database: Vec<u8>,
) -> Result<(), OakStatus> {
let config_map = ConfigMap {
items: hashmap! {"database".to_string() => database},
};
runtime.channel_write(entry_handle, config_map.encode()?)
}

fn submit_sample(
runtime: &oak_runtime::RuntimeProxy,
entry_handle: oak_abi::Handle,
latitude: f32,
longitude: f32,
) -> grpc::Result<ListPointsOfInterestResponse> {
let req = ListPointsOfInterestRequest {
location: Some(Location {
latitude,
longitude,
}),
};
oak_tests::grpc_request(
&runtime,
entry_handle,
"/oak.examples.trusted_information_retrieval.TrustedInformationRetrieval/ListPointsOfInterest",
&req,
)
}

#[test]
fn test_parse_database() {
let database = parse_database(&XML_DATABASE.as_bytes().to_vec());
Expand All @@ -129,46 +88,6 @@ fn test_parse_database() {
assert_eq!(database.unwrap().len(), 2);
}

#[test]
fn test_trusted_information_retrieval() {
env_logger::init();

let (runtime, entry_handle) = oak_tests::run_single_module_default(MODULE_CONFIG_NAME)
.expect("Unable to configure runtime with test wasm!");

// Send test database.
assert_matches!(
send_database(&runtime, entry_handle, XML_DATABASE.as_bytes().to_vec(),),
Ok(_)
);

// Test nearest point of interest
assert_eq!(
submit_sample(&runtime, entry_handle, 4.0, -10.0,),
Ok(ListPointsOfInterestResponse {
point_of_interest: Some(PointOfInterest {
name: "Opened station 1".to_string(),
location: Some(Location {
latitude: 0.0,
longitude: 0.0,
}),
}),
}),
);
assert_eq!(
submit_sample(&runtime, entry_handle, 9.0, -10.0,),
Ok(ListPointsOfInterestResponse {
point_of_interest: Some(PointOfInterest {
name: "Opened station 2".to_string(),
location: Some(Location {
latitude: 10.0,
longitude: 0.0,
}),
}),
}),
);
}

fn distance(first: (f32, f32), second: (f32, f32)) -> f32 {
TrustedInformationRetrievalNode::distance(
Location {
Expand All @@ -192,11 +111,7 @@ fn test_distance() {
}

fn assert_approx_eq(left: f32, right: f32) {
let left_abs = left.abs();
let right_abs = right.abs();
let diff = (left_abs - right_abs).abs();

if diff > f32::EPSILON {
if (left - right).abs() > 0.01 {
panic!("{} is not equal to {}", left, right);
}
}
Loading