use httpmock::{Method::GET, MockRef, MockServer};
use serial_test::serial;
mod common;
use goose::goose::GooseMethod;
use goose::prelude::*;
use goose::GooseConfiguration;
const INDEX_PATH: &str = "/";
const A_404_PATH: &str = "/404";
const INDEX_KEY: usize = 0;
const A_404_KEY: usize = 1;
const EXPECT_WORKERS: usize = 2;
#[derive(Clone)]
enum TestType {
NoErrorSummary,
ErrorSummary,
}
pub async fn get_index(user: &GooseUser) -> GooseTaskResult {
let _goose = user.get(INDEX_PATH).await?;
Ok(())
}
pub async fn get_404_path(user: &GooseUser) -> GooseTaskResult {
let _goose = user.get(A_404_PATH).await?;
Ok(())
}
fn setup_mock_server_endpoints(server: &MockServer) -> Vec<MockRef> {
vec![
server.mock(|when, then| {
when.method(GET).path(INDEX_PATH);
then.status(200);
}),
server.mock(|when, then| {
when.method(GET).path(A_404_PATH);
then.status(404);
}),
]
}
fn common_build_configuration(server: &MockServer, custom: &mut Vec<&str>) -> GooseConfiguration {
let mut configuration = vec![
"--users",
"2",
"--hatch-rate",
"4",
"--run-time",
"2",
"--no-reset-metrics",
];
configuration.append(custom);
common::build_configuration(&server, configuration)
}
fn validate_error(
goose_metrics: &GooseMetrics,
mock_endpoints: &[MockRef],
configuration: &GooseConfiguration,
test_type: TestType,
) {
assert!(mock_endpoints[INDEX_KEY].hits() > 0);
assert!(mock_endpoints[A_404_KEY].hits() > 0);
let index_metrics = goose_metrics
.requests
.get(&format!("GET {}", INDEX_PATH))
.unwrap();
let a_404_metrics = goose_metrics
.requests
.get(&format!("GET {}", A_404_PATH))
.unwrap();
let a_404_errors = goose_metrics.errors.clone();
assert!(index_metrics.path == INDEX_PATH);
assert!(index_metrics.method == GooseMethod::Get);
assert!(a_404_metrics.path == A_404_PATH);
assert!(a_404_metrics.method == GooseMethod::Get);
mock_endpoints[INDEX_KEY].assert_hits(index_metrics.response_time_counter);
mock_endpoints[INDEX_KEY].assert_hits(index_metrics.success_count);
mock_endpoints[A_404_KEY].assert_hits(a_404_metrics.response_time_counter);
mock_endpoints[A_404_KEY].assert_hits(a_404_metrics.fail_count);
match test_type {
TestType::ErrorSummary => {
assert!(a_404_errors.len() == 1);
for error in a_404_errors {
assert!(error.1.method == GooseMethod::Get);
assert!(error.1.name == A_404_PATH);
assert!(error.1.occurrences == a_404_metrics.fail_count);
}
}
TestType::NoErrorSummary => {
assert!(a_404_errors.is_empty());
}
}
assert!(index_metrics.fail_count == 0);
assert!(a_404_metrics.success_count == 0);
assert!(goose_metrics.users == configuration.users.unwrap());
}
fn get_tasks() -> GooseTaskSet {
taskset!("LoadTest")
.register_task(task!(get_index))
.register_task(task!(get_404_path))
}
fn run_standalone_test(test_type: TestType) {
let server = MockServer::start();
let mock_endpoints = setup_mock_server_endpoints(&server);
let mut configuration_flags = match test_type {
TestType::NoErrorSummary => vec!["--no-error-summary"],
TestType::ErrorSummary => vec![],
};
let configuration = common_build_configuration(&server, &mut configuration_flags);
let goose_metrics = common::run_load_test(
common::build_load_test(configuration.clone(), &get_tasks(), None, None),
None,
);
validate_error(&goose_metrics, &mock_endpoints, &configuration, test_type);
}
fn run_gaggle_test(test_type: TestType) {
let server = MockServer::start();
let mock_endpoints = setup_mock_server_endpoints(&server);
let worker_configuration = common::build_configuration(&server, vec!["--worker"]);
let goose_attack = common::build_load_test(worker_configuration, &get_tasks(), None, None);
let worker_handles = common::launch_gaggle_workers(goose_attack, EXPECT_WORKERS);
let manager_configuration = match test_type {
TestType::NoErrorSummary => common_build_configuration(
&server,
&mut vec![
"--manager",
"--expect-workers",
&EXPECT_WORKERS.to_string(),
"--no-error-summary",
],
),
TestType::ErrorSummary => common_build_configuration(
&server,
&mut vec!["--manager", "--expect-workers", &EXPECT_WORKERS.to_string()],
),
};
let manager_goose_attack =
common::build_load_test(manager_configuration.clone(), &get_tasks(), None, None);
let goose_metrics = common::run_load_test(manager_goose_attack, Some(worker_handles));
validate_error(
&goose_metrics,
&mock_endpoints,
&manager_configuration,
test_type,
);
}
#[test]
fn test_error_summary() {
run_standalone_test(TestType::ErrorSummary);
}
#[test]
#[cfg_attr(not(feature = "gaggle"), ignore)]
#[serial]
fn test_error_summary_gaggle() {
run_gaggle_test(TestType::ErrorSummary);
}
#[test]
fn test_no_error_summary() {
run_standalone_test(TestType::NoErrorSummary);
}
#[test]
#[cfg_attr(not(feature = "gaggle"), ignore)]
#[serial]
fn test_no_error_summary_gaggle() {
run_gaggle_test(TestType::NoErrorSummary);
}