Skip to content

Commit

Permalink
add from_string api to Config
Browse files Browse the repository at this point in the history
Change-Id: Ic69d177b69b5ebb16383432ca6c5f3b31f583836
  • Loading branch information
s-kipnis committed Nov 24, 2023
1 parent 3c17763 commit bbaa536
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
12 changes: 9 additions & 3 deletions packages/check-sql/src/config/ms_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::config::yaml::{Get, Yaml};
use anyhow::{anyhow, bail, Context, Result};
use std::path::{Path, PathBuf};
use std::time::Duration;
use yaml_rust::YamlLoader;

mod keys {
pub const MSSQL: &str = "mssql";
Expand Down Expand Up @@ -119,6 +120,13 @@ impl Default for Config {
}

impl Config {
pub fn from_string(source: &str) -> Result<Option<Self>> {
YamlLoader::load_from_str(source)?
.get(0)
.and_then(|e| Config::from_yaml(e).transpose())
.transpose()
}

pub fn from_yaml(yaml: &Yaml) -> Result<Option<Self>> {
let mssql = yaml.get(keys::MSSQL);
if mssql.is_badvalue() {
Expand Down Expand Up @@ -974,9 +982,7 @@ discovery:

#[test]
fn test_config() {
let c = Config::from_yaml(&create_yaml(data::TEST_CONFIG))
.unwrap()
.unwrap();
let c = Config::from_string(data::TEST_CONFIG).unwrap().unwrap();
assert_eq!(c.instances().len(), 2);
assert!(c.instances()[0].piggyback().is_some());
assert_eq!(
Expand Down
11 changes: 3 additions & 8 deletions packages/check-sql/src/ms_sql/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,6 @@ pub async fn get_computer_name(client: &mut Client) -> Result<Option<String>> {
mod tests {
use super::*;
use crate::config::ms_sql::Config;
use yaml_rust::YamlLoader;

fn make_config_with_auth_type(auth_type: &str) -> Config {
const BASE: &str = r#"
Expand All @@ -1438,13 +1437,9 @@ mssql:
port: 65345 # we use weird port to avoid connection
timeout: 1
"#;
Config::from_yaml(
&YamlLoader::load_from_str(&BASE.replace("type_tag", auth_type))
.expect("fix test string!")[0]
.clone(),
)
.unwrap()
.unwrap()
Config::from_string(&BASE.replace("type_tag", auth_type))
.unwrap()
.unwrap()
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
19 changes: 8 additions & 11 deletions packages/check-sql/tests/test_ms_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ use check_sql::ms_sql::{
api::{Client, Section},
queries,
};
use check_sql::{config::ms_sql::Endpoint, config::CheckConfig, ms_sql::api};
use check_sql::{
config::ms_sql::{Config, Endpoint},
config::CheckConfig,
ms_sql::api,
};
use common::tools::{self, SqlDbEndpoint};
use tempfile::TempDir;
use yaml_rust::YamlLoader;

fn expected_instances() -> Vec<String> {
const EXPECTED_INSTANCES: [&str; 3] = ["MSSQLSERVER", "SQLEXPRESS_NAME", "SQLEXPRESS_WOW"];
Expand Down Expand Up @@ -133,8 +136,7 @@ async fn test_validate_all_instances_remote() {
let instances = api::detect_instance_engines(&mut client).await.unwrap();
let is = [&instances.0[..], &instances.1[..]].concat();

let r = YamlLoader::load_from_str(&create_remote_config(endpoint)).unwrap();
let cfg = check_sql::config::ms_sql::Config::from_yaml(&r[0])
let cfg = Config::from_string(&create_remote_config(endpoint))
.unwrap()
.unwrap();
assert!(is.len() >= 3, "we need at least 3 instances to check");
Expand Down Expand Up @@ -522,14 +524,12 @@ async fn validate_availability_groups_section(instance: &InstanceEngine, endpoin
#[ignore]
#[tokio::test(flavor = "multi_thread")]
async fn test_validate_all_instances_remote_extra() {
use yaml_rust::YamlLoader;
if let Some(endpoint) = tools::get_remote_sql_from_env_var() {
let mut client = tools::create_remote_client(&endpoint).await.unwrap();
let instances = api::detect_instance_engines(&mut client).await.unwrap();
let is = [&instances.0[..], &instances.1[..]].clone().concat();
let ms_sql = check_sql::config::ms_sql::Config::from_yaml(
&YamlLoader::load_from_str(
r"---
let ms_sql = Config::from_string(
r"---
mssql:
standard:
authentication:
Expand All @@ -539,9 +539,6 @@ mssql:
connection:
hostname: your_host
",
)
.expect("fix test string!")[0]
.clone(),
)
.unwrap()
.unwrap();
Expand Down

0 comments on commit bbaa536

Please sign in to comment.