-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zoe Spellman
committed
Mar 7, 2024
1 parent
4a2453e
commit 732dbd4
Showing
8 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Feature: Can retrieve and set the internal log level via the REST endpoint | ||
|
||
Scenario: Espio wants to find out the current log level | ||
Given Espio doesn't know the log level | ||
When Espio asks for the log level | ||
Then Espio gets an answer | ||
|
||
Scenario Outline: Espio wants to set the log level | ||
Given the service's current log level | ||
When Espio requests it changes to <level> | ||
Then the log level is set to <level> | ||
|
||
Examples: | ||
| level | | ||
| error | | ||
| info | | ||
| debug | | ||
| warn | | ||
| trace | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::str::FromStr; | ||
|
||
use axum::async_trait; | ||
use log::Level; | ||
use ratings::features::admin::log_level::interface::{GetLogLevelResponse, SetLogLevelRequest}; | ||
use reqwest::Url; | ||
|
||
use super::Client; | ||
|
||
#[async_trait] | ||
pub trait LogClient: Client { | ||
fn rest_url(&self) -> Url { | ||
Url::from_str(self.url()) | ||
.unwrap() | ||
.join("/v1/admin/log-level") | ||
.unwrap() | ||
} | ||
|
||
async fn get_log_level( | ||
&self, | ||
) -> Result<GetLogLevelResponse, Box<dyn std::error::Error + Send + Sync>> { | ||
Ok(serde_json::from_str( | ||
&reqwest::get(self.rest_url()) | ||
.await? | ||
.error_for_status()? | ||
.text() | ||
.await?, | ||
)?) | ||
} | ||
|
||
async fn set_log_level( | ||
&self, | ||
level: Level, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
reqwest::Client::new() | ||
.post(self.rest_url()) | ||
.header("Content-Type", "application/json") | ||
.body(serde_json::to_string(&SetLogLevelRequest { level }).unwrap()) | ||
.send() | ||
.await? | ||
.error_for_status_ref()?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![allow(dead_code)] | ||
#![cfg(test)] | ||
|
||
pub mod assert; | ||
pub mod client; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use std::str::FromStr; | ||
|
||
use cucumber::{given, then, when, Parameter, World}; | ||
|
||
use helpers::client::*; | ||
use ratings::utils::Config; | ||
|
||
mod helpers; | ||
|
||
#[derive(Copy, Clone, Eq, PartialEq, Parameter, Debug)] | ||
#[param(name = "level", regex = "info|warn|debug|trace|error")] | ||
pub struct Level(log::Level); | ||
|
||
impl FromStr for Level { | ||
type Err = <log::Level as FromStr>::Err; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(Level(log::Level::from_str(s)?)) | ||
} | ||
} | ||
|
||
impl From<log::Level> for Level { | ||
fn from(value: log::Level) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl From<Level> for log::Level { | ||
fn from(value: Level) -> Self { | ||
value.0 | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, World)] | ||
#[world(init = Self::new)] | ||
struct LogWorld { | ||
client: TestClient, | ||
current_level: Option<Level>, | ||
} | ||
|
||
impl LogWorld { | ||
fn new() -> Self { | ||
let config = Config::load().expect("could not load config"); | ||
let client = TestClient::new(config.socket()); | ||
Self { | ||
client, | ||
current_level: None, | ||
} | ||
} | ||
} | ||
|
||
#[given(expr = "Espio doesn't know the log level")] | ||
fn unknown_level(world: &mut LogWorld) { | ||
world.current_level = None | ||
} | ||
|
||
#[when(expr = "Espio asks for the log level")] | ||
#[given(expr = "the service's current log level")] | ||
async fn get_log_level(world: &mut LogWorld) { | ||
world.current_level = Some( | ||
world | ||
.client | ||
.get_log_level() | ||
.await | ||
.expect("could not get log level") | ||
.level | ||
.into(), | ||
) | ||
} | ||
|
||
#[when(expr = "Espio requests it changes to {level}")] | ||
async fn set_log_level(world: &mut LogWorld, level: Level) { | ||
world | ||
.client | ||
.set_log_level(level.into()) | ||
.await | ||
.expect("problem setting log level"); | ||
} | ||
|
||
#[then(expr = "Espio gets an answer")] | ||
fn got_any_level(world: &mut LogWorld) { | ||
assert!( | ||
world.current_level.is_some(), | ||
"did not get a valid level from the endpoint" | ||
); | ||
} | ||
|
||
#[then(expr = "the log level is set to {level}")] | ||
async fn got_expected_level(world: &mut LogWorld, level: Level) { | ||
let post_set_level = world | ||
.client | ||
.get_log_level() | ||
.await | ||
.expect("could not get log level") | ||
.level; | ||
|
||
assert_eq!(level.0, post_set_level) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
dotenvy::from_filename(".env_files/test.env").ok(); | ||
|
||
LogWorld::cucumber() | ||
.repeat_skipped() | ||
.init_tracing() | ||
.max_concurrent_scenarios(1) | ||
.run_and_exit("tests/features/admin/log-level.feature") | ||
.await | ||
} |