about summary refs log tree commit diff
path: root/tvix/store/src/tests/path_info_service.rs
blob: dca61fe12a56ccdff1db15e5e21d62a09085af4f (plain) (blame)
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
use tempfile::TempDir;

use crate::proto::path_info_service_server::PathInfoService;
use crate::proto::GetPathInfoRequest;
use crate::proto::{get_path_info_request, PathInfo};
use crate::sled_path_info_service::SledPathInfoService;

use lazy_static::lazy_static;

lazy_static! {
    static ref DUMMY_OUTPUT_HASH: Vec<u8> = vec![
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00
    ];
}

/// Trying to get a non-existent PathInfo should return a not found error.
#[tokio::test]
async fn not_found() -> anyhow::Result<()> {
    let service = SledPathInfoService::new(TempDir::new()?.path().to_path_buf())?;

    let resp = service
        .get(tonic::Request::new(GetPathInfoRequest {
            by_what: Some(get_path_info_request::ByWhat::ByOutputHash(
                DUMMY_OUTPUT_HASH.to_vec(),
            )),
        }))
        .await;

    match resp {
        Err(status) => {
            assert_eq!(status.code(), tonic::Code::NotFound);
        }
        Ok(_) => panic!("must fail"),
    };

    Ok(())
}

/// Put a PathInfo into the store, get it back.
#[tokio::test]
async fn put_get() -> anyhow::Result<()> {
    let service = SledPathInfoService::new(TempDir::new()?.path().to_path_buf())?;

    let path_info = PathInfo {
        node: Some(crate::proto::Node {
            node: Some(crate::proto::node::Node::Symlink(
                crate::proto::SymlinkNode {
                    name: "00000000000000000000000000000000-foo".to_string(),
                    target: "doesntmatter".to_string(),
                },
            )),
        }),
        ..Default::default()
    };

    let resp = service.put(tonic::Request::new(path_info.clone())).await;

    assert!(resp.is_ok());
    assert_eq!(resp.expect("must succeed").into_inner(), path_info);

    let resp = service
        .get(tonic::Request::new(GetPathInfoRequest {
            by_what: Some(get_path_info_request::ByWhat::ByOutputHash(
                DUMMY_OUTPUT_HASH.to_vec(),
            )),
        }))
        .await;

    assert!(resp.is_ok());
    assert_eq!(resp.expect("must succeed").into_inner(), path_info);

    Ok(())
}