eggress-testkit 1.0.3

Test utilities for eggress proxy
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
use std::fmt;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// Structured test outcome for parity reporting.
///
/// Distinguishes the reason for a skip so that reports cannot be
/// mistaken for passing evidence when tests did not actually run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TestStatus {
    /// Test ran and passed.
    Passed,
    /// Test ran and failed.
    Failed,
    /// Test skipped because the env-var gate was not set (e.g. `EGRESS_REQUIRE_EXTERNAL_INTEROP`).
    SkippedMissingGate,
    /// Test skipped because the required external tool is not installed.
    SkippedMissingTool,
    /// Test was not run at all (e.g. CI job did not include this test group).
    NotRun,
}

impl fmt::Display for TestStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Passed => write!(f, "passed"),
            Self::Failed => write!(f, "failed"),
            Self::SkippedMissingGate => write!(f, "skipped_missing_gate"),
            Self::SkippedMissingTool => write!(f, "skipped_missing_tool"),
            Self::NotRun => write!(f, "not_run"),
        }
    }
}

impl FromStr for TestStatus {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "passed" => Ok(Self::Passed),
            "failed" => Ok(Self::Failed),
            "skipped_missing_gate" => Ok(Self::SkippedMissingGate),
            "skipped_missing_tool" => Ok(Self::SkippedMissingTool),
            "not_run" => Ok(Self::NotRun),
            other => Err(format!("unknown test status: \"{}\"", other)),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ParityReport {
    pub eggress_commit: Option<String>,
    pub pproxy_version: String,
    pub os_platform: String,
    pub rust_version: String,
    pub python_version: String,
    pub feature_gates: Vec<String>,
    pub features: Vec<FeatureReport>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FeatureReport {
    pub feature_id: String,
    pub category: String,
    pub manifest_evidence: String,
    pub tests_executed: Vec<String>,
    pub status: TestStatus,
    pub skip_reason: Option<String>,
    pub observed_divergence: Option<String>,
    pub suggested_evidence: String,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct ManifestEntry {
    pub feature_id: String,
    pub category: String,
    pub evidence: String,
    #[serde(default)]
    pub tests: Vec<String>,
}

impl Default for ParityReport {
    fn default() -> Self {
        Self::new()
    }
}

impl ParityReport {
    pub fn new() -> Self {
        let rust_version = Command::new("rustc")
            .arg("--version")
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    String::from_utf8(o.stdout)
                        .ok()
                        .map(|s| s.trim().to_string())
                } else {
                    None
                }
            })
            .unwrap_or_else(|| "unknown".to_string());

        let python_version = detect_python_version().unwrap_or_else(|| "unknown".to_string());
        let pproxy_version = detect_pproxy_version().unwrap_or_else(|| "unknown".to_string());

        let os_platform = std::env::consts::OS.to_string();

        Self {
            eggress_commit: detect_eggress_commit(),
            pproxy_version,
            os_platform,
            rust_version,
            python_version,
            feature_gates: Vec::new(),
            features: Vec::new(),
        }
    }

    pub fn add_feature(&mut self, feature: FeatureReport) {
        self.features.push(feature);
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string_pretty(self).expect("serialization should not fail")
    }

    pub fn to_markdown(&self) -> String {
        let mut md = String::new();

        md.push_str("# Eggress Parity Report\n\n");

        md.push_str("## Environment\n\n");
        md.push_str("| Field | Value |\n");
        md.push_str("|-------|-------|\n");
        md.push_str(&format!(
            "| eggress commit | {} |\n",
            self.eggress_commit.as_deref().unwrap_or("n/a")
        ));
        md.push_str(&format!("| pproxy version | {} |\n", self.pproxy_version));
        md.push_str(&format!("| OS platform | {} |\n", self.os_platform));
        md.push_str(&format!("| rust version | {} |\n", self.rust_version));
        md.push_str(&format!("| python version | {} |\n", self.python_version));

        if !self.feature_gates.is_empty() {
            md.push_str("\n## Feature Gates\n\n");
            for gate in &self.feature_gates {
                md.push_str(&format!("- `{}`\n", gate));
            }
        }

        md.push_str("\n## Feature Results\n\n");
        md.push_str("| Feature ID | Category | Status | Manifest Evidence | Tests Executed | Skip Reason | Divergence | Suggested Evidence |\n");
        md.push_str("|-----------|----------|--------|-------------------|----------------|-------------|------------|--------------------|\n");

        for f in &self.features {
            let tests = f.tests_executed.join(", ");
            let skip = f.skip_reason.as_deref().unwrap_or("-");
            let divergence = f.observed_divergence.as_deref().unwrap_or("-");
            md.push_str(&format!(
                "| {} | {} | {} | {} | {} | {} | {} | {} |\n",
                f.feature_id,
                f.category,
                f.status,
                f.manifest_evidence,
                tests,
                skip,
                divergence,
                f.suggested_evidence,
            ));
        }

        md
    }

