Add unit tests for Labeled middleware (#738)

I've added unit tests for the `Labeled` middleware used to add Destination labels in the proxy, as @olix0r requested in https://github.com/runconduit/conduit/pull/661#discussion_r179897783. 

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
Eliza Weisman 2018-04-12 15:10:01 -07:00 committed by GitHub
parent 61d15a6c3e
commit b6180d8bfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 84 additions and 0 deletions

View File

@ -282,3 +282,87 @@ impl fmt::Display for DstLabels {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod test {
use super::*;
use futures::{Async, Poll, Future};
use futures::future::{self, FutureResult};
use http;
use tower::Service;
struct MockInnerService;
impl Service for MockInnerService {
type Request = http::Request<()>;
type Response = Option<String>;
type Error = ();
type Future = FutureResult<Self::Response, Self::Error>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
future::ok(req.extensions()
.get::<DstLabels>()
.map(|&DstLabels(ref inner)| inner.as_ref().to_string()))
}
}
#[test]
fn no_labels() {
let mut labeled = Labeled {
metric_labels: None,
inner: MockInnerService,
};
let labels = labeled.call(http::Request::new(()))
.wait().expect("call");
assert_eq!(None, labels);
}
#[test]
fn one_label() {
let (watch, _) =
Watch::new(DstLabels::new(vec![("foo", "bar")]));
let mut labeled = Labeled {
metric_labels: Some(watch),
inner: MockInnerService,
};
let labels = labeled.call(http::Request::new(()))
.wait().expect("call");
assert_eq!(Some("dst_foo=\"bar\"".to_string()), labels);
}
#[test]
fn label_updates() {
let (watch, mut store) =
Watch::new(DstLabels::new(vec![("foo", "bar")]));
let mut labeled = Labeled {
metric_labels: Some(watch),
inner: MockInnerService,
};
let labels = labeled.call(http::Request::new(()))
.wait().expect("first call");
assert_eq!(Some("dst_foo=\"bar\"".to_string()), labels);
store.store(DstLabels::new(vec![("foo", "baz")]))
.expect("store (\"foo\", \"baz\")");
let labels = labeled.call(http::Request::new(()))
.wait().expect("second call");
assert_eq!(Some("dst_foo=\"baz\"".to_string()), labels);
store.store(DstLabels::new(vec![
("foo", "baz"),
("quux", "quuux")
]))
.expect("store (\"foo\", \"baz\"), (\"quux\", \"quuux\")");
let labels = labeled.call(http::Request::new(()))
.wait().expect("third call");
assert_eq!(Some("dst_foo=\"baz\",dst_quux=\"quuux\"".to_string()), labels);
}
}