cf-integration 0.3.0

Integration and conformance harness for ContextForge control-plane and data-plane services
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::Path;

use cf_integration::infrastructure::StackMode;
use cf_integration::infrastructure::config::{
    AppConfig, ConfigBootstrap, ConfigRequirements, Environment,
};
use cf_integration::performance::{LoadRequest, LoadSettings, LocustCommand};
use tempfile::TempDir;

fn environment(values: &[(&str, &str)]) -> Environment {
    values
        .iter()
        .map(|(key, value)| (OsString::from(key), OsString::from(value)))
        .collect()
}

fn repository_root(dotenv: Option<&str>) -> TempDir {
    let root = tempfile::tempdir().expect("temporary repository root should be created");
    fs::write(root.path().join("Cargo.toml"), "[package]\n")
        .expect("temporary Cargo manifest should be written");
    fs::create_dir_all(root.path().join("docker"))
        .expect("temporary docker directory should be created");
    fs::write(
        root.path()
            .join("docker/docker-compose.cf-integration.yaml"),
        "services: {}\n",
    )
    .expect("temporary Compose file should be written");
    if let Some(contents) = dotenv {
        fs::write(root.path().join(".env"), contents).expect("dotenv should be written");
    }
    root
}

fn config(root: &Path, process: &Environment) -> AppConfig {
    let bootstrap = ConfigBootstrap::load(process, root).expect("bootstrap should load");
    AppConfig::load(bootstrap, ConfigRequirements::RUNTIME).expect("application config should load")
}

fn args(smoke: bool) -> LoadRequest {
    LoadRequest {
        smoke,
        users: None,
        spawn_rate: None,
        run_time: None,
    }
}

#[test]
fn full_load_uses_configured_defaults() {
    let root = repository_root(None);
    let settings = LoadSettings::resolve(&config(root.path(), &Environment::new()), &args(false))
        .expect("default load settings should resolve");

    assert_eq!(settings.users().get(), 100);
    assert_eq!(settings.spawn_rate(), 10.0);
    assert_eq!(settings.run_time(), "5m");
}

#[test]
fn command_line_values_override_process_environment() {
    let root = repository_root(None);
    let process = environment(&[
        ("LOCUST_USERS", "20"),
        ("LOCUST_SPAWN_RATE", "2.5"),
        ("LOCUST_RUN_TIME", "30s"),
    ]);
    let mut cli = args(true);
    cli.users = Some(3);
    cli.spawn_rate = Some(0.5);
    cli.run_time = Some(String::from("15s"));

    let settings = LoadSettings::resolve(&config(root.path(), &process), &cli)
        .expect("CLI settings should resolve");

    assert_eq!(settings.users().get(), 3);
    assert_eq!(settings.spawn_rate(), 0.5);
    assert_eq!(settings.run_time(), "15s");
}

#[test]
fn smoke_replaces_dotenv_and_default_values_but_preserves_process_values() {
    let root = repository_root(Some(
        "LOCUST_USERS=90\nLOCUST_SPAWN_RATE=9\nLOCUST_RUN_TIME=9m\n",
    ));
    let process = environment(&[("LOCUST_SPAWN_RATE", "2.5")]);

    let settings = LoadSettings::resolve(&config(root.path(), &process), &args(true))
        .expect("smoke settings should resolve");

    assert_eq!(settings.users().get(), 1);
    assert_eq!(settings.spawn_rate(), 2.5);
    assert_eq!(settings.run_time(), "10s");
}

#[test]
fn invalid_process_values_return_contextual_errors_including_empty_values() {
    for (key, value, expected) in [
        (
            "LOCUST_USERS",
            "",
            "LOCUST_USERS must be an integer greater than zero",
        ),
        (
            "LOCUST_SPAWN_RATE",
            "NaN",
            "LOCUST_SPAWN_RATE must be a finite number greater than zero",
        ),
        ("LOCUST_RUN_TIME", "forever", "positive Locust duration"),
    ] {
        let root = repository_root(None);
        let process = environment(&[(key, value)]);

        let error = LoadSettings::resolve(&config(root.path(), &process), &args(false))
            .expect_err("invalid load settings should fail");

        assert!(
            error.to_string().contains(expected),
            "unexpected error: {error:#}"
        );
    }
}

