mssql_testing/
container.rs

1//! SQL Server container support via testcontainers.
2
3use testcontainers::Image;
4use testcontainers::core::{ContainerPort, WaitFor};
5
6/// SQL Server container image.
7///
8/// Uses the official Microsoft SQL Server container image.
9#[derive(Debug, Clone)]
10pub struct SqlServerContainer {
11    /// SQL Server SA password.
12    pub password: String,
13    /// Container tag (version).
14    pub tag: String,
15}
16
17impl Default for SqlServerContainer {
18    fn default() -> Self {
19        Self {
20            password: "Password123!".to_string(),
21            tag: "2022-latest".to_string(),
22        }
23    }
24}
25
26impl SqlServerContainer {
27    /// Create a new SQL Server container configuration.
28    #[must_use]
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    /// Set the SA password.
34    #[must_use]
35    pub fn with_password(mut self, password: impl Into<String>) -> Self {
36        self.password = password.into();
37        self
38    }
39
40    /// Set the container tag (SQL Server version).
41    #[must_use]
42    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
43        self.tag = tag.into();
44        self
45    }
46}
47
48impl Image for SqlServerContainer {
49    fn name(&self) -> &str {
50        "mcr.microsoft.com/mssql/server"
51    }
52
53    fn tag(&self) -> &str {
54        &self.tag
55    }
56
57    fn ready_conditions(&self) -> Vec<WaitFor> {
58        vec![
59            WaitFor::message_on_stdout("SQL Server is now ready for client connections"),
60            WaitFor::seconds(5),
61        ]
62    }
63
64    fn env_vars(
65        &self,
66    ) -> impl IntoIterator<
67        Item = (
68            impl Into<std::borrow::Cow<'_, str>>,
69            impl Into<std::borrow::Cow<'_, str>>,
70        ),
71    > {
72        vec![
73            ("ACCEPT_EULA", "Y"),
74            ("MSSQL_SA_PASSWORD", self.password.as_str()),
75            ("MSSQL_PID", "Developer"),
76        ]
77    }
78
79    fn expose_ports(&self) -> &[ContainerPort] {
80        &[ContainerPort::Tcp(1433)]
81    }
82}