cf-integration 0.3.1

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
407
408
409
410
411
//! Docker Compose command construction and rendered-contract validation.

use std::ffi::OsString;
use std::fmt;
use std::path::{Path, PathBuf};

use serde_json::Value;

use crate::infrastructure::process::CommandSpec;

const LEGACY_FAST_TIME_IMAGE_PREFIXES: &[&str] = &[
    "ghcr.io/ibm/fast-time-server:",
    "ghcr.io/ibm/fast-time-server@",
];

/// Compose service keys and their public container display names.
pub(crate) const SERVICE_DISPLAY_NAMES: &[(&str, &str)] = &[
    ("auth_keygen", "cf-dataplane-auth-keygen"),
    ("gateway", "cf-controlplane"),
    ("migration", "cf-migration"),
    ("register_fast_time", "cf-register-fast-time"),
    ("fast_time_server", "cf-fast-time-server"),
    ("nginx", "cf-nginx"),
    ("postgres", "cf-postgres"),
    ("pgbouncer", "cf-pgbouncer"),
    ("redis", "cf-redis"),
    ("dataplane", "cf-dataplane"),
    ("config_writer", "cf-dataplane-config-writer"),
    ("locust", "cf-locust"),
    ("locust_worker", "cf-locust-worker"),
    ("locust_token", "cf-locust-token"),
    ("a2a_echo_agent", "cf-a2a-echo-agent"),
    ("a2a_echo_agent_v0_3_0", "cf-a2a-echo-agent-v0-3-0"),
    ("register_a2a_echo", "cf-register-a2a-echo"),
    ("mcp_inspector", "cf-mcp-inspector"),
    ("keycloak", "cf-keycloak"),
    ("mcp_conformance_server", "cf-conformance-server"),
    ("mcp_conformance_proxy", "cf-conformance-proxy"),
    ("clickstack", "cf-clickstack"),
];

/// Immutable Compose project invocation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ComposeProject {
    project_name: OsString,
    files: Vec<PathBuf>,
    profiles: Vec<OsString>,
}

impl ComposeProject {
    /// Builds the standalone official MCP conformance fixture project.
    #[must_use]
    pub(crate) fn conformance_fixture(repository_root: &Path, project_name: OsString) -> Self {
        Self {
            project_name,
            files: vec![
                repository_root
                    .join("docker")
                    .join("docker-compose.cf-conformance-fixture.yaml"),
            ],
            profiles: vec![OsString::from("conformance")],
        }
    }

    /// Builds the independently managed local ClickStack project.
    #[must_use]
    pub(crate) fn observability(repository_root: &Path, project_name: OsString) -> Self {
        Self {
            project_name,
            files: vec![
                repository_root
                    .join("docker")
                    .join("docker-compose.cf-telemetry.yaml"),
            ],
            profiles: Vec::new(),
        }
    }

    /// Builds the control-plane plus dataplane overlay project.
    #[must_use]
    pub(crate) fn dataplane(
        repository_root: &Path,
        controlplane_checkout: &Path,
        project_name: OsString,
        build_dataplane: bool,
    ) -> Self {
        let mut files = vec![
            controlplane_checkout.join("docker-compose.yml"),
            repository_root
                .join("docker")
                .join("docker-compose.cf-controlplane-build-labels.yaml"),
            repository_root
                .join("docker")
                .join("docker-compose.cf-dataplane.yaml"),
            repository_root
                .join("docker")
                .join("docker-compose.cf-integration.yaml"),
            repository_root
                .join("docker")
                .join("docker-compose.cf-dataplane-config.yaml"),
        ];
        if build_dataplane {
            files.push(
                repository_root
                    .join("docker")
                    .join("docker-compose.cf-dataplane-build.yaml"),
            );
        }
        Self {
            project_name,
            files,
            profiles: Vec::new(),
        }
    }

