tropel-engine 0.6.0

The tropel load-test engine: scenario execution, VU scheduling, the protocol registry and the metrics pipeline.
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Non-run CLI subcommands: `inspect`, `archive`, `extensions`, `build` and
//! `version`, plus the shared data-file loader. Split out of the former
//! `cli.rs` god-file.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tropel_sdk::scenario::{Scenario, ScenarioItem};
use tropel_sdk::traits::{Driver, InputAdapter};
use tropel_sdk::{Result, TropelError};

use crate::cli_registry::build_registry;

/// `tropel inspect <input>` — show how an input resolves and what it contains
/// WITHOUT running a load test. Useful to verify a collection/HAR/script
/// parses correctly, which adapter/driver handles it, and what load profile a
/// k6 script declares.
pub(crate) async fn inspect_command(
    input: &Path,
    format: Option<&str>,
    plugins_dir: Option<&Path>,
    subprocess_adapter: &[String],
) -> Result<()> {
    let registry = build_registry(subprocess_adapter, plugins_dir)?;
    let bytes = std::fs::read(input)
        .map_err(|e| TropelError::Parse(format!("Failed to read '{}': {}", input.display(), e)))?;

    println!("Tropel Inspect — v{}", env!("CARGO_PKG_VERSION"));
    println!("Input: {}", input.display());

    // 1. Drivers first (same precedence as the engine).
    let driver: Option<Box<dyn Driver>> = if let Some(fmt) = format {
        registry.resolve_driver_by_id(fmt)
    } else {
        registry.resolve_driver(&bytes)
    };
    if let Some(driver) = driver {
        println!("Resolved by driver: {}", driver.id());
        println!("Kind: imperative (runs JS per iteration)");
        // Note: an empty env is passed here — scripts that derive their
        // options from `__ENV` will show the defaults rather than what a
        // configured `run` would apply. `inspect` is a dry-run verification
        // tool; threading real env/`-e` values here is a future enhancement.
        match driver
            .declared_options(&bytes, Some(input), &HashMap::new())
            .await
        {
            Ok(Some(opts)) => {
                println!("Declared options:");
                if let Some(exec) = &opts.execution {
                    println!("  execution: {} ({:?})", exec.executor_name(), exec);
                }
                if let Some(scenarios) = &opts.scenarios {
                    println!("  scenarios: {}", scenarios.len());
                    for (name, sc) in scenarios {
                        println!(
                            "    - {}: {} ({:?})",
                            name,
                            sc.execution.executor_name(),
                            sc.execution
                        );
                    }
                }
                if !opts.thresholds.is_empty() {
                    println!("  thresholds: {}", opts.thresholds.len());
                    for (name, t) in &opts.thresholds {
                        println!("    - {}: {}", name, t.expression);
                    }
                }
            }
            Ok(None) => {
                println!("Declared options: (none)");
            }
            Err(e) => {
                // Backlog line 153: the script DECLARES options but they are
                // malformed — surface the error rather than silently showing
                // nothing.
                println!("Declared options: ERROR — {}", e);
            }
        }
        return Ok(());
    }

    // 2. Fall back to input adapters (declarative).
    let adapter: Box<dyn InputAdapter> = if let Some(fmt) = format {
        registry.resolve_input_by_id(fmt).ok_or_else(|| {
            let available = registry.list_inputs();
            TropelError::Config(format!(
                "Unknown input format '{}'. Available formats: {}",
                fmt,
                available.join(", ")
            ))
        })?
    } else {
        registry.resolve_input(&bytes).ok_or_else(|| {
            let available = registry.list_inputs();
            TropelError::Parse(format!(
                "No input adapter recognized '{}'. Available adapters: {}",
                input.display(),
                if available.is_empty() {
                    "(none registered — check build configuration)".to_string()
                } else {
                    available.join(", ")
                }
            ))
        })?
    };

    println!("Resolved by adapter: {}", adapter.id());
    println!("Kind: declarative (static request list)");
    let scenario = adapter.parse_with_path(&bytes, Some(input))?;
    print_scenario_summary(&scenario);
    Ok(())
}

