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

Add metrics integration test #76

Merged
merged 2 commits into from
Jul 17, 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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ rand = "0.7.3"
warp = "0.2.3"

[dev-dependencies]
reqwest = "0.10.6"
regex = "1.3.9"
13 changes: 12 additions & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,19 @@ pub async fn echo_server() -> SocketAddr {

// run_proxy creates a instance of the Server proxy and runs it, returning a cancel function
pub fn run_proxy(logger: &Logger, registry: FilterRegistry, config: Config) -> Box<dyn FnOnce()> {
run_proxy_with_metrics(logger, registry, config, Metrics::default())
}

// run_proxy_with_metrics creates a instance of the Server proxy and
// runs it, returning a cancel function
pub fn run_proxy_with_metrics(
logger: &Logger,
registry: FilterRegistry,
config: Config,
metrics: Metrics,
) -> Box<dyn FnOnce()> {
let (close, stop) = oneshot::channel::<()>();
let proxy = Server::new(logger.clone(), registry, Metrics::default());
let proxy = Server::new(logger.clone(), registry, metrics);
// run the proxy
tokio::spawn(async move {
proxy.run(Arc::new(config), stop).await.unwrap();
Expand Down
110 changes: 110 additions & 0 deletions tests/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2020 Google LLC All Rights Reserved.
*
* 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.
*/

extern crate quilkin;

#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use slog::info;

use prometheus::Registry;
use quilkin::config::{Config, ConnectionConfig, EndPoint, Local};
use quilkin::extensions::FilterRegistry;
use quilkin::server::Metrics;
use quilkin::test_utils::{
echo_server, logger, recv_multiple_packets, run_proxy, run_proxy_with_metrics,
};
use regex::Regex;

#[tokio::test]
async fn metrics_server() {
let base_logger = logger();
let server_metrics = Metrics::new(Some("[::]:9092".parse().unwrap()), Registry::default());

// create two echo servers as endpoints
let echo = echo_server().await;

// create server configuration
let server_port = 12346;
let server_config = Config {
local: Local { port: server_port },
filters: vec![],
connections: ConnectionConfig::Server {
endpoints: vec![EndPoint {
name: "server".to_string(),
address: echo,
connection_ids: vec![],
}],
},
};

let close_server = run_proxy_with_metrics(
&base_logger,
FilterRegistry::new(),
server_config,
server_metrics,
);

// create a local client
let client_port = 12347;
let client_config = Config {
local: Local { port: client_port },
filters: vec![],
connections: ConnectionConfig::Client {
addresses: vec![SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
server_port,
)],
connection_id: String::from(""),
lb_policy: None,
},
};
let close_client = run_proxy(&base_logger, FilterRegistry::new(), client_config);

// let's send the packet
let (mut recv_chan, mut send) = recv_multiple_packets(&base_logger).await;

// game_client
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), client_port);
info!(base_logger, "Sending hello"; "addr" => local_addr);
send.send_to("hello".as_bytes(), &local_addr).await.unwrap();

let _ = recv_chan.recv().await.unwrap();

let resp = reqwest::get("http://localhost:9092/metrics")
.await
.unwrap()
.text()
.await
.unwrap();

let re =
Regex::new(r#"quilkin_session_tx_packets_total\{downstream="(.*)",upstream="(.*)"} 1"#)
.unwrap();
assert!(re.is_match(&resp));

for c in re.captures_iter(&resp) {
let downstream = (&c[1]).parse::<SocketAddr>().unwrap();
let upstream = (&c[2]).parse::<SocketAddr>().unwrap();
assert_ne!(downstream, upstream);
}

close_server();
close_client();
}
}