Files
uv/crates/uv-client/tests/user_agent_version.rs
T
samypr100 42973cd9cb feat: add linehaul info to uv-client (#2493)
## Summary

Closes #1958

This adds linehaul metadata to uv's user-agent when pep 508 markers are
provided to the RegistryClientBuilder. Thanks to #2381, we were able to
leverage most information from markers and avoid inconsistency.

Linehaul is meant to be accompanying metadata pip sends in it's user
agent when talking to registries. You can see this output by running
something like `python -c 'from pip._internal.network.session import
user_agent; print(user_agent())'`.
In PyPI, this metadata processed by the
[linehaul-cloud-function](https://github.com/pypi/linehaul-cloud-function).
More info about linehaul can be found in #1958.

Below are some examples from pip:

* Linux GHA: `pip/24.0
{"ci":true,"cpu":"x86_64","distro":{"id":"jammy","libc":{"lib":"glibc","version":"2.35"},"name":"Ubuntu","version":"22.04"},"implementation":{"name":"CPython","version":"3.12.2"},"installer":{"name":"pip","version":"24.0"},"openssl_version":"OpenSSL
3.0.2 15 Mar
2022","python":"3.12.2","rustc_version":"1.76.0","system":{"name":"Linux","release":"6.5.0-1016-azure"}}`
* Windows GHA: `pip/24.0
{"ci":true,"cpu":"AMD64","implementation":{"name":"CPython","version":"3.12.2"},"installer":{"name":"pip","version":"24.0"},"openssl_version":"OpenSSL
3.0.13 30 Jan
2024","python":"3.12.2","rustc_version":"1.76.0","system":{"name":"Windows","release":"2022Server"}}`
* OSX GHA: `pip/24.0
{"ci":true,"cpu":"arm64","distro":{"name":"macOS","version":"14.2.1"},"implementation":{"name":"CPython","version":"3.12.2"},"installer":{"name":"pip","version":"24.0"},"openssl_version":"OpenSSL
3.0.13 30 Jan
2024","python":"3.12.2","rustc_version":"1.76.0","system":{"name":"Darwin","release":"23.2.0"}}`



Here's how uv results look like (sorry for the keys not having the same
order):

* Linux GHA: `uv/0.1.21
{"installer":{"name":"uv","version":"0.1.21"},"python":"3.12.2","implementation":{"name":"CPython","version":"3.12.2"},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":"Linux","release":"6.5.0-1016-azure"},"cpu":"x86_64","openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}`
* Windows GHA: `uv/0.1.21
{"installer":{"name":"uv","version":"0.1.21"},"python":"3.12.2","implementation":{"name":"CPython","version":"3.12.2"},"distro":null,"system":{"name":"Windows","release":"2022Server"},"cpu":"AMD64","openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}`
* OSX GHA: `uv/0.1.21
{"installer":{"name":"uv","version":"0.1.21"},"python":"3.12.2","implementation":{"name":"CPython","version":"3.12.2"},"distro":{"name":"macOS","version":"14.2.1","id":null,"libc":null},"system":{"name":"Darwin","release":"23.2.0"},"cpu":"arm64","openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}`

Distro information (such as the one pip uses `from pip._vendor import
distro` to retrieve instead of `platform` module) was not retrieved from
markers. Instead, the linux release codename/name/version uses
`sys-info` crate, adding about 50us of extra overhead on linux. The
distro osx version re-used the [mac_os version
implementation](https://github.com/astral-sh/uv/blob/99c992e38b220fbcda09b0b43602b3db2321480b/crates/platform-host/src/mac_os.rs)
from #2381 which adds about 20us of overhead on osx. I tried to use
other crates to avoid re-introducing `mac_os.rs` but most of them didn't
yield satisfactory performance (40ms-60ms~) or had the wrong values
needed (e.g. darwin version vs osx version).

I also didn't add libc retrieval or rustc retrieval as those seem to add
substantial overhead due to querying `ldd` or `rustc`. PyPy version
detection was also not added to avoid adding extra overhead to [support
PyPy for
linehaul](https://github.com/pypa/pip/blob/24.0/src/pip/_internal/network/session.py#L123).
All other behavior was kept 1-1 to match what pip's linehaul
implementation does (as of 24.0). This also aligns with what was
discussed in #1958.

## Test Plan

Added new integration test to uv-client.

---------

Co-authored-by: konstin <konstin@mailbox.org>
2024-03-18 10:46:58 +00:00

216 lines
6.8 KiB
Rust

use anyhow::Result;
use futures::future;
use hyper::header::USER_AGENT;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{Body, Request, Response};
use pep508_rs::{MarkerEnvironment, StringVersion};
use platform_tags::{Arch, Os, Platform};
use tokio::net::TcpListener;
use uv_cache::Cache;
use uv_client::LineHaul;
use uv_client::RegistryClientBuilder;
use uv_version::version;
#[tokio::test]
async fn test_user_agent_has_version() -> Result<()> {
// Set up the TCP listener on a random available port
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
// Spawn the server loop in a background task
tokio::spawn(async move {
let svc = service_fn(move |req: Request<Body>| {
// Get User Agent Header and send it back in the response
let user_agent = req
.headers()
.get(USER_AGENT)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
.unwrap_or_default(); // Empty Default
future::ok::<_, hyper::Error>(Response::new(Body::from(user_agent)))
});
// Start Hyper Server
let (socket, _) = listener.accept().await.unwrap();
Http::new()
.http1_keep_alive(false)
.serve_connection(socket, svc)
.with_upgrades()
.await
.expect("Server Started");
});
// Initialize uv-client
let cache = Cache::temp()?;
let client = RegistryClientBuilder::new(cache).build();
// Send request to our dummy server
let res = client
.uncached_client()
.get(format!("http://{addr}"))
.send()
.await?;
// Check the HTTP status
assert!(res.status().is_success());
// Check User Agent
let body = res.text().await?;
// Verify body matches regex
assert_eq!(body, format!("uv/{}", version()));
Ok(())
}
#[tokio::test]
async fn test_user_agent_has_linehaul() -> Result<()> {
// Set up the TCP listener on a random available port
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
// Spawn the server loop in a background task
tokio::spawn(async move {
let svc = service_fn(move |req: Request<Body>| {
// Get User Agent Header and send it back in the response
let user_agent = req
.headers()
.get(USER_AGENT)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
.unwrap_or_default(); // Empty Default
future::ok::<_, hyper::Error>(Response::new(Body::from(user_agent)))
});
// Start Hyper Server
let (socket, _) = listener.accept().await.unwrap();
Http::new()
.http1_keep_alive(false)
.serve_connection(socket, svc)
.with_upgrades()
.await
.expect("Server Started");
});
// Add some representative markers for an Ubuntu CI runner
let markers = MarkerEnvironment {
implementation_name: "cpython".to_string(),
implementation_version: StringVersion {
string: "3.12.2".to_string(),
version: "3.12.2".parse()?,
},
os_name: "posix".to_string(),
platform_machine: "x86_64".to_string(),
platform_python_implementation: "CPython".to_string(),
platform_release: "6.5.0-1016-azure".to_string(),
platform_system: "Linux".to_string(),
platform_version: "#16~22.04.1-Ubuntu SMP Fri Feb 16 15:42:02 UTC 2024".to_string(),
python_full_version: StringVersion {
string: "3.12.2".to_string(),
version: "3.12.2".parse()?,
},
python_version: StringVersion {
string: "3.12".to_string(),
version: "3.12".parse()?,
},
sys_platform: "linux".to_string(),
};
// Linux only
let platform = Platform::new(
Os::Manylinux {
major: 2,
minor: 38,
},
Arch::X86_64,
);
// Initialize uv-client
let cache = Cache::temp()?;
let mut builder = RegistryClientBuilder::new(cache).markers(&markers);
if cfg!(target_os = "linux") {
builder = builder.platform(&platform);
}
let client = builder.build();
// Send request to our dummy server
let res = client
.uncached_client()
.get(format!("http://{addr}"))
.send()
.await?;
// Check the HTTP status
assert!(res.status().is_success());
// Check User Agent
let body = res.text().await?;
// Unpack User-Agent with linehaul
let (uv_version, uv_linehaul) = body
.split_once(' ')
.expect("Failed to split User-Agent header.");
// Deserializing Linehaul
let linehaul: LineHaul = serde_json::from_str(uv_linehaul)?;
// Assert uv version
assert_eq!(uv_version, format!("uv/{}", version()));
// Assert linehaul
let installer_info = linehaul.installer.unwrap();
let system_info = linehaul.system.unwrap();
let impl_info = linehaul.implementation.unwrap();
assert_eq!(installer_info.name.unwrap(), "uv".to_string());
assert_eq!(installer_info.version.unwrap(), version());
assert_eq!(system_info.name.unwrap(), markers.platform_system);
assert_eq!(system_info.release.unwrap(), markers.platform_release);
assert_eq!(
impl_info.name.unwrap(),
markers.platform_python_implementation
);
assert_eq!(
impl_info.version.unwrap(),
markers.python_full_version.version.to_string()
);
assert_eq!(
linehaul.python.unwrap(),
markers.python_full_version.version.to_string()
);
assert_eq!(linehaul.cpu.unwrap(), markers.platform_machine);
assert_eq!(linehaul.openssl_version, None);
assert_eq!(linehaul.setuptools_version, None);
assert_eq!(linehaul.rustc_version, None);
#[cfg(windows)]
assert_eq!(linehaul.distro, None);
// Using os_info as to confirm our values are as expected in both Linux and OSX.
#[cfg(target_os = "linux")]
{
let info = os_info::get();
let distro_info = linehaul.distro.unwrap();
assert_eq!(distro_info.id.unwrap(), info.codename().unwrap());
assert_eq!(distro_info.name.unwrap(), info.os_type().to_string());
assert_eq!(distro_info.version.unwrap(), info.version().to_string());
assert!(distro_info.libc.is_some());
}
// Using os_info as sys-info yields Darwin version, and not mac release version.
#[cfg(target_os = "macos")]
{
let info = os_info::get();
let distro_info = linehaul.distro.unwrap();
assert_eq!(distro_info.id, None);
assert_eq!(distro_info.name.unwrap(), "macOS");
assert_eq!(distro_info.version.unwrap(), info.version().to_string());
assert_eq!(distro_info.libc, None);
}
Ok(())
}