|
| 1 | +# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Tests the performance of MMDS token generation and verification.""" |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from framework.utils import configure_mmds, populate_data_store |
| 10 | + |
| 11 | +# Default IPv4 address for MMDS |
| 12 | +DEFAULT_IPV4 = "169.254.169.254" |
| 13 | + |
| 14 | +# Number of iterations for performance measurements |
| 15 | +ITERATIONS = 500 |
| 16 | + |
| 17 | + |
| 18 | +def parse_curl_timing(timing_line): |
| 19 | + """Parse curl timing output and extract timing information in milliseconds.""" |
| 20 | + # curl -w format outputs timing in seconds, convert to milliseconds |
| 21 | + # Expected format: "time_total:0.123456" |
| 22 | + match = re.search(r"time_total:([\d.]+)", timing_line) |
| 23 | + if match: |
| 24 | + return float(match.group(1)) * 1000 # Convert to milliseconds |
| 25 | + |
| 26 | + raise ValueError(f"Could not parse timing from curl output: {timing_line}") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mmds_microvm(uvm_plain_any): |
| 31 | + """Creates a microvm with MMDS configured for performance testing.""" |
| 32 | + uvm = uvm_plain_any |
| 33 | + uvm.spawn(log_level="Info") |
| 34 | + uvm.basic_config() |
| 35 | + uvm.add_net_iface() |
| 36 | + |
| 37 | + # Configure MMDS V2 (requires tokens) |
| 38 | + configure_mmds(uvm, iface_ids=["eth0"], version="V2", ipv4_address=DEFAULT_IPV4) |
| 39 | + |
| 40 | + # Populate with minimal test data |
| 41 | + test_data = {"latest": {"meta-data": {"instance-id": "i-1234567890abcdef0"}}} |
| 42 | + populate_data_store(uvm, test_data) |
| 43 | + |
| 44 | + uvm.start() |
| 45 | + |
| 46 | + uvm.ssh.check_output(f"ip route add {DEFAULT_IPV4} dev eth0") |
| 47 | + |
| 48 | + return uvm |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.nonci |
| 52 | +def test_mmds_token(mmds_microvm, metrics): |
| 53 | + """ |
| 54 | + Test MMDS token generation performance using curl timing from within the guest. |
| 55 | +
|
| 56 | + This test measures the time it takes to generate MMDS session tokens |
| 57 | + using curl's built-in timing capabilities. |
| 58 | + """ |
| 59 | + |
| 60 | + metrics.set_dimensions( |
| 61 | + { |
| 62 | + "performance_test": "test_mmds_performance", |
| 63 | + **mmds_microvm.dimensions, |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + # Measure token generation performance |
| 68 | + for _ in range(ITERATIONS): |
| 69 | + # Curl command to generate token with timing |
| 70 | + token_cmd = ( |
| 71 | + f'curl -m 2 -s -w "\\ntime_total:%{{time_total}}" ' |
| 72 | + f'-X PUT -H "X-metadata-token-ttl-seconds: 60" ' |
| 73 | + f"http://{DEFAULT_IPV4}/latest/api/token" |
| 74 | + ) |
| 75 | + _, stdout, stderr = mmds_microvm.ssh.check_output(token_cmd) |
| 76 | + assert stderr == "", "Error generating token" |
| 77 | + |
| 78 | + # Parse timing and token from output |
| 79 | + lines = stdout.strip().split("\n") |
| 80 | + token = lines[0].strip() # First line is the token |
| 81 | + |
| 82 | + # Verify token was generated successfully |
| 83 | + assert len(token) > 0, f"Token generation failed. Output: {stdout}" |
| 84 | + |
| 85 | + generation_time_ms = parse_curl_timing(lines[-1]) |
| 86 | + metrics.put_metric("token_generation_time", generation_time_ms, "Milliseconds") |
| 87 | + |
| 88 | + # Curl command to verify token with timing |
| 89 | + request_cmd = ( |
| 90 | + f'curl -m 2 -s -w "\\ntime_total:%{{time_total}}" ' |
| 91 | + f'-X GET -H "X-metadata-token: {token}" -H "Accept: application/json" ' |
| 92 | + f"http://{DEFAULT_IPV4}/latest/meta-data/instance-id" |
| 93 | + ) |
| 94 | + _, stdout, stderr = mmds_microvm.ssh.check_output(request_cmd) |
| 95 | + assert stderr == "", "MMDS request failed" |
| 96 | + |
| 97 | + # Parse response and timing |
| 98 | + lines = stdout.strip().split("\n") |
| 99 | + response = lines[0].strip() # First line is the response |
| 100 | + |
| 101 | + # Verify request was successful |
| 102 | + assert ( |
| 103 | + "i-1234567890abcdef0" in response |
| 104 | + ), f"MMDS request failed. Response: {response}" |
| 105 | + |
| 106 | + request_time_ms = parse_curl_timing(lines[-1]) |
| 107 | + metrics.put_metric("request_time", request_time_ms, "Milliseconds") |
0 commit comments