Skip to content

Commit

Permalink
refactor: split deploy into smaller functions
Browse files Browse the repository at this point in the history
The smaller functions are easier to test and don't require as much mocking setup.
  • Loading branch information
jacderida committed Aug 3, 2023
1 parent 7d86f8b commit 8741d35
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 225 deletions.
45 changes: 32 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,28 +235,33 @@ impl TestnetDeploy {
Ok(())
}

pub async fn deploy(
pub async fn create_infra(
&self,
name: &str,
node_count: u16,
repo_owner: Option<String>,
branch: Option<String>,
vm_count: u16,
enable_build_vm: bool,
) -> Result<()> {
if (repo_owner.is_some() && branch.is_none()) || (branch.is_some() && repo_owner.is_none())
{
return Err(Error::CustomBinConfigError);
}
println!("Selecting {name} workspace...");
self.terraform_runner.workspace_select(name)?;
let args = vec![
("node_count".to_string(), node_count.to_string()),
(
"use_custom_bin".to_string(),
repo_owner.is_some().to_string(),
),
("node_count".to_string(), vm_count.to_string()),
("use_custom_bin".to_string(), enable_build_vm.to_string()),
];
println!("Running terraform apply...");
self.terraform_runner.apply(args)?;
Ok(())
}

pub async fn provision_genesis_node(
&self,
name: &str,
repo_owner: Option<String>,
branch: Option<String>,
) -> Result<()> {
if (repo_owner.is_some() && branch.is_none()) || (branch.is_some() && repo_owner.is_none())
{
return Err(Error::CustomBinConfigError);
}
println!("Running ansible against genesis node...");
self.ansible_runner.run_playbook(
PathBuf::from("genesis_node.yml"),
Expand All @@ -267,6 +272,20 @@ impl TestnetDeploy {
Ok(())
}

pub async fn deploy(
&self,
name: &str,
node_count: u16,
repo_owner: Option<String>,
branch: Option<String>,
) -> Result<()> {
self.create_infra(name, node_count, repo_owner.is_some())
.await?;
self.provision_genesis_node(name, repo_owner, branch)
.await?;
Ok(())
}

pub async fn clean(&self, name: &str) -> Result<()> {
let workspaces = self.terraform_runner.workspace_list()?;
if !workspaces.contains(&name.to_string()) {
Expand Down
72 changes: 72 additions & 0 deletions src/tests/create_infra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use super::super::{CloudProvider, TestnetDeploy};
use super::setup::*;
use crate::ansible::MockAnsibleRunnerInterface;
use crate::terraform::MockTerraformRunnerInterface;
use color_eyre::Result;
use mockall::predicate::*;

#[tokio::test]
async fn should_run_terraform_apply() -> Result<()> {
let (tmp_dir, working_dir) = setup_working_directory()?;
let s3_repository = setup_default_s3_repository(&working_dir)?;
let mut terraform_runner = MockTerraformRunnerInterface::new();
terraform_runner
.expect_workspace_select()
.times(1)
.with(eq("beta".to_string()))
.returning(|_| Ok(()));
terraform_runner
.expect_apply()
.times(1)
.with(eq(vec![
("node_count".to_string(), "30".to_string()),
("use_custom_bin".to_string(), "false".to_string()),
]))
.returning(|_| Ok(()));

let testnet = TestnetDeploy::new(
Box::new(terraform_runner),
Box::new(MockAnsibleRunnerInterface::new()),
working_dir.to_path_buf(),
CloudProvider::DigitalOcean,
s3_repository,
);

testnet.create_infra("beta", 30, false).await?;

drop(tmp_dir);
Ok(())
}

#[tokio::test]
async fn should_run_terraform_apply_with_custom_bin_set_when_repo_is_supplied() -> Result<()> {
let (tmp_dir, working_dir) = setup_working_directory()?;
let s3_repository = setup_default_s3_repository(&working_dir)?;
let mut terraform_runner = MockTerraformRunnerInterface::new();
terraform_runner
.expect_workspace_select()
.times(1)
.with(eq("beta".to_string()))
.returning(|_| Ok(()));
terraform_runner
.expect_apply()
.times(1)
.with(eq(vec![
("node_count".to_string(), "30".to_string()),
("use_custom_bin".to_string(), "true".to_string()),
]))
.returning(|_| Ok(()));

let testnet = TestnetDeploy::new(
Box::new(terraform_runner),
Box::new(MockAnsibleRunnerInterface::new()),
working_dir.to_path_buf(),
CloudProvider::DigitalOcean,
s3_repository,
);

testnet.create_infra("beta", 30, true).await?;

drop(tmp_dir);
Ok(())
}
211 changes: 0 additions & 211 deletions src/tests/deploy.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod clean;
mod deploy;
mod create_infra;
mod init;
mod provision_genesis_node;
mod setup;

const RPC_CLIENT_BIN_NAME: &str = "safenode_rpc_client";

0 comments on commit 8741d35

Please sign in to comment.