scorpiofs 0.2.2

FUSE-based virtual filesystem with Antares overlay for monorepo builds
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use core::panic;
use std::{
    collections::{BTreeSet, HashMap},
    env,
    ffi::OsStr,
    fs, io,
    net::TcpStream,
    path::{Path, PathBuf},
    process::Command,
    sync::Arc,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

// use http::Method;
use lazy_static::lazy_static;
use rfuse3::raw::logfs::LoggingFileSystem;
use scorpiofs::{
    dicfuse::store,
    fuse::MegaFuse,
    manager::{fetch::CheckHash, ScorpioManager},
    server::mount_filesystem,
    util::config,
};
use serde::{Deserialize, Serialize};
// use testcontainers::core::wait::HttpWaitStrategy;
use testcontainers::core::wait::LogWaitStrategy;
use testcontainers::{
    core::{IntoContainerPort, Mount, ReuseDirective, WaitFor},
    runners::AsyncRunner,
    ContainerAsync, GenericImage, ImageExt,
};
use tokio::sync::{mpsc, oneshot};

#[derive(Debug, Clone, Serialize, Deserialize)]
enum SCORCommand {
    ImportArc(),                // test init the scorpio directory structure
    WatchDir(),                 // update the directory structure
    LoadDir(String),            // test cd/ls and preload the directory structure
    GitAddFile(String, String), // add a new file and update to check the watch_dir
    GitDeleteFile(String),      // remove a new file and update to check the watch_dir
    Shutdown,                   // finish and close the file system service
    ReadFileContent(String),    // read the content of a file
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum CommandResult {
    StoreDirectoryStructure(HashMap<i32, BTreeSet<String>>), //used to return the directory structure
    Success,
    Error(String),
    InitFinish(usize),    // used to indicate the initialization is finished
    FileContent(Vec<u8>), // used to return the content of a file
}

lazy_static! {
    static ref TARGET: String = {
        let mut manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); // Get env at compile time
        manifest.pop();
        manifest.to_str().unwrap().to_string()
    };
    static ref MONO: PathBuf = {
        let path = if cfg!(target_os = "windows") {
            format!("{}/target/debug/mono.exe", TARGET.as_str())
        } else {
            format!("{}/target/debug/mono", TARGET.as_str())
        };
        PathBuf::from(path)
    };

    static ref SCOR_DIR: PathBuf = {
        Path::new("/tmp/scorpio_dir_test").to_path_buf()
    };

}
fn run_cmd(program: &str, args: &[&str], stdin: Option<&str>, envs: Option<Vec<(&str, &str)>>) {
    let mut cmd = assert_cmd::Command::new(program);
    let mut cmd = cmd.args(args);
    if let Some(stdin) = stdin {
        cmd = cmd.write_stdin(stdin);
    }
    if let Some(envs) = envs {
        cmd = cmd.envs(envs);
    }
    let assert = cmd.assert().success();
    let output = assert.get_output();

    println!(
        "Command success: {} {}\nStatus: {}\nStdout: {}",
        program,
        args.join(" "),
        output.status,
        String::from_utf8_lossy(&output.stdout),
    );
}

fn run_git_cmd(args: &[&str]) {
    run_cmd("git", args, None, None);
}

fn is_port_in_use(port: u16) -> bool {
    TcpStream::connect_timeout(
        &format!("127.0.0.1:{port}").parse().unwrap(),
        Duration::from_millis(1000),
    )
    .is_ok()
}

///clone the git repository and push to the mono server
fn git_clone(url: &str, mono_server_url: &str) -> io::Result<HashMap<i32, BTreeSet<String>>> {
    use std::collections::{BTreeSet, HashMap};

    fs::create_dir_all(SCOR_DIR.to_owned())?;

    let is_valid_git_repo = || -> bool {
        let git_config_path = SCOR_DIR.join("dir_test").join(".git");
        if !git_config_path.exists() {
            return false;
        }
        true
    };

    env::set_current_dir(SCOR_DIR.to_owned())?;

    if !is_valid_git_repo() {
        println!("No valid git repo found, cloning from {url}");
        run_git_cmd(&["clone", url]);

        let repo_name = url.split('/').next_back().unwrap().trim_end_matches(".git");
        let repo_dir = SCOR_DIR.join(repo_name);
        env::set_current_dir(&repo_dir)?;

        let mono_url = format!("{mono_server_url}/third-party/dir_test.git");
        run_git_cmd(&["remote", "add", "mono", mono_url.as_str()]);
        run_git_cmd(&["push", "--all", "mono"]);
    } else {
        println!("Using existing git repository");
        let repo_dir = SCOR_DIR.join("dir_test");

        env::set_current_dir(&repo_dir)?;
        let mono_url = format!("{mono_server_url}/third-party/dir_test.git");
        run_git_cmd(&["remote", "remove", "mono"]);
        run_git_cmd(&["remote", "add", "mono", mono_url.as_str()]);
        run_git_cmd(&["push", "--all", "mono"]);
    }

    let mut depth_items: HashMap<i32, BTreeSet<String>> = HashMap::new();

    let output = Command::new("git").args(["ls-files"]).output()?;
    let files = String::from_utf8_lossy(&output.stdout);

    for file in files.lines() {
        let depth = file.chars().filter(|&c| c == '/').count() as i32;
        depth_items
            .entry(depth)
            .or_default()
            .insert(file.to_string());

        let parts: Vec<&str> = file.split('/').collect();
        for i in 0..parts.len() {
            if i == 0 {
                depth_items
                    .entry(0)
                    .or_default()
                    .insert(parts[0].to_string());
            } else {
                let parent_path = parts[0..i].join("/");
                let parent_depth = (i - 1) as i32;
                depth_items
                    .entry(parent_depth)
                    .or_default()
                    .insert(parent_path);
            }
        }
    }

    let config = format!(
        r#"
lfs_url = "{}"
store_path = "{}/store"
config_file = "config.toml"
git_author = "MEGA"
git_email = "admin@mega.org"
workspace = "{}/mount"
base_url = "{}"
dicfuse_readable = "true"
load_dir_depth = "4"
    "#,
        mono_server_url,
        SCOR_DIR.to_str().unwrap(),
        SCOR_DIR.to_str().unwrap(),
        mono_server_url,
    );

    let store_path = SCOR_DIR.join("store");
    let _ = fs::remove_dir_all(&store_path); // Clear old store
    let mount_path = SCOR_DIR.join("mount");
    let _ = fs::create_dir_all(&mount_path);
    let umount_result = Command::new("umount")
        .args(["-f", mount_path.to_str().unwrap()])
        .output();

    match umount_result {
        Ok(output) => {
            if !output.status.success() {
                println!(
                    "Umount warning: {}",
                    String::from_utf8_lossy(&output.stderr)
                );
            }
        }
        Err(e) => {
            println!("Umount command failed (this is usually okay): {e}");
        }
    }

    fs::write(SCOR_DIR.join("scorpio.toml"), config)?;
    Ok(depth_items)
}
async fn mono_container(mapping_port: u16) -> ContainerAsync<GenericImage> {
    println!("MONO {:?} ", MONO.to_str().unwrap());
    if !MONO.exists() {
        panic!("MONO binary not found in \"target/debug/\", skip lfs test");
    }
    if is_port_in_use(mapping_port) {
        panic!("port {} is already in use", mapping_port);
    }
    let port_str = mapping_port.to_string();
    let cmd = vec![
        "/root/mono",
        "service",
        "multi",
        "http",
        "-p",
        &port_str,
        "--host",
        "0.0.0.0",
    ];

    GenericImage::new("ubuntu", "latest")
        .with_exposed_port(mapping_port.tcp())
        // .with_wait_for(WaitFor::Http(Box::new(
        //     HttpWaitStrategy::new("/")
        //         .with_method(Method::GET)
        //         .with_expected_status_code(404_u16),
        // )))
        .with_wait_for(WaitFor::Log(LogWaitStrategy::stdout("CommonHttpOptions")))
        .with_mapped_port(mapping_port, mapping_port.tcp())
        .with_mount(Mount::bind_mount(MONO.to_str().unwrap(), "/root/mono"))
        .with_working_dir("/root")
        .with_reuse(ReuseDirective::Never)
        .with_cmd(cmd)
        .start()
        .await
        .expect("Failed to start mono_server")
}

pub async fn mono_bootstrap_servers(mapping_port: u16) -> (ContainerAsync<GenericImage>, String) {
    let container = mono_container(mapping_port).await;
    let mega_ip = container.get_bridge_ip_address().await.unwrap();
    let mega_port: u16 = container.get_host_port_ipv4(mapping_port).await.unwrap();
    (container, format!("http://{mega_ip}:{mega_port}"))
}

#[tokio::test]
#[ignore = "requires mono binary in target/debug/ and Docker environment"]
///Use container to run mono server and test the scorpio service
async fn test_scorpio_service_with_containers() {
    let (_container, mono_server_url) = mono_bootstrap_servers(12001).await;
    println!("container: {mono_server_url}");
    let dir_list = git_clone("https://github.com/yyjeqhc/dir_test.git", &mono_server_url).unwrap();

    let (cmd_tx, cmd_rx) = mpsc::channel(32);
    let (result_tx, mut result_rx) = mpsc::channel(32);

    let scorpio_handle = tokio::spawn(test_scorpio_dir(cmd_rx, result_tx));

    // This is the preload's relative depth for the dir to load.
    let mut max_depth = 0;

    // Wait for the store:init_notify: Arc<Notify>
    tokio::select! {
        _ = scorpio_handle => {
            panic!("start the scorpio service failed");
        }
        success = result_rx.recv() => {
            if let Some(CommandResult::InitFinish(depth)) = success {
                   println!("scorpio service started successfully, max depth: {depth}");
                   max_depth = depth;
            }
        }
    }
    println!("\n===== ImportArc =====");

    cmd_tx.send(SCORCommand::ImportArc()).await.unwrap();

    if let Some(result) = result_rx.recv().await {
        match result {
            CommandResult::StoreDirectoryStructure(store_items) => {
                for i in 0..max_depth as i32 {
                    assert_eq!(
                        dir_list.get(&i).unwrap(),
                        store_items.get(&i).unwrap(),
                        "dir structure at depth {i} does not match."
                    );
                }
                println!("import_arc,load dir success");
            }
            _ => {
                cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                let _ = result_rx.recv().await;
                // let _ = scorpio_handle.await;
                panic!("ImportArc failed to load dir.");
            }
        }
    }

    println!("\n===== add a file and WatchDir =====");
    let test_file = "test_file.txt";
    for i in 0..2 {
        let test_content = format!(
            "now: {} {}",
            i,
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
        );

        cmd_tx
            .send(SCORCommand::GitAddFile(
                test_file.to_string(),
                test_content.to_owned(),
            ))
            .await
            .unwrap();

        if let Some(result) = result_rx.recv().await {
            match result {
                CommandResult::Success => {
                    println!("git add success.");
                }
                _ => {
                    cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                    // let _ = scorpio_handle.await;
                    let _ = result_rx.recv().await;

                    panic!("git add file error.");
                }
            }
        }

        cmd_tx.send(SCORCommand::WatchDir()).await.unwrap();

        if let Some(result) = result_rx.recv().await {
            match result {
                CommandResult::StoreDirectoryStructure(result) => {
                    assert!(
                        result.get(&0).unwrap().contains(&test_file.to_string()),
                        "WatchDir fail: did not find the added file"
                    );
                }
                _ => {
                    cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                    // let _ = scorpio_handle.await;
                    let _ = result_rx.recv().await;

                    panic!("WatchDir error.");
                }
            }
        }
        cmd_tx
            .send(SCORCommand::ReadFileContent(test_file.to_string()))
            .await
            .unwrap();
        if let Some(result) = result_rx.recv().await {
            match result {
                CommandResult::FileContent(result) => {
                    assert_eq!(
                        result,
                        test_content.as_bytes(),
                        "ReadFileContent failed to get the correct content"
                    );
                }
                _ => {
                    cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                    // let _ = scorpio_handle.await;
                    let _ = result_rx.recv().await;

                    panic!("WatchDir error.");
                }
            }
        }
    }

    println!("\n===== remove ad file and WatchDir =====");

    cmd_tx
        .send(SCORCommand::GitDeleteFile(test_file.to_string()))
        .await
        .unwrap();

    if let Some(result) = result_rx.recv().await {
        match result {
            CommandResult::Success => {
                println!("git remove success.");
            }
            _ => {
                cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                // let _ = scorpio_handle.await;
                let _ = result_rx.recv().await;

                panic!("git remove file error.");
            }
        }
    }

    cmd_tx.send(SCORCommand::WatchDir()).await.unwrap();

    if let Some(result) = result_rx.recv().await {
        match result {
            CommandResult::StoreDirectoryStructure(result) => {
                assert!(
                    !result.get(&0).unwrap().contains(&test_file.to_string()),
                    "WatchDir fail: did not remove the file"
                );
            }
            _ => {
                cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                // let _ = scorpio_handle.await;
                let _ = result_rx.recv().await;
                panic!("WatchDir: delete file error.");
            }
        }
    }

    cmd_tx
        .send(SCORCommand::LoadDir(
            "/third-party/dir_test/1/1/2/3".to_string(),
        ))
        .await
        .unwrap();

    if let Some(result) = result_rx.recv().await {
        match result {
            CommandResult::StoreDirectoryStructure(store_items) => {
                let mut expected_items: HashMap<i32, BTreeSet<String>> = HashMap::new();
                let test_path = "1/1/2/3/".to_string();
                for git_files in dir_list.values() {
                    for file in git_files {
                        if file.starts_with(&test_path) {
                            let relative_path = file.trim_start_matches(&test_path);
                            expected_items
                                .entry(relative_path.matches('/').count() as i32)
                                .or_default()
                                .insert(relative_path.to_string());
                        }
                    }
                }
                for i in 0..max_depth as i32 {
                    assert_eq!(
                        expected_items.get(&i).unwrap(),
                        store_items.get(&i).unwrap(),
                        "dir structure at depth {i} does not match."
                    );
                }
                println!("load_dir,preload dir success");
            }
            _ => {
                cmd_tx.send(SCORCommand::Shutdown).await.unwrap();
                let _ = result_rx.recv().await;
                panic!("load_dir fail");
            }
        }
    }

    cmd_tx.send(SCORCommand::Shutdown).await.unwrap();

    // let _ = scorpio_handle.await;
    let _ = result_rx.recv().await;

    println!("success to finish the test.");
}

async fn test_scorpio_dir(
    mut cmd_rx: mpsc::Receiver<SCORCommand>,
    result_tx: mpsc::Sender<CommandResult>,
) {
    if let Err(e) = config::init_config(SCOR_DIR.join("scorpio.toml").to_str().unwrap()) {
        eprintln!("init config fail {e:?}");
        let _ = result_tx
            .send(CommandResult::Error("load config error".to_string()))
            .await;
        return;
    }

    let mut manager = ScorpioManager { works: vec![] };
    manager.check().await;
    //init scorpio configuration
    let fuse_interface = MegaFuse::new_from_manager(&manager).await;
    let workspace = config::workspace();
    let mountpoint = OsStr::new(workspace);
    let lgfs = LoggingFileSystem::new(fuse_interface.clone());

    let (shutdown_tx, shutdown_rx) = oneshot::channel();

    let mut mount_handle: rfuse3::raw::MountHandle = mount_filesystem(lgfs, mountpoint).await;

    let arc_fuse = Arc::new(fuse_interface);
    let repo_dir = SCOR_DIR.join("dir_test");

    let shutdown_tx = shutdown_tx;
    let fuse_interface = arc_fuse.clone();
    let store = fuse_interface.dic.clone().store.clone();

    store.wait_for_ready().await;
    result_tx
        .send(CommandResult::InitFinish(store.max_depth()))
        .await
        .expect("Failed to send success signal");
    let cmd_handle = {
        let result_tx = result_tx.clone();

        tokio::spawn(async move {
            while let Some(cmd) = cmd_rx.recv().await {
                match cmd {
                    SCORCommand::ImportArc() => {
                        let base_path = "/third-party/dir_test";
                        let depth_items = store.get_dir_by_path(base_path).await;
                        if depth_items.is_empty() {
                            let _ = result_tx
                                .send(CommandResult::Error("ImportArc fail".to_string()))
                                .await;
                        } else {
                            let _ = result_tx
                                .send(CommandResult::StoreDirectoryStructure(depth_items))
                                .await;
                        }
                    }
                    SCORCommand::WatchDir() => {
                        store::watch_dir(store.clone()).await;
                        let dir_items = store.get_dir_by_path("/third-party/dir_test").await;
                        let _ = result_tx
                            .send(CommandResult::StoreDirectoryStructure(dir_items))
                            .await;
                    }
                    SCORCommand::LoadDir(path) => {
                        let max_depth = path.matches('/').count() + config::load_dir_depth();
                        let _ = store::load_dir(store.clone(), path.to_owned(), max_depth).await;
                        let dir_items = store.get_dir_by_path(&path).await;
                        if dir_items.is_empty() {
                            let _ = result_tx
                                .send(CommandResult::Error(format!("LoadDir fail: {path}")))
                                .await;
                        } else {
                            let _ = result_tx
                                .send(CommandResult::StoreDirectoryStructure(dir_items))
                                .await;
                        }
                    }
                    SCORCommand::ReadFileContent(path) => {
                        env::set_current_dir(&repo_dir).unwrap();
                        let base_path = "/third-party/dir_test/";

                        let file_path = base_path.to_string() + &path;
                        // if !file_path.exists() {
                        //     let _ = result_tx
                        //         .send(CommandResult::Error(format!("File not found: {}", path)))
                        //         .await;
                        //     continue;
                        // }
                        println!("ReadFileContent {file_path:?}");
                        let content = store
                            .get_file_content_by_path(&file_path)
                            .await
                            .expect("Failed to get file content");
                        let _ = result_tx.send(CommandResult::FileContent(content)).await;
                    }
                    SCORCommand::GitAddFile(path, content) => {
                        env::set_current_dir(&repo_dir).unwrap();
                        let _ = fs::write(repo_dir.join(&path), content);
                        run_git_cmd(&["add", &path]);
                        run_git_cmd(&["commit", "-m", "add file"]);
                        run_git_cmd(&["push", "--all", "mono"]);
                        result_tx.send(CommandResult::Success).await.unwrap();
                    }
                    SCORCommand::GitDeleteFile(path) => {
                        env::set_current_dir(&repo_dir).unwrap();
                        fs::remove_file(repo_dir.join(&path)).unwrap();
                        run_git_cmd(&["add", &path]);
                        run_git_cmd(&["commit", "-m", "remove file"]);
                        run_git_cmd(&["push", "--all", "mono"]);
                        result_tx.send(CommandResult::Success).await.unwrap();
                    }
                    SCORCommand::Shutdown => {
                        let _ = shutdown_tx.send(());
                        break;
                    }
                }
            }
        })
    };

    tokio::select! {
        res = &mut mount_handle => res.unwrap(),
        _ = cmd_handle => {
            println!("unmount....");
            mount_handle.unmount().await.unwrap();
            let _ = result_tx.send(CommandResult::Success).await;
        }
        _ = shutdown_rx => {
            println!("unmount....");
            mount_handle.unmount().await.unwrap();
            let _ = result_tx.send(CommandResult::Success).await;
        }
    }

    // let _ = cmd_handle.await;
    // let _ = daemon_handle.await;

    println!("success to close the scorpio service.");
}