Mercurial > crates > systemd-socket
annotate tests/comm.rs @ 1:ef8bf41097ac
Added integration tests
author | Martin Habovstiak <martin.habovstiak@gmail.com> |
---|---|
date | Fri, 27 Nov 2020 10:07:35 +0100 |
parents | |
children | 372afb9a700f |
rev | line source |
---|---|
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
1 use std::process::Child; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
2 use std::ffi::OsStr; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
3 use std::io::{self, Read, Write}; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
4 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
5 pub trait Test { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
6 const SOCKET_ADDR: &'static str; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
7 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
8 fn spawn_slave(program_name: &OsStr) -> io::Result<Child>; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
9 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
10 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
11 const REQUEST: &[u8] = b"orange coin"; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
12 const RESPONSE: &[u8] = b"good"; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
13 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
14 fn main_master(slave: io::Result<Child>) { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
15 let mut slave = slave.expect("failed to run systemd-socket-activate"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
16 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
17 // give slave some time to bind the socket just to be sure |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
18 std::thread::sleep(std::time::Duration::from_secs(1)); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
19 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
20 let mut client_socket = std::net::TcpStream::connect("127.0.0.1:4242").expect("Failed to connect to 127.0.0.1:4242"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
21 client_socket.write_all(REQUEST).expect("failed to send data"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
22 let mut buf = [0u8; RESPONSE.len()]; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
23 client_socket.read_exact(&mut buf).expect("failed to read response"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
24 assert_eq!(buf, RESPONSE); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
25 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
26 let status = slave.wait().expect("faild to wait for slave"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
27 if !status.success() { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
28 panic!("slave did not exit with succcess, status: {}", status); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
29 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
30 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
31 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
32 fn main_slave(addr: &str) { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
33 use systemd_socket::SocketAddr; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
34 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
35 let socket = addr |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
36 .parse::<SocketAddr>() |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
37 .expect("failed to parse socket") |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
38 .bind() |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
39 .expect("failed to bind"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
40 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
41 let (mut client_socket, _) = socket.accept().expect("failed to accept"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
42 let mut buf = [0u8; REQUEST.len()]; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
43 client_socket.read_exact(&mut buf).expect("failed to read response"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
44 assert_eq!(buf, REQUEST); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
45 client_socket.write_all(RESPONSE).expect("failed to send data"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
46 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
47 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
48 pub fn main<T: Test>() { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
49 let mut args = std::env::args_os(); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
50 let program_name = args.next().expect("missing program name"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
51 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
52 match std::env::var_os("SYSTEMD_SOCKET_INTEGRATION_TEST") { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
53 None => main_master(T::spawn_slave(&program_name)), |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
54 Some(arg) if arg == "slave" => main_slave(T::SOCKET_ADDR), |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
55 Some(arg) => panic!("Unknown argument '{:?}'", arg), |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
56 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
57 } |