test nameserver with dig
This commit is contained in:
parent
cc753de77c
commit
61bb4bb315
60
src/lib.rs
60
src/lib.rs
@ -1,5 +1,5 @@
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::process::{self, ExitStatus, Output};
|
use std::process::{self, Child, Output};
|
||||||
use std::sync::atomic;
|
use std::sync::atomic;
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
@ -48,7 +48,7 @@ fn nsd_conf(domain: &str) -> String {
|
|||||||
template.render(context! { domain => domain }).unwrap()
|
template.render(context! { domain => domain }).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Domain<'a> {
|
pub enum Domain<'a> {
|
||||||
Root,
|
Root,
|
||||||
Tld { domain: &'a str },
|
Tld { domain: &'a str },
|
||||||
}
|
}
|
||||||
@ -63,11 +63,12 @@ impl Domain<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct NsdContainer {
|
pub struct NsdContainer {
|
||||||
inner: Container,
|
child: Child,
|
||||||
|
container: Container,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NsdContainer {
|
impl NsdContainer {
|
||||||
pub fn new(domain: Domain) -> Result<Self> {
|
pub fn start(domain: Domain) -> Result<Self> {
|
||||||
let container = Container::run(Image::Nsd)?;
|
let container = Container::run(Image::Nsd)?;
|
||||||
|
|
||||||
container.exec(&["mkdir", "-p", "/etc/nsd/zones"])?;
|
container.exec(&["mkdir", "-p", "/etc/nsd/zones"])?;
|
||||||
@ -86,11 +87,19 @@ impl NsdContainer {
|
|||||||
|
|
||||||
container.cp(zone_path, &zone_file_contents, CHMOD_RW_EVERYONE)?;
|
container.cp(zone_path, &zone_file_contents, CHMOD_RW_EVERYONE)?;
|
||||||
|
|
||||||
Ok(Self { inner: container })
|
let child = container.spawn(&["nsd", "-d"])?;
|
||||||
|
|
||||||
|
Ok(Self { child, container })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&self) -> Result<ExitStatus> {
|
pub fn ip_addr(&self) -> Result<String> {
|
||||||
self.inner.exec2(&["nsd", "-d"])
|
self.container.ip_addr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for NsdContainer {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = self.child.kill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,14 +199,13 @@ impl Container {
|
|||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME
|
pub fn spawn(&self, cmd: &[&str]) -> Result<Child> {
|
||||||
pub fn exec2(&self, cmd: &[&str]) -> Result<ExitStatus> {
|
|
||||||
let mut command = Command::new("docker");
|
let mut command = Command::new("docker");
|
||||||
command.args(&["exec", "-t", &self.id]).args(cmd);
|
command.args(&["exec", "-t", &self.id]).args(cmd);
|
||||||
|
|
||||||
let status = command.status()?;
|
let child = command.spawn()?;
|
||||||
|
|
||||||
Ok(status)
|
Ok(child)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ip_addr(&self) -> Result<String> {
|
pub fn ip_addr(&self) -> Result<String> {
|
||||||
@ -296,28 +304,34 @@ mod tests {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO create `nsd.conf` file at runtime
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tld_setup() -> Result<()> {
|
fn tld_setup() -> Result<()> {
|
||||||
let tld_ns = NsdContainer::new(Domain::Tld { domain: "com." })?;
|
let tld_ns = NsdContainer::start(Domain::Tld { domain: "com." })?;
|
||||||
tld_ns.start()?;
|
let ip_addr = tld_ns.ip_addr()?;
|
||||||
|
|
||||||
|
let client = Container::run(Image::Client)?;
|
||||||
|
let output = client.exec(&["dig", &format!("@{ip_addr}"), "SOA", "com."])?;
|
||||||
|
|
||||||
|
assert!(output.status.success());
|
||||||
|
let stdout = core::str::from_utf8(&output.stdout)?;
|
||||||
|
println!("{stdout}");
|
||||||
|
assert!(stdout.contains("status: NOERROR"));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn root_setup() -> Result<()> {
|
fn root_setup() -> Result<()> {
|
||||||
let root_ns = NsdContainer::new(Domain::Root)?;
|
let root_ns = NsdContainer::start(Domain::Root)?;
|
||||||
root_ns.start()?;
|
let ip_addr = root_ns.ip_addr()?;
|
||||||
|
|
||||||
// let root_ns = Container::run(Image::Nsd)?;
|
let client = Container::run(Image::Client)?;
|
||||||
|
let output = client.exec(&["dig", &format!("@{ip_addr}"), "SOA", "."])?;
|
||||||
|
|
||||||
// root_ns.exec(&["mkdir", "-p", "/etc/nsd/zones"])?;
|
assert!(output.status.success());
|
||||||
// let zone_path = "/etc/nsd/zones/main.zone";
|
let stdout = core::str::from_utf8(&output.stdout)?;
|
||||||
// root_ns.cp("/etc/nsd/nsd.conf", &nsd_conf("."), CHMOD_RW_EVERYONE)?;
|
println!("{stdout}");
|
||||||
// root_ns.cp(zone_path, &root_zone(), CHMOD_RW_EVERYONE)?;
|
assert!(stdout.contains("status: NOERROR"));
|
||||||
|
|
||||||
// root_ns.exec2(&["nsd", "-d"])?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user