Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions backend/src/api/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,19 @@ pub async fn get_block_sdp(
.into_response())
}

/// The status for a failed element or pad property call: 404 when the flow,
/// its pipeline, the element or the pad is not there, 400 for everything else
/// (a bad value, a property that cannot change in this state).
fn property_error_status(e: &PipelineError) -> StatusCode {
match e {
PipelineError::FlowNotFound(_)
| PipelineError::FlowNotRunning(_)
| PipelineError::ElementNotFound(_)
| PipelineError::PadNotFound { .. } => StatusCode::NOT_FOUND,
_ => StatusCode::BAD_REQUEST,
}
}

/// Get current property values from a running element.
///
/// Returns all readable properties and their current values from an element
Expand Down Expand Up @@ -1155,7 +1168,7 @@ pub async fn get_element_properties(
.map_err(|e| {
error!("Failed to get element properties: {}", e);
(
StatusCode::BAD_REQUEST,
property_error_status(&e),
Json(ErrorResponse::with_details(
"Failed to get element properties",
e.to_string(),
Expand Down Expand Up @@ -1208,7 +1221,7 @@ pub async fn update_element_property(
.map_err(|e| {
error!("Failed to update property: {}", e);
(
StatusCode::BAD_REQUEST,
property_error_status(&e),
Json(ErrorResponse::with_details(
"Failed to update property",
e.to_string(),
Expand Down Expand Up @@ -1272,7 +1285,7 @@ pub async fn get_pad_properties(
.map_err(|e| {
error!("Failed to get pad properties: {}", e);
(
StatusCode::BAD_REQUEST,
property_error_status(&e),
Json(ErrorResponse::with_details(
"Failed to get pad properties",
e.to_string(),
Expand Down Expand Up @@ -1336,7 +1349,7 @@ pub async fn update_pad_property(
.map_err(|e| {
error!("Failed to update pad property: {}", e);
(
StatusCode::BAD_REQUEST,
property_error_status(&e),
Json(ErrorResponse::with_details(
"Failed to update pad property",
e.to_string(),
Expand Down
4 changes: 4 additions & 0 deletions backend/src/gst/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ pub enum PipelineError {
#[error("Flow not found: {0}")]
FlowNotFound(String),

/// The flow exists, or might, but has no running pipeline.
#[error("Flow not running: {0}")]
FlowNotRunning(String),

#[error("Property {property} on element {element} cannot be changed in {state:?} state")]
PropertyNotMutable {
element: String,
Expand Down
24 changes: 12 additions & 12 deletions backend/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,9 +1759,9 @@ impl AppState {

let pipelines = self.inner.pipelines.read().await;

let manager = pipelines.get(flow_id).ok_or_else(|| {
PipelineError::InvalidFlow(format!("Pipeline not running for flow: {}", flow_id))
})?;
let manager = pipelines
.get(flow_id)
.ok_or_else(|| PipelineError::FlowNotRunning(flow_id.to_string()))?;

manager.update_element_property(element_id, property_name, &value, ramp_ms)?;

Expand Down Expand Up @@ -2803,9 +2803,9 @@ impl AppState {
) -> Result<HashMap<String, PropertyValue>, PipelineError> {
let pipelines = self.inner.pipelines.read().await;

let manager = pipelines.get(flow_id).ok_or_else(|| {
PipelineError::InvalidFlow(format!("Pipeline not running for flow: {}", flow_id))
})?;
let manager = pipelines
.get(flow_id)
.ok_or_else(|| PipelineError::FlowNotRunning(flow_id.to_string()))?;

manager.get_element_properties(element_id)
}
Expand All @@ -2828,9 +2828,9 @@ impl AppState {
// Update the running pipeline
{
let pipelines = self.inner.pipelines.read().await;
let manager = pipelines.get(flow_id).ok_or_else(|| {
PipelineError::InvalidFlow(format!("Pipeline not running for flow: {}", flow_id))
})?;
let manager = pipelines
.get(flow_id)
.ok_or_else(|| PipelineError::FlowNotRunning(flow_id.to_string()))?;
manager.update_pad_property(element_id, pad_name, property_name, &value)?;
}

Expand Down Expand Up @@ -2891,9 +2891,9 @@ impl AppState {
) -> Result<HashMap<String, PropertyValue>, PipelineError> {
let pipelines = self.inner.pipelines.read().await;

let manager = pipelines.get(flow_id).ok_or_else(|| {
PipelineError::InvalidFlow(format!("Pipeline not running for flow: {}", flow_id))
})?;
let manager = pipelines
.get(flow_id)
.ok_or_else(|| PipelineError::FlowNotRunning(flow_id.to_string()))?;

manager.get_pad_properties(element_id, pad_name)
}
Expand Down
174 changes: 174 additions & 0 deletions backend/tests/property_not_found_status_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//! Regression test for issue #928.
//!
//! The element and pad property endpoints document `404` for a flow that is
//! not running and for an element or pad that is not there, but answered
//! `400` for every error. A client could not tell "not there" from "bad
//! value". A missing or stopped flow was also reported as an invalid flow.
//!
//! `videotestsrc` is in gstreamer1.0-plugins-base and `fakesink` in core,
//! both installed in every CI test job.

pub mod common;

use axum::{
body::Body,
http::{Method, Request, StatusCode},
Router,
};
use std::collections::HashMap;
use strom_types::{Flow, FlowId, Link};
use tower::ServiceExt; // for `oneshot`

/// `videotestsrc is-live=true → fakesink`.
fn build_flow() -> Flow {
let mut flow = Flow::new("property_not_found_status_test");
flow.elements.push(strom_types::Element {
id: "src".to_string(),
element_type: "videotestsrc".to_string(),
properties: HashMap::from([(
"is-live".to_string(),
strom_types::PropertyValue::Bool(true),
)]),
position: [100.0, 200.0].into(),
pad_properties: HashMap::new(),
});
flow.elements.push(strom_types::Element {
id: "sink".to_string(),
element_type: "fakesink".to_string(),
properties: HashMap::new(),
position: [300.0, 200.0].into(),
pad_properties: HashMap::new(),
});
flow.links.push(Link {
from: "src:src".to_string(),
to: "sink:sink".to_string(),
});
flow
}

async fn status(
app: &Router,
method: Method,
uri: &str,
body: Option<serde_json::Value>,
) -> StatusCode {
let request = Request::builder().method(method).uri(uri);
let request = match body {
Some(body) => request
.header("content-type", "application/json")
.body(Body::from(body.to_string())),
None => request.body(Body::empty()),
}
.unwrap();
app.clone().oneshot(request).await.unwrap().status()
}

/// GET and PATCH on the element and on its `sink` pad, with the status each
/// returned, labelled for the assertion message.
async fn all_four(
app: &Router,
flow_id: &FlowId,
element: &str,
pad: &str,
) -> Vec<(String, StatusCode)> {
let element_uri = format!("/api/flows/{}/elements/{}/properties", flow_id, element);
let pad_uri = format!(
"/api/flows/{}/elements/{}/pads/{}/properties",
flow_id, element, pad
);
let patch = serde_json::json!({ "property_name": "name", "value": "x" });
vec![
(
format!("GET {}", element_uri),
status(app, Method::GET, &element_uri, None).await,
),
(
format!("PATCH {}", element_uri),
status(app, Method::PATCH, &element_uri, Some(patch.clone())).await,
),
(
format!("GET {}", pad_uri),
status(app, Method::GET, &pad_uri, None).await,
),
(
format!("PATCH {}", pad_uri),
status(app, Method::PATCH, &pad_uri, Some(patch)).await,
),
]
}

fn assert_all(results: &[(String, StatusCode)], expected: StatusCode) {
for (call, got) in results {
assert_eq!(
*got, expected,
"{} answered {}, expected {}",
call, got, expected
);
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn missing_flow_element_or_pad_answers_404() {
gstreamer::init().unwrap();
let state = common::state::new();
let flow = build_flow();
let flow_id = flow.id;
state.upsert_flow(flow).await.expect("upsert_flow failed");
let app = strom::create_app_with_state(state.app()).await;

// A flow that does not exist.
let unknown: FlowId = "00000000-0000-4000-8000-000000000928".parse().unwrap();
assert_all(
&all_four(&app, &unknown, "src", "src").await,
StatusCode::NOT_FOUND,
);

// A flow that exists but is not running.
assert_all(
&all_four(&app, &flow_id, "src", "src").await,
StatusCode::NOT_FOUND,
);

state.start_flow(&flow_id).await.expect("start_flow failed");

// A running flow without that element.
assert_all(
&all_four(&app, &flow_id, "nope", "src").await,
StatusCode::NOT_FOUND,
);

// A running element without that pad. fakesink has no request pads, so
// the PATCH cannot create one either.
let pad_uri = format!("/api/flows/{}/elements/sink/pads/nope/properties", flow_id);
assert_eq!(
status(&app, Method::GET, &pad_uri, None).await,
StatusCode::NOT_FOUND
);
let patch = serde_json::json!({ "property_name": "offset", "value": 0 });
assert_eq!(
status(&app, Method::PATCH, &pad_uri, Some(patch)).await,
StatusCode::NOT_FOUND
);

// Everything is there: the reads succeed.
let element_uri = format!("/api/flows/{}/elements/src/properties", flow_id);
assert_eq!(
status(&app, Method::GET, &element_uri, None).await,
StatusCode::OK
);
let pad_uri = format!("/api/flows/{}/elements/src/pads/src/properties", flow_id);
assert_eq!(
status(&app, Method::GET, &pad_uri, None).await,
StatusCode::OK
);

// A property the element does not have stays a 400: the element is there,
// the request is wrong.
let patch = serde_json::json!({ "property_name": "no-such-property", "value": 1 });
assert_eq!(
status(&app, Method::PATCH, &element_uri, Some(patch)).await,
StatusCode::BAD_REQUEST
);

state.stop_flow(&flow_id).await.expect("stop_flow failed");
}
Loading