Skip to content

Commit

Permalink
fix spi-conf tests (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 authored May 13, 2024
1 parent 2531fb9 commit 1143c63
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ tardis = { version = "0.1.0-rc.15" }
# "ext-redis",
# "ext-axum",
# ] }
spacegate-shell = { git = "https://github.com/ideal-world/spacegate.git", branch = "master", features = [
spacegate-shell = { git = "https://github.com/ideal-world/spacegate.git", branch = "dev", features = [
"cache",
"k8s",
# "ext-redis",
] }

spacegate-plugin = { git = "https://github.com/ideal-world/spacegate.git", branch = "master" }
spacegate-plugin = { git = "https://github.com/ideal-world/spacegate.git", branch = "dev" }
4 changes: 2 additions & 2 deletions backend/gateways/spacegate-plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mod plugin;

pub const PACKAGE_NAME: &str = "spacegate_lib";
use plugin::op_redis_publisher;
use spacegate_shell::plugin::SgPluginRepository;
pub fn register_lib_plugins(repo: &SgPluginRepository) {
use spacegate_shell::plugin::PluginRepository;
pub fn register_lib_plugins(repo: &PluginRepository) {
repo.register::<ip_time::IpTimePlugin>();
repo.register::<anti_replay::AntiReplayPlugin>();
repo.register::<anti_xss::AntiXssPlugin>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use spacegate_shell::hyper::{Request, Response, StatusCode};
use spacegate_shell::kernel::extension::PeerAddr;
use spacegate_shell::kernel::helper_layers::function::Inner;
use spacegate_shell::plugin::{Plugin, PluginError};
use spacegate_shell::spacegate_ext_redis::{redis::AsyncCommands, RedisClient};
use spacegate_shell::ext_redis::{redis::AsyncCommands, RedisClient};
use spacegate_shell::{BoxError, SgBody, SgRequestExt, SgResponseExt};

use tardis::serde_json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ impl Plugin for RedisPublisherPlugin {
const CODE: &'static str = "op_redis_publisher";

fn create(config: PluginConfig) -> Result<Self, BoxError> {
let id = config.none_mono_id();
let layer_config = serde_json::from_value::<RedisPublisherConfig>(config.spec.clone())?;

Ok(Self {
key: id.redis_prefix(),
key: config.id.redis_prefix(),
jsonpath_inst: if let Ok(jsonpath_inst) = JsonPathInst::from_str(&layer_config.success_json_path).map_err(|e| log::error!("[Plugin.AuditLog] invalid json path:{e}")) {
Some(jsonpath_inst)
} else {
Expand Down
2 changes: 1 addition & 1 deletion backend/services/spacegate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spacegate-shell = { workspace = true, features = [
"k8s",
"plugin-all",
"ext-redis",
# "ext-axum",
"ext-axum",
"cache",
] }
tardis = { workspace = true }
Expand Down
11 changes: 3 additions & 8 deletions backend/services/spacegate/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;
use spacegate_plugins::register_lib_plugins;
use spacegate_shell::plugin::SgPluginRepository;
use spacegate_shell::plugin::PluginRepository;
use spacegate_shell::BoxError;
use tardis::basic::tracing::TardisTracing;
use tardis::tokio;
Expand All @@ -25,14 +25,9 @@ fn main() -> Result<(), BoxError> {
}
let rt = builder.build().expect("fail to build runtime");
let namespaces = std::env::args().nth(1).or(config.spacegate_ns);
register_lib_plugins(SgPluginRepository::global());
register_lib_plugins(PluginRepository::global());
rt.block_on(async move {
let local_set = tokio::task::LocalSet::new();
local_set
.run_until(async move {
let join_handle = spacegate_shell::startup_k8s(namespaces.as_deref()).await.expect("fail to start spacegate");
join_handle.await.expect("join handle error")
})
.await
local_set.run_until(async move { spacegate_shell::startup_k8s(namespaces.as_deref()).await }).await
})
}
5 changes: 4 additions & 1 deletion backend/spi/spi-conf/src/serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ pub async fn register(req: RegisterRequest, funs: &TardisFunsInst, ctx: &TardisC
// add a cert
let ext = json!({
"owner": ctx.owner,
"own_paths": ctx.own_paths
"own_paths": ctx.own_paths,
"ak": ctx.ak
})
.to_string();
let mut add_cert_req = RbumCertAddReq {
Expand Down Expand Up @@ -228,8 +229,10 @@ pub async fn auth(ak: &str, sk: &str, funs: &TardisFunsInst) -> TardisResult<Tar
let ext: serde_json::Value = serde_json::from_str(&cert.ext).map_err(|_| funs.err().internal_error("spi-conf", "auth", "invalid ext", "500-conf-invalid-cert-ext"))?;
let owner = ext.get("owner").and_then(serde_json::Value::as_str).unwrap_or_default();
let own_paths = ext.get("own_paths").and_then(serde_json::Value::as_str).unwrap_or_default();
let ak = ext.get("ak").and_then(serde_json::Value::as_str).unwrap_or_default();
ctx.owner = owner.to_owned();
ctx.own_paths = own_paths.to_owned();
ctx.ak = ak.to_owned();
Ok(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion backend/spi/spi-conf/src/serv/pg/conf_pg_config_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn get_config(descriptor: &mut ConfigDescriptor, _funs: &TardisFunsIns
let qry_result = conn
.query_one(
&format!(
r#"SELECT (content, md5) FROM {table_name} cc
r#"SELECT "content", "md5" FROM {table_name} cc
WHERE cc.namespace_id=$1 AND cc.grp=$2 AND cc.data_id=$3
"#,
),
Expand Down
2 changes: 1 addition & 1 deletion backend/spi/spi-conf/tests/spi_conf_api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> {
let mut client = TestHttpClient::new("https://127.0.0.1:8080/spi-conf".to_string());
client.set_auth(&TardisContext {
own_paths: "t1/app001".to_string(),
ak: "".to_string(),
ak: "app001".to_string(),
roles: vec![],
groups: vec![],
owner: "app001".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions backend/spi/spi-conf/tests/spi_conf_listener_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> {
log::info!("username: {username}, password: {password}");
client.set_auth(&TardisContext {
own_paths: "t1/app001".to_string(),
ak: "".to_string(),
ak: "app001".to_string(),
roles: vec![],
groups: vec![],
owner: "app001".to_string(),
Expand Down Expand Up @@ -119,7 +119,7 @@ pub async fn test_listener(client: &mut TestHttpClient) -> TardisResult<()> {
let update_counter = Arc::new(AtomicUsize::new(0));
let ctx_raw = Arc::new(TardisContext {
own_paths: "t1/app001".to_string(),
ak: "".to_string(),
ak: "app001".to_string(),
roles: vec![],
groups: vec![],
owner: "app001".to_string(),
Expand Down
8 changes: 4 additions & 4 deletions backend/spi/spi-conf/tests/spi_conf_nacos_compatible_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const SCHEMA: &str = "https";
async fn spi_conf_namespace_test() -> TardisResult<()> {
std::env::set_var(
"RUST_LOG",
"info,tardis=debug,spi_conf_listener_test=debug,sqlx=off,sea_orm=off,bios_spi_conf=DEBUG,poem_grpc=TRACE,tonic=TRACE",
"info,tardis=debug,spi_conf_listener_test=debug,sqlx=off,sea_orm=debug,bios_spi_conf=DEBUG,poem_grpc=TRACE,tonic=TRACE",
);
std::env::set_var("PROFILE", "nacos");
let docker = testcontainers::clients::Cli::default();
Expand Down Expand Up @@ -51,7 +51,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> {
log::info!("username: {username}, password: {password}");
client.set_auth(&TardisContext {
own_paths: "t1/app001".to_string(),
ak: "".to_string(),
ak: "app001".to_string(),
roles: vec![],
groups: vec![],
owner: "app001".to_string(),
Expand All @@ -71,7 +71,7 @@ async fn test_tardis_compatibility(_test_client: &TestHttpClient) -> TardisResul
let config = TardisFuns::fw_config();
let ctx = TardisContext {
own_paths: "t1/app001".to_string(),
ak: "".to_string(),
ak: "app001".to_string(),
roles: vec![],
groups: vec![],
owner: "app001".to_string(),
Expand Down Expand Up @@ -196,7 +196,7 @@ async fn test_tardis_compatibility(_test_client: &TestHttpClient) -> TardisResul
let _resp = nacos_client
.publish_config(
&NacosConfigDescriptor::new("hc-db.yaml", "hc", &(Default::default())),
&mut std::fs::File::open("tests/config/test-prod.yaml").expect("fail to open"),
&mut std::fs::File::open("tests/config/conf-nacos.toml").expect("fail to open"),
)
.await
.expect("publish failed");
Expand Down
6 changes: 0 additions & 6 deletions backend/spi/spi-conf/tests/spi_conf_test_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use testcontainers_modules::redis::Redis;
pub struct Holder<'d> {
pub pg: Container<'d, GenericImage>,
pub redis: Container<'d, Redis>,
pub mq: Container<'d, GenericImage>,
}

#[allow(dead_code)]
Expand All @@ -27,14 +26,9 @@ pub async fn init_tardis(docker: &Cli) -> TardisResult<Holder> {
let port = redis_container.get_host_port_ipv4(6379);
let url = format!("redis://127.0.0.1:{port}/0");
std::env::set_var("TARDIS_FW.CACHE.URL", url);
let mq_container = TardisTestContainer::rabbit_custom(docker);
let port = mq_container.get_host_port_ipv4(5672);
let url = format!("amqp://guest:guest@127.0.0.1:{port}/%2f");
std::env::set_var("TARDIS_FW.MQ.URL", url);
let holder = Holder {
pg: reldb_container,
redis: redis_container,
mq: mq_container,
};
TardisFuns::init(Some("tests/config")).await?;
bios_basic::rbum::rbum_initializer::init(DOMAIN_CODE, RbumConfig::default()).await?;
Expand Down

0 comments on commit 1143c63

Please sign in to comment.