From 725c295cc3dead56f3a34e61c0cab40e44106384 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Fri, 24 Jan 2020 11:23:10 -0800 Subject: [PATCH 1/2] api_server: convert the VmmActionError to a JSON object The response from this function has content-type:application/json and Swagger specifies that the JSON object must have "fault_message". However the implementation was returning a plan string such as "vCPUs resume failed." which broke clients. Signed-off-by: Kazuyoshi Kato --- src/api_server/src/parsed_request.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/api_server/src/parsed_request.rs b/src/api_server/src/parsed_request.rs index 33ae0c7b3ae..dc5f20fb76e 100644 --- a/src/api_server/src/parsed_request.rs +++ b/src/api_server/src/parsed_request.rs @@ -92,7 +92,9 @@ impl ParsedRequest { vmm_action_error ); let mut response = Response::new(Version::Http11, StatusCode::BadRequest); - response.set_body(Body::new(vmm_action_error.to_string())); + response.set_body(Body::new(ApiServer::json_fault_message( + vmm_action_error.to_string(), + ))); response } } @@ -465,18 +467,20 @@ mod tests { assert_eq!(&buf[..], expected_response.as_bytes()); // Error. - let mut buf: [u8; 160] = [0; 160]; - let response = ParsedRequest::convert_to_response(Err(VmmActionError::from( - StartMicrovmError::EventFd, - ))); + let error = VmmActionError::from(StartMicrovmError::EventFd); + let mut buf: [u8; 185] = [0; 185]; + let json = ApiServer::json_fault_message(error.to_string()); + let response = ParsedRequest::convert_to_response(Err(error)); assert!(response.write_all(&mut buf.as_mut()).is_ok()); + let expected_response = format!( "HTTP/1.1 400 \r\n\ Server: Firecracker API\r\n\ Connection: keep-alive\r\n\ Content-Type: application/json\r\n\ - Content-Length: 42\r\n\r\n{}", - VmmActionError::from(StartMicrovmError::EventFd).to_string() + Content-Length: {}\r\n\r\n{}", + json.len(), + json, ); assert_eq!(&buf[..], expected_response.as_bytes()); } From aeb810023bb506118084e835bc6328b0d2be76bf Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Fri, 24 Jan 2020 11:50:25 -0800 Subject: [PATCH 2/2] tests: check "PUT /logger" returns an error as a JSON object When "PUT /logger" returns an error, the error message must be wrapped as a JSON object. Signed-off-by: Kazuyoshi Kato --- .../functional/test_logging.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/integration_tests/functional/test_logging.py b/tests/integration_tests/functional/test_logging.py index 354c059dbcf..bdb355ab56d 100644 --- a/tests/integration_tests/functional/test_logging.py +++ b/tests/integration_tests/functional/test_logging.py @@ -118,6 +118,23 @@ def test_error_logs(test_microvm_with_ssh): ) +def test_log_config_failure(test_microvm_with_api): + """Check passing invalid FIFOs is detected and reported as an error.""" + microvm = test_microvm_with_api + microvm.spawn() + microvm.basic_config() + + response = microvm.logger.put( + log_fifo='invalid log fifo', + metrics_fifo='invalid metrics fifo', + level='Info', + show_level=True, + show_log_origin=True, + ) + assert microvm.api_session.is_status_bad_request(response.status_code) + assert response.json()['fault_message'] + + def log_file_contains_strings(log_fifo, string_list): """Check if the log file contains all strings in string_list.