/// Recursively print a scenario's request tree plus totals.
pub(crate) fn print_scenario_summary(scenario: &Scenario) {
    println!("Scenario: {}", scenario.info.name);
    if let Some(desc) = &scenario.info.description {
        println!("  description: {}", desc);
    }
    if let Some(auth) = &scenario.auth {
        println!("  global auth: {:?}", auth);
    }
    println!("  variables: {} defined", scenario.variables.len());
    for (k, v) in &scenario.variables {
        println!("    {} = {}", k, v);
    }

    fn walk(items: &[ScenarioItem], depth: usize, out: &mut (usize, usize)) {
        for item in items {
            let indent = "  ".repeat(depth);
            match &item.request {
                Some(req) => {
                    out.0 += 1;
                    let scripted = !item.test.is_empty() || !item.prerequest.is_empty();
                    if scripted {
                        out.1 += 1;
                    }
                    println!(
                        "{}{}{} {}{}",
                        indent,
                        item.name,
                        req.method,
                        req.url,
                        if scripted { " (scripted)" } else { "" }
                    );
                }
                None => {
                    println!("{}{} (folder)", indent, item.name);
                    walk(&item.items, depth + 1, out);
                }
            }
        }
    }

    let mut counts = (0usize, 0usize);
    walk(&scenario.items, 1, &mut counts);
    println!("Totals: {} requests ({} with scripts)", counts.0, counts.1);
}

/// `tropel archive <input> -o <dir>` — bundle a test into a self-contained
/// directory so it can be replayed on another machine (or after the original
/// files move). Copies the input plus any referenced data/env/config files and
/// writes a `tropel-archive.json` manifest describing how to re-run it.
pub(crate) async fn archive_command(
    input: &Path,
    format: Option<&str>,
    output: Option<&Path>,
    data_file: Option<&Path>,
    env_file: Option<&Path>,
    config: Option<&Path>,
) -> Result<()> {
    let out_dir = output.unwrap_or_else(|| Path::new("./tropel-archive"));
    std::fs::create_dir_all(out_dir).map_err(TropelError::Io)?;

    // Input file — the core of the bundle.
    let input_name = input
        .file_name()
        .ok_or_else(|| {
            TropelError::Config(format!("Input '{}' has no file name", input.display()))
        })?
        .to_string_lossy()
        .to_string();
    let bundled_input = out_dir.join(&input_name);
    std::fs::copy(input, &bundled_input).map_err(TropelError::Io)?;

    // Referenced dependency files, each copied next to the input. All files
    // land in ONE flat directory keyed by file name, so a dep sharing the
    // input's name (or another dep's name) would silently overwrite — guard
    // against collisions so the bundle stays deterministic.
    let mut deps: Vec<(String, PathBuf, PathBuf)> = Vec::new(); // (role, src, dest)
    let mut used_names: HashMap<String, String> = HashMap::new(); // name -> role
    used_names.insert(input_name.clone(), "input".to_string());
    let mut copy_dep = |role: &str, src: &Path, out: &Path| -> Result<()> {
        let name = src
            .file_name()
            .ok_or_else(|| {
                TropelError::Config(format!("{} '{}' has no file name", role, src.display()))
            })?
            .to_string_lossy()
            .to_string();
        if let Some(existing) = used_names.get(&name) {
            return Err(TropelError::Config(format!(
                "archive: '{}' (from {}) collides with {} — all bundled files \
                 share one directory, rename it or bundle separately",
                name,
                src.display(),
                existing
            )));
        }
        used_names.insert(name.clone(), role.to_string());
        let dest = out.join(&name);
        std::fs::copy(src, &dest).map_err(TropelError::Io)?;
        deps.push((role.to_string(), src.to_path_buf(), dest));
        Ok(())
    };
    if let Some(d) = data_file {
        copy_dep("data_file", d, out_dir)?;
    }
    if let Some(e) = env_file {
        copy_dep("env_file", e, out_dir)?;
    }
    if let Some(c) = config {
        copy_dep("config", c, out_dir)?;
    }

    // Manifest: how to re-run this bundle.
    let mut manifest = serde_json::Map::new();
    manifest.insert(
        "version".into(),
        serde_json::Value::String(env!("CARGO_PKG_VERSION").into()),
    );
    manifest.insert(
        "input".into(),
        serde_json::Value::String(input_name.clone()),
    );
    if let Some(fmt) = format {
        manifest.insert("format".into(), serde_json::Value::String(fmt.to_string()));
    }
    let mut dep_map = serde_json::Map::new();
    for (role, _src, dest) in &deps {
        dep_map.insert(
            role.clone(),
            serde_json::Value::String(
                dest.file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string(),
            ),
        );
    }
    manifest.insert("bundled_files".into(), serde_json::Value::Object(dep_map));

    // Build the suggested re-run command relative to the bundle directory.
    let mut run_cmd = format!("tropel run {}", input_name);
    if let Some(fmt) = format {
        run_cmd.push_str(&format!(" --format {}", fmt));
    }
    for (role, _src, dest) in &deps {
        let flag = match role.as_str() {
            "data_file" => "--data-file",
            "env_file" => "--env-file",
            "config" => "--config",
            _ => continue,
        };
        run_cmd.push_str(&format!(
            " {} {}",
            flag,
            dest.file_name().unwrap_or_default().to_string_lossy()
        ));
    }
    manifest.insert("run".into(), serde_json::Value::String(run_cmd.clone()));

    let manifest_path = out_dir.join("tropel-archive.json");
    let manifest_json = serde_json::Value::Object(manifest);
    std::fs::write(
        &manifest_path,
        serde_json::to_string_pretty(&manifest_json)
            .map_err(|e| TropelError::Other(format!("manifest serialize: {}", e)))?,
    )
    .map_err(TropelError::Io)?;

    println!("Tropel Archive — v{}", env!("CARGO_PKG_VERSION"));
    println!("Bundle created in: {}", out_dir.display());
    println!(
        "  input:  {} (from {})",
        bundled_input.display(),
        input.display()
    );
    for (role, src, dest) in &deps {
        println!("  {}: {} (from {})", role, dest.display(), src.display());
    }
    println!("  manifest: {}", manifest_path.display());
    println!("Re-run from the bundle directory:");
    println!("  cd {} && {}", out_dir.display(), run_cmd);
    Ok(())
}