    /// Builds the minimal external-dataplane project used by standalone workflows.
    #[must_use]
    pub(crate) fn standalone_dataplane(
        repository_root: &Path,
        project_name: OsString,
        build_dataplane: bool,
    ) -> Self {
        let mut files = vec![
            repository_root
                .join("docker")
                .join("docker-compose.cf-dataplane-standalone.yaml"),
            repository_root
                .join("docker")
                .join("docker-compose.cf-dataplane-config.yaml"),
        ];
        if build_dataplane {
            files.push(
                repository_root
                    .join("docker")
                    .join("docker-compose.cf-dataplane-build.yaml"),
            );
        }
        Self {
            project_name,
            files,
            profiles: Vec::new(),
        }
    }

    /// Builds the stock control-plane-only project.
    #[must_use]
    pub(crate) fn controlplane(
        repository_root: &Path,
        controlplane_checkout: &Path,
        project_name: OsString,
        enable_sso: bool,
    ) -> Self {
        let mut profiles = Vec::new();
        if enable_sso {
            profiles.push(OsString::from("sso"));
        }
        Self {
            project_name,
            files: vec![
                controlplane_checkout.join("docker-compose.yml"),
                repository_root
                    .join("docker")
                    .join("docker-compose.cf-controlplane-build-labels.yaml"),
            ],
            profiles,
        }
    }

    /// Ordered Compose override files.
    #[must_use]
    #[cfg(test)]
    pub(crate) fn files(&self) -> &[PathBuf] {
        &self.files
    }

    /// Explicitly enabled Compose profiles.
    #[must_use]
    #[cfg(test)]
    pub(crate) fn profiles(&self) -> &[OsString] {
        &self.profiles
    }

