diff --git a/src/build_platform/mod.rs b/src/build_platform/mod.rs index be1ce938..5804afa6 100644 --- a/src/build_platform/mod.rs +++ b/src/build_platform/mod.rs @@ -153,6 +153,12 @@ impl Image { pub fn name(&self) -> String { self.name.clone() } + + pub fn name_without_repository(&self) -> &str { + self.name + .strip_prefix(&format!("{}/", self.repository_name())) + .unwrap_or(&self.name) + } } impl Default for Image { diff --git a/src/container_registry/docr.rs b/src/container_registry/docr.rs index e369495a..922bda17 100644 --- a/src/container_registry/docr.rs +++ b/src/container_registry/docr.rs @@ -201,7 +201,7 @@ impl ContainerRegistry for DOCR { let url = format!( "https://api.digitalocean.com/v2/registry/{}/repositories/{}/tags", image.registry_name, - image.name() + image.name_without_repository() ); let res = reqwest::blocking::Client::new() diff --git a/src/transaction.rs b/src/transaction.rs index f29f9b82..46696e28 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -93,13 +93,23 @@ impl<'a> Transaction<'a> { ) } + pub fn build_environment( + &mut self, + environment: &Rc>, + option: DeploymentOption, + ) -> Result<(), EnvironmentError> { + self.steps.push(Step::BuildEnvironment(environment.clone(), option)); + + Ok(()) + } + pub fn deploy_environment_with_options( &mut self, environment: &Rc>, option: DeploymentOption, ) -> Result<(), EnvironmentError> { // add build step - self.steps.push(Step::BuildEnvironment(environment.clone(), option)); + self.build_environment(environment, option)?; // add deployment step self.steps.push(Step::DeployEnvironment(environment.clone())); diff --git a/test_utilities/src/common.rs b/test_utilities/src/common.rs index 64042fb1..047f8e03 100644 --- a/test_utilities/src/common.rs +++ b/test_utilities/src/common.rs @@ -25,6 +25,7 @@ use qovery_engine::cloud_provider::aws::AWS; use qovery_engine::cloud_provider::digitalocean::application::DoRegion; use qovery_engine::cloud_provider::digitalocean::kubernetes::DOKS; use qovery_engine::cloud_provider::digitalocean::DO; +use qovery_engine::cloud_provider::environment::Environment; use qovery_engine::cloud_provider::kubernetes::Kubernetes; use qovery_engine::cloud_provider::models::NodeGroups; use qovery_engine::cloud_provider::scaleway::application::ScwZone; @@ -70,18 +71,27 @@ pub trait Cluster { } pub trait Infrastructure { + fn build_environment( + &self, + environment: &EnvironmentRequest, + logger: Box, + engine_config: &EngineConfig, + ) -> (Environment, TransactionResult); + fn deploy_environment( &self, environment: &EnvironmentRequest, logger: Box, engine_config: &EngineConfig, ) -> TransactionResult; + fn pause_environment( &self, environment: &EnvironmentRequest, logger: Box, engine_config: &EngineConfig, ) -> TransactionResult; + fn delete_environment( &self, environment: &EnvironmentRequest, @@ -91,6 +101,33 @@ pub trait Infrastructure { } impl Infrastructure for EnvironmentRequest { + fn build_environment( + &self, + environment: &EnvironmentRequest, + logger: Box, + engine_config: &EngineConfig, + ) -> (Environment, TransactionResult) { + let mut tx = Transaction::new(engine_config, logger.clone(), Box::new(|| false), Box::new(|_| {})).unwrap(); + let env = environment.to_environment_domain( + engine_config.context(), + engine_config.cloud_provider(), + engine_config.container_registry().registry_info(), + logger, + ); + + let env = Rc::new(RefCell::new(env)); + let _ = tx.build_environment( + &env, + DeploymentOption { + force_build: true, + force_push: true, + }, + ); + + let ret = tx.commit(); + (Rc::try_unwrap(env).ok().unwrap().into_inner(), ret) + } + fn deploy_environment( &self, environment: &EnvironmentRequest, diff --git a/tests/aws/aws_environment.rs b/tests/aws/aws_environment.rs index 67189f37..c14c00ef 100644 --- a/tests/aws/aws_environment.rs +++ b/tests/aws/aws_environment.rs @@ -16,9 +16,56 @@ use test_utilities::aws::aws_default_engine_config; use test_utilities::utilities::{context, init, kubernetes_config_path}; use tracing::{span, Level}; -// TODO: -// - Tests that applications are always restarted when receiving a CREATE action -// see: https://github.com/Qovery/engine/pull/269 +#[cfg(feature = "test-aws-self-hosted")] +#[named] +#[test] +fn aws_test_build_phase() { + // This test tries to run up to the build phase of the engine + // basically building and pushing each applications + let test_name = function_name!(); + engine_run_test(|| { + init(); + let span = span!(Level::INFO, "test", name = test_name); + let _enter = span.enter(); + + let logger = logger(); + let secrets = FuncTestsSecrets::new(); + let context = context( + secrets + .AWS_TEST_ORGANIZATION_ID + .as_ref() + .expect("AWS_TEST_ORGANIZATION_ID is not set") + .as_str(), + secrets + .AWS_TEST_CLUSTER_ID + .as_ref() + .expect("AWS_TEST_CLUSTER_ID is not set") + .as_str(), + ); + let engine_config = aws_default_engine_config(&context, logger.clone()); + let mut environment = test_utilities::common::working_minimal_environment( + &context, + secrets + .DEFAULT_TEST_DOMAIN + .expect("DEFAULT_TEST_DOMAIN is not set in secrets") + .as_str(), + ); + + environment.routers = vec![]; + let ea = environment.clone(); + + let (env, ret) = environment.build_environment(&ea, logger.clone(), &engine_config); + assert!(matches!(ret, TransactionResult::Ok)); + + // Check the the image exist in the registry + let img_exist = engine_config + .container_registry() + .does_image_exists(&env.applications[0].get_build().image); + assert!(img_exist); + + test_name.to_string() + }) +} #[cfg(feature = "test-aws-self-hosted")] #[named] diff --git a/tests/digitalocean/do_environment.rs b/tests/digitalocean/do_environment.rs index b3df0295..0d036a95 100644 --- a/tests/digitalocean/do_environment.rs +++ b/tests/digitalocean/do_environment.rs @@ -20,6 +20,54 @@ use tracing::{span, warn, Level}; // Note: All those tests relies on a test cluster running on DigitalOcean infrastructure. // This cluster should be live in order to have those tests passing properly. +#[cfg(feature = "test-do-self-hosted")] +#[named] +#[test] +fn digitalocean_test_build_phase() { + let test_name = function_name!(); + engine_run_test(|| { + init(); + + let span = span!(Level::INFO, "test", name = test_name); + let _enter = span.enter(); + + let secrets = FuncTestsSecrets::new(); + let logger = logger(); + let context = context( + secrets + .DIGITAL_OCEAN_TEST_ORGANIZATION_ID + .as_ref() + .expect("DIGITAL_OCEAN_TEST_ORGANIZATION_ID is not set"), + secrets + .DIGITAL_OCEAN_TEST_CLUSTER_ID + .as_ref() + .expect("DIGITAL_OCEAN_TEST_CLUSTER_ID is not set"), + ); + let engine_config = do_default_engine_config(&context, logger.clone()); + let environment = test_utilities::common::working_minimal_environment( + &context, + secrets + .DEFAULT_TEST_DOMAIN + .as_ref() + .expect("DEFAULT_TEST_DOMAIN is not set in secrets") + .as_str(), + ); + + let env_action = environment.clone(); + + let (env, ret) = environment.build_environment(&env_action, logger.clone(), &engine_config); + assert!(matches!(ret, TransactionResult::Ok)); + + // Check the the image exist in the registry + let img_exist = engine_config + .container_registry() + .does_image_exists(&env.applications[0].get_build().image); + assert!(img_exist); + + test_name.to_string() + }) +} + #[cfg(feature = "test-do-self-hosted")] #[named] #[test] diff --git a/tests/lib.rs b/tests/lib.rs index 1d73348f..bbc13eb3 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,6 +1,6 @@ #[macro_use] extern crate maplit; + mod aws; mod digitalocean; mod scaleway; -mod unit; diff --git a/tests/scaleway/scw_environment.rs b/tests/scaleway/scw_environment.rs index 68114006..ae788b39 100644 --- a/tests/scaleway/scw_environment.rs +++ b/tests/scaleway/scw_environment.rs @@ -19,6 +19,56 @@ use tracing::{span, warn, Level}; // Note: All those tests relies on a test cluster running on Scaleway infrastructure. // This cluster should be live in order to have those tests passing properly. +#[cfg(feature = "test-scw-self-hosted")] +#[named] +#[test] +fn scaleway_test_build_phase() { + let test_name = function_name!(); + engine_run_test(|| { + init(); + + let span = span!(Level::INFO, "test", name = test_name); + let _enter = span.enter(); + + let logger = logger(); + let secrets = FuncTestsSecrets::new(); + let context = context( + secrets + .SCALEWAY_TEST_ORGANIZATION_ID + .as_ref() + .expect("SCALEWAY_TEST_ORGANIZATION_ID") + .as_str(), + secrets + .SCALEWAY_TEST_CLUSTER_ID + .as_ref() + .expect("SCALEWAY_TEST_CLUSTER_ID") + .as_str(), + ); + let engine_config = scw_default_engine_config(&context, logger.clone()); + let environment = test_utilities::common::working_minimal_environment( + &context, + secrets + .DEFAULT_TEST_DOMAIN + .as_ref() + .expect("DEFAULT_TEST_DOMAIN is not set in secrets") + .as_str(), + ); + + let env_action = environment.clone(); + + let (env, ret) = environment.build_environment(&env_action, logger.clone(), &engine_config); + assert!(matches!(ret, TransactionResult::Ok)); + + // Check the the image exist in the registry + let img_exist = engine_config + .container_registry() + .does_image_exists(&env.applications[0].get_build().image); + assert!(img_exist); + + test_name.to_string() + }) +} + #[cfg(feature = "test-scw-self-hosted")] #[named] #[test] diff --git a/tests/unit/mod.rs b/tests/unit/mod.rs deleted file mode 100644 index 8b137891..00000000 --- a/tests/unit/mod.rs +++ /dev/null @@ -1 +0,0 @@ -