-
Notifications
You must be signed in to change notification settings - Fork 80
feat: add Hickory DNS server module #472
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
Open
gibbz00
wants to merge
2
commits into
testcontainers:main
Choose a base branch
from
gibbz00:hickory_dns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| use std::borrow::Cow; | ||
|
|
||
| use testcontainers::{ | ||
| core::{ContainerPort, WaitFor}, | ||
| CopyDataSource, CopyToContainer, Image, | ||
| }; | ||
|
|
||
| const NAME: &str = "hickorydns/hickory-dns"; | ||
| const TAG: &str = "latest"; | ||
|
|
||
| const CONFIG_PATH: &str = "/etc/named.toml"; | ||
| const ZONE_DIR: &str = "/var/named"; | ||
|
|
||
| /// Module to work with a [`Hickory DNS`] server inside of tests. | ||
| /// | ||
| /// Based on the official [`Hickory DNS docker image`]. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// use testcontainers_modules::{hickory_dns::HickoryDns, testcontainers::runners::SyncRunner}; | ||
| /// | ||
| /// const CONFIG: &str = r#" | ||
| /// [[zones]] | ||
| /// file = "example.com.zone" | ||
| /// zone = "example.com" | ||
| /// zone_type = "Primary" | ||
| /// "#; | ||
| /// | ||
| /// const ZONE: &str = r#" | ||
| /// @ 3600 IN SOA ns.example.com. admin.example.com. ( | ||
| /// 1 ; SerialNA | ||
| /// 1h ; Refresh | ||
| /// 10m ; Retry | ||
| /// 10d ; Expire | ||
| /// 10h ; Negative Caching TTL | ||
| /// ) | ||
| /// | ||
| /// simple IN A 10.0.0.1 | ||
| /// "#; | ||
| /// | ||
| /// let instance = HickoryDns::new(CONFIG.as_bytes().to_vec()) | ||
| /// .with_zone("example.com.zone", ZONE.as_bytes().to_vec()) | ||
| /// .start() | ||
| /// .unwrap(); | ||
| /// | ||
| /// let port = instance | ||
| /// .get_host_port_ipv4(HickoryDns::INTERNAL_PORT) | ||
| /// .unwrap(); | ||
| /// | ||
| /// let dig_str = format!("dig @127.0.0.1 -p {port} simple.example.com."); | ||
| /// ``` | ||
| /// | ||
| /// [`Hickory DNS`]: https://github.com/hickory-dns/hickory-dns | ||
| /// [`Hickory DNS docker image`]: https://hub.docker.com/r/hickorydns/hickory-dns | ||
| #[derive(Debug, Clone)] | ||
| pub struct HickoryDns { | ||
| files: Vec<CopyToContainer>, | ||
| } | ||
|
|
||
| impl HickoryDns { | ||
| /// Internal port for TCP and UDP connections. | ||
| pub const INTERNAL_PORT: u16 = 53; | ||
|
|
||
| /// # Arguments | ||
| /// | ||
| /// - `config`: Server config file to be placed in `/etc/named.toml`. Futher | ||
| /// example configurations are provided in the project's [`test_configs`] | ||
| /// directory. | ||
| /// | ||
| /// [`test_configs`]: https://github.com/hickory-dns/hickory-dns/tree/main/tests/test-data/test_configs | ||
| pub fn new(config: impl Into<CopyDataSource>) -> Self { | ||
| let config_file = CopyToContainer::new(config.into(), CONFIG_PATH); | ||
|
|
||
| Self { | ||
| files: vec![config_file], | ||
| } | ||
| } | ||
|
|
||
| /// # Arguments | ||
| /// | ||
| /// - `filename`: Referenced from the config file for the corresponding zone. | ||
| /// - `description`: Zone file description as described in [RFC 1034 (section 3.6.1)][RFC1034] and [RFC 1035 (section 5)][RFC1035] | ||
| /// | ||
| /// [RFC1034]: https://datatracker.ietf.org/doc/html/rfc1034#section-3.6.1 | ||
| /// [RFC1035]: https://datatracker.ietf.org/doc/html/rfc1035#section-5 | ||
| pub fn with_zone( | ||
| mut self, | ||
| filename: impl Into<Cow<'static, str>>, | ||
| description: impl Into<CopyDataSource>, | ||
| ) -> Self { | ||
| let target = format!("{ZONE_DIR}/{}", filename.into()); | ||
| let zone_file = CopyToContainer::new(description, target); | ||
| self.files.push(zone_file); | ||
|
|
||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Image for HickoryDns { | ||
| fn name(&self) -> &str { | ||
| NAME | ||
| } | ||
|
|
||
| fn tag(&self) -> &str { | ||
| TAG | ||
| } | ||
|
|
||
| fn ready_conditions(&self) -> Vec<WaitFor> { | ||
| vec![WaitFor::message_on_stdout( | ||
| "server starting up, awaiting connections...", | ||
| )] | ||
| } | ||
|
|
||
| fn expose_ports(&self) -> &[testcontainers::core::ContainerPort] { | ||
| &[ | ||
| ContainerPort::Tcp(Self::INTERNAL_PORT), | ||
| ContainerPort::Udp(Self::INTERNAL_PORT), | ||
| ] | ||
| } | ||
|
|
||
| fn copy_to_sources(&self) -> impl IntoIterator<Item = &CopyToContainer> { | ||
| &self.files | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::net::{IpAddr, Ipv4Addr}; | ||
|
|
||
| use hickory_resolver::{ | ||
| config::{ConnectionConfig, NameServerConfig, ProtocolConfig, ResolverConfig}, | ||
| net::runtime::TokioRuntimeProvider, | ||
| }; | ||
| use testcontainers::runners::AsyncRunner; | ||
|
|
||
| use super::*; | ||
|
|
||
| const CONFIG: &str = r#" | ||
| [[zones]] | ||
| file = "example.com.zone" | ||
| zone = "example.com" | ||
| zone_type = "Primary" | ||
| "#; | ||
|
|
||
| const ZONE: &str = r#" | ||
| @ 3600 IN SOA ns.example.com. admin.example.com. ( | ||
| 1 ; SerialNA | ||
| 1h ; Refresh | ||
| 10m ; Retry | ||
| 10d ; Expire | ||
| 10h ; Negative Caching TTL | ||
| ) | ||
|
|
||
| simple IN A 10.0.0.1 | ||
| "#; | ||
|
|
||
| #[tokio::test] | ||
| async fn a_record_query() -> Result<(), Box<dyn std::error::Error + 'static>> { | ||
| let container = HickoryDns::new(CONFIG.as_bytes().to_vec()) | ||
| .with_zone("example.com.zone", ZONE.as_bytes().to_vec()) | ||
| .start() | ||
| .await?; | ||
|
|
||
| let port = container | ||
| .get_host_port_ipv4(HickoryDns::INTERNAL_PORT) | ||
| .await?; | ||
|
|
||
| let name_server_config = { | ||
| let mut tcp_connection = ConnectionConfig::new(ProtocolConfig::Tcp); | ||
| tcp_connection.port = port; | ||
|
|
||
| let mut udp_connection = ConnectionConfig::new(ProtocolConfig::Udp); | ||
| udp_connection.port = port; | ||
|
|
||
| NameServerConfig::new( | ||
| IpAddr::V4(Ipv4Addr::LOCALHOST), | ||
| true, | ||
| vec![udp_connection, tcp_connection], | ||
| ) | ||
| }; | ||
|
|
||
| let resolver_config = ResolverConfig::from_parts(None, vec![], vec![name_server_config]); | ||
|
|
||
| let resolver = hickory_resolver::Resolver::builder_with_config( | ||
| resolver_config, | ||
| TokioRuntimeProvider::new(), | ||
| ) | ||
| .build()?; | ||
|
|
||
| let response = resolver.ipv4_lookup("simple.example.com.").await?; | ||
|
|
||
| let actual = response.answers().first().unwrap().data.ip_addr().unwrap(); | ||
|
|
||
| let expected = "10.0.0.1".parse::<IpAddr>().unwrap(); | ||
|
|
||
| assert_eq!(expected, actual); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We avoid using latest as the default, because like this what the module provides can break in the future when there are updates in the image that would need changes here (e.g. the waiting condition).
What we usually do is reference a fixed release version here and only update it if we need to (e.g. in the example above where we need to change something to be compatible).