diff --git a/.env b/.env new file mode 100644 index 00000000..c6e03cfc --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +# Diesel +DATABASE_URL=postgres://chirpstack_test:chirpstack_test@localhost/chirpstack_test?sslmode=disable + +# Testing +TEST_POSTGRESQL_DSN="postgres://chirpstack_test:chirpstack_test@localhost/chirpstack_test?sslmode=disable" +TEST_REDIS_URL="redis://localhost/1" +TEST_MOSQUITTO_SERVER="tcp://localhost:1883/" +TEST_KAFKA_BROKER="localhost:9092" +TEST_AMQP_URL="amqp://guest:guest@localhost:5672" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 44ff352e..b9377bab 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .* !/chirpstack/.rpm !/.cargo +!/.env # Log files *.log diff --git a/Cargo.lock b/Cargo.lock index b297c4ca..2b5638af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -945,6 +945,7 @@ dependencies = [ "clap 4.3.0", "diesel", "diesel_migrations", + "dotenv", "futures", "gcp_auth", "geohash", @@ -1452,6 +1453,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dtoa" version = "1.0.6" diff --git a/chirpstack/Cargo.toml b/chirpstack/Cargo.toml index c42d6a06..403c01e3 100644 --- a/chirpstack/Cargo.toml +++ b/chirpstack/Cargo.toml @@ -132,6 +132,7 @@ pin-project = "1.0" [dev-dependencies] httpmock = "0.6" bytes = "1.4" +dotenv = "0.15" # Debian packaging. [package.metadata.deb] diff --git a/chirpstack/src/integration/amqp.rs b/chirpstack/src/integration/amqp.rs index 39052f16..582df506 100644 --- a/chirpstack/src/integration/amqp.rs +++ b/chirpstack/src/integration/amqp.rs @@ -244,6 +244,8 @@ impl<'a> IntegrationTrait for Integration<'a> { #[cfg(test)] pub mod test { + use std::env; + use super::*; use futures::stream::StreamExt; use lapin::options::{ @@ -256,8 +258,11 @@ pub mod test { #[tokio::test] async fn test_amqp() { + dotenv::dotenv().ok(); + dotenv::from_filename(".env.local").ok(); + let conf = Config { - url: "amqp://guest:guest@rabbitmq:5672".to_string(), + url: env::var("TEST_AMQP_URL").unwrap(), json: true, event_routing_key: "application.{{application_id}}.device.{{dev_eui}}.event.{{event}}" .to_string(), diff --git a/chirpstack/src/integration/kafka.rs b/chirpstack/src/integration/kafka.rs index 85f38475..e34e58b3 100644 --- a/chirpstack/src/integration/kafka.rs +++ b/chirpstack/src/integration/kafka.rs @@ -223,6 +223,8 @@ impl<'a> IntegrationTrait for Integration<'a> { #[cfg(test)] pub mod test { + use std::env; + use super::*; use crate::test; use rdkafka::consumer::stream_consumer::StreamConsumer; @@ -239,8 +241,11 @@ pub mod test { async fn test_kafka() { let _guard = test::prepare().await; + dotenv::dotenv().ok(); + dotenv::from_filename(".env.local").ok(); + let conf = Config { - brokers: vec!["kafka:9092".to_string()], + brokers: vec![env::var("TEST_KAFKA_BROKER").unwrap()], topic: "chirpstack".to_string(), json: true, ..Default::default() @@ -249,7 +254,7 @@ pub mod test { let consumer: StreamConsumer = loop { match ClientConfig::new() .set("group.id", "testgroup") - .set("bootstrap.servers", "kafka:9092") + .set("bootstrap.servers", env::var("TEST_KAFKA_BROKER").unwrap()) .set("allow.auto.create.topics", "true") .set("auto.offset.reset", "beginning") .create() diff --git a/chirpstack/src/integration/mqtt.rs b/chirpstack/src/integration/mqtt.rs index fa6c0592..ea4d2186 100644 --- a/chirpstack/src/integration/mqtt.rs +++ b/chirpstack/src/integration/mqtt.rs @@ -437,6 +437,7 @@ async fn message_callback( #[cfg(test)] pub mod test { + use std::env; use super::*; use crate::config::MqttIntegration; @@ -487,7 +488,7 @@ pub mod test { let conf = MqttIntegration { event_topic: "application/{{application_id}}/device/{{dev_eui}}/event/{{event}}".into(), json: true, - server: "tcp://mosquitto:1883/".into(), + server: env::var("TEST_MOSQUITTO_SERVER").unwrap(), clean_session: true, ..Default::default() }; diff --git a/chirpstack/src/test/mod.rs b/chirpstack/src/test/mod.rs index 3e17262b..ee25c7dd 100644 --- a/chirpstack/src/test/mod.rs +++ b/chirpstack/src/test/mod.rs @@ -1,3 +1,4 @@ +use std::env; use std::sync::{Mutex, Once}; use crate::{adr, config, region, storage}; @@ -20,6 +21,9 @@ lazy_static! { } pub async fn prepare<'a>() -> std::sync::MutexGuard<'a, ()> { + dotenv::dotenv().ok(); + dotenv::from_filename(".env.local").ok(); + // Set a mutex lock to make sure database dependent tests are not overlapping. At the end of // the function the guard is returned, so that the mutex guard can be kept during the lifetime // of the function running the tests. @@ -32,10 +36,8 @@ pub async fn prepare<'a>() -> std::sync::MutexGuard<'a, ()> { // set test config let mut conf: config::Configuration = Default::default(); - conf.postgresql.dsn = - "postgres://chirpstack_test:chirpstack_test@postgres/chirpstack_test?sslmode=disable" - .to_string(); - conf.redis.servers = vec!["redis://redis/1".to_string()]; + conf.postgresql.dsn = env::var("TEST_POSTGRESQL_DSN").unwrap(); + conf.redis.servers = vec![env::var("TEST_REDIS_URL").unwrap()]; conf.network.enabled_regions = vec!["eu868".to_string()]; conf.regions = vec![config::Region { id: "eu868".to_string(), diff --git a/docker-compose.yml b/docker-compose.yml index 022e6ff2..59081700 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -52,6 +52,7 @@ services: image: rabbitmq:3-management-alpine ports: - "15672:15672" + - "5672:5672" zookeeper: image: 'bitnami/zookeeper:3' @@ -63,7 +64,7 @@ services: environment: - KAFKA_BROKER_ID=1 - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092 - - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092 + - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181 - ALLOW_PLAINTEXT_LISTENER=yes depends_on: diff --git a/shell.nix b/shell.nix index d2e0ea20..4c5d1fff 100644 --- a/shell.nix +++ b/shell.nix @@ -19,4 +19,5 @@ pkgs.mkShell { BINDGEN_EXTRA_CLANG_ARGS = "-I${pkgs.llvmPackages.libclang.lib}/lib/clang/${pkgs.llvmPackages.libclang.version}/include"; DOCKER_BUILDKIT = "1"; NIX_STORE = "/nix/store"; + TZ="UTC"; } \ No newline at end of file