Skip to content

Add unit tests for configuration parsing and validation #18

@cbaugus

Description

@cbaugus

Summary

The application reads numerous environment variables for configuration but has no tests for configuration parsing or validation logic. Invalid configurations can cause runtime panics or unexpected behavior.

Current Configuration Variables

Variable Type Required Default
TARGET_URL String Yes -
REQUEST_METHOD String No "GET"
POST_BODY JSON String No -
SKIP_TLS_VERIFY bool No false
CLIENT_CERT_PATH Path No -
CLIENT_KEY_PATH Path No -
RESOLVE_TARGET_ADDR IP:Port No -
CUSTOM_HEADERS String No -
LOAD_MODEL String No "concurrent"
MAX_CONCURRENT u32 No 10
TARGET_RPS f64 No -
MIN_RPS f64 No -
MAX_RPS f64 No -
RAMP_DURATION Duration No -
TEST_DURATION Duration No "1m"
PEAK_RPS f64 No -
MIDDAY_RPS f64 No -
NIGHT_RPS f64 No -
Various phase durations Duration No -

Test Cases Required

Required Variables

  • Missing TARGET_URL produces clear error
  • Empty TARGET_URL produces clear error
  • Invalid URL format produces clear error

Load Model Selection

  • LOAD_MODEL=concurrent selects Concurrent model
  • LOAD_MODEL=rps selects Rps model (requires TARGET_RPS)
  • LOAD_MODEL=ramp selects RampRps model (requires MIN_RPS, MAX_RPS, RAMP_DURATION)
  • LOAD_MODEL=daily selects DailyTraffic model
  • Unknown LOAD_MODEL value produces clear error
  • Missing required params for model produces clear error

mTLS Configuration

  • Both CLIENT_CERT_PATH and CLIENT_KEY_PATH required together
  • Only cert without key produces error
  • Only key without cert produces error
  • Invalid cert path produces clear error
  • Invalid key path produces clear error

Numeric Validation

  • Negative MAX_CONCURRENT produces error
  • Zero MAX_CONCURRENT produces error or warning
  • Negative RPS values produce error
  • MIN_RPS > MAX_RPS produces error

Duration Validation

  • Invalid duration format produces clear error
  • Zero duration produces warning or error
  • Extremely long durations are accepted

DNS Override

  • Valid RESOLVE_TARGET_ADDR format (IP:port) accepted
  • Invalid format produces clear error
  • IPv6 addresses handled correctly

Acceptance Criteria

  • All environment variables have test coverage
  • Missing required variables produce clear errors
  • Invalid values produce descriptive error messages
  • Default values are documented and tested
  • mTLS requires both cert and key
  • Load model parameters validated for each model type

Implementation Notes

This issue pairs well with #15 (Extract Config struct). Tests should:

  1. Use temp_env or similar crate to set env vars in tests
  2. Create a Config::from_env() function that can be unit tested
  3. Return Result<Config, ConfigError> instead of panicking

Example Test Structure

#[cfg(test)]
mod config_tests {
    use super::*;
    use temp_env::with_vars;

    #[test]
    fn test_missing_target_url_error() {
        with_vars(vec![("TARGET_URL", None::<&str>)], || {
            let result = Config::from_env();
            assert!(result.is_err());
            assert!(result.unwrap_err().to_string().contains("TARGET_URL"));
        });
    }

    #[test]
    fn test_rps_model_requires_target_rps() {
        with_vars(vec![
            ("TARGET_URL", Some("https://example.com")),
            ("LOAD_MODEL", Some("rps")),
            // TARGET_RPS intentionally missing
        ], || {
            let result = Config::from_env();
            assert!(result.is_err());
        });
    }

    #[test]
    fn test_mtls_requires_both_cert_and_key() {
        with_vars(vec![
            ("TARGET_URL", Some("https://example.com")),
            ("CLIENT_CERT_PATH", Some("/path/to/cert.pem")),
            // CLIENT_KEY_PATH intentionally missing
        ], || {
            let result = Config::from_env();
            assert!(result.is_err());
        });
    }
}

Dependencies

Priority

Medium - Configuration errors currently cause panics with stack traces. Better validation would improve user experience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions