loonfs-server 0.2.0

The reference LoonFS HTTP server.
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
//! Basic end-to-end HTTP namespace and file flows.

use crate::common::http_split_support::*;
use crate::common::start_server;
use loonfs::publish::{
    MAX_COMMIT_CONTENT_TOKENS, MAX_COMMIT_EXTERNAL_CONTENT_REFS, MAX_COMMIT_MESSAGE_BYTES,
    MAX_COMMIT_OPERATIONS,
};
use loonfs_api::{
    ChangeSeq, CommitId, DestinationBehavior, InodeKind, DEFAULT_MAX_PAGE_LIMIT,
    DEFAULT_PAGE_LIMIT, LIMIT_COMMIT_MAX_CONTENT_TOKENS, LIMIT_COMMIT_MAX_EXTERNAL_CONTENT_REFS,
    LIMIT_COMMIT_MAX_MESSAGE_BYTES, LIMIT_COMMIT_MAX_OPERATIONS, LIMIT_DOWNLOAD_MAX_CONCURRENT,
    LIMIT_DOWNLOAD_MAX_CONTENT_BYTES, LIMIT_PAGINATION_DEFAULT, LIMIT_PAGINATION_MAX,
    LIMIT_UPLOAD_MAX_CONCURRENT, LIMIT_UPLOAD_MAX_CONTENT_BYTES,
};
use loonfs_client::{ClientError, CreateDirectoryOptions, NamespacePath, PutFileOptions};
use loonfs_test_support::http::raw_agent;
use loonfs_test_support::ids::namespace_id;
use tempfile::tempdir;

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn delete_namespace_is_terminal_and_retires_the_id() {
    let temp_dir = tempdir().expect("tempdir");
    let harness = start_server(test_config(
        temp_dir.path().join("store"),
        "loonfs-server-test",
        "http-smoke",
    ))
    .await;

    let namespace = namespace_id("doomed");
    harness
        .client
        .create_namespace(&namespace)
        .await
        .expect("create namespace");
    let target = NamespacePath::parse("doomed", "/note.txt").expect("parse path");
    harness
        .client
        .put_file_bytes(&target, b"last words", &replace_file_options())
        .await
        .expect("write file");

    // A stale precondition refuses the delete and deletes nothing.
    let stale = harness
        .client
        .delete_namespace(&namespace, Some(ChangeSeq(0)))
        .await
        .expect_err("stale precondition");
    match stale {
        ClientError::Api {
            status,
            code,
            message,
            details,
            ..
        } => {
            assert_eq!(status, 409);
            assert_eq!(code, "stale_head");
            // The rejection reports both sequences, so a caller that still
            // means to delete knows what to retry against without parsing
            // anything: in words for a person, typed for a program.
            assert_eq!(message, "expected head sequence 0, found 1");
            let details = details.expect("a stale precondition carries its sequences");
            assert_eq!(details.expected_head_seq, Some(ChangeSeq(0)));
            assert_eq!(details.actual_head_seq, Some(ChangeSeq(1)));
        }
        other => unreachable!("expected stale_head, got {other:?}"),
    }
    harness
        .client
        .namespace_status(&namespace)
        .await
        .expect("still alive after failed precondition");

    let response = harness
        .client
        .delete_namespace(&namespace, None)
        .await
        .expect("delete namespace");
    assert_eq!(response.namespace_id.as_str(), "doomed");
    assert_eq!(response.head_seq, ChangeSeq(1));

    // Terminal: status is 410, reads fail, repeat deletes report
    // deleted, and the id is retired.
    let status = harness
        .client
        .namespace_status(&namespace)
        .await
        .expect_err("deleted namespace has no status");
    match status {
        ClientError::Api { status, code, .. } => {
            assert_eq!(status, 410);
            assert_eq!(code, "namespace_deleted");
        }
        other => unreachable!("expected namespace_deleted, got {other:?}"),
    }
    let read = harness
        .client
        .get_file_bytes(&target)
        .await
        .expect_err("reads observe the deleted namespace");
    match read {
        ClientError::Api { code, .. } => assert_eq!(code, "namespace_deleted"),
        other => unreachable!("expected namespace_deleted, got {other:?}"),
    }
    let again = harness
        .client
        .delete_namespace(&namespace, None)
        .await
        .expect_err("repeat delete");
    match again {
        ClientError::Api { status, code, .. } => {
            assert_eq!(status, 410);
            assert_eq!(code, "namespace_deleted");
        }
        other => unreachable!("expected namespace_deleted, got {other:?}"),
    }
    let recreate = harness
        .client
        .create_namespace(&namespace)
        .await
        .expect_err("the id is retired");
    match recreate {
        ClientError::Api { status, code, .. } => {
            assert_eq!(status, 410);
            assert_eq!(code, "namespace_deleted");
        }
        other => unreachable!("expected namespace_deleted, got {other:?}"),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn capabilities_endpoint_advertises_capabilities() {
    let temp_dir = tempdir().expect("tempdir");
    let harness = start_server(test_config(
        temp_dir.path().join("store"),
        "loonfs-server-test",
        "http-smoke",
    ))
    .await;

    let capabilities = harness
        .client
        .capabilities()
        .await
        .expect("fetch capabilities");
    assert_eq!(capabilities.protocol_version, "v0");
    assert!(capabilities.has_profile("core/v0"));
    assert!(capabilities.has_profile("admin/v0"));
    assert!(!capabilities.supports("core.namespaces.list"));
    assert!(capabilities.supports("core.namespaces.create"));
    assert!(capabilities.supports("core.namespaces.fork"));
    assert!(capabilities.supports("core.namespaces.delete"));
    // A local-filesystem deployment presigns nothing, so none of the three
    // transfer capabilities is advertised — and because it presigns no
    // uploads either, no file it holds can be larger than it will proxy.
    assert!(!capabilities.supports("core.uploads.direct_put"));
    assert!(!capabilities.supports("core.uploads.direct_multipart"));
    assert!(!capabilities.supports("core.downloads.direct_get"));
    assert_eq!(
        capabilities.limits.get(LIMIT_PAGINATION_DEFAULT),
        Some(&u64::from(DEFAULT_PAGE_LIMIT))
    );
    assert_eq!(
        capabilities.limits.get(LIMIT_PAGINATION_MAX),
        Some(&u64::from(DEFAULT_MAX_PAGE_LIMIT))
    );
    // The two halves a client pre-validates a write against: what this
    // deployment will carry over the wire, and what the runtime will accept
    // as one commit. Both are advertised, so neither has to be discovered by
    // being rejected.
    let config = test_config(
        temp_dir.path().join("store"),
        "loonfs-server-test",
        "http-smoke",
    );
    for (limit, expected) in [
        (LIMIT_UPLOAD_MAX_CONTENT_BYTES, config.max_upload_bytes),
        (LIMIT_DOWNLOAD_MAX_CONTENT_BYTES, config.max_download_bytes),
        (
            LIMIT_UPLOAD_MAX_CONCURRENT,
            config.max_concurrent_uploads as u64,
        ),
        (
            LIMIT_DOWNLOAD_MAX_CONCURRENT,
            config.max_concurrent_downloads as u64,
        ),
        (LIMIT_COMMIT_MAX_OPERATIONS, MAX_COMMIT_OPERATIONS as u64),
        (
            LIMIT_COMMIT_MAX_CONTENT_TOKENS,
            MAX_COMMIT_CONTENT_TOKENS as u64,
        ),
        (
            LIMIT_COMMIT_MAX_EXTERNAL_CONTENT_REFS,
            MAX_COMMIT_EXTERNAL_CONTENT_REFS as u64,
        ),
        (
            LIMIT_COMMIT_MAX_MESSAGE_BYTES,
            MAX_COMMIT_MESSAGE_BYTES as u64,
        ),
    ] {
        assert_eq!(
            capabilities.limits.get(limit),
            Some(&expected),
            "`{limit}` is not advertised with the value this deployment enforces"
        );
    }

    let cached = harness
        .client
        .capabilities()
        .await
        .expect("cached capabilities");
    assert_eq!(cached, capabilities);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn http_round_trip_supports_namespace_create_and_file_read_write() {
    let temp_dir = tempdir().expect("tempdir");
    let harness = start_server(test_config(
        temp_dir.path().join("store"),
        "loonfs-server-test",
        "http-smoke",
    ))
    .await;

    harness
        .client
        .create_namespace(&namespace_id("demo"))
        .await
        .expect("create namespace");
    let directory = NamespacePath::parse("demo", "/notes").expect("parse directory path");
    harness
        .client
        .create_directory(&directory, &CreateDirectoryOptions::default())
        .await
        .expect("create directory");
    let directory_entry = harness
        .client
        .stat_path(&directory)
        .await
        .expect("stat directory");
    assert_eq!(directory_entry.inode_kind, InodeKind::Directory);

    let target = NamespacePath::parse("demo", "/notes/hello.txt").expect("parse namespace path");
    let written = harness
        .client
        .put_file_bytes(
            &target,
            b"hello over http\n",
            &PutFileOptions {
                behavior: DestinationBehavior::Replace,
                commit_id: Some(CommitId::parse("smoke-write-1").expect("valid commit id")),
                message: None,
                expected_revision_no: None,
            },
        )
        .await
        .expect("write bytes");
    // Path operations echo the commit id they committed under.
    assert_eq!(written.commit_id.as_str(), "smoke-write-1");
    assert_eq!(written.committed_seq, ChangeSeq(2));

    let entry = harness.client.stat_path(&target).await.expect("stat path");
    assert_eq!(entry.size_bytes, Some(16));

    let bytes = harness
        .client
        .get_file_bytes(&target)
        .await
        .expect("read file");
    assert_eq!(bytes, b"hello over http\n");

    let status = harness
        .client
        .namespace_status(&namespace_id("demo"))
        .await
        .expect("namespace status");
    assert_eq!(status.namespace_id.as_str(), "demo");
    assert_eq!(status.head_seq, ChangeSeq(2));

    let missing = harness
        .client
        .namespace_status(&namespace_id("absent"))
        .await
        .expect_err("missing namespace status");
    match missing {
        ClientError::Api { status, code, .. } => {
            assert_eq!(status, 404);
            assert_eq!(code, "namespace_not_found");
        }
        other => unreachable!("expected API error for missing namespace, got {other:?}"),
    }

    harness.server.abort();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn http_namespace_listing_route_is_not_exposed() {
    let temp_dir = tempdir().expect("tempdir");
    let harness = start_server(test_config(
        temp_dir.path().join("store"),
        "loonfs-server-test",
        "http-no-namespace-list",
    ))
    .await;

    harness
        .client
        .create_namespace(&namespace_id("demo"))
        .await
        .expect("create demo");

    let response = raw_agent()
        .get(&format!("{}/v0/namespaces", harness.server_url))
        .set("authorization", "Bearer test-token")
        .call();
    match response {
        Err(ureq::Error::Status(status, _)) => {
            assert!(
                status == 404 || status == 405,
                "GET /v0/namespaces should be missing or method-not-allowed, got {status}"
            );
        }
        Ok(_) => unreachable!("GET /v0/namespaces must not return a namespace list"),
        Err(error) => unreachable!("unexpected transport error: {error}"),
    }

    harness.server.abort();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn http_namespace_fork_shares_content_and_diverges() {
    let temp_dir = tempdir().expect("tempdir");
    let harness = start_server(test_config(
        temp_dir.path().join("store"),
        "loonfs-server-fork",
        "http-fork-smoke",
    ))
    .await;

    harness
        .client
        .create_namespace(&namespace_id("demo"))
        .await
        .expect("create namespace");
    let source_path = NamespacePath::parse("demo", "/docs/shared.txt").expect("source path");
    let clone_path = NamespacePath::parse("clone", "/docs/shared.txt").expect("clone path");
    harness
        .client
        .put_file_bytes(&source_path, b"base\n", &replace_file_options())
        .await
        .expect("write source");

    let forked = harness
        .client
        .fork_namespace(&namespace_id("demo"), &namespace_id("clone"))
        .await
        .expect("fork namespace");
    assert_eq!(forked.namespace_id.as_str(), "clone");

    let source_entry = harness
        .client
        .stat_path(&source_path)
        .await
        .expect("source stat");
    let clone_entry = harness
        .client
        .stat_path(&clone_path)
        .await
        .expect("clone stat");
    assert_eq!(source_entry.content_ref, clone_entry.content_ref);
    assert_eq!(
        harness
            .client
            .get_file_bytes(&clone_path)
            .await
            .expect("read clone"),
        b"base\n"
    );

    harness
        .client
        .put_file_bytes(
            &source_path,
            b"source-after-fork\n",
            &replace_file_options(),
        )
        .await
        .expect("replace source");
    assert_eq!(
        harness
            .client
            .get_file_bytes(&clone_path)
            .await
            .expect("read clone after source write"),
        b"base\n"
    );

    let clone_write = harness
        .client
        .put_file_bytes(&clone_path, b"clone-after-fork\n", &replace_file_options())
        .await
        .expect("replace clone");
    assert_eq!(clone_write.committed_seq, ChangeSeq(2));
    assert_eq!(
        harness
            .client
            .get_file_bytes(&source_path)
            .await
            .expect("read source"),
        b"source-after-fork\n"
    );
    assert_eq!(
        harness
            .client
            .get_file_bytes(&clone_path)
            .await
            .expect("read clone"),
        b"clone-after-fork\n"
    );

    match harness
        .client
        .list_changes(&namespace_id("clone"), ChangeSeq(0), None)
        .await
    {
        Err(ClientError::Api { code, .. }) => assert_eq!(code, "rebootstrap_required"),
        other => unreachable!("expected rebootstrap_required, got {other:?}"),
    }
    let clone_changes = harness
        .client
        .list_changes(&namespace_id("clone"), ChangeSeq(1), None)
        .await
        .expect("clone changes");
    assert_eq!(clone_changes.changes.len(), 1);
    assert_eq!(clone_changes.changes[0].seq, ChangeSeq(2));

    harness.server.abort();
}