pub(crate) async fn list_extensions(plugins_dir: Option<&std::path::Path>) -> Result<()> {
    let registry = build_registry(&[], plugins_dir)?;

    let inputs = registry.list_inputs();

    println!("Tropel Extensions — v{}", env!("CARGO_PKG_VERSION"));
    println!();

    if inputs.is_empty() {
        println!("  No input adapters registered.");
        println!("  Use `tropel build --with <crate>` to build a custom binary with extensions.");
    } else {
        println!("  Input formats:");
        for fmt in &inputs {
            println!(
                "    - {}  (use: `tropel run input.{} --format {})",
                fmt, fmt, fmt
            );
        }
        println!();
        println!("  Use `tropel run <file> --format <name>` to select a specific format.");
        println!("  Without `--format`, the engine auto-detects from file content.");
    }

    let protocols = registry.list_protocols();
    if !protocols.is_empty() {
        println!();
        println!("  Protocols:");
        for p in &protocols {
            println!("    - {}", p);
        }
    }

    let outputs = registry.list_outputs();
    if !outputs.is_empty() {
        println!();
        println!("  Outputs:");
        for o in &outputs {
            println!("    - {}", o);
        }
    }

    Ok(())
}

pub(crate) async fn build_custom(
    with: &[String],
    output: &std::path::Path,
    release: bool,
) -> Result<()> {
    use tropel_build::{build, BuildConfig};

    // Each `--with` spec is parsed AND validated here — names/versions/URLs
    // are injected into the generated Cargo.toml, so a malformed or hostile
    // value must fail before any file is written (build-time code injection).
    let mut extensions = Vec::with_capacity(with.len());
    for spec in with {
        extensions.push(tropel_build::parse_dep_spec(spec)?);
    }

    let config = BuildConfig {
        extensions,
        output: output.to_path_buf(),
        release,
    };

    build(&config)
}

