Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Separate urls for services
Browse files Browse the repository at this point in the history
  • Loading branch information
senia-psm committed Feb 24, 2024
1 parent 10f910d commit 9c5b687
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 303 deletions.
28 changes: 22 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ struct GolemCommand {

#[arg(short = 'u', long)]
/// Golem base url. Default: GOLEM_BASE_URL environment variable or http://localhost:9881.
///
/// You can also specify different URLs for different services
/// via GOLEM_TEMPLATE_BASE_URL and GOLEM_WORKER_BASE_URL
/// environment variables.
golem_url: Option<String>,

#[command(subcommand)]
Expand Down Expand Up @@ -117,7 +121,14 @@ async fn async_main(cmd: GolemCommand) -> Result<(), Box<dyn std::error::Error>>
.golem_url
.or_else(|| std::env::var("GOLEM_BASE_URL").ok())
.unwrap_or("http://localhost:9881".to_string());
let url = Url::parse(&url_str).unwrap();
let template_url_str = std::env::var("GOLEM_TEMPLATE_BASE_URL")
.ok()
.unwrap_or(url_str.to_string());
let worker_url_str = std::env::var("GOLEM_WORKER_BASE_URL")
.ok()
.unwrap_or(url_str);
let template_url = Url::parse(&template_url_str).unwrap();
let worker_url = Url::parse(&worker_url_str).unwrap();
let allow_insecure_str = std::env::var("GOLEM_ALLOW_INSECURE").unwrap_or("false".to_string());
let allow_insecure = allow_insecure_str != "false";

Expand All @@ -127,24 +138,29 @@ async fn async_main(cmd: GolemCommand) -> Result<(), Box<dyn std::error::Error>>
}
let client = builder.connection_verbose(true).build()?;

