Skip to content

Commit 5a66c9b

Browse files
committed
refactor: extract parse_resolv_conf so tests call the real function
Move the resolv.conf parsing logic out of resolve_upstream_dns() into its own parse_resolv_conf() function. The 10 deterministic tests now exercise the production code path instead of a reimplemented helper. Signed-off-by: Brian Taylor <brian.taylor818@gmail.com>
1 parent bacc15d commit 5a66c9b

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

crates/openshell-bootstrap/src/docker.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -251,25 +251,30 @@ fn home_dir() -> Option<String> {
251251
/// would bypass that proxy and break DNS.
252252
///
253253
/// Returns an empty vec if no usable resolvers are found.
254+
/// Parse resolv.conf content, extracting nameserver IPs and filtering loopback addresses.
255+
fn parse_resolv_conf(contents: &str) -> Vec<String> {
256+
contents
257+
.lines()
258+
.filter_map(|line| {
259+
let line = line.trim();
260+
if !line.starts_with("nameserver") {
261+
return None;
262+
}
263+
let ip = line.split_whitespace().nth(1)?;
264+
if ip.starts_with("127.") || ip == "::1" {
265+
return None;
266+
}
267+
Some(ip.to_string())
268+
})
269+
.collect()
270+
}
271+
254272
fn resolve_upstream_dns() -> Vec<String> {
255273
let paths = ["/run/systemd/resolve/resolv.conf"];
256274

257275
for path in &paths {
258276
if let Ok(contents) = std::fs::read_to_string(path) {
259-
let resolvers: Vec<String> = contents
260-
.lines()
261-
.filter_map(|line| {
262-
let line = line.trim();
263-
if !line.starts_with("nameserver") {
264-
return None;
265-
}
266-
let ip = line.split_whitespace().nth(1)?;
267-
if ip.starts_with("127.") || ip == "::1" {
268-
return None;
269-
}
270-
Some(ip.to_string())
271-
})
272-
.collect();
277+
let resolvers = parse_resolv_conf(&contents);
273278

274279
if !resolvers.is_empty() {
275280
tracing::debug!(
@@ -1285,25 +1290,6 @@ mod tests {
12851290
);
12861291
}
12871292

1288-
/// Helper: parse resolv.conf content using the same logic as resolve_upstream_dns().
1289-
/// Allows deterministic testing without depending on host DNS config.
1290-
fn parse_resolv_conf(contents: &str) -> Vec<String> {
1291-
contents
1292-
.lines()
1293-
.filter_map(|line| {
1294-
let line = line.trim();
1295-
if !line.starts_with("nameserver") {
1296-
return None;
1297-
}
1298-
let ip = line.split_whitespace().nth(1)?;
1299-
if ip.starts_with("127.") || ip == "::1" {
1300-
return None;
1301-
}
1302-
Some(ip.to_string())
1303-
})
1304-
.collect()
1305-
}
1306-
13071293
#[test]
13081294
fn parse_resolv_conf_filters_ipv4_loopback() {
13091295
let input = "nameserver 127.0.0.1\nnameserver 127.0.0.53\nnameserver 127.0.0.11\n";

0 commit comments

Comments
 (0)