summaryrefslogtreecommitdiff
path: root/test/process/mounts.zig
blob: 8a7fc6fe5466b92eea5069b465902f7028b26f94 (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
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
//! docs/typ/reference.typ, Mounts <mounts>: sessions mounting each other's
//! trees over their sockets, at startup and at run time.
const std = @import("std");
const process = @import("../process.zig");
const Conn = process.Conn;
const Detached = process.Detached;
const has = process.has;
const check = process.check;

pub const cases = .{
    .{ "mounts: --mount at startup reads a peer's panes and its disk, a Save through it lands there, and a path not there is a miss in the log", startup },
    .{ "mounts: Mount and Unmount at run time, refused in words when mounted already or in use; a session's /n lists its mounts; a file through a mount opens at startup", runtime },
};

/// A session whose pane `serial` holds "replaced\nevent\n", as fs.py's
/// `first` had one when its peers mounted it.
fn peer(gpa: std.mem.Allocator, exe: [:0]const u8, a: std.mem.Allocator) !struct { Detached, *Conn, u32 } {
    var d = try Detached.start(gpa, exe, "first", .{});
    errdefer d.deinit();
    const c = try Conn.dial(gpa, d.socket());
    const serial = try c.newPane();
    try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{serial}), "replaced\nevent\n");
    return .{ d, c, serial };
}

fn startup(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var arena = std.heap.ArenaAllocator.init(gpa);
    defer arena.deinit();
    const a = arena.allocator();
    var first, const client, const serial = try peer(gpa, exe, a);
    defer first.deinit();
    defer client.deinit(gpa);
    const address = first.abs("pardes-9p-first.sock");
    const own = first.abs("pardes-9p-second.sock");
    var second = try Detached.start(gpa, exe, "second", .{
        .root = first.root(),
        .args = &.{ try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0), try std.fmt.allocPrintSentinel(a, "--mount=own={s}", .{own}, 0) },
    });
    defer second.deinit();
    const remote = try Conn.dial(gpa, second.socket());
    defer remote.deinit(gpa);
    var opened = try remote.look(a, "/n/own/pane/1/body", null);
    try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body"));
    opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/pane/{d}/body", .{serial}), null);
    try std.testing.expectEqualStrings("replaced\nevent\n", try remote.pane(a, opened, "body"));
    try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "saved to peer\n");
    try remote.exec(opened, "Save\n");
    try std.testing.expectEqualStrings("saved to peer\n", try client.pane(a, serial, "body"));

    var large: std.ArrayList(u8) = .empty;
    for (0..60000) |_| try large.appendSlice(a, "mounted file contents\n");
    try first.put("large.txt", large.items);
    opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{first.abs("large.txt")}), null);
    try check(std.mem.eql(u8, try remote.pane(a, opened, "body"), large.items), "the mounted file is not whole", .{});
    const edited = try std.fmt.allocPrint(a, "{s}edited\n", .{large.items[0 .. large.items.len - 3]});
    try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), edited);
    try remote.exec(opened, "Save\n");
    try check(std.mem.eql(u8, try first.get(a, "large.txt"), edited), "the Save through the mount is not on disk", .{});
    try check(std.mem.eql(u8, try client.read(a, try std.fmt.allocPrint(a, "/os{s}", .{first.abs("large.txt")})), edited), "the peer's /os disagrees", .{});
    // A path not there under a peer's mount is a miss, as any path to no
    // file is: the write answered, the err logged.
    try remote.write("/look", try std.fmt.allocPrint(a, "/n/peer/os{s}\n", .{first.abs("no-such-file.txt")}));
    try check(has(try remote.read(a, "/log"), "no such file"), "no miss logged", .{});
    try client.exec(serial, "Del\n");
    for (try client.list(a, "/pane")) |n| try check(!std.mem.eql(u8, n, try std.fmt.allocPrint(a, "{d}", .{serial})), "Del left the pane", .{});
}

fn runtime(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var arena = std.heap.ArenaAllocator.init(gpa);
    defer arena.deinit();
    const a = arena.allocator();
    var first, const client, _ = try peer(gpa, exe, a);
    defer first.deinit();
    defer client.deinit(gpa);
    const address = first.abs("pardes-9p-first.sock");
    {
        var d = try Detached.start(gpa, exe, "runtime", .{ .root = first.root() });
        defer d.deinit();
        const remote = try Conn.dial(gpa, d.socket());
        defer remote.deinit(gpa);
        const own_address = first.abs("pardes-9p-runtime.sock");
        const control = try remote.newPane();
        try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{address}));
        var opened = try remote.look(a, "/n/peer/pane/1/body", control);
        try std.testing.expectEqualStrings(try client.read(a, "/pane/1/body"), try remote.pane(a, opened, "body"));
        // A builtin that fails through exec fails the write (one rule).
        try remote.execRefused(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address}), "already mounted");
        try remote.execRefused(control, "Unmount peer\n", "mount in use"); // a pane is open on it
        try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "runtime mounted Save\n");
        try remote.exec(opened, "Save\n");
        try std.testing.expectEqualStrings("runtime mounted Save\n", try client.read(a, "/pane/1/body"));
        try std.testing.expectEqualStrings("initial\n", try remote.read(a, "/pane/1/body"));
        const roots = try remote.look(a, "/n", control);
        try check(has(try remote.pane(a, roots, "body"), "/n/peer/\n"), "/n lists no peer", .{});
        try remote.exec(opened, "Del\n");
        try remote.exec(control, "Unmount peer\n");
        try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{roots}), "get\n");
        try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body"));
        try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address}));
        opened = try remote.look(a, "/n/peer/pane/1/body", control);
        try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body"));
    }
    var d = try Detached.start(gpa, exe, "mounted-startup", .{
        .root = first.root(),
        .args = &.{try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0)},
        .file = "/n/peer/pane/1/body:1",
    });
    defer d.deinit();
    try d.files.expect("/pane/1/body", "runtime mounted Save\n");
}