Add dotenv for test dependency config.

This commit is contained in:
Orne Brocaar 2023-05-29 16:57:51 +01:00
parent c0d4270508
commit 4f90c87784
10 changed files with 42 additions and 9 deletions

9
.env Normal file
View File

@ -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"

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
.*
!/chirpstack/.rpm
!/.cargo
!/.env
# Log files
*.log

7
Cargo.lock generated
View File

@ -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"

View File

@ -132,6 +132,7 @@ pin-project = "1.0"
[dev-dependencies]
httpmock = "0.6"
bytes = "1.4"
dotenv = "0.15"
# Debian packaging.
[package.metadata.deb]

View File

@ -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(),

View File

@ -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()

View File

@ -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()
};

View File

@ -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(),

View File

@ -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:

View File

@ -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";
}