Skip to content

Commit

Permalink
move test tools in separate mod
Browse files Browse the repository at this point in the history
Change-Id: I4b4d679954c4d564761e271d60a79171fa9e009e
  • Loading branch information
s-kipnis committed Oct 18, 2023
1 parent 74c0465 commit 8293319
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 74 deletions.
76 changes: 2 additions & 74 deletions packages/check-sql/tests/test_ms_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,9 @@
// This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
// conditions defined in the file COPYING, which is part of this source code package.

use check_sql::{config::CheckConfig, ms_sql::api};

mod tools {
use assert_cmd::Command;
use std::io::Write;
use tempfile::NamedTempFile;

pub fn run_bin() -> Command {
Command::cargo_bin("check-sql").unwrap()
}
mod tools;

pub const SQL_DB_ENDPOINT: &str = "CI_TEST_SQL_DB_ENDPOINT";
const SQL_DB_ENDPOINT_SPLITTER: char = ':';
pub struct SqlDbEndpoint {
pub host: String,
pub user: String,
pub pwd: String,
}

pub fn get_remote_sql_from_env_var() -> Option<SqlDbEndpoint> {
if let Ok(content) = std::env::var(SQL_DB_ENDPOINT) {
let x: Vec<&str> = content.split(SQL_DB_ENDPOINT_SPLITTER).collect();
if x.len() == 3 {
return Some(SqlDbEndpoint {
host: x[0].to_owned(),
user: x[1].to_owned(),
pwd: x[2].to_owned(),
});
} else {
println!(
"Error: environment variable {} is invalid, must have format 'host:user:password' expected",
SQL_DB_ENDPOINT
);
}
} else {
println!("Error: environment variable {} is absent", SQL_DB_ENDPOINT);
}
None
}

pub fn create_remote_config(end_point: &SqlDbEndpoint) -> NamedTempFile {
let mut l = NamedTempFile::new().unwrap();
let config = format!(
r#"
---
mssql:
standard:
authentication:
username: {}
password: {}
type: "sql_server"
connection:
hostname: {}
"#,
end_point.user, end_point.pwd, end_point.host
);
l.write_all(config.as_bytes()).unwrap();
l
}
pub fn create_local_config() -> NamedTempFile {
let mut l = NamedTempFile::new().unwrap();
let config = r#"
---
mssql:
standard:
authentication:
username: "nobody"
type: "integrated"
connection:
hostname: "localhost"
"#;
l.write_all(config.as_bytes()).unwrap();
l
}
}
use check_sql::{config::CheckConfig, ms_sql::api};
#[cfg(windows)]
#[tokio::test(flavor = "multi_thread")]
async fn test_local_connection() {
Expand Down
75 changes: 75 additions & 0 deletions packages/check-sql/tests/tools.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (C) 2023 Checkmk GmbH - License: GNU General Public License v2
// This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
// conditions defined in the file COPYING, which is part of this source code package.

use assert_cmd::Command;
use std::io::Write;
use tempfile::NamedTempFile;

pub fn run_bin() -> Command {
Command::cargo_bin("check-sql").unwrap()
}

pub const SQL_DB_ENDPOINT: &str = "CI_TEST_SQL_DB_ENDPOINT";
const SQL_DB_ENDPOINT_SPLITTER: char = ':';
pub struct SqlDbEndpoint {
pub host: String,
pub user: String,
pub pwd: String,
}

pub fn get_remote_sql_from_env_var() -> Option<SqlDbEndpoint> {
if let Ok(content) = std::env::var(SQL_DB_ENDPOINT) {
let x: Vec<&str> = content.split(SQL_DB_ENDPOINT_SPLITTER).collect();
if x.len() == 3 {
return Some(SqlDbEndpoint {
host: x[0].to_owned(),
user: x[1].to_owned(),
pwd: x[2].to_owned(),
});
} else {
println!(
"Error: environment variable {} is invalid, must have format 'host:user:password' expected",
SQL_DB_ENDPOINT
);
}
} else {
println!("Error: environment variable {} is absent", SQL_DB_ENDPOINT);
}
None
}

pub fn create_remote_config(end_point: &SqlDbEndpoint) -> NamedTempFile {
let mut l = NamedTempFile::new().unwrap();
let config = format!(
r#"
---
mssql:
standard:
authentication:
username: {}
password: {}
type: "sql_server"
connection:
hostname: {}
"#,
end_point.user, end_point.pwd, end_point.host
);
l.write_all(config.as_bytes()).unwrap();
l
}
pub fn create_local_config() -> NamedTempFile {
let mut l = NamedTempFile::new().unwrap();
let config = r#"
---
mssql:
standard:
authentication:
username: "nobody"
type: "integrated"
connection:
hostname: "localhost"
"#;
l.write_all(config.as_bytes()).unwrap();
l
}

0 comments on commit 8293319

Please sign in to comment.