Skip to content

Commit

Permalink
ci: refactor code for testibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Sep 21, 2024
1 parent a452ebb commit 8ad1d0c
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 126 deletions.
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion argo-cd
Submodule argo-cd updated 875 files
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) enum Command {
/// Base arguments for subcommands that share common parameters.
#[derive(Parser, Debug)]
pub(crate) struct BaseArgs {
/// Path to the configuration file (default: config.yml).
/// Path to the configuration file
#[clap(short, long, default_value = "config.yml")]
pub(crate) config_path: std::path::PathBuf,
}
Expand Down
23 changes: 21 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub(crate) struct ArgoConfig {
impl Default for ArgoConfig {
fn default() -> Self {
ArgoConfig {
application: String::from(""),
base_url: String::from(""),
application: String::from("propeller"),
base_url: String::from("http://localhost:3100"),
danger_accept_insecure: Option::from(false),
sync_timeout_seconds: Option::from(60),
}
Expand All @@ -40,12 +40,31 @@ pub(crate) struct PostgresConfig {
pub(crate) database: String,
}

impl Default for PostgresConfig {
fn default() -> Self {
PostgresConfig {
host: String::from("localhost"),
port: 5432,
database: String::from("propeller"),
}
}
}

#[derive(Clone, Deserialize, Debug)]
pub(crate) struct VaultConfig {
pub(crate) base_url: String,
pub(crate) path: String,
}

impl Default for VaultConfig {
fn default() -> Self {
VaultConfig {
base_url: String::from("http://localhost:8200"),
path: String::from("propeller"),
}
}
}

pub(crate) fn read_config(config_path: PathBuf) -> Config {
let path_string = config_path.clone().into_os_string().into_string().unwrap();
debug!("Reading config at: {path_string}");
Expand Down
102 changes: 92 additions & 10 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

use crate::config::{Config, PostgresConfig};
use postgres::Error;
use postgres::{Client, NoTls};
use std::sync::Arc;

use crate::config::{Config, PostgresConfig};
pub trait ClientFactory {
fn create_client(&self, connection_string: &str) -> Result<Client, Error>;
}

struct PropellerClientFactory;

pub(crate) struct PostgresClient {
impl ClientFactory for PropellerClientFactory {
fn create_client(&self, connection_string: &str) -> Result<Client, Error> {
Client::connect(connection_string, NoTls)
}
}

pub struct PostgresClient {
postgres_config: PostgresConfig,
client_factory: Arc<dyn ClientFactory>,
}

impl PostgresClient {
pub(crate) fn init(config: &Config) -> PostgresClient {
PostgresClient {
postgres_config: config.postgres.clone(),
client_factory: Arc::new(PropellerClientFactory),
}
}

Expand All @@ -23,13 +38,80 @@ impl PostgresClient {
let port = self.postgres_config.port;
let database = self.postgres_config.database.as_str();

Client::connect(
format!(
"host={host} port={port} dbname={database} user={username} password={password}"
)
.as_str(),
NoTls,
)
.expect("Failed to build PostgreSQL connection")
let connection_string = format!(
"host={host} port={port} dbname={database} user={username} password={password}"
);

self.client_factory
.create_client(&connection_string)
.expect("Failed to build PostgreSQL connection")
}

#[cfg(test)]
pub(crate) fn with_client_factory(
config: &Config,
client_factory: Arc<dyn ClientFactory>,
) -> PostgresClient {
PostgresClient {
postgres_config: config.postgres.clone(),
client_factory,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::config::{ArgoConfig, VaultConfig};

struct MockClientFactory;

impl ClientFactory for MockClientFactory {
fn create_client(&self, connection_string: &str) -> Result<Client, Error> {
assert!(connection_string.contains("host=testhost"));
assert!(connection_string.contains("port=2345"));
assert!(connection_string.contains("dbname=testdb"));
assert!(connection_string.contains("user=testuser"));
assert!(connection_string.contains("password=testpass"));

Client::connect("", NoTls)
}
}

#[test]
fn init() {
let config = create_config_with_testdb();

let fixture = PostgresClient::init(&config);

assert_eq!(fixture.postgres_config.host, "testhost");
assert_eq!(fixture.postgres_config.port, 2345);
assert_eq!(fixture.postgres_config.database, "testdb");
}

#[test]
#[should_panic(expected = "both host and hostaddr are missing")]
fn connect_for_user() {
let config = create_config_with_testdb();

let client = PostgresClient::with_client_factory(
&config,
Arc::new(MockClientFactory) as Arc<dyn ClientFactory>,
);

// This should panic
client.connect_for_user("testuser".to_string(), "testpass".to_string());
}

fn create_config_with_testdb() -> Config {
Config {
argo_cd: ArgoConfig::default(),
postgres: PostgresConfig {
host: "testhost".to_string(),
port: 2345,
database: "testdb".to_string(),
},
vault: VaultConfig::default(),
}
}
}
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ mod tests {

fn should_process_path(path: &Path) -> bool {
let path_str = path.to_str().unwrap();
if path_str.starts_with("tests/utilities") {
path_str == "tests/utilities/src" || path_str.starts_with("tests/utilities/src/")
if path_str.starts_with("tests/utilities") || path_str.starts_with("tests\\utilities") {
path_str == "tests/utilities/src"
|| path_str.starts_with("tests/utilities/src/")
|| path_str == "tests\\utilities\\src"
|| path_str.starts_with("tests\\utilities\\src/")
} else {
true
}
Expand Down
Loading

0 comments on commit 8ad1d0c

Please sign in to comment.