#[test]
fn run_time_rejects_units_and_group_orders_locust_does_not_support() {
    for run_time in ["1ms", "1d", "1s1s", "1s1m", "1m1h"] {
        let root = repository_root(None);
        let mut cli = args(false);
        cli.run_time = Some(run_time.to_owned());

        let error = LoadSettings::resolve(&config(root.path(), &Environment::new()), &cli)
            .expect_err("unsupported Locust run time must fail before process execution");

        assert!(
            error.to_string().contains("positive Locust duration"),
            "unexpected error for {run_time}: {error:#}"
        );
    }

    let root = repository_root(None);
    let mut cli = args(false);
    cli.run_time = Some("1h20m3s".to_owned());
    let settings = LoadSettings::resolve(&config(root.path(), &Environment::new()), &cli)
        .expect("ordered Locust h/m/s groups should resolve");
    assert_eq!(settings.run_time(), "1h20m3s");
}

#[test]
fn dataplane_locust_command_has_exact_compose_shape_and_environment() {
    let root = repository_root(None);
    let config = config(root.path(), &Environment::new());
    let settings = LoadSettings::resolve(&config, &args(false)).expect("settings should resolve");

    let run = LocustCommand::new_with_protocol_version(
        &config,
        StackMode::Dataplane,
        &settings,
        "scoped.jwt.value",
        Some("server-id"),
        "2025-11-25",
    )
    .expect("dataplane Locust command should build");

    let integration_dir = root.path().join(".integration");
    let asset_root = config.asset_root();
    let report_dir = integration_dir
        .join("reports")
        .join("load")
        .join("dataplane")
        .join("locust");
    assert_eq!(run.report_dir(), report_dir);
    assert!(run.report_dir().is_dir());
    assert_eq!(
        run.command().arguments(),
        [
            OsString::from("compose"),
            OsString::from("-p"),
            OsString::from("cf"),
            OsString::from("-f"),
            integration_dir
                .join("mcp-context-forge")
                .join("docker-compose.yml")
                .into_os_string(),
            OsString::from("-f"),
            asset_root
                .join("docker")
                .join("docker-compose.cf-controlplane-build-labels.yaml")
                .into_os_string(),
            OsString::from("-f"),
            asset_root
                .join("docker")
                .join("docker-compose.cf-dataplane.yaml")
                .into_os_string(),
            OsString::from("-f"),
            asset_root
                .join("docker")
                .join("docker-compose.cf-integration.yaml")
                .into_os_string(),
            OsString::from("--profile"),
            OsString::from("testing"),
            OsString::from("run"),
            OsString::from("--rm"),
            OsString::from("--no-deps"),
            OsString::from("--volume"),
            volume_argument(&report_dir),
            OsString::from("locust"),
        ]
    );

    let expected_environment = HashMap::from([
        ("CF_INTEGRATION_DIR", integration_dir.as_os_str()),
        ("CF_INTEGRATION_ROOT", asset_root.as_os_str()),
        ("LOCUST_LOCUSTFILE", OsStr::new("locustfile_mcp.py")),
        ("LOCUST_MODE", OsStr::new("headless")),
        ("LOCUST_REQUEST_TIMEOUT_SECONDS", OsStr::new("60")),
        ("LOCUST_RUN_TIME", OsStr::new("5m")),
        ("LOCUST_SPAWN_RATE", OsStr::new("10")),
        ("LOCUST_USERS", OsStr::new("100")),
        ("MCPGATEWAY_BEARER_TOKEN", OsStr::new("scoped.jwt.value")),
        ("MCP_SERVER_ID", OsStr::new("server-id")),
        ("MCP_STACK_MODE", OsStr::new("dataplane")),
        ("MCP_PROTOCOL_VERSION", OsStr::new("2025-11-25")),
    ]);
    for (key, expected) in expected_environment {
        assert_eq!(
            run.command().environment().get(OsStr::new(key)),
            Some(&expected.to_owned()),
            "environment mismatch for {key}"
        );
    }
    assert_eq!(run.command().environment().len(), 12);
    assert!(
        !run.command()
            .environment()
            .contains_key(OsStr::new("COMPOSE_PROGRESS")),
        "Locust Compose runs must retain terminal-aware progress"
    );
}

