-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathintegration_tests.rs
115 lines (101 loc) · 4.56 KB
/
integration_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use zmq::{Context, Message};
use uuid::Uuid;
use camera_traps::events;
use event_engine::events::{Event};
mod common;
/** This integration test injects a NewImageEvent into a running camera-traps application.
* At the time of this writing the rust implementation of these camera-traps plugins are
* simply chained together:
*
* - image_recv_plugin
* - image_score_plugin
* - image_store_plugin
*
* Upon receiving an image event to which they are subscribed, each plugin generates a new
* output event containing dummy data.
*
* *Set Up*
*
* This test looks for its configuration file path in the TRAPS_INTEGRATION_CONFIG_FILE
* environment variable. If this variable is not found, the test looks in the user's
* ~/traps-integration.toml file. If a configuration cannot be loaded, the test aborts.
*
* See common/mod.rs for configuration implementation details.
* See camera-traps/resources/traps-integration.toml for an example configuration. Here
* is an example configuration that processes 10 images:
*
* iterations = 10
* image_input_dir = "~/traps/input"
*
* [external_plugin_config]
* plugin_name = "ext_image_gen_test_plugin"
* id = "d3266646-41ec-11ed-a96f-5391348bab46"
* external_port = 6000
* subscriptions = [
* "PluginTerminateEvent"
* ]
* The input directory should contain at least one .png image file. The camera-traps
* application determines where image files are written, if anywhere. The camera-traps
* application must also be configured with this external plugin.
*
* *Execution*
*
* The camera-traps application must be running before staring this this test. An easy
* way to invoke the application is to type "cargo run" into a terminal where the
* current directory is the camera-traps top-level directory.
*
* This test can be easily started in two ways. The first is to click on the virtual
* "Run Test|Debug" prompt that is displayed above the inject_new_image() function
* definition in Visual Studio. The other way is to type one of these commands in a
* terminal with current directory the camera-traps top-level directory:
*
* - cargo test --test integration_tests
*
* - cargo test --test integration_tests -- --show-output
*
* Logging in the camera-traps application terminal will indicate progress.
*/
#[test]
//#[ignore]
fn inject_new_image() {
// Write to stdout. To make this appear when running cargo,
// issue: cargo test -- --nocapture
println!("Starting the inject_new_image integration test.");
// Obtain the integration test configuration.
let parms = common::get_parms().expect("Unable to retrieve integration test parameter from file.");
println!("{}", format!("{:#?}", parms));
// Create this process's zqm context.
let context = Context::new();
// Plugin socket utilizes REQ socket type connect to the camera-traps application.
let socket = context.socket(zmq::REQ).expect("Failed to create socket.");
let socket_connect_str = "tcp://localhost:".to_string() + &parms.config.external_plugin_config.external_port.to_string();
socket.connect(socket_connect_str.as_str()).expect("Failed to connect socket to camera-traps application.");
// Read the first image file from the input directory.
let image = common::read_first_file_from_dir(&parms.config.image_input_dir).
expect(("Could not read image file: ".to_string() +
&parms.config.image_input_dir).as_str());
// Main loop runs a configurable number of iterations.
let mut iterations = parms.config.iterations;
while iterations > 0 {
// Create an event and serialize it for transmission to camera-traps.
let ev = events::NewImageEvent::new(Uuid::new_v4(),
"png".to_string(),
image.clone());
let bytes = match ev.to_bytes() {
Ok(v) => v,
Err(e) => {
// Game over.
panic!("{}", e.to_string());
}
};
// Send the event.
socket.send(bytes, 0).expect("Failed to send byte array to camera-traps application.");
// Get the reply
let mut reply = Message::new();
socket.recv(&mut reply, 0).expect("Failed on reply.");
// Decement the iteration count.
iterations -= 1;
}
// Disconnect from the camera-traps application.
socket.disconnect(socket_connect_str.as_str()).expect("Failed to close socket.");
}