-
Notifications
You must be signed in to change notification settings - Fork 1
More Integration Tests for datadog-trace-agent #62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
070093d
Add an integration test that covers all of datadog-trace-agent
Lewis-E f024d26
Update integration test windows pipe names
Lewis-E 97598ae
Add windows full integration test for datadog-trace-agent
Lewis-E d8d7775
Extract test helpers
Lewis-E dfea0d4
Improve test quality
Lewis-E 7922bac
Add type
Lewis-E 5518b13
Have windows tests run with the correct pipe features
Lewis-E 7dda491
Fix dogstatsd initialization
Lewis-E File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Simple mock HTTP server for testing flushers | ||
|
|
||
| use http_body_util::BodyExt; | ||
| use hyper::{body::Incoming, Request, Response}; | ||
| use hyper_util::rt::TokioIo; | ||
| use libdd_common::hyper_migration; | ||
| use std::net::SocketAddr; | ||
| use std::sync::{Arc, Mutex}; | ||
| use tokio::net::TcpListener; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct ReceivedRequest { | ||
| pub method: String, | ||
| pub path: String, | ||
| pub headers: Vec<(String, String)>, | ||
| pub body: Vec<u8>, | ||
| } | ||
|
|
||
| pub struct MockServer { | ||
| pub addr: SocketAddr, | ||
| pub received_requests: Arc<Mutex<Vec<ReceivedRequest>>>, | ||
| shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>, | ||
| } | ||
|
|
||
| impl MockServer { | ||
| /// Start a mock HTTP server on a random port | ||
| pub async fn start() -> Self { | ||
| let listener = TcpListener::bind("127.0.0.1:0") | ||
| .await | ||
| .expect("Failed to bind mock server"); | ||
| let addr = listener.local_addr().expect("Failed to get local addr"); | ||
|
|
||
| let received_requests = Arc::new(Mutex::new(Vec::new())); | ||
| let requests_clone = received_requests.clone(); | ||
|
|
||
| let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel(); | ||
|
|
||
| tokio::spawn(async move { | ||
| loop { | ||
| tokio::select! { | ||
| result = listener.accept() => { | ||
| let (stream, _) = match result { | ||
| Ok(conn) => conn, | ||
| Err(e) => { | ||
| eprintln!("Mock server accept error: {}", e); | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| let io = TokioIo::new(stream); | ||
| let requests = requests_clone.clone(); | ||
|
|
||
| tokio::spawn(async move { | ||
| let service = hyper::service::service_fn(move |req: Request<Incoming>| { | ||
| let requests = requests.clone(); | ||
| async move { | ||
| // Capture the request | ||
| let method = req.method().to_string(); | ||
| let path = req.uri().path().to_string(); | ||
| let headers: Vec<(String, String)> = req | ||
| .headers() | ||
| .iter() | ||
| .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string())) | ||
| .collect(); | ||
|
|
||
| // Read the body | ||
| let body_bytes = req | ||
| .into_body() | ||
| .collect() | ||
| .await | ||
| .map(|collected| collected.to_bytes().to_vec()) | ||
| .unwrap_or_default(); | ||
|
|
||
| // Store the request | ||
| requests.lock().unwrap().push(ReceivedRequest { | ||
| method, | ||
| path, | ||
| headers, | ||
| body: body_bytes, | ||
| }); | ||
|
|
||
| // Return 200 OK | ||
| Ok::<_, hyper::http::Error>( | ||
| Response::builder() | ||
| .status(200) | ||
| .body(hyper_migration::Body::from(r#"{"ok":true}"#)) | ||
| .unwrap(), | ||
| ) | ||
| } | ||
| }); | ||
|
|
||
| let _ = hyper::server::conn::http1::Builder::new() | ||
| .serve_connection(io, service) | ||
| .await; | ||
| }); | ||
| } | ||
| _ = &mut shutdown_rx => { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| MockServer { | ||
| addr, | ||
| received_requests, | ||
| shutdown_tx: Some(shutdown_tx), | ||
| } | ||
| } | ||
|
|
||
| /// Get the base URL of the mock server | ||
| pub fn url(&self) -> String { | ||
| format!("http://{}", self.addr) | ||
| } | ||
|
|
||
| /// Get all received requests | ||
| #[allow(dead_code)] | ||
| pub fn get_requests(&self) -> Vec<ReceivedRequest> { | ||
| self.received_requests.lock().unwrap().clone() | ||
| } | ||
|
|
||
| /// Get requests matching a path | ||
| pub fn get_requests_for_path(&self, path: &str) -> Vec<ReceivedRequest> { | ||
| self.received_requests | ||
| .lock() | ||
| .unwrap() | ||
| .iter() | ||
| .filter(|req| req.path == path) | ||
| .cloned() | ||
| .collect() | ||
| } | ||
|
|
||
| /// Clear all received requests | ||
| #[allow(dead_code)] | ||
| pub fn clear_requests(&self) { | ||
| self.received_requests.lock().unwrap().clear(); | ||
| } | ||
| } | ||
|
|
||
| impl Drop for MockServer { | ||
| fn drop(&mut self) { | ||
| if let Some(shutdown_tx) = self.shutdown_tx.take() { | ||
| let _ = shutdown_tx.send(()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.