#[test]
fn controlplane_uses_the_same_harness_mcp_adapter_and_does_not_require_server_id() {
    let root = repository_root(None);
    let process = environment(&[
        ("LOCUST_REQUEST_TIMEOUT_SECONDS", "2.5"),
        ("MCP_PROTOCOL_VERSION", "2025-11-25"),
        ("MCP_TOOL_NAMES", "safe_time,safe_echo"),
    ]);
    let config = config(root.path(), &process);
    let settings = LoadSettings::resolve(&config, &args(true)).expect("settings should resolve");

    let run = LocustCommand::new_with_protocol_version(
        &config,
        StackMode::Controlplane,
        &settings,
        "admin.jwt.value",
        None,
        "2025-06-18",
    )
    .expect("control-plane Locust command should build");

    assert_eq!(
        run.report_dir(),
        root.path()
            .join(".integration/reports/load/controlplane/locust")
    );
    assert_eq!(
        run.command()
            .environment()
            .get(OsStr::new("LOCUST_LOCUSTFILE")),
        Some(&OsString::from("locustfile_mcp.py"))
    );
    assert_eq!(
        run.command()
            .environment()
            .get(OsStr::new("MCP_STACK_MODE")),
        Some(&OsString::from("controlplane"))
    );
    assert_eq!(
        run.command()
            .environment()
            .get(OsStr::new("LOCUST_REQUEST_TIMEOUT_SECONDS")),
        Some(&OsString::from("2.5"))
    );
    assert_eq!(
        run.command()
            .environment()
            .get(OsStr::new("MCP_PROTOCOL_VERSION")),
        Some(&OsString::from("2025-06-18"))
    );
    assert_eq!(
        run.command()
            .environment()
            .get(OsStr::new("MCP_TOOL_NAMES")),
        Some(&OsString::from("safe_time,safe_echo"))
    );
    assert!(
        !run.command()
            .environment()
            .contains_key(OsStr::new("MCP_SERVER_ID"))
    );
    let arguments = run.command().arguments();
    let entrypoint = arguments
        .windows(2)
        .find(|pair| pair[0] == "--entrypoint")
        .expect("control-plane command should replace the shell entrypoint");
    assert_eq!(entrypoint[1], "locust");
    assert!(
        arguments
            .windows(2)
            .any(|pair| pair == [OsString::from("-e"), OsString::from("MCP_STACK_MODE")]),
        "control-plane command must propagate MCP_STACK_MODE into the container"
    );
    for name in [
        "LOCUST_REQUEST_TIMEOUT_SECONDS",
        "MCP_PROTOCOL_VERSION",
        "MCP_TOOL_NAMES",
    ] {
        assert!(
            arguments
                .windows(2)
                .any(|pair| pair == [OsString::from("-e"), OsString::from(name)]),
            "control-plane command must propagate {name} into the container"
        );
    }
    assert!(arguments.contains(&OsString::from("/mnt/locust-cf/locustfile_mcp.py")));
    let mut expected_adapter_mount = config
        .asset_root()
        .join("scripts")
        .join("locustfile_mcp.py")
        .into_os_string();
    expected_adapter_mount.push(":/mnt/locust-cf/locustfile_mcp.py:ro");
    let adapter_mount = arguments
        .windows(2)
        .find(|pair| pair[0] == "--volume" && pair[1] == expected_adapter_mount)
        .expect("control-plane command should mount the harness MCP adapter");
    assert_eq!(adapter_mount[0], "--volume");
    assert!(arguments.contains(&OsString::from("--only-summary")));
    assert!(!arguments.contains(&OsString::from("/bin/sh")));
    assert!(!arguments.contains(&OsString::from("-c")));
}

#[test]
fn locust_request_timeout_rejects_empty_non_finite_and_non_positive_values() {
    for value in ["", "NaN", "inf", "0", "-1"] {
        let root = repository_root(None);
        let process = environment(&[("LOCUST_REQUEST_TIMEOUT_SECONDS", value)]);
        let config = config(root.path(), &process);
        let settings =
            LoadSettings::resolve(&config, &args(false)).expect("load settings should resolve");

        let error = LocustCommand::new_with_protocol_version(
            &config,
            StackMode::Controlplane,
            &settings,
            "token",
            None,
            "2025-11-25",
        )
        .expect_err("invalid request timeout should fail before launch");

        assert!(
            error.to_string().contains(
                "LOCUST_REQUEST_TIMEOUT_SECONDS must be a finite number greater than zero"
            ),
            "unexpected error for {value:?}: {error:#}"
        );
    }
}

#[test]
fn dataplane_requires_nonempty_server_id_and_all_modes_require_a_token() {
    let root = repository_root(None);
    let config = config(root.path(), &Environment::new());
    let settings = LoadSettings::resolve(&config, &args(false)).expect("settings should resolve");

    let missing_server = LocustCommand::new_with_protocol_version(
        &config,
        StackMode::Dataplane,
        &settings,
        "token",
        None,
        "2025-11-25",
    )
    .expect_err("dataplane server ID should be required");
    assert!(missing_server.to_string().contains("server ID"));

    let missing_token = LocustCommand::new_with_protocol_version(
        &config,
        StackMode::Controlplane,
        &settings,
        "",
        None,
        "2025-11-25",
    )
    .expect_err("bearer token should be required");
    assert!(missing_token.to_string().contains("bearer token"));
}

fn volume_argument(report_dir: &Path) -> OsString {
    let mut argument = report_dir.as_os_str().to_owned();
    argument.push(":/mnt/reports");
    argument
}