pub(crate) fn print_version() -> Result<()> {
    println!("Tropel v{}", env!("CARGO_PKG_VERSION"));
    // Derived from [workspace.package] repository (inherited by member
    // crates) so the banner can never drift from the manifest again — the
    // literal URL went stale twice (prasadthx → transithq).
    println!("Repository: {}", env!("CARGO_PKG_REPOSITORY"));
    println!("License: Apache-2.0");
    // P4b: shims are JS-only and version independently of the engine; surface
    // it so the P6 version handshake has both numbers to compare.
    println!("Shim bundle: v{}", crate::js_bootstrap::SHIM_BUNDLE_VERSION);
    Ok(())
}

/// `tropel new [script.js]` — write a minimal, runnable k6-style script
/// template (TR-251). k6's `k6 new` writes the same default script; an empty
/// target script is the most common onboarding step.
pub(crate) fn new_command(output: &Path) -> Result<()> {
    if output.exists() {
        return Err(TropelError::Config(format!(
            "'{}' already exists — refusing to overwrite",
            output.display()
        )));
    }
    let template = r#"import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
  vus: 1,
  duration: '30s',
};

export default function () {
  const res = http.get('https://test.k6.io/');
  check(res, {
    'status is 200': (r) => r.status === 200,
  });
  sleep(1);
}
"#;
    std::fs::write(output, template).map_err(TropelError::Io)?;
    println!("Created '{}'", output.display());
    Ok(())
}

/// Load iteration data from a CSV or JSON file.
pub(crate) fn load_data_file(path: &PathBuf) -> Result<Vec<HashMap<String, serde_json::Value>>> {
    let content = std::fs::read_to_string(path).map_err(TropelError::Io)?;

    let trimmed = content.trim();

    if trimmed.starts_with('[') {
        let data: Vec<HashMap<String, serde_json::Value>> = serde_json::from_str(trimmed)
            .map_err(|e| TropelError::Parse(format!("JSON data-file parse error: {}", e)))?;
        return Ok(data);
    }

    if trimmed.contains(',') || trimmed.starts_with('"') {
        let mut reader = csv::ReaderBuilder::new()
            .has_headers(true)
            .flexible(true)
            .from_reader(content.as_bytes());

        let headers: Vec<String> = reader
            .headers()
            .map_err(|e| TropelError::Parse(format!("CSV header error: {}", e)))?
            .iter()
            .map(|h| h.to_string())
            .collect();

        let mut rows = Vec::new();
        for result in reader.records() {
            let record =
                result.map_err(|e| TropelError::Parse(format!("CSV record error: {}", e)))?;
            let mut map = HashMap::new();
            for (i, field) in record.iter().enumerate() {
                if i < headers.len() {
                    map.insert(
                        headers[i].clone(),
                        serde_json::Value::String(field.to_string()),
                    );
                }
            }
            rows.push(map);
        }
        return Ok(rows);
    }

    Ok(vec![])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_writes_a_runnable_template_and_refuses_overwrite() {
        let dir = std::env::temp_dir();
        let path = dir.join(format!("tropel-new-{}.js", std::process::id()));
        let _ = std::fs::remove_file(&path);

        new_command(&path).unwrap();
        let script = std::fs::read_to_string(&path).unwrap();
        assert!(
            script.contains("export default function"),
            "template must be a runnable script, got: {}",
            script
        );
        assert!(script.contains("http.get"));
        // Refuses to overwrite an existing file (k6 new also errors).
        let err = new_command(&path).unwrap_err();
        assert!(
            err.to_string().contains("already exists"),
            "must refuse to overwrite, got: {}",
            err
        );
        let _ = std::fs::remove_file(&path);
    }
}