    /// Replaces the enabled profile set, primarily for exhaustive cleanup.
    #[must_use]
    pub(crate) fn with_profiles<I, S>(mut self, profiles: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<OsString>,
    {
        self.profiles = profiles.into_iter().map(Into::into).collect();
        self
    }

    /// Applies the official MCP conformance fixture's Compose overrides.
    ///
    /// This is separate from enabling the fixture profile so services affected
    /// by the overlay start with the required configuration before the
    /// profile-gated fixture itself is launched.
    #[must_use]
    pub(crate) fn with_conformance_overlay(mut self, repository_root: &Path) -> Self {
        let fixture = repository_root
            .join("docker")
            .join("docker-compose.cf-conformance-fixture.yaml");
        if !self.files.contains(&fixture) {
            self.files.push(fixture);
        }
        let overlay = repository_root
            .join("docker")
            .join("docker-compose.cf-conformance.yaml");
        if !self.files.contains(&overlay) {
            self.files.push(overlay);
        }
        self
    }

    /// Applies conformance-only settings to the control-plane service.
    #[must_use]
    pub(crate) fn with_controlplane_conformance_overlay(mut self, repository_root: &Path) -> Self {
        let overlay = repository_root
            .join("docker")
            .join("docker-compose.cf-conformance-controlplane.yaml");
        if !self.files.contains(&overlay) {
            self.files.push(overlay);
        }
        self
    }

    /// Applies the control-plane runtime settings used by conformance runs.
    #[must_use]
    pub(crate) fn with_conformance_runtime(mut self, repository_root: &Path) -> Self {
        let overlay = repository_root
            .join("docker")
            .join("docker-compose.cf-conformance-runtime.yaml");
        if !self.files.contains(&overlay) {
            self.files.push(overlay);
        }
        self
    }

    /// Enables OTLP exporters for routed services; ClickStack runs independently.
    #[must_use]
    pub(crate) fn with_observability(
        mut self,
        repository_root: &Path,
        include_dataplane: bool,
    ) -> Self {
        let controlplane = repository_root
            .join("docker")
            .join("docker-compose.cf-controlplane-observability.yaml");
        if !self.files.contains(&controlplane) {
            self.files.push(controlplane);
        }
        if include_dataplane {
            self = self.with_dataplane_observability(repository_root);
        }
        self
    }

    /// Enables only the external dataplane OTLP exporters.
    #[must_use]
    pub(crate) fn with_dataplane_observability(mut self, repository_root: &Path) -> Self {
        let overlay = repository_root
            .join("docker")
            .join("docker-compose.cf-dataplane-observability.yaml");
        if !self.files.contains(&overlay) {
            self.files.push(overlay);
        }
        self
    }

    /// Enables the isolated official MCP conformance server fixture.
    #[must_use]
    pub(crate) fn with_conformance_fixture(self, repository_root: &Path) -> Self {
        let mut project = self.with_conformance_overlay(repository_root);

        let profile = OsString::from("conformance");
        if !project.profiles.contains(&profile) {
            project.profiles.push(profile);
        }

        project
    }

    /// Creates a `docker compose` command with project, files, and profiles.
    pub(crate) fn command<I, S>(&self, arguments: I) -> CommandSpec
    where
        I: IntoIterator<Item = S>,
        S: Into<OsString>,
    {
        let mut command = CommandSpec::new("docker")
            .arg("compose")
            .arg("-p")
            .arg(self.project_name.clone());
        for file in &self.files {
            command = command.arg("-f").arg(file.as_os_str().to_owned());
        }
        for profile in &self.profiles {
            command = command.arg("--profile").arg(profile.clone());
        }
        command.args(arguments)
    }
}

/// One deterministic integration Compose contract violation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ContractViolation(String);

impl fmt::Display for ContractViolation {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// Validates the rendered integration Compose configuration.
///
/// The order of returned violations is stable and intended for operator-facing
/// diagnostics and regression tests.
#[must_use]
pub(crate) fn validate_integration_contract(
    rendered: &Value,
    expected_fast_time_image: &str,
) -> Vec<ContractViolation> {
    let Some(services) = rendered.get("services").and_then(Value::as_object) else {
        return vec![violation("rendered Compose config has no services object")];
    };
    let mut violations = Vec::new();

    for &(service_name, expected_name) in SERVICE_DISPLAY_NAMES {
        let Some(service) = services.get(service_name) else {
            continue;
        };
        let actual_name = service
            .get("labels")
            .and_then(|labels| labels.get("name"))
            .and_then(Value::as_str)
            .unwrap_or("");
        if actual_name != expected_name {
            violations.push(violation(format!(
                "{service_name} display name is {actual_name:?}; expected {expected_name:?}"
            )));
        }
    }

    match services.get("fast_time_server") {
        None => violations.push(violation(
            "fast_time_server is missing from the integration compose config",
        )),
        Some(service) => {
            let image = service.get("image").and_then(Value::as_str).unwrap_or("");
            if image != expected_fast_time_image {
                violations.push(violation(format!(
                    "fast_time_server image is {image:?}; expected {expected_fast_time_image:?}"
                )));
            }
        }
    }

    let mut service_names = services.keys().collect::<Vec<_>>();
    service_names.sort_unstable();
    for service_name in service_names {
        let image = services[service_name]
            .get("image")
            .and_then(Value::as_str)
            .unwrap_or("");
        if LEGACY_FAST_TIME_IMAGE_PREFIXES
            .iter()
            .any(|prefix| image.starts_with(prefix))
        {
            violations.push(violation(format!(
                "{service_name} uses legacy Fast Time image {image:?}"
            )));
        }
    }

    let registration_command = services
        .get("register_fast_time")
        .and_then(|service| service.get("command"))
        .map(command_text)
        .unwrap_or_default();
    if !registration_command.contains("http://fast_time_server:9080/health") {
        violations.push(violation(
            "register_fast_time does not wait for fast_time_server on port 9080",
        ));
    }
    if !registration_command.contains("http://fast_time_server:9080/mcp") {
        violations.push(violation(
            "register_fast_time does not register the streamable HTTP endpoint at /mcp",
        ));
    }

    violations
}

fn command_text(command: &Value) -> String {
    match command {
        Value::String(command) => command.clone(),
        Value::Array(parts) => parts
            .iter()
            .map(|part| {
                part.as_str()
                    .map_or_else(|| part.to_string(), str::to_owned)
            })
            .collect::<Vec<_>>()
            .join("\n"),
        _ => String::new(),
    }
}

fn violation(message: impl Into<String>) -> ContractViolation {
    ContractViolation(message.into())
}