    pub fn write_json(&self, path: &Path) -> std::io::Result<()> {
        fs::write(path, self.to_json())
    }

    pub fn write_markdown(&self, path: &Path) -> std::io::Result<()> {
        fs::write(path, self.to_markdown())
    }
}

pub fn detect_eggress_commit() -> Option<String> {
    if let Ok(val) = std::env::var("EGRESS_COMMIT") {
        if !val.is_empty() {
            return Some(val);
        }
    }

    Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .ok()
        .and_then(|o| {
            if o.status.success() {
                String::from_utf8(o.stdout)
                    .ok()
                    .map(|s| s.trim().to_string())
            } else {
                None
            }
        })
}

pub fn detect_python_version() -> Option<String> {
    Command::new("python3")
        .arg("--version")
        .output()
        .ok()
        .and_then(|o| {
            if o.status.success() {
                String::from_utf8(o.stdout)
                    .ok()
                    .map(|s| s.trim().to_string())
            } else {
                None
            }
        })
}

pub fn detect_pproxy_version() -> Option<String> {
    Command::new("python3")
        .args(["-c", "import pproxy; print(pproxy.__version__)"])
        .output()
        .ok()
        .and_then(|o| {
            if o.status.success() {
                String::from_utf8(o.stdout)
                    .ok()
                    .map(|s| s.trim().to_string())
            } else {
                None
            }
        })
}

#[derive(Debug, Deserialize)]
struct ManifestFile {
    #[serde(default)]
    features: Vec<ManifestEntry>,
}

pub fn load_manifest(path: &Path) -> std::io::Result<Vec<ManifestEntry>> {
    let content = fs::read_to_string(path)?;
    let manifest: ManifestFile = toml::from_str(&content)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
    Ok(manifest.features)
}