let context = Context {
base_url: url.clone(),
let template_context = Context {
base_url: template_url.clone(),
client: client.clone(),
};

let worker_context = Context {
base_url: worker_url.clone(),
client: client.clone(),
};

let template_client = TemplateClientLive {
client: golem_client::api::TemplateClientLive {
context: context.clone(),
context: template_context,
},
};
let template_srv = TemplateHandlerLive {
client: template_client,
};
let worker_client = WorkerClientLive {
client: golem_client::api::WorkerClientLive {
context: context.clone(),
context: worker_context.clone(),
},
context: context.clone(),
context: worker_context.clone(),
allow_insecure,
};
let worker_srv = WorkerHandlerLive {
Expand Down
30 changes: 20 additions & 10 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::context::ContextInfo;
use libtest_mimic::Failed;
use serde::de::DeserializeOwned;
use serde_json::Value;
Expand All @@ -6,7 +7,6 @@ use std::fmt::Debug;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use crate::context::golem_template_service::GolemTemplateServiceInfo;

#[derive(Debug, Clone)]
pub struct CliConfig {
Expand Down Expand Up @@ -36,35 +36,39 @@ pub trait Cli {
#[derive(Debug, Clone)]
pub struct CliLive {
pub config: CliConfig,
golem_port: u16,
golem_template_port: u16,
golem_worker_port: u16,
golem_cli_path: PathBuf,
}

impl CliLive {
pub fn with_short_args(&self) -> Self {
CliLive {
config: CliConfig { short_args: true },
golem_port: self.golem_port,
golem_template_port: self.golem_template_port,
golem_worker_port: self.golem_worker_port,
golem_cli_path: self.golem_cli_path.clone(),
}
}

pub fn with_long_args(&self) -> Self {
CliLive {
config: CliConfig { short_args: false },
golem_port: self.golem_port,
golem_template_port: self.golem_template_port,
golem_worker_port: self.golem_worker_port,
golem_cli_path: self.golem_cli_path.clone(),
}
}

// TODO; Use NginxInfo
pub fn make(golem: &GolemTemplateServiceInfo) -> Result<CliLive, Failed> {
pub fn make(context: &ContextInfo) -> Result<CliLive, Failed> {
let golem_cli_path = PathBuf::from("./target/debug/golem-cli");

if golem_cli_path.exists() {
Ok(CliLive {
config: CliConfig { short_args: false },
golem_port: golem.local_http_port,
golem_template_port: context.golem_template_service.local_http_port,
golem_worker_port: context.golem_worker_service.local_http_port,
golem_cli_path,
})
} else {
Expand All @@ -76,8 +80,12 @@ impl CliLive {
}
}

fn base_url(&self) -> String {
format!("http://localhost:{}", self.golem_port)
fn template_base_url(&self) -> String {
format!("http://localhost:{}", self.golem_template_port)
}

fn worker_base_url(&self) -> String {
format!("http://localhost:{}", self.golem_worker_port)
}

fn run_inner<S: AsRef<OsStr> + Debug>(&self, args: &[S]) -> Result<String, Failed> {
Expand All @@ -87,7 +95,8 @@ impl CliLive {
);

let output = Command::new(&self.golem_cli_path)
.env("GOLEM_BASE_URL", self.base_url())
.env("GOLEM_TEMPLATE_BASE_URL", self.template_base_url())
.env("GOLEM_WORKER_BASE_URL", self.worker_base_url())
.arg(self.config.arg('F', "format"))
.arg("json")
.args(args)
Expand Down Expand Up @@ -139,7 +148,8 @@ impl Cli for CliLive {
);

let mut child = Command::new(&self.golem_cli_path)
.env("GOLEM_BASE_URL", self.base_url())
.env("GOLEM_TEMPLATE_BASE_URL", self.template_base_url())
.env("GOLEM_WORKER_BASE_URL", self.worker_base_url())
.arg(self.config.arg('F', "format"))
.arg("json")
.args(args)
Expand Down
22 changes: 12 additions & 10 deletions tests/context.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
pub mod db;
pub mod golem_template_service;
pub mod golem_worker_service;
pub mod redis;
pub mod shard_manager;
pub mod worker;
pub mod nginx;

pub mod golem_template_service;

use crate::context::db::{Db, DbInfo};
use crate::context::golem_template_service::{GolemTemplateService, GolemTemplateServiceInfo};
use crate::context::golem_worker_service::{GolemWorkerService, GolemWorkerServiceInfo};
use crate::context::redis::{Redis, RedisInfo};
use crate::context::shard_manager::{ShardManager, ShardManagerInfo};
use crate::context::worker::{WorkerExecutors, WorkerExecutorsInfo};
use libtest_mimic::Failed;
use std::path::PathBuf;
use testcontainers::clients;
use crate::context::golem_template_service::{GolemTemplateService, GolemTemplateServiceInfo};
use crate::context::golem_worker_service::{GolemWorkerService, GolemWorkerServiceInfo};

const NETWORK: &str = "golem_test_network";
const TAG: &str = "v0.0.60";
Expand Down Expand Up @@ -74,7 +72,6 @@ pub struct Context<'docker_client> {
golem_template_service: GolemTemplateService<'docker_client>,
golem_worker_service: GolemWorkerService<'docker_client>,
worker_executors: WorkerExecutors<'docker_client>,
nginx: Nginx<'docker_client>
}

impl Context<'_> {
Expand All @@ -87,11 +84,16 @@ impl Context<'_> {
let redis = Redis::make(docker, &env_config)?;
let shard_manager = ShardManager::start(docker, &env_config, &redis.info())?;

let golem_template_service =
GolemTemplateService::start(docker, &env_config, &db.info())?;
let golem_template_service = GolemTemplateService::start(docker, &env_config, &db.info())?;

let golem_worker_service =
GolemWorkerService::start(docker, &env_config, &shard_manager.info(), &db.info(), &redis.info(), &golem_template_service.info())?;
let golem_worker_service = GolemWorkerService::start(
docker,
&env_config,
&shard_manager.info(),
&db.info(),
&redis.info(),
&golem_template_service.info(),
)?;

let worker_executors = WorkerExecutors::start(
docker,
Expand Down
5 changes: 4 additions & 1 deletion tests/context/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ impl DbInfo {
.expect("Invalid Sqlite database path")
.to_string(),
),
("GOLEM__DB__CONFIG__MAX_CONNECTIONS".to_string(), "10".to_string())
(
"GOLEM__DB__CONFIG__MAX_CONNECTIONS".to_string(),
"10".to_string(),
),
]
.into(),
}
Expand Down
17 changes: 8 additions & 9 deletions tests/context/golem_template_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::context::db::DbInfo;
use crate::context::{EnvConfig, ShardManagerInfo, NETWORK, TAG};
use crate::context::{EnvConfig, NETWORK, TAG};
use golem_cli::clients::template::{TemplateClient, TemplateClientLive};
use libtest_mimic::Failed;
use std::collections::HashMap;
Expand Down Expand Up @@ -125,9 +125,11 @@ impl<'docker_client> GolemTemplateService<'docker_client> {
let env_vars = GolemTemplateService::env_vars(grpc_port, http_port, env_config, db);
let name = "golem_template_service";

let image = RunnableImage::from(GolemTemplateServiceImage::new(grpc_port, http_port, env_vars))
.with_container_name(name)
.with_network(NETWORK);
let image = RunnableImage::from(GolemTemplateServiceImage::new(
grpc_port, http_port, env_vars,
))
.with_container_name(name)
.with_network(NETWORK);
let node = docker.run(image);

Ok(GolemTemplateService {
Expand Down Expand Up @@ -187,7 +189,7 @@ impl<'docker_client> GolemTemplateService<'docker_client> {
"Expected to have precompiled Golem Template Service at {}",
service_run.to_str().unwrap_or("")
)
.into());
.into());
}

println!("Starting {}", service_run.to_str().unwrap_or(""));
Expand All @@ -198,10 +200,7 @@ impl<'docker_client> GolemTemplateService<'docker_client> {
let mut child = Command::new(service_run)
.current_dir(service_dir)
.envs(GolemTemplateService::env_vars(
grpc_port,
http_port,
env_config,
db,
grpc_port, http_port, env_config, db,
))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down
37 changes: 23 additions & 14 deletions tests/context/golem_worker_service.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::context::db::DbInfo;
use crate::context::golem_template_service::GolemTemplateServiceInfo;
use crate::context::redis::RedisInfo;
use crate::context::{EnvConfig, ShardManagerInfo, NETWORK, TAG};
use golem_cli::clients::template::{TemplateClient, TemplateClientLive};
use libtest_mimic::Failed;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::time::Duration;
use testcontainers::core::WaitFor;
use testcontainers::{clients, Container, Image, RunnableImage};
use url::Url;
use golem_cli::clients::worker::{WorkerClient, WorkerClientLive};
use crate::context::golem_template_service::GolemTemplateServiceInfo;
use crate::context::redis::{Redis, RedisInfo};

#[derive(Debug)]
struct GolemWorkerServiceImage {
Expand Down Expand Up @@ -106,12 +102,26 @@ impl<'docker_client> GolemWorkerService<'docker_client> {
let grpc_port = 9092;
let custom_request_port = 9093;

let env_vars = GolemWorkerService::env_vars(grpc_port, http_port, custom_request_port, env_config, shard_manager, db, redis, template);
let env_vars = GolemWorkerService::env_vars(
grpc_port,
http_port,
custom_request_port,
env_config,
shard_manager,
db,
redis,
template,
);
let name = "golem_worker_service";

let image = RunnableImage::from(GolemWorkerServiceImage::new(grpc_port, http_port, custom_request_port, env_vars))
.with_container_name(name)
.with_network(NETWORK);
let image = RunnableImage::from(GolemWorkerServiceImage::new(
grpc_port,
http_port,
custom_request_port,
env_vars,
))
.with_container_name(name)
.with_network(NETWORK);
let node = docker.run(image);

Ok(GolemWorkerService {
Expand Down Expand Up @@ -195,7 +205,6 @@ impl<'docker_client> GolemWorkerService<'docker_client> {
let grpc_port = 9092;
let custom_request_port = 9093;


let mut child = Command::new(service_run)
.current_dir(service_dir)
.envs(GolemWorkerService::env_vars(
Expand All @@ -206,7 +215,7 @@ impl<'docker_client> GolemWorkerService<'docker_client> {
shard_manager,
db,
redis,
template
template,
))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down Expand Up @@ -262,7 +271,7 @@ impl<'docker_client> GolemWorkerService<'docker_client> {
local_http_port: self.local_http_port,
http_port: self.http_port,
grpc_port: self.grpc_port,
custom_request_port: self.custom_request_port
custom_request_port: self.custom_request_port,
}
}
}
Expand Down Expand Up @@ -290,5 +299,5 @@ pub struct GolemWorkerServiceInfo {
pub local_http_port: u16,
pub http_port: u16,
pub grpc_port: u16,
pub custom_request_port: u16
pub custom_request_port: u16,
}
Loading

0 comments on commit 9c5b687

Please sign in to comment.