From 35a085a3056ca1a97346a766f2e6c9acf3b4adcc Mon Sep 17 00:00:00 2001 From: James MacMahon Date: Wed, 25 May 2022 11:26:50 -0400 Subject: [PATCH] use lazy_static for httptest server instead of hard coding the httptest server port, put the httptest server build into a lazy_static block. --- nexus/tests/integration_tests/endpoints.rs | 3 +- nexus/tests/integration_tests/unauthorized.rs | 39 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/nexus/tests/integration_tests/endpoints.rs b/nexus/tests/integration_tests/endpoints.rs index 569f2c16324..ff547f13c1f 100644 --- a/nexus/tests/integration_tests/endpoints.rs +++ b/nexus/tests/integration_tests/endpoints.rs @@ -7,6 +7,7 @@ //! This is used for various authz-related tests. //! THERE ARE NO TESTS IN THIS FILE. +use crate::integration_tests::unauthorized::HTTP_SERVER; use http::method::Method; use lazy_static::lazy_static; use nexus_test_utils::RACK_UUID; @@ -234,7 +235,7 @@ lazy_static! { name: DEMO_IMAGE_NAME.clone(), description: String::from(""), }, - source: params::ImageSource::Url(String::from("http://127.0.0.1:5555/image.raw")), + source: params::ImageSource::Url(HTTP_SERVER.url("/image.raw").to_string()), block_size: params::BlockSize::try_from(4096).unwrap(), }; diff --git a/nexus/tests/integration_tests/unauthorized.rs b/nexus/tests/integration_tests/unauthorized.rs index b1eca1fa743..f71824a69af 100644 --- a/nexus/tests/integration_tests/unauthorized.rs +++ b/nexus/tests/integration_tests/unauthorized.rs @@ -55,24 +55,6 @@ async fn test_unauthorized(cptestctx: &ControlPlaneTestContext) { let client = &cptestctx.external_client; let log = &cptestctx.logctx.log; - // Run a httptest server - let server = ServerBuilder::new() - .bind_addr("127.0.0.1:5555".parse().unwrap()) - .run() - .unwrap(); - - // Fake some data - server.expect( - Expectation::matching(request::method_path("HEAD", "/image.raw")) - .times(1..) - .respond_with( - status_code(200).append_header( - "Content-Length", - format!("{}", 4096 * 1000), - ), - ), - ); - // Create test data. info!(log, "setting up resource hierarchy"); for request in &*SETUP_REQUESTS { @@ -118,7 +100,7 @@ EXAMPLE: 0 3111 5555 3111 5555 5555 0 /organizations The number in each cell is the last digit of the 400-level response that was expected for this test case. - In this case, an unauthenthicated request to "GET /organizations" returned + In this case, an unauthenticated request to "GET /organizations" returned 401. All requests to "PUT /organizations" returned 405. G GET PUT POST DEL TRCE G URL @@ -141,6 +123,25 @@ struct SetupReq { } lazy_static! { + pub static ref HTTP_SERVER: httptest::Server = { + // Run a httptest server + let server = ServerBuilder::new().run().unwrap(); + + // Fake some data + server.expect( + Expectation::matching(request::method_path("HEAD", "/image.raw")) + .times(1..) + .respond_with( + status_code(200).append_header( + "Content-Length", + format!("{}", 4096 * 1000), + ), + ), + ); + + server + }; + /// List of requests to execute at setup time static ref SETUP_REQUESTS: Vec = vec![ // Create a separate Silo (not used for anything else)