Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ane 904 preflight check #136

Merged
merged 7 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/dynamic-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
- run: cargo nextest run
env:
RUN_INTEGRATION_TESTS: "1"
# This key is used for integration tests
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}
# nextest doesn't run doctests, but does test everything else: https://github.com/nextest-rs/nextest/issues/16
# run doctests after; this won't result in any extra rebuilds and is very quick.
# doctest overview: https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html
Expand Down
57 changes: 54 additions & 3 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::time::Duration;

use error_stack::{Result, ResultExt};
use error_stack::{report, Result, ResultExt};
use futures::TryStreamExt;
use futures::{future::try_join_all, try_join, StreamExt};
use governor::{Quota, RateLimiter};
Expand All @@ -18,7 +18,9 @@ use tracing::{debug, info};
use uuid::Uuid;

use crate::api::fossa::{self, CliMetadata, ProjectMetadata};
use crate::api::remote::Reference;
use crate::api::remote::git::repository;
use crate::api::remote::{Integrations, Protocol, Reference};
use crate::ext::result::WrapErr;
use crate::ext::tracing::span_record;
use crate::fossa_cli::{self, DesiredVersion, Location, SourceUnits};
use crate::queue::Queue;
Expand Down Expand Up @@ -80,6 +82,14 @@ pub enum Error {
/// If we fail to run FOSSA CLI, this error is raised.
#[error("run FOSSA CLI")]
RunFossaCli,

/// Failed to connect to at least one integration
#[error("integration connections")]
IntegrationConnection,

/// Failed to connect to FOSSA
#[error("FOSSA connection")]
FossaConnection,
}

/// Similar to [`AppContext`], but scoped for this subcommand.
Expand All @@ -104,9 +114,50 @@ pub async fn main<D: Database>(ctx: &AppContext, config: Config, db: D) -> Resul
db,
};

let preflight_checks = preflight_checks(&ctx);
let healthcheck_worker = healthcheck(&ctx.db);
let integration_worker = integrations(&ctx);
try_join!(healthcheck_worker, integration_worker).discard_ok()
try_join!(preflight_checks, healthcheck_worker, integration_worker).discard_ok()
}

/// Checks and catches network misconfigurations before Broker attempts its operations
async fn preflight_checks<D: Database>(ctx: &CmdContext<D>) -> Result<(), Error> {
let validate_integration_connections = integration_connections(ctx.config.integrations());
let validate_fossa_connection = fossa_connection(&ctx.config);
try_join!(validate_integration_connections, validate_fossa_connection).discard_ok()
}

#[tracing::instrument(skip_all)]
/// Validate that Broker can connect to at least one integration
async fn integration_connections(integrations: &Integrations) -> Result<(), Error> {
JeffreyHuynh1 marked this conversation as resolved.
Show resolved Hide resolved
if integrations.as_ref().is_empty() {
return Ok(());
}

for integration in integrations.iter() {
let Protocol::Git(transport) = integration.protocol();
if repository::ls_remote(transport).await.is_ok() {
return Ok(());
}
}

report!(Error::IntegrationConnection)
.wrap_err()
.help("run broker fix for detailed explanation on failing integration connections")
.describe("integration connections")
}

#[tracing::instrument(skip_all)]
/// Validate that Broker can connect to FOSSA
async fn fossa_connection(config: &Config) -> Result<(), Error> {
JeffreyHuynh1 marked this conversation as resolved.
Show resolved Hide resolved
match fossa::OrgConfig::lookup(config.fossa_api()).await {
Ok(_) => Ok(()),
Err(err) => err
.change_context(Error::FossaConnection)
.wrap_err()
.help("ensure that your fossa key is configured correctly")
csasarak marked this conversation as resolved.
Show resolved Hide resolved
.describe("fossa key"),
}
}

/// Conduct internal diagnostics to ensure Broker is still in a good state.
Expand Down
3 changes: 3 additions & 0 deletions tests/it/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::Path;

use tempfile::TempDir;

use crate::guard_integration_test;
use crate::temp_config;

macro_rules! run {
Expand Down Expand Up @@ -47,6 +48,8 @@ fn interrupt(pid: u32) {
#[track_caller]
#[cfg(target_family = "unix")]
fn run_and_interrupt_broker(tmp: &TempDir, config_path: &Path) {
guard_integration_test!();

let config_path = config_path.to_string_lossy().to_string();
let data_root = tmp.path().to_string_lossy().to_string();
let child = run!(broker => "run -c {config_path} -r {data_root}");
Expand Down
10 changes: 8 additions & 2 deletions tests/it/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ macro_rules! temp_config {
() => {{
let tmp = tempfile::tempdir().expect("must create tempdir");
let dir = tmp.path().join("debug");
let fossa_key = std::env::var("FOSSA_API_KEY").expect("test");

let content = indoc::formatdoc! {r#"
fossa_endpoint: https://app.fossa.com
fossa_integration_key: abcd1234
fossa_integration_key: {}
version: 1
debugging:
location: {dir:?}
retention:
days: 1
integrations:
"#};
"#, fossa_key };

let path = tmp.path().join("config.yml");
std::fs::write(&path, content).expect("must write config file");
Expand Down Expand Up @@ -291,6 +293,10 @@ macro_rules! guard_integration_test {
if std::env::var("RUN_INTEGRATION_TESTS").is_err() {
return;
}

if std::env::var("FOSSA_API_KEY").is_err() {
return;
}
};
}

Expand Down
Loading