pub fn manifest_entry_to_feature(entry: &ManifestEntry) -> FeatureReport {
    FeatureReport {
        feature_id: entry.feature_id.clone(),
        category: entry.category.clone(),
        manifest_evidence: entry.evidence.clone(),
        tests_executed: entry.tests.clone(),
        status: TestStatus::NotRun,
        skip_reason: None,
        observed_divergence: None,
        suggested_evidence: entry.evidence.clone(),
    }
}

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

    fn sample_report() -> ParityReport {
        ParityReport {
            eggress_commit: Some("abc1234".to_string()),
            pproxy_version: "1.1.4".to_string(),
            os_platform: "macos".to_string(),
            rust_version: "rustc 1.75.0 (82e1608df 2023-12-21)".to_string(),
            python_version: "Python 3.12.0".to_string(),
            feature_gates: vec!["EGRESS_REQUIRE_SHADOWSOCKS_INTEROP".to_string()],
            features: vec![
                FeatureReport {
                    feature_id: "socks5_tcp".to_string(),
                    category: "protocol".to_string(),
                    manifest_evidence: "SOCKS5 supported".to_string(),
                    tests_executed: vec!["test_socks5_connect".to_string()],
                    status: TestStatus::Passed,
                    skip_reason: None,
                    observed_divergence: None,
                    suggested_evidence: "SOCKS5 supported".to_string(),
                },
                FeatureReport {
                    feature_id: "shadowsocks_udp".to_string(),
                    category: "protocol".to_string(),
                    manifest_evidence: "SS UDP supported".to_string(),
                    tests_executed: vec![],
                    status: TestStatus::SkippedMissingTool,
                    skip_reason: Some("pproxy not available".to_string()),
                    observed_divergence: None,
                    suggested_evidence: "SS UDP supported".to_string(),
                },
            ],
        }
    }

    #[test]
    fn json_round_trip() {
        let report = sample_report();
        let json = report.to_json();
        let parsed: ParityReport = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, report);
    }

    #[test]
    fn markdown_generation() {
        let report = sample_report();
        let md = report.to_markdown();
        assert!(md.contains("# Eggress Parity Report"));
        assert!(md.contains("| eggress commit | abc1234 |"));
        assert!(md.contains("| pproxy version | 1.1.4 |"));
        assert!(md.contains("| rust version | rustc 1.75.0 (82e1608df 2023-12-21) |"));
        assert!(md.contains("| python version | Python 3.12.0 |"));
        assert!(md.contains("| socks5_tcp | protocol | passed |"));
        assert!(md.contains("| shadowsocks_udp | protocol | skipped_missing_tool |"));
        assert!(md.contains("EGRESS_REQUIRE_SHADOWSOCKS_INTEROP"));
    }

    #[test]
    fn manifest_loading() {
        let toml_content = r#"
[[features]]
feature_id = "test_feature"
category = "test_cat"
evidence = "test evidence"
tests = ["test_a", "test_b"]
"#;
        let file = NamedTempFile::new().unwrap();
        fs::write(file.path(), toml_content).unwrap();

        let entries = load_manifest(file.path()).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].feature_id, "test_feature");
        assert_eq!(entries[0].category, "test_cat");
        assert_eq!(entries[0].evidence, "test evidence");
        assert_eq!(entries[0].tests, vec!["test_a", "test_b"]);
    }

    #[test]
    fn manifest_entry_to_feature_conversion() {
        let entry = ManifestEntry {
            feature_id: "conv_test".to_string(),
            category: "convert".to_string(),
            evidence: "some evidence".to_string(),
            tests: vec!["t1".to_string()],
        };
        let feature = manifest_entry_to_feature(&entry);
        assert_eq!(feature.feature_id, "conv_test");
        assert_eq!(feature.status, TestStatus::NotRun);
        assert_eq!(feature.manifest_evidence, "some evidence");
        assert_eq!(feature.suggested_evidence, "some evidence");
        assert!(feature.skip_reason.is_none());
    }

    #[test]
    fn manifest_loading_empty_array() {
        let toml_content = "# empty\n";
        let file = NamedTempFile::new().unwrap();
        fs::write(file.path(), toml_content).unwrap();

        let entries = load_manifest(file.path()).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn markdown_with_no_features() {
        let report = ParityReport {
            eggress_commit: None,
            pproxy_version: "unknown".to_string(),
            os_platform: "linux".to_string(),
            rust_version: "unknown".to_string(),
            python_version: "unknown".to_string(),
            feature_gates: vec![],
            features: vec![],
        };
        let md = report.to_markdown();
        assert!(md.contains("| eggress commit | n/a |"));
        assert!(md.contains("| pproxy version | unknown |"));
        assert!(md.contains("| Feature ID | Category | Status"));
    }

    #[test]
    fn write_json_to_file() {
        let report = sample_report();
        let file = NamedTempFile::new().unwrap();
        report.write_json(file.path()).unwrap();

        let content = fs::read_to_string(file.path()).unwrap();
        let parsed: ParityReport = serde_json::from_str(&content).unwrap();
        assert_eq!(parsed, report);
    }

    #[test]
    fn write_markdown_to_file() {
        let report = sample_report();
        let file = NamedTempFile::new().unwrap();
        report.write_markdown(file.path()).unwrap();

        let content = fs::read_to_string(file.path()).unwrap();
        assert!(content.contains("# Eggress Parity Report"));
        assert!(content.contains("socks5_tcp"));
    }

    #[test]
    fn test_status_roundtrip() {
        for variant in &[
            TestStatus::Passed,
            TestStatus::Failed,
            TestStatus::SkippedMissingGate,
            TestStatus::SkippedMissingTool,
            TestStatus::NotRun,
        ] {
            let s = variant.to_string();
            let parsed: TestStatus = s.parse().unwrap();
            assert_eq!(parsed, *variant);
        }
    }

    #[test]
    fn test_status_invalid_parse() {
        let result = "bogus".parse::<TestStatus>();
        assert!(result.is_err());
    }

    #[test]
    fn test_status_serde() {
        let status = TestStatus::SkippedMissingGate;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"skipped_missing_gate\"");
        let parsed: TestStatus = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